David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 4 | "encoding/json" |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 5 | "fmt" |
| 6 | "log" |
| 7 | "net/url" |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 8 | "os/exec" |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 9 | "regexp" |
| 10 | "strconv" |
| 11 | "strings" |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 12 | "time" |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 13 | |
| 14 | maas "github.com/juju/gomaasapi" |
| 15 | ) |
| 16 | |
| 17 | // Action how to get from there to here |
| 18 | type Action func(*maas.MAASObject, MaasNode, ProcessingOptions) error |
| 19 | |
| 20 | // Transition the map from where i want to be from where i might be |
| 21 | type Transition struct { |
| 22 | Target string |
| 23 | Current string |
| 24 | Using Action |
| 25 | } |
| 26 | |
David K. Bainbridge | 6ea57c1 | 2016-06-06 23:29:12 -0700 | [diff] [blame] | 27 | type Power struct { |
| 28 | Name string `json:"name"` |
| 29 | MacAddress string `json:"mac_address"` |
| 30 | PowerPassword string `json:"power_password"` |
| 31 | PowerAddress string `json:"power_address"` |
| 32 | } |
| 33 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 34 | // ProcessingOptions used to determine on what hosts to operate |
| 35 | type ProcessingOptions struct { |
| 36 | Filter struct { |
| 37 | Zones struct { |
| 38 | Include []string |
| 39 | Exclude []string |
| 40 | } |
| 41 | Hosts struct { |
| 42 | Include []string |
| 43 | Exclude []string |
| 44 | } |
| 45 | } |
David K. Bainbridge | 6ea57c1 | 2016-06-06 23:29:12 -0700 | [diff] [blame] | 46 | Mappings map[string]interface{} |
| 47 | Verbose bool |
| 48 | Preview bool |
| 49 | AlwaysRename bool |
David K. Bainbridge | 068e87d | 2016-06-30 13:53:19 -0700 | [diff] [blame^] | 50 | Provisioner Provisioner |
David K. Bainbridge | 6ea57c1 | 2016-06-06 23:29:12 -0700 | [diff] [blame] | 51 | ProvisionURL string |
| 52 | ProvisionTTL time.Duration |
| 53 | PowerHelper string |
| 54 | PowerHelperUser string |
| 55 | PowerHelperHost string |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | // Transitions the actual map |
| 59 | // |
| 60 | // Currently this is a hand compiled / optimized "next step" table. This should |
| 61 | // really be generated from the state machine chart input. Once this has been |
| 62 | // accomplished you should be able to determine the action to take given your |
| 63 | // target state and your current state. |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 64 | var Transitions = map[string]map[string][]Action{ |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 65 | "Deployed": { |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 66 | "New": []Action{Reset, Commission}, |
| 67 | "Deployed": []Action{Provision, Done}, |
| 68 | "Ready": []Action{Reset, Aquire}, |
| 69 | "Allocated": []Action{Reset, Deploy}, |
| 70 | "Retired": []Action{Reset, AdminState}, |
| 71 | "Reserved": []Action{Reset, AdminState}, |
| 72 | "Releasing": []Action{Reset, Wait}, |
| 73 | "DiskErasing": []Action{Reset, Wait}, |
| 74 | "Deploying": []Action{Reset, Wait}, |
| 75 | "Commissioning": []Action{Reset, Wait}, |
| 76 | "Missing": []Action{Reset, Fail}, |
| 77 | "FailedReleasing": []Action{Reset, Fail}, |
| 78 | "FailedDiskErasing": []Action{Reset, Fail}, |
| 79 | "FailedDeployment": []Action{Reset, Fail}, |
| 80 | "Broken": []Action{Reset, Fail}, |
| 81 | "FailedCommissioning": []Action{Reset, Fail}, |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 82 | }, |
| 83 | } |
| 84 | |
| 85 | const ( |
| 86 | // defaultStateMachine Would be nice to drive from a graph language |
| 87 | defaultStateMachine string = ` |
David K. Bainbridge | d9b966f | 2016-05-31 13:30:05 -0700 | [diff] [blame] | 88 | (New)->(Commissioning) |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 89 | (Commissioning)->(FailedCommissioning) |
| 90 | (FailedCommissioning)->(New) |
| 91 | (Commissioning)->(Ready) |
| 92 | (Ready)->(Deploying) |
| 93 | (Ready)->(Allocated) |
| 94 | (Allocated)->(Deploying) |
| 95 | (Deploying)->(Deployed) |
| 96 | (Deploying)->(FailedDeployment) |
| 97 | (FailedDeployment)->(Broken) |
| 98 | (Deployed)->(Releasing) |
| 99 | (Releasing)->(FailedReleasing) |
| 100 | (FailedReleasing)->(Broken) |
| 101 | (Releasing)->(DiskErasing) |
| 102 | (DiskErasing)->(FailedEraseDisk) |
| 103 | (FailedEraseDisk)->(Broken) |
| 104 | (Releasing)->(Ready) |
| 105 | (DiskErasing)->(Ready) |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 106 | (Broken)->(Ready) |
David K. Bainbridge | d9b966f | 2016-05-31 13:30:05 -0700 | [diff] [blame] | 107 | (Deployed)->(Provisioning) |
| 108 | (Provisioning)->|a| |
| 109 | |a|->(Execute Script)->|b| |
| 110 | |a|->(HTTP PUT) |
| 111 | (HTTP PUT)->(HTTP GET) |
| 112 | (HTTP GET)->(HTTP GET) |
| 113 | (HTTP GET)->|b| |
| 114 | |b|->(Provisioned) |
| 115 | |b|->(ProvisionError) |
| 116 | (ProvisionError)->(Provisioning)` |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 117 | ) |
| 118 | |
| 119 | // updateName - changes the name of the MAAS node based on the configuration file |
| 120 | func updateNodeName(client *maas.MAASObject, node MaasNode, options ProcessingOptions) error { |
| 121 | macs := node.MACs() |
| 122 | |
| 123 | // Get current node name and strip off domain name |
| 124 | current := node.Hostname() |
| 125 | if i := strings.IndexRune(current, '.'); i != -1 { |
| 126 | current = current[:i] |
| 127 | } |
| 128 | for _, mac := range macs { |
| 129 | if entry, ok := options.Mappings[mac]; ok { |
| 130 | if name, ok := entry.(map[string]interface{})["hostname"]; ok && current != name.(string) { |
| 131 | nodesObj := client.GetSubObject("nodes") |
| 132 | nodeObj := nodesObj.GetSubObject(node.ID()) |
| 133 | log.Printf("RENAME '%s' to '%s'\n", node.Hostname(), name.(string)) |
| 134 | |
| 135 | if !options.Preview { |
| 136 | nodeObj.Update(url.Values{"hostname": []string{name.(string)}}) |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | return nil |
| 142 | } |
| 143 | |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 144 | // Reset we are at the target state, nothing to do |
| 145 | var Reset = func(client *maas.MAASObject, node MaasNode, options ProcessingOptions) error { |
| 146 | if options.Verbose { |
| 147 | log.Printf("RESET: %s", node.Hostname()) |
| 148 | } |
| 149 | |
| 150 | if options.AlwaysRename { |
| 151 | updateNodeName(client, node, options) |
| 152 | } |
| 153 | |
David K. Bainbridge | 068e87d | 2016-06-30 13:53:19 -0700 | [diff] [blame^] | 154 | options.Provisioner.Clear(node.ID()) |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 155 | |
| 156 | return nil |
| 157 | } |
| 158 | |
| 159 | // Provision we are at the target state, nothing to do |
| 160 | var Provision = func(client *maas.MAASObject, node MaasNode, options ProcessingOptions) error { |
| 161 | if options.Verbose { |
| 162 | log.Printf("CHECK PROVISION: %s", node.Hostname()) |
| 163 | } |
| 164 | |
| 165 | if options.AlwaysRename { |
| 166 | updateNodeName(client, node, options) |
| 167 | } |
| 168 | |
David K. Bainbridge | 068e87d | 2016-06-30 13:53:19 -0700 | [diff] [blame^] | 169 | record, err := options.Provisioner.Get(node.ID()) |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 170 | if err != nil { |
| 171 | log.Printf("[warn] unable to retrieve provisioning state of node '%s' : %s", node.Hostname(), err) |
David K. Bainbridge | 068e87d | 2016-06-30 13:53:19 -0700 | [diff] [blame^] | 172 | } else if record == nil || record.Status == Failed { |
| 173 | var label string |
| 174 | if record == nil { |
| 175 | label = "NotFound" |
| 176 | } else { |
| 177 | label = record.Status.String() |
| 178 | } |
David K. Bainbridge | 3ee7641 | 2016-06-15 18:56:08 -0700 | [diff] [blame] | 179 | if options.Verbose { |
David K. Bainbridge | 068e87d | 2016-06-30 13:53:19 -0700 | [diff] [blame^] | 180 | log.Printf("[info] Current state of node '%s' is '%s'", node.Hostname(), label) |
David K. Bainbridge | 3ee7641 | 2016-06-15 18:56:08 -0700 | [diff] [blame] | 181 | } |
David K. Bainbridge | 068e87d | 2016-06-30 13:53:19 -0700 | [diff] [blame^] | 182 | ips := node.IPs() |
| 183 | ip := "" |
| 184 | if len(ips) > 0 { |
| 185 | ip = ips[0] |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 186 | } |
David K. Bainbridge | 068e87d | 2016-06-30 13:53:19 -0700 | [diff] [blame^] | 187 | macs := node.MACs() |
| 188 | mac := "" |
| 189 | if len(macs) > 0 { |
| 190 | mac = macs[0] |
| 191 | } |
| 192 | if options.Verbose { |
| 193 | log.Printf("[info] POSTing '%s' (%s) to '%s'", node.Hostname(), |
| 194 | node.ID(), options.ProvisionURL) |
| 195 | } |
| 196 | err = options.Provisioner.Provision(&ProvisionRequest{ |
| 197 | Id: node.ID(), |
| 198 | Name: node.Hostname(), |
| 199 | Ip: ip, |
| 200 | Mac: mac, |
| 201 | }) |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 202 | |
| 203 | if err != nil { |
David K. Bainbridge | 068e87d | 2016-06-30 13:53:19 -0700 | [diff] [blame^] | 204 | log.Printf("[error] unable to provision '%s' (%s) : %s", node.ID, node.Hostname(), err) |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 205 | } |
David K. Bainbridge | 068e87d | 2016-06-30 13:53:19 -0700 | [diff] [blame^] | 206 | |
| 207 | } else if options.ProvisionTTL > 0 && |
| 208 | record.Status == Running && time.Since(time.Unix(record.Timestamp, 0)) > options.ProvisionTTL { |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 209 | log.Printf("[error] Provisioning of node '%s' has passed provisioning TTL of '%v'", |
| 210 | node.Hostname(), options.ProvisionTTL) |
David K. Bainbridge | 068e87d | 2016-06-30 13:53:19 -0700 | [diff] [blame^] | 211 | options.Provisioner.Clear(node.ID()) |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 212 | } else if options.Verbose { |
David K. Bainbridge | 218fdd6 | 2016-06-15 10:31:38 -0700 | [diff] [blame] | 213 | log.Printf("[info] Not invoking provisioning for '%s', current state is '%s'", node.Hostname(), |
David K. Bainbridge | 068e87d | 2016-06-30 13:53:19 -0700 | [diff] [blame^] | 214 | record.Status.String()) |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | return nil |
| 218 | } |
| 219 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 220 | // Done we are at the target state, nothing to do |
| 221 | var Done = func(client *maas.MAASObject, node MaasNode, options ProcessingOptions) error { |
| 222 | // As devices are normally in the "COMPLETED" state we don't want to |
| 223 | // log this fact unless we are in verbose mode. I suspect it would be |
| 224 | // nice to log it once when the device transitions from a non COMPLETE |
| 225 | // state to a complete state, but that would require keeping state. |
| 226 | if options.Verbose { |
| 227 | log.Printf("COMPLETE: %s", node.Hostname()) |
| 228 | } |
| 229 | |
| 230 | if options.AlwaysRename { |
| 231 | updateNodeName(client, node, options) |
| 232 | } |
| 233 | |
| 234 | return nil |
| 235 | } |
| 236 | |
| 237 | // Deploy cause a node to deploy |
| 238 | var Deploy = func(client *maas.MAASObject, node MaasNode, options ProcessingOptions) error { |
| 239 | log.Printf("DEPLOY: %s", node.Hostname()) |
| 240 | |
| 241 | if options.AlwaysRename { |
| 242 | updateNodeName(client, node, options) |
| 243 | } |
| 244 | |
| 245 | if !options.Preview { |
| 246 | nodesObj := client.GetSubObject("nodes") |
| 247 | myNode := nodesObj.GetSubObject(node.ID()) |
| 248 | // Start the node with the trusty distro. This should really be looked up or |
| 249 | // a parameter default |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 250 | _, err := myNode.CallPost("start", url.Values{"distro_series": []string{"trusty"}}) |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 251 | if err != nil { |
| 252 | log.Printf("ERROR: DEPLOY '%s' : '%s'", node.Hostname(), err) |
| 253 | return err |
| 254 | } |
| 255 | } |
| 256 | return nil |
| 257 | } |
| 258 | |
| 259 | // Aquire aquire a machine to a specific operator |
| 260 | var Aquire = func(client *maas.MAASObject, node MaasNode, options ProcessingOptions) error { |
| 261 | log.Printf("AQUIRE: %s", node.Hostname()) |
| 262 | nodesObj := client.GetSubObject("nodes") |
| 263 | |
| 264 | if options.AlwaysRename { |
| 265 | updateNodeName(client, node, options) |
| 266 | } |
| 267 | |
| 268 | if !options.Preview { |
| 269 | // With a new version of MAAS we have to make sure the node is linked |
| 270 | // to the subnet vid DHCP before we move to the Aquire state. To do this |
| 271 | // We need to unlink the interface to the subnet and then relink it. |
| 272 | // |
| 273 | // Iterate through all the interfaces on the node, searching for ones |
| 274 | // that are valid and not DHCP and move them to DHCP |
| 275 | ifcsObj := client.GetSubObject("nodes").GetSubObject(node.ID()).GetSubObject("interfaces") |
| 276 | ifcsListObj, err := ifcsObj.CallGet("", url.Values{}) |
| 277 | if err != nil { |
| 278 | return err |
| 279 | } |
| 280 | |
| 281 | ifcsArray, err := ifcsListObj.GetArray() |
| 282 | if err != nil { |
| 283 | return err |
| 284 | } |
| 285 | |
| 286 | for _, ifc := range ifcsArray { |
| 287 | ifcMap, err := ifc.GetMap() |
| 288 | if err != nil { |
| 289 | return err |
| 290 | } |
| 291 | |
| 292 | // Iterate over the links assocated with the interface, looking for |
| 293 | // links with a subnect as well as a mode of "auto" |
| 294 | links, ok := ifcMap["links"] |
| 295 | if ok { |
| 296 | linkArray, err := links.GetArray() |
| 297 | if err != nil { |
| 298 | return err |
| 299 | } |
| 300 | |
| 301 | for _, link := range linkArray { |
| 302 | linkMap, err := link.GetMap() |
| 303 | if err != nil { |
| 304 | return err |
| 305 | } |
| 306 | subnet, ok := linkMap["subnet"] |
| 307 | if ok { |
| 308 | subnetMap, err := subnet.GetMap() |
| 309 | if err != nil { |
| 310 | return err |
| 311 | } |
| 312 | |
| 313 | val, err := linkMap["mode"].GetString() |
| 314 | if err != nil { |
| 315 | return err |
| 316 | } |
| 317 | |
| 318 | if val == "auto" { |
| 319 | // Found one we like, so grab the subnet from the data and |
| 320 | // then relink this as DHCP |
| 321 | cidr, err := subnetMap["cidr"].GetString() |
| 322 | if err != nil { |
| 323 | return err |
| 324 | } |
| 325 | |
| 326 | fifcID, err := ifcMap["id"].GetFloat64() |
| 327 | if err != nil { |
| 328 | return err |
| 329 | } |
| 330 | ifcID := strconv.Itoa(int(fifcID)) |
| 331 | |
| 332 | flID, err := linkMap["id"].GetFloat64() |
| 333 | if err != nil { |
| 334 | return err |
| 335 | } |
| 336 | lID := strconv.Itoa(int(flID)) |
| 337 | |
| 338 | ifcObj := ifcsObj.GetSubObject(ifcID) |
| 339 | _, err = ifcObj.CallPost("unlink_subnet", url.Values{"id": []string{lID}}) |
| 340 | if err != nil { |
| 341 | return err |
| 342 | } |
| 343 | _, err = ifcObj.CallPost("link_subnet", url.Values{"mode": []string{"DHCP"}, "subnet": []string{cidr}}) |
| 344 | if err != nil { |
| 345 | return err |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | _, err = nodesObj.CallPost("acquire", |
| 353 | url.Values{"name": []string{node.Hostname()}}) |
| 354 | if err != nil { |
| 355 | log.Printf("ERROR: AQUIRE '%s' : '%s'", node.Hostname(), err) |
| 356 | return err |
| 357 | } |
| 358 | } |
| 359 | return nil |
| 360 | } |
| 361 | |
| 362 | // Commission cause a node to be commissioned |
| 363 | var Commission = func(client *maas.MAASObject, node MaasNode, options ProcessingOptions) error { |
| 364 | updateNodeName(client, node, options) |
| 365 | |
| 366 | // Need to understand the power state of the node. We only want to move to "Commissioning" if the node |
| 367 | // power is off. If the node power is not off, then turn it off. |
| 368 | state := node.PowerState() |
| 369 | switch state { |
| 370 | case "on": |
| 371 | // Attempt to turn the node off |
| 372 | log.Printf("POWER DOWN: %s", node.Hostname()) |
| 373 | if !options.Preview { |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 374 | //POST /api/1.0/nodes/{system_id}/ op=stop |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 375 | nodesObj := client.GetSubObject("nodes") |
| 376 | nodeObj := nodesObj.GetSubObject(node.ID()) |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 377 | _, err := nodeObj.CallPost("stop", url.Values{"stop_mode": []string{"soft"}}) |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 378 | if err != nil { |
| 379 | log.Printf("ERROR: Commission '%s' : changing power start to off : '%s'", node.Hostname(), err) |
| 380 | } |
| 381 | return err |
| 382 | } |
| 383 | break |
| 384 | case "off": |
| 385 | // We are off so move to commissioning |
| 386 | log.Printf("COMISSION: %s", node.Hostname()) |
| 387 | if !options.Preview { |
| 388 | nodesObj := client.GetSubObject("nodes") |
| 389 | nodeObj := nodesObj.GetSubObject(node.ID()) |
| 390 | |
| 391 | updateNodeName(client, node, options) |
| 392 | |
| 393 | _, err := nodeObj.CallPost("commission", url.Values{}) |
| 394 | if err != nil { |
| 395 | log.Printf("ERROR: Commission '%s' : '%s'", node.Hostname(), err) |
| 396 | } |
| 397 | return err |
| 398 | } |
| 399 | break |
| 400 | default: |
| 401 | // We are in a state from which we can't move forward. |
David K. Bainbridge | 6ea57c1 | 2016-06-06 23:29:12 -0700 | [diff] [blame] | 402 | log.Printf("[warn]: %s has invalid power state '%s'", node.Hostname(), state) |
| 403 | |
| 404 | // If a power helper script is set, we have an unknown power state, and |
| 405 | // we have not power type then attempt to use the helper script to discover |
| 406 | // and set the power settings |
| 407 | if options.PowerHelper != "" && node.PowerType() == "" { |
| 408 | cmd := exec.Command(options.PowerHelper, |
| 409 | append([]string{options.PowerHelperUser, options.PowerHelperHost}, |
| 410 | node.MACs()...)...) |
| 411 | stdout, err := cmd.Output() |
| 412 | if err != nil { |
| 413 | log.Printf("[error] Failed while executing power helper script '%s' : %s", |
| 414 | options.PowerHelper, err) |
| 415 | return err |
| 416 | } |
| 417 | power := Power{} |
| 418 | err = json.Unmarshal(stdout, &power) |
| 419 | if err != nil { |
| 420 | log.Printf("[error] Failed to parse output of power helper script '%s' : %s", |
| 421 | options.PowerHelper, err) |
| 422 | return err |
| 423 | } |
| 424 | switch power.Name { |
| 425 | case "amt": |
| 426 | params := map[string]string{ |
| 427 | "mac_address": power.MacAddress, |
| 428 | "power_pass": power.PowerPassword, |
| 429 | "power_address": power.PowerAddress, |
| 430 | } |
| 431 | node.UpdatePowerParameters(power.Name, params) |
| 432 | default: |
| 433 | log.Printf("[warn] Unsupported power type discovered '%s'", power.Name) |
| 434 | } |
| 435 | } |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 436 | break |
| 437 | } |
| 438 | return nil |
| 439 | } |
| 440 | |
| 441 | // Wait a do nothing state, while work is being done |
| 442 | var Wait = func(client *maas.MAASObject, node MaasNode, options ProcessingOptions) error { |
| 443 | log.Printf("WAIT: %s", node.Hostname()) |
| 444 | return nil |
| 445 | } |
| 446 | |
| 447 | // Fail a state from which we cannot, currently, automatically recover |
| 448 | var Fail = func(client *maas.MAASObject, node MaasNode, options ProcessingOptions) error { |
| 449 | log.Printf("FAIL: %s", node.Hostname()) |
| 450 | return nil |
| 451 | } |
| 452 | |
| 453 | // AdminState an administrative state from which we should make no automatic transition |
| 454 | var AdminState = func(client *maas.MAASObject, node MaasNode, options ProcessingOptions) error { |
| 455 | log.Printf("ADMIN: %s", node.Hostname()) |
| 456 | return nil |
| 457 | } |
| 458 | |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 459 | func findActions(target string, current string) ([]Action, error) { |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 460 | targets, ok := Transitions[target] |
| 461 | if !ok { |
| 462 | log.Printf("[warn] unable to find transitions to target state '%s'", target) |
| 463 | return nil, fmt.Errorf("Could not find transition to target state '%s'", target) |
| 464 | } |
| 465 | |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 466 | actions, ok := targets[current] |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 467 | if !ok { |
| 468 | log.Printf("[warn] unable to find transition from current state '%s' to target state '%s'", |
| 469 | current, target) |
| 470 | return nil, fmt.Errorf("Could not find transition from current state '%s' to target state '%s'", |
| 471 | current, target) |
| 472 | } |
| 473 | |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 474 | return actions, nil |
| 475 | } |
| 476 | |
| 477 | // ProcessActions |
| 478 | func ProcessActions(actions []Action, client *maas.MAASObject, node MaasNode, options ProcessingOptions) error { |
| 479 | var err error |
| 480 | for _, action := range actions { |
| 481 | if err = action(client, node, options); err != nil { |
David K. Bainbridge | 6ea57c1 | 2016-06-06 23:29:12 -0700 | [diff] [blame] | 482 | log.Printf("[error] Error while processing action for node '%s' : %s", |
| 483 | node.Hostname(), err) |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 484 | break |
| 485 | } |
| 486 | } |
| 487 | return err |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | // ProcessNode something |
| 491 | func ProcessNode(client *maas.MAASObject, node MaasNode, options ProcessingOptions) error { |
| 492 | substatus, err := node.GetInteger("substatus") |
| 493 | if err != nil { |
| 494 | return err |
| 495 | } |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 496 | actions, err := findActions("Deployed", MaasNodeStatus(substatus).String()) |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 497 | if err != nil { |
| 498 | return err |
| 499 | } |
| 500 | |
| 501 | if options.Preview { |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 502 | ProcessActions(actions, client, node, options) |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 503 | } else { |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 504 | go ProcessActions(actions, client, node, options) |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 505 | } |
| 506 | return nil |
| 507 | } |
| 508 | |
| 509 | func buildFilter(filter []string) ([]*regexp.Regexp, error) { |
| 510 | |
| 511 | results := make([]*regexp.Regexp, len(filter)) |
| 512 | for i, v := range filter { |
| 513 | r, err := regexp.Compile(v) |
| 514 | if err != nil { |
| 515 | return nil, err |
| 516 | } |
| 517 | results[i] = r |
| 518 | } |
| 519 | return results, nil |
| 520 | } |
| 521 | |
| 522 | func matchedFilter(include []*regexp.Regexp, target string) bool { |
| 523 | for _, e := range include { |
| 524 | if e.MatchString(target) { |
| 525 | return true |
| 526 | } |
| 527 | } |
| 528 | return false |
| 529 | } |
| 530 | |
| 531 | // ProcessAll something |
| 532 | func ProcessAll(client *maas.MAASObject, nodes []MaasNode, options ProcessingOptions) []error { |
| 533 | errors := make([]error, len(nodes)) |
| 534 | includeHosts, err := buildFilter(options.Filter.Hosts.Include) |
| 535 | if err != nil { |
| 536 | log.Fatalf("[error] invalid regular expression for include filter '%s' : %s", options.Filter.Hosts.Include, err) |
| 537 | } |
| 538 | |
| 539 | includeZones, err := buildFilter(options.Filter.Zones.Include) |
| 540 | if err != nil { |
| 541 | log.Fatalf("[error] invalid regular expression for include filter '%v' : %s", options.Filter.Zones.Include, err) |
| 542 | } |
| 543 | |
| 544 | for i, node := range nodes { |
| 545 | // For hostnames we always match on an empty filter |
| 546 | if len(includeHosts) >= 0 && matchedFilter(includeHosts, node.Hostname()) { |
| 547 | |
| 548 | // For zones we don't match on an empty filter |
| 549 | if len(includeZones) >= 0 && matchedFilter(includeZones, node.Zone()) { |
| 550 | err := ProcessNode(client, node, options) |
| 551 | if err != nil { |
| 552 | errors[i] = err |
| 553 | } else { |
| 554 | errors[i] = nil |
| 555 | } |
| 556 | } else { |
| 557 | if options.Verbose { |
| 558 | log.Printf("[info] ignoring node '%s' as its zone '%s' didn't match include zone name filter '%v'", |
| 559 | node.Hostname(), node.Zone(), options.Filter.Zones.Include) |
| 560 | } |
| 561 | } |
| 562 | } else { |
| 563 | if options.Verbose { |
| 564 | log.Printf("[info] ignoring node '%s' as it didn't match include hostname filter '%v'", |
| 565 | node.Hostname(), options.Filter.Hosts.Include) |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | return errors |
| 570 | } |