|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2020 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | +// Author Adam Janikowski |
| 21 | +// |
| 22 | + |
| 23 | +package main |
| 24 | + |
| 25 | +import ( |
| 26 | + "fmt" |
| 27 | + "io/ioutil" |
| 28 | + "os" |
| 29 | + "strings" |
| 30 | + |
| 31 | + "github.com/pkg/errors" |
| 32 | + "github.com/rs/zerolog/log" |
| 33 | + "github.com/spf13/cobra" |
| 34 | +) |
| 35 | + |
| 36 | +type cmdUUIDInputStruct struct { |
| 37 | + uuidPath, uuid, engine, enginePath string |
| 38 | + require bool |
| 39 | +} |
| 40 | + |
| 41 | +var ( |
| 42 | + cmdUUID = &cobra.Command{ |
| 43 | + Use: "uuid", |
| 44 | + RunE: cmdUUIDSave, |
| 45 | + Hidden: true, |
| 46 | + } |
| 47 | + |
| 48 | + cmdUUIDInput cmdUUIDInputStruct |
| 49 | +) |
| 50 | + |
| 51 | +func init() { |
| 52 | + cmdMain.AddCommand(cmdUUID) |
| 53 | + f := cmdUUID.Flags() |
| 54 | + f.StringVar(&cmdUUIDInput.uuidPath, "uuid-path", "", "Path to the UUID file") |
| 55 | + f.StringVar(&cmdUUIDInput.engine, "engine", "", "Path to the ENGINE file") |
| 56 | + f.StringVar(&cmdUUIDInput.uuid, "uuid", "", "UUID of server") |
| 57 | + f.StringVar(&cmdUUIDInput.enginePath, "engine-path", "", "ENGINE of server") |
| 58 | + f.BoolVar(&cmdUUIDInput.require, "require", false, "Ensure that UUID and ENGINE file is in place") |
| 59 | +} |
| 60 | + |
| 61 | +func cmdUUIDSave(cmd *cobra.Command, args []string) error { |
| 62 | + if cmdUUIDInput.uuidPath == "" { |
| 63 | + return errors.Errorf("Path cannot be empty") |
| 64 | + } |
| 65 | + |
| 66 | + if cmdUUIDInput.require { |
| 67 | + if cmdUUIDInput.enginePath == "" { |
| 68 | + return errors.Errorf("Path to the ENGINE cannot be empty") |
| 69 | + } |
| 70 | + |
| 71 | + if cmdUUIDInput.engine == "" { |
| 72 | + return errors.Errorf("ENGINE cannot be empty") |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + if cmdUUIDInput.uuid == "" { |
| 77 | + return errors.Errorf("UUID cannot be empty") |
| 78 | + } |
| 79 | + |
| 80 | + log.Info().Msg("Saving UUID in file") |
| 81 | + |
| 82 | + if exists, err := fileExists(cmdUUIDInput.uuidPath); err != nil { |
| 83 | + log.Error().Err(err).Msg("Unable to get file info") |
| 84 | + return err |
| 85 | + } else if !exists { |
| 86 | + if cmdUUIDInput.require { |
| 87 | + log.Warn().Msg("Init phase is not defined, but file does not exists") |
| 88 | + |
| 89 | + if exists, err := fileExists(cmdUUIDInput.uuidPath); err != nil { |
| 90 | + log.Error().Err(err).Msg("Unable to get ENGINE info") |
| 91 | + return err |
| 92 | + } else if !exists { |
| 93 | + log.Info().Msg("ENGINE file does not exist - able to proceed") |
| 94 | + } else { |
| 95 | + log.Error().Msg("ENGINE file found but UUID is missing - will not proceed") |
| 96 | + return errors.Errorf("ENGINE file found but UUID is missing - will not proceed") |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + fileContent := fmt.Sprintf("%s\n", cmdUUIDInput.uuid) |
| 101 | + if err := ioutil.WriteFile(cmdUUIDInput.uuidPath, []byte(fileContent), 0644); err != nil { |
| 102 | + log.Error().Err(err).Msg("Unable to save UUID") |
| 103 | + return err |
| 104 | + } |
| 105 | + |
| 106 | + log.Info().Msg("UUID saved") |
| 107 | + return nil |
| 108 | + } |
| 109 | + |
| 110 | + if equal, content, err := checkFileContent(cmdUUIDInput.uuidPath, cmdUUIDInput.uuid); err != nil { |
| 111 | + log.Error().Err(err).Msg("Unable to get UUID info") |
| 112 | + return err |
| 113 | + } else if !equal { |
| 114 | + log.Error().Str("expected", cmdUUIDInput.uuid).Str("actual", content).Msg("UUID does not match expected") |
| 115 | + return errors.Errorf("UUID mismatch") |
| 116 | + } else { |
| 117 | + log.Info().Msg("UUID is valid") |
| 118 | + } |
| 119 | + |
| 120 | + if cmdUUIDInput.require { |
| 121 | + if exists, err := fileExists(cmdUUIDInput.enginePath); err != nil { |
| 122 | + log.Error().Err(err).Msg("Unable to get ENGINE info") |
| 123 | + return err |
| 124 | + } else if exists { |
| 125 | + if equal, content, err := checkFileContent(cmdUUIDInput.enginePath, cmdUUIDInput.engine); err != nil { |
| 126 | + log.Error().Err(err).Msg("Unable to get ENGINE info") |
| 127 | + return err |
| 128 | + } else if !equal { |
| 129 | + log.Error().Str("expected", cmdUUIDInput.engine).Str("actual", content).Msg("ENGINE does not match expected") |
| 130 | + return errors.Errorf("ENGINE mismatch") |
| 131 | + } else { |
| 132 | + log.Info().Msg("ENGINE is valid") |
| 133 | + } |
| 134 | + } else { |
| 135 | + log.Info().Msg("ENGINE file is missing, but UUID is in place - we can proceed") |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + log.Info().Msg("Init Phase is valid") |
| 140 | + return nil |
| 141 | +} |
| 142 | + |
| 143 | +func fileExists(path string) (bool, error) { |
| 144 | + if _, err := os.Stat(path); err != nil { |
| 145 | + if os.IsNotExist(err) { |
| 146 | + return false, nil |
| 147 | + } |
| 148 | + |
| 149 | + return false, err |
| 150 | + } |
| 151 | + |
| 152 | + return true, nil |
| 153 | +} |
| 154 | + |
| 155 | +func checkFileContent(path, expected string) (bool, string, error) { |
| 156 | + content, err := ioutil.ReadFile(path) |
| 157 | + if err != nil { |
| 158 | + return false, "", err |
| 159 | + } |
| 160 | + |
| 161 | + contentString := strings.TrimSuffix(string(content), "\n") |
| 162 | + |
| 163 | + return contentString == expected, contentString, nil |
| 164 | +} |
0 commit comments