David K. Bainbridge | df9df63 | 2016-07-07 18:47:46 -0700 | [diff] [blame] | 1 | // Copyright 2016 Open Networking Laboratory |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 14 | package main |
| 15 | |
| 16 | import ( |
| 17 | "encoding/json" |
| 18 | "flag" |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 19 | "github.com/Sirupsen/logrus" |
David K. Bainbridge | 6ea57c1 | 2016-06-06 23:29:12 -0700 | [diff] [blame] | 20 | "github.com/kelseyhightower/envconfig" |
David K. Bainbridge | 4f8143d | 2016-10-27 17:14:48 -0700 | [diff] [blame] | 21 | "io/ioutil" |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 22 | "net/url" |
| 23 | "os" |
David K. Bainbridge | 4f8143d | 2016-10-27 17:14:48 -0700 | [diff] [blame] | 24 | "regexp" |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 25 | "time" |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 26 | |
| 27 | maas "github.com/juju/gomaasapi" |
| 28 | ) |
| 29 | |
David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame^] | 30 | const appName = "AUTOMATION" |
| 31 | |
David K. Bainbridge | 6ea57c1 | 2016-06-06 23:29:12 -0700 | [diff] [blame] | 32 | type Config struct { |
David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame^] | 33 | PowerHelperUser string `default:"cord" envconfig:"POWER_HELPER_USER" desc:"user when integrating with virtual box power mgmt"` |
| 34 | PowerHelperHost string `default:"127.0.0.1" envconfig:"POWER_HELPER_HOST" desc:"virtual box host"` |
| 35 | PowerHelperScript string `default:"" envconfig:"POWER_HELPER_SCRIPT" desc:"script for virtual box power mgmt support"` |
| 36 | ProvisionUrl string `default:"" envconfig:"PROVISION_URL" desc:"connection string to connect to provisioner uservice"` |
| 37 | ProvisionTtl string `default:"1h" envconfig:"PROVISION_TTL" desc:"duration to wait for a provisioning request to complete, before considered a failure"` |
| 38 | LogLevel string `default:"warning" envconfig:"LOG_LEVEL" desc:"detail level for logging"` |
| 39 | LogFormat string `default:"text" envconfig:"LOG_FORMAT" desc:"log output format, text or json"` |
| 40 | ApiKey string `envconfig:"MAAS_API_KEY" required:"true" desc:"API key to access MAAS server"` |
David K. Bainbridge | 4f8143d | 2016-10-27 17:14:48 -0700 | [diff] [blame] | 41 | ApiKeyFile string `default:"/secrets/maas_api_key" envconfig:"MAAS_API_KEY_FILE" desc:"file to hold the secret"` |
| 42 | ShowApiKey bool `default:"false" envconfig:"MAAS_SHOW_API_KEY" desc:"Show API in clear text in logs"` |
David K. Bainbridge | e9d7af7 | 2016-10-14 08:42:55 -0700 | [diff] [blame] | 43 | MaasUrl string `default:"http://localhost/MAAS" envconfig:"MAAS_URL" desc:"URL to access MAAS server"` |
| 44 | ApiVersion string `default:"1.0" envconfig:"MAAS_API_VERSION" desc:"API version to use with MAAS server"` |
| 45 | QueryInterval time.Duration `default:"15s" envconfig:"MAAS_QUERY_INTERVAL" desc:"frequency to query MAAS service for nodes"` |
| 46 | PreviewOnly bool `default:"false" envconfig:"PREVIEW_ONLY" desc:"display actions that would be taken, but don't execute them"` |
| 47 | AlwaysRename bool `default:"true" envconfig:"ALWAYS_RENAME" desc:"attempt to rename hosts at every stage or workflow"` |
| 48 | Mappings string `default:"{}" envconfig:"MAC_TO_NAME_MAPPINGS" desc:"custom MAC address to host name mappings"` |
| 49 | FilterSpec string `default:"{\"hosts\":{\"include\":[\".*\"]},\"zones\":{\"include\":[\"default\"]}}" envconfig:"HOST_FILTER_SPEC" desc:"constrain hosts that are automated"` |
David K. Bainbridge | 6ea57c1 | 2016-06-06 23:29:12 -0700 | [diff] [blame] | 50 | } |
| 51 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 52 | // checkError if the given err is not nil, then fatally log the message, else |
| 53 | // return false. |
| 54 | func checkError(err error, message string, v ...interface{}) bool { |
| 55 | if err != nil { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 56 | log.Fatalf(message, v...) |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 57 | } |
| 58 | return false |
| 59 | } |
| 60 | |
| 61 | // checkWarn if the given err is not nil, then log the message as a warning and |
| 62 | // return true, else return false. |
| 63 | func checkWarn(err error, message string, v ...interface{}) bool { |
| 64 | if err != nil { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 65 | log.Warningf(message, v...) |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 66 | return true |
| 67 | } |
| 68 | return false |
| 69 | } |
| 70 | |
| 71 | // fetchNodes do a HTTP GET to the MAAS server to query all the nodes |
| 72 | func fetchNodes(client *maas.MAASObject) ([]MaasNode, error) { |
| 73 | nodeListing := client.GetSubObject("nodes") |
| 74 | listNodeObjects, err := nodeListing.CallGet("list", url.Values{}) |
| 75 | if checkWarn(err, "unable to get the list of all nodes: %s", err) { |
| 76 | return nil, err |
| 77 | } |
| 78 | listNodes, err := listNodeObjects.GetArray() |
| 79 | if checkWarn(err, "unable to get the node objects for the list: %s", err) { |
| 80 | return nil, err |
| 81 | } |
| 82 | |
| 83 | var nodes = make([]MaasNode, len(listNodes)) |
| 84 | for index, nodeObj := range listNodes { |
| 85 | node, err := nodeObj.GetMAASObject() |
| 86 | if !checkWarn(err, "unable to retrieve object for node: %s", err) { |
| 87 | nodes[index] = MaasNode{node} |
| 88 | } |
| 89 | } |
| 90 | return nodes, nil |
| 91 | } |
| 92 | |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 93 | var log = logrus.New() |
David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame^] | 94 | var appFlags = flag.NewFlagSet("", flag.ContinueOnError) |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 95 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 96 | func main() { |
| 97 | |
David K. Bainbridge | 6ea57c1 | 2016-06-06 23:29:12 -0700 | [diff] [blame] | 98 | config := Config{} |
David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame^] | 99 | appFlags.Usage = func() { |
| 100 | envconfig.Usage(appName, &config) |
| 101 | } |
| 102 | if err := appFlags.Parse(os.Args[1:]); err != nil { |
| 103 | if err != flag.ErrHelp { |
| 104 | os.Exit(1) |
| 105 | } else { |
| 106 | return |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | err := envconfig.Process(appName, &config) |
| 111 | |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 112 | if err != nil { |
| 113 | log.Fatalf("Unable to parse configuration options : %s", err) |
| 114 | } |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 115 | |
| 116 | options := ProcessingOptions{ |
David K. Bainbridge | e9d7af7 | 2016-10-14 08:42:55 -0700 | [diff] [blame] | 117 | Preview: config.PreviewOnly, |
| 118 | AlwaysRename: config.AlwaysRename, |
David K. Bainbridge | 068e87d | 2016-06-30 13:53:19 -0700 | [diff] [blame] | 119 | Provisioner: NewProvisioner(&ProvisionerConfig{Url: config.ProvisionUrl}), |
David K. Bainbridge | 6ea57c1 | 2016-06-06 23:29:12 -0700 | [diff] [blame] | 120 | ProvisionURL: config.ProvisionUrl, |
| 121 | PowerHelper: config.PowerHelperScript, |
| 122 | PowerHelperUser: config.PowerHelperUser, |
| 123 | PowerHelperHost: config.PowerHelperHost, |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 124 | } |
| 125 | |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 126 | switch config.LogFormat { |
| 127 | case "json": |
| 128 | log.Formatter = &logrus.JSONFormatter{} |
| 129 | default: |
| 130 | log.Formatter = &logrus.TextFormatter{ |
| 131 | FullTimestamp: true, |
| 132 | ForceColors: true, |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | level, err := logrus.ParseLevel(config.LogLevel) |
| 137 | if err != nil { |
| 138 | level = logrus.WarnLevel |
| 139 | } |
| 140 | log.Level = level |
| 141 | |
David K. Bainbridge | 6ea57c1 | 2016-06-06 23:29:12 -0700 | [diff] [blame] | 142 | options.ProvisionTTL, err = time.ParseDuration(config.ProvisionTtl) |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 143 | checkError(err, "unable to parse specified duration of '%s' : %s", err) |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 144 | |
| 145 | // Determine the filter, this can either be specified on the the command |
| 146 | // line as a value or a file reference. If none is specified the default |
| 147 | // will be used |
David K. Bainbridge | e9d7af7 | 2016-10-14 08:42:55 -0700 | [diff] [blame] | 148 | if len(config.FilterSpec) > 0 { |
| 149 | if config.FilterSpec[0] == '@' { |
| 150 | name := os.ExpandEnv((config.FilterSpec)[1:]) |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 151 | file, err := os.OpenFile(name, os.O_RDONLY, 0) |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 152 | checkError(err, "unable to open file '%s' to load the filter : %s", name, err) |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 153 | decoder := json.NewDecoder(file) |
| 154 | err = decoder.Decode(&options.Filter) |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 155 | checkError(err, "unable to parse filter configuration from file '%s' : %s", name, err) |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 156 | } else { |
David K. Bainbridge | e9d7af7 | 2016-10-14 08:42:55 -0700 | [diff] [blame] | 157 | err := json.Unmarshal([]byte(config.FilterSpec), &options.Filter) |
| 158 | checkError(err, "unable to parse filter specification: '%s' : %s", config.FilterSpec, err) |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 159 | } |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // Determine the mac to name mapping, this can either be specified on the the command |
| 163 | // line as a value or a file reference. If none is specified the default |
| 164 | // will be used |
David K. Bainbridge | e9d7af7 | 2016-10-14 08:42:55 -0700 | [diff] [blame] | 165 | if len(config.Mappings) > 0 { |
| 166 | if config.Mappings[0] == '@' { |
| 167 | name := os.ExpandEnv(config.Mappings[1:]) |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 168 | file, err := os.OpenFile(name, os.O_RDONLY, 0) |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 169 | checkError(err, "unable to open file '%s' to load the mac name mapping : %s", name, err) |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 170 | decoder := json.NewDecoder(file) |
| 171 | err = decoder.Decode(&options.Mappings) |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 172 | checkError(err, "unable to parse filter configuration from file '%s' : %s", name, err) |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 173 | } else { |
David K. Bainbridge | e9d7af7 | 2016-10-14 08:42:55 -0700 | [diff] [blame] | 174 | err := json.Unmarshal([]byte(config.Mappings), &options.Mappings) |
| 175 | checkError(err, "unable to parse mac name mapping: '%s' : %s", config.Mappings, err) |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 176 | } |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 177 | } |
| 178 | |
David K. Bainbridge | e9d7af7 | 2016-10-14 08:42:55 -0700 | [diff] [blame] | 179 | // Get human readable strings for config output |
| 180 | mappingsAsJson, err := json.Marshal(options.Mappings) |
| 181 | checkError(err, "Unable to marshal MAC to hostname mappings to JSON : %s", err) |
| 182 | mappingsPrefix := "" |
| 183 | |
| 184 | if len(config.Mappings) > 0 && config.Mappings[0] == '@' { |
| 185 | mappingsPrefix = "[" + config.Mappings + "]" |
| 186 | } |
| 187 | |
| 188 | filterAsJson, err := json.Marshal(options.Filter) |
| 189 | checkError(err, "Unable to marshal host filter to JSON : %s", err) |
| 190 | filterPrefix := "" |
| 191 | if len(config.FilterSpec) > 0 && config.FilterSpec[0] == '@' { |
| 192 | filterPrefix = "[" + config.FilterSpec + "]" |
| 193 | } |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 194 | |
David K. Bainbridge | 4f8143d | 2016-10-27 17:14:48 -0700 | [diff] [blame] | 195 | re := regexp.MustCompile("[^:]") |
| 196 | pubKey := config.ApiKey |
| 197 | if !config.ShowApiKey { |
| 198 | pubKey = re.ReplaceAllString(config.ApiKey, "X") |
| 199 | } |
| 200 | |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 201 | log.Infof(`Configuration: |
David K. Bainbridge | 4f8143d | 2016-10-27 17:14:48 -0700 | [diff] [blame] | 202 | POWER_HELPER_USER: %s |
| 203 | POWER_HELPER_HOST: %s |
| 204 | POWER_HELPER_SCRIPT: %s |
| 205 | PROVISION_URL: %s |
| 206 | PROVISION_TTL: %s |
| 207 | MAAS_URL: %s |
| 208 | MAAS_SHOW_API_KEY: %t |
| 209 | MAAS_API_KEY: %s |
| 210 | MAAS_API_KEY_FILE: %s |
| 211 | MAAS_API_VERSION: %s |
| 212 | MAAS_QUERY_INTERVAL: %s |
| 213 | HOST_FILTER_SPEC: %+v |
| 214 | MAC_TO_NAME_MAPPINGS: %+v |
| 215 | PREVIEW_ONLY: %t |
| 216 | ALWAYS_RENAME: %t |
| 217 | LOG_LEVEL: %s |
| 218 | LOG_FORMAT: %s`, |
David K. Bainbridge | e9d7af7 | 2016-10-14 08:42:55 -0700 | [diff] [blame] | 219 | config.PowerHelperUser, config.PowerHelperHost, config.PowerHelperScript, |
| 220 | config.ProvisionUrl, config.ProvisionTtl, |
David K. Bainbridge | 4f8143d | 2016-10-27 17:14:48 -0700 | [diff] [blame] | 221 | config.MaasUrl, config.ShowApiKey, |
| 222 | pubKey, config.ApiKeyFile, config.ApiVersion, config.QueryInterval, |
David K. Bainbridge | e9d7af7 | 2016-10-14 08:42:55 -0700 | [diff] [blame] | 223 | filterPrefix+string(filterAsJson), mappingsPrefix+string(mappingsAsJson), |
| 224 | config.PreviewOnly, config.AlwaysRename, |
| 225 | config.LogLevel, config.LogFormat) |
David K. Bainbridge | 37ccf1e | 2016-06-02 12:47:15 -0700 | [diff] [blame] | 226 | |
David K. Bainbridge | 4f8143d | 2016-10-27 17:14:48 -0700 | [diff] [blame] | 227 | // Attempt to load the API key from a file if it was not set via the environment |
| 228 | // and if the file exists |
| 229 | if config.ApiKey == "" { |
| 230 | log.Debugf("Attempting to read MAAS API key from file '%s', because it was not set via environment", config.ApiKeyFile) |
| 231 | keyBytes, err := ioutil.ReadFile(config.ApiKeyFile) |
| 232 | if err != nil { |
| 233 | log.Warnf("Failed to read MAAS API key from file '%s', was the file mounted as a volume? : %s ", |
| 234 | config.ApiKeyFile, err) |
| 235 | } else { |
| 236 | config.ApiKey = string(keyBytes) |
| 237 | if config.ShowApiKey { |
| 238 | pubKey = config.ApiKey |
| 239 | } else { |
| 240 | pubKey = re.ReplaceAllString(config.ApiKey, "X") |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
David K. Bainbridge | e9d7af7 | 2016-10-14 08:42:55 -0700 | [diff] [blame] | 245 | authClient, err := maas.NewAuthenticatedClient(config.MaasUrl, config.ApiKey, config.ApiVersion) |
| 246 | checkError(err, "Unable to use specified client key, '%s', to authenticate to the MAAS server: %s", |
David K. Bainbridge | 4f8143d | 2016-10-27 17:14:48 -0700 | [diff] [blame] | 247 | pubKey, err) |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 248 | |
| 249 | // Create an object through which we will communicate with MAAS |
| 250 | client := maas.NewMAAS(*authClient) |
| 251 | |
| 252 | // This utility essentially polls the MAAS server for node state and |
| 253 | // process the node to the next state. This is done by kicking off the |
| 254 | // process every specified duration. This means that the first processing of |
| 255 | // nodes will have "period" in the future. This is really not the behavior |
| 256 | // we want, we really want, do it now, and then do the next one in "period". |
| 257 | // So, the code does one now. |
| 258 | nodes, _ := fetchNodes(client) |
| 259 | ProcessAll(client, nodes, options) |
| 260 | |
David K. Bainbridge | e9d7af7 | 2016-10-14 08:42:55 -0700 | [diff] [blame] | 261 | if !(config.PreviewOnly) { |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 262 | // Create a ticker and fetch and process the nodes every "period" |
David K. Bainbridge | e9d7af7 | 2016-10-14 08:42:55 -0700 | [diff] [blame] | 263 | for { |
| 264 | log.Infof("query server at %s", time.Now()) |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 265 | nodes, _ := fetchNodes(client) |
| 266 | ProcessAll(client, nodes, options) |
David K. Bainbridge | e9d7af7 | 2016-10-14 08:42:55 -0700 | [diff] [blame] | 267 | |
| 268 | // Sleep for the Interval and then process again. |
| 269 | time.Sleep(config.QueryInterval) |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | } |