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