check point commit to ensure things are saved in more than one place. this commit contains the first integration of the docker build artifacts as well as the first integration of an automation test environment for MAAS based on virtual box
Change-Id: I236f12392501b4ed589aba2b748ba0c45e148f2e
diff --git a/automation/Dockerfile b/automation/Dockerfile
new file mode 100644
index 0000000..5e1be43
--- /dev/null
+++ b/automation/Dockerfile
@@ -0,0 +1,8 @@
+FROM golang:alpine
+
+RUN apk --update add git
+
+WORKDIR /go
+RUN go get github.com/ciena/cord-maas-automation
+
+ENTRYPOINT ["/go/bin/cord-maas-automation"]
diff --git a/automation/Godeps/Godeps.json b/automation/Godeps/Godeps.json
new file mode 100644
index 0000000..77ff54f
--- /dev/null
+++ b/automation/Godeps/Godeps.json
@@ -0,0 +1,18 @@
+{
+ "ImportPath": "_/Users/dbainbri/src/develop/mf",
+ "GoVersion": "go1.5",
+ "Packages": [
+ "github.com/ciena/maas-flow"
+ ],
+ "Deps": [
+ {
+ "ImportPath": "github.com/juju/gomaasapi",
+ "Rev": "e173bc8d8d3304ff11b0ded5f6d4eea0cb560a40"
+ },
+ {
+ "ImportPath": "gopkg.in/mgo.v2/bson",
+ "Comment": "r2015.12.06-2-g03c9f3e",
+ "Rev": "03c9f3ee4c14c8e51ee521a6a7d0425658dd6f64"
+ }
+ ]
+}
diff --git a/automation/LICENSE b/automation/LICENSE
new file mode 100644
index 0000000..8dada3e
--- /dev/null
+++ b/automation/LICENSE
@@ -0,0 +1,201 @@
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "{}"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright {yyyy} {name of copyright owner}
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
diff --git a/automation/Makefile b/automation/Makefile
new file mode 100644
index 0000000..d3aa83a
--- /dev/null
+++ b/automation/Makefile
@@ -0,0 +1,17 @@
+.PHONY: help
+help:
+ @echo "image - create docker image for the MAAS deploy flow utility"
+ @echo "save - save the docker image for the MAAS deployment flow utility to a local tar file"
+ @echo "clean - remove any generated files"
+ @echo "help - this message"
+
+.PHONY: image
+image:
+ docker build -t cord/maas-automation:0.1-prerelease .
+
+save: image
+ docker save -o cord_maas-automation.1-prerelease.tar cord/maas-automation:0.1-prerelease
+
+.PHONT: clean
+clean:
+ rm -f cord_maas-automation.1-prerelease.tar
diff --git a/automation/README.md b/automation/README.md
new file mode 100644
index 0000000..ee49648
--- /dev/null
+++ b/automation/README.md
@@ -0,0 +1,86 @@
+# Metal as a Service Automation (maas-flow)
+This is a utility that works in conjunction with an Ubuntu Metal as a Service
+([MAAS](http://maas.io)) deployment. By default, the MAAS system allows an
+operator to manually control the lifecycle of a compute host as it comes on
+line leveraging PXE, DHCP, DNS, etc.
+
+The utility leverages the MAAS REST API to periodically monitor the **status**
+of the hosts under control of MAAS and continuous attempts to move those hosts
+into a **deployed** state. (Note: this will likely change in the future to
+support additional target states.)
+
+### Filtering Hosts on which to Operate
+Using a filter the operator can control on which hosts automation acts. The
+filter is a basic **JSON** object and can either be specified as a string on
+the command line or a file which contains the filter. When specifying a file
+the value of the **-filter** command line option should be a **@** followed by
+the name of the file, i.e. @$HOME/some/file, and it may container environment
+variable.
+
+The structure of the filter object is:
+```
+{
+ "hosts" : {
+ "include" : [],
+ "exclude" : []
+ },
+ "zones" : {
+ "include" : [],
+ "exclude" : []
+ }
+}
+```
+For **hosts** the **include** and **exclude** values are a list of regular
+expressions which are mapped against the hostname of a device under control of
+MAAS.
+
+for **zones** the **include** and **exclude** values are a list of regular
+expression which are mapped against the zone with which a host is associated.
+
+When both **include** and **exclude** values are specified the **include**
+is processed followed by the **exclude**.
+
+The default filter, if none is specified, is depicted below. Essentially it
+specifies that the automation will act on all hosts in only the **default**
+zone. (*NOTE: This default filter may change in the future.*)
+```
+{
+ "hosts" : {
+ "include" : [],
+ "exclude" : []
+ },
+ "zones" : {
+ "include" : ["default"],
+ "exclude" : []
+ }
+}
+```
+
+*NOTE:* only include is currently (January 26, 2016) supported.
+
+### Connecting to MAAS
+The connection to MAAS is controlled by command line parameters, specifically:
+* **-apiVersion** - (default: *1.0*) specifies the version of the MAAS API to use
+* **-apiKey** - (default: *none*) specifies the API key to use to authenticate to
+the MAAS server. For a given user this can be found on under their account
+settings in the MAAS UI. This value is important as the automation is acting
+on behalf of this user and the SSH keys that are pushed to hosts will be the
+SSH keys associated with this user.
+* **-maas** - (default: *http://localhost/MAAS*) specifies the base URL on which
+to contact the MAAS server.
+* **-period** - (default: *15s*) specifies how often the automation queries the
+MAAS server to retrieve the state of the hosts. Automation must query the state
+of the hosts from MAAS as MAAS does not support an asynchronous change
+mechanism today. This value should be set such that the automation can fully
+process all the hosts within a period.
+
+### Docker Image
+The project contains a `Dockerfile` that can be used to construct a docker
+image from the repository. The docker image is also provided via Docker Hub at
+https://hub.docker.com/r/ciena/maas-flow/.
+
+### State machine
+The state machine on which the MAAS automation is based is depicted below.
+Currently (January 26, 2016) the automation only supports a deployed target
+state and will not act on hosts that are in a failed, broken, or error state.
+![](lifecycle.png)
diff --git a/automation/lifecycle.png b/automation/lifecycle.png
new file mode 100644
index 0000000..422f247
--- /dev/null
+++ b/automation/lifecycle.png
Binary files differ
diff --git a/automation/maas-flow.go b/automation/maas-flow.go
new file mode 100644
index 0000000..1514e32
--- /dev/null
+++ b/automation/maas-flow.go
@@ -0,0 +1,167 @@
+package main
+
+import (
+ "encoding/json"
+ "flag"
+ "log"
+ "net/url"
+ "os"
+ "strings"
+ "time"
+ "unicode"
+
+ maas "github.com/juju/gomaasapi"
+)
+
+const (
+ // defaultFilter specifies the default filter to use when none is specified
+ defaultFilter = `{
+ "hosts" : {
+ "include" : [ ".*" ],
+ "exclude" : []
+ },
+ "zones" : {
+ "include" : ["default"],
+ "exclude" : []
+ }
+ }`
+ defaultMapping = "{}"
+)
+
+var apiKey = flag.String("apikey", "", "key with which to access MAAS server")
+var maasURL = flag.String("maas", "http://localhost/MAAS", "url over which to access MAAS")
+var apiVersion = flag.String("apiVersion", "1.0", "version of the API to access")
+var queryPeriod = flag.String("period", "15s", "frequency the MAAS service is polled for node states")
+var preview = flag.Bool("preview", false, "displays the action that would be taken, but does not do the action, in this mode the nodes are processed only once")
+var mappings = flag.String("mappings", "{}", "the mac to name mappings")
+var always = flag.Bool("always-rename", true, "attempt to rename at every stage of workflow")
+var verbose = flag.Bool("verbose", false, "display verbose logging")
+var filterSpec = flag.String("filter", strings.Map(func(r rune) rune {
+ if unicode.IsSpace(r) {
+ return -1
+ }
+ return r
+}, defaultFilter), "constrain by hostname what will be automated")
+
+// checkError if the given err is not nil, then fatally log the message, else
+// return false.
+func checkError(err error, message string, v ...interface{}) bool {
+ if err != nil {
+ log.Fatalf("[error] "+message, v)
+ }
+ return false
+}
+
+// checkWarn if the given err is not nil, then log the message as a warning and
+// return true, else return false.
+func checkWarn(err error, message string, v ...interface{}) bool {
+ if err != nil {
+ log.Printf("[warn] "+message, v)
+ return true
+ }
+ return false
+}
+
+// fetchNodes do a HTTP GET to the MAAS server to query all the nodes
+func fetchNodes(client *maas.MAASObject) ([]MaasNode, error) {
+ nodeListing := client.GetSubObject("nodes")
+ listNodeObjects, err := nodeListing.CallGet("list", url.Values{})
+ if checkWarn(err, "unable to get the list of all nodes: %s", err) {
+ return nil, err
+ }
+ listNodes, err := listNodeObjects.GetArray()
+ if checkWarn(err, "unable to get the node objects for the list: %s", err) {
+ return nil, err
+ }
+
+ var nodes = make([]MaasNode, len(listNodes))
+ for index, nodeObj := range listNodes {
+ node, err := nodeObj.GetMAASObject()
+ if !checkWarn(err, "unable to retrieve object for node: %s", err) {
+ nodes[index] = MaasNode{node}
+ }
+ }
+ return nodes, nil
+}
+
+func main() {
+
+ flag.Parse()
+
+ options := ProcessingOptions{
+ Preview: *preview,
+ Verbose: *verbose,
+ AlwaysRename: *always,
+ }
+
+ // Determine the filter, this can either be specified on the the command
+ // line as a value or a file reference. If none is specified the default
+ // will be used
+ if len(*filterSpec) > 0 {
+ if (*filterSpec)[0] == '@' {
+ name := os.ExpandEnv((*filterSpec)[1:])
+ file, err := os.OpenFile(name, os.O_RDONLY, 0)
+ checkError(err, "[error] unable to open file '%s' to load the filter : %s", name, err)
+ decoder := json.NewDecoder(file)
+ err = decoder.Decode(&options.Filter)
+ checkError(err, "[error] unable to parse filter configuration from file '%s' : %s", name, err)
+ } else {
+ err := json.Unmarshal([]byte(*filterSpec), &options.Filter)
+ checkError(err, "[error] unable to parse filter specification: '%s' : %s", *filterSpec, err)
+ }
+ } else {
+ err := json.Unmarshal([]byte(defaultFilter), &options.Filter)
+ checkError(err, "[error] unable to parse default filter specificiation: '%s' : %s", defaultFilter, err)
+ }
+
+ // Determine the mac to name mapping, this can either be specified on the the command
+ // line as a value or a file reference. If none is specified the default
+ // will be used
+ if len(*mappings) > 0 {
+ if (*mappings)[0] == '@' {
+ name := os.ExpandEnv((*mappings)[1:])
+ file, err := os.OpenFile(name, os.O_RDONLY, 0)
+ checkError(err, "[error] unable to open file '%s' to load the mac name mapping : %s", name, err)
+ decoder := json.NewDecoder(file)
+ err = decoder.Decode(&options.Mappings)
+ checkError(err, "[error] unable to parse filter configuration from file '%s' : %s", name, err)
+ } else {
+ err := json.Unmarshal([]byte(*mappings), &options.Mappings)
+ checkError(err, "[error] unable to parse mac name mapping: '%s' : %s", *mappings, err)
+ }
+ } else {
+ err := json.Unmarshal([]byte(defaultMapping), &options.Mappings)
+ checkError(err, "[error] unable to parse default mac name mappings: '%s' : %s", defaultMapping, err)
+ }
+
+ // Verify the specified period for queries can be converted into a Go duration
+ period, err := time.ParseDuration(*queryPeriod)
+ checkError(err, "[error] unable to parse specified query period duration: '%s': %s", queryPeriod, err)
+
+ authClient, err := maas.NewAuthenticatedClient(*maasURL, *apiKey, *apiVersion)
+ if err != nil {
+ checkError(err, "[error] Unable to use specified client key, '%s', to authenticate to the MAAS server: %s", *apiKey, err)
+ }
+
+ // Create an object through which we will communicate with MAAS
+ client := maas.NewMAAS(*authClient)
+
+ // This utility essentially polls the MAAS server for node state and
+ // process the node to the next state. This is done by kicking off the
+ // process every specified duration. This means that the first processing of
+ // nodes will have "period" in the future. This is really not the behavior
+ // we want, we really want, do it now, and then do the next one in "period".
+ // So, the code does one now.
+ nodes, _ := fetchNodes(client)
+ ProcessAll(client, nodes, options)
+
+ if !(*preview) {
+ // Create a ticker and fetch and process the nodes every "period"
+ ticker := time.NewTicker(period)
+ for t := range ticker.C {
+ log.Printf("[info] query server at %s", t)
+ nodes, _ := fetchNodes(client)
+ ProcessAll(client, nodes, options)
+ }
+ }
+}
diff --git a/automation/mappings.json b/automation/mappings.json
new file mode 100644
index 0000000..be99f7a
--- /dev/null
+++ b/automation/mappings.json
@@ -0,0 +1,26 @@
+{
+ "2c:60:0c:e3:c0:f1":{
+ "hostname":"cord-r1-s1"
+ },
+ "2c:60:0c:e3:c4:bd":{
+ "hostname":"cord-r1-s2"
+ },
+ "2c:60:0c:e3:c2:83":{
+ "hostname":"cord-r1-s3"
+ },
+ "2c:60:0c:e3:bb:ae":{
+ "hostname":"cord-r1-s4"
+ },
+ "2c:60:0c:e3:bf:b0":{
+ "hostname":"cord-r1-s5"
+ },
+ "2c:60:0c:e3:be:ff":{
+ "hostname":"cord-r1-s6"
+ },
+ "2c:60:0c:e3:c5:fe":{
+ "hostname":"cord-r1-s7"
+ },
+ "2c:60:0c:e3:bd:10":{
+ "hostname":"cord-r1-s8"
+ }
+}
diff --git a/automation/node.go b/automation/node.go
new file mode 100644
index 0000000..d364fe0
--- /dev/null
+++ b/automation/node.go
@@ -0,0 +1,116 @@
+package main
+
+import (
+ "fmt"
+
+ maas "github.com/juju/gomaasapi"
+)
+
+// MaasNodeStatus MAAS lifecycle status for nodes
+type MaasNodeStatus int
+
+// MAAS Node Statuses
+const (
+ Invalid MaasNodeStatus = -1
+ New MaasNodeStatus = 0
+ Commissioning MaasNodeStatus = 1
+ FailedCommissioning MaasNodeStatus = 2
+ Missing MaasNodeStatus = 3
+ Ready MaasNodeStatus = 4
+ Reserved MaasNodeStatus = 5
+ Deployed MaasNodeStatus = 6
+ Retired MaasNodeStatus = 7
+ Broken MaasNodeStatus = 8
+ Deploying MaasNodeStatus = 9
+ Allocated MaasNodeStatus = 10
+ FailedDeployment MaasNodeStatus = 11
+ Releasing MaasNodeStatus = 12
+ FailedReleasing MaasNodeStatus = 13
+ DiskErasing MaasNodeStatus = 14
+ FailedDiskErasing MaasNodeStatus = 15
+)
+
+var names = []string{"New", "Commissioning", "FailedCommissioning", "Missing", "Ready", "Reserved",
+ "Deployed", "Retired", "Broken", "Deploying", "Allocated", "FailedDeployment",
+ "Releasing", "FailedReleasing", "DiskErasing", "FailedDiskErasing"}
+
+func (v MaasNodeStatus) String() string {
+ return names[v]
+}
+
+// FromString lookup the constant value for a given node state name
+func FromString(name string) (MaasNodeStatus, error) {
+ for i, v := range names {
+ if v == name {
+ return MaasNodeStatus(i), nil
+ }
+ }
+ return -1, fmt.Errorf("Unknown MAAS node state name, '%s'", name)
+}
+
+// MaasNode convenience wrapper for an MAAS node on top of a generic MAAS object
+type MaasNode struct {
+ maas.MAASObject
+}
+
+// GetString get attribute value as string
+func (n *MaasNode) GetString(key string) (string, error) {
+ return n.GetMap()[key].GetString()
+}
+
+// GetFloat64 get attribute value as float64
+func (n *MaasNode) GetFloat64(key string) (float64, error) {
+ return n.GetMap()[key].GetFloat64()
+}
+
+// ID get the system id of the node
+func (n *MaasNode) ID() string {
+ id, _ := n.GetString("system_id")
+ return id
+}
+
+func (n *MaasNode) PowerState() string {
+ state, _ := n.GetString("power_state")
+ return state
+}
+
+// Hostname get the hostname
+func (n *MaasNode) Hostname() string {
+ hn, _ := n.GetString("hostname")
+ return hn
+}
+
+// MACs get the MAC Addresses
+func (n *MaasNode) MACs() []string {
+ macsObj, _ := n.GetMap()["macaddress_set"]
+ macs, _ := macsObj.GetArray()
+ if len(macs) == 0 {
+ return []string{}
+ }
+ result := make([]string, len(macs))
+ for i, mac := range macs {
+ obj, _ := mac.GetMap()
+ addr, _ := obj["mac_address"]
+ s, _ := addr.GetString()
+ result[i] = s
+ }
+
+ return result
+}
+
+// Zone get the zone
+func (n *MaasNode) Zone() string {
+ zone := n.GetMap()["zone"]
+ attrs, _ := zone.GetMap()
+ v, _ := attrs["name"].GetString()
+ return v
+}
+
+// GetInteger get attribute value as integer
+func (n *MaasNode) GetInteger(key string) (int, error) {
+ v, err := n.GetMap()[key].GetFloat64()
+ if err != nil {
+ return 0, err
+ }
+ return int(v), nil
+}
diff --git a/automation/sample-filter.json b/automation/sample-filter.json
new file mode 100644
index 0000000..2a81a99
--- /dev/null
+++ b/automation/sample-filter.json
@@ -0,0 +1,14 @@
+{
+ "hosts":{
+ "include":[
+ ".*"
+ ]
+ },
+ "zones":{
+ "include":[
+ "default",
+ "petaluma-lab"
+ ]
+ }
+}
+
diff --git a/automation/state.go b/automation/state.go
new file mode 100644
index 0000000..4b94089
--- /dev/null
+++ b/automation/state.go
@@ -0,0 +1,422 @@
+package main
+
+import (
+ "fmt"
+ "log"
+ "net/url"
+ "regexp"
+ "strconv"
+ "strings"
+
+ maas "github.com/juju/gomaasapi"
+)
+
+// Action how to get from there to here
+type Action func(*maas.MAASObject, MaasNode, ProcessingOptions) error
+
+// Transition the map from where i want to be from where i might be
+type Transition struct {
+ Target string
+ Current string
+ Using Action
+}
+
+// ProcessingOptions used to determine on what hosts to operate
+type ProcessingOptions struct {
+ Filter struct {
+ Zones struct {
+ Include []string
+ Exclude []string
+ }
+ Hosts struct {
+ Include []string
+ Exclude []string
+ }
+ }
+ Mappings map[string]interface{}
+ Verbose bool
+ Preview bool
+ AlwaysRename bool
+}
+
+// Transitions the actual map
+//
+// Currently this is a hand compiled / optimized "next step" table. This should
+// really be generated from the state machine chart input. Once this has been
+// accomplished you should be able to determine the action to take given your
+// target state and your current state.
+var Transitions = map[string]map[string]Action{
+ "Deployed": {
+ "New": Commission,
+ "Deployed": Done,
+ "Ready": Aquire,
+ "Allocated": Deploy,
+ "Retired": AdminState,
+ "Reserved": AdminState,
+ "Releasing": Wait,
+ "DiskErasing": Wait,
+ "Deploying": Wait,
+ "Commissioning": Wait,
+ "Missing": Fail,
+ "FailedReleasing": Fail,
+ "FailedDiskErasing": Fail,
+ "FailedDeployment": Fail,
+ "Broken": Fail,
+ "FailedCommissioning": Fail,
+ },
+}
+
+const (
+ // defaultStateMachine Would be nice to drive from a graph language
+ defaultStateMachine string = `
+ (New)->(Commissioning)
+ (Commissioning)->(FailedCommissioning)
+ (FailedCommissioning)->(New)
+ (Commissioning)->(Ready)
+ (Ready)->(Deploying)
+ (Ready)->(Allocated)
+ (Allocated)->(Deploying)
+ (Deploying)->(Deployed)
+ (Deploying)->(FailedDeployment)
+ (FailedDeployment)->(Broken)
+ (Deployed)->(Releasing)
+ (Releasing)->(FailedReleasing)
+ (FailedReleasing)->(Broken)
+ (Releasing)->(DiskErasing)
+ (DiskErasing)->(FailedEraseDisk)
+ (FailedEraseDisk)->(Broken)
+ (Releasing)->(Ready)
+ (DiskErasing)->(Ready)
+ (Broken)->(Ready)`
+)
+
+// updateName - changes the name of the MAAS node based on the configuration file
+func updateNodeName(client *maas.MAASObject, node MaasNode, options ProcessingOptions) error {
+ macs := node.MACs()
+
+ // Get current node name and strip off domain name
+ current := node.Hostname()
+ if i := strings.IndexRune(current, '.'); i != -1 {
+ current = current[:i]
+ }
+ for _, mac := range macs {
+ if entry, ok := options.Mappings[mac]; ok {
+ if name, ok := entry.(map[string]interface{})["hostname"]; ok && current != name.(string) {
+ nodesObj := client.GetSubObject("nodes")
+ nodeObj := nodesObj.GetSubObject(node.ID())
+ log.Printf("RENAME '%s' to '%s'\n", node.Hostname(), name.(string))
+
+ if !options.Preview {
+ nodeObj.Update(url.Values{"hostname": []string{name.(string)}})
+ }
+ }
+ }
+ }
+ return nil
+}
+
+// Done we are at the target state, nothing to do
+var Done = func(client *maas.MAASObject, node MaasNode, options ProcessingOptions) error {
+ // As devices are normally in the "COMPLETED" state we don't want to
+ // log this fact unless we are in verbose mode. I suspect it would be
+ // nice to log it once when the device transitions from a non COMPLETE
+ // state to a complete state, but that would require keeping state.
+ if options.Verbose {
+ log.Printf("COMPLETE: %s", node.Hostname())
+ }
+
+ if options.AlwaysRename {
+ updateNodeName(client, node, options)
+ }
+
+ return nil
+}
+
+// Deploy cause a node to deploy
+var Deploy = func(client *maas.MAASObject, node MaasNode, options ProcessingOptions) error {
+ log.Printf("DEPLOY: %s", node.Hostname())
+
+ if options.AlwaysRename {
+ updateNodeName(client, node, options)
+ }
+
+ if !options.Preview {
+ nodesObj := client.GetSubObject("nodes")
+ myNode := nodesObj.GetSubObject(node.ID())
+ // Start the node with the trusty distro. This should really be looked up or
+ // a parameter default
+ _, err := myNode.CallPost("start", url.Values {"distro_series" : []string{"trusty"}})
+ if err != nil {
+ log.Printf("ERROR: DEPLOY '%s' : '%s'", node.Hostname(), err)
+ return err
+ }
+ }
+ return nil
+}
+
+// Aquire aquire a machine to a specific operator
+var Aquire = func(client *maas.MAASObject, node MaasNode, options ProcessingOptions) error {
+ log.Printf("AQUIRE: %s", node.Hostname())
+ nodesObj := client.GetSubObject("nodes")
+
+ if options.AlwaysRename {
+ updateNodeName(client, node, options)
+ }
+
+ if !options.Preview {
+ // With a new version of MAAS we have to make sure the node is linked
+ // to the subnet vid DHCP before we move to the Aquire state. To do this
+ // We need to unlink the interface to the subnet and then relink it.
+ //
+ // Iterate through all the interfaces on the node, searching for ones
+ // that are valid and not DHCP and move them to DHCP
+ ifcsObj := client.GetSubObject("nodes").GetSubObject(node.ID()).GetSubObject("interfaces")
+ ifcsListObj, err := ifcsObj.CallGet("", url.Values{})
+ if err != nil {
+ return err
+ }
+
+ ifcsArray, err := ifcsListObj.GetArray()
+ if err != nil {
+ return err
+ }
+
+ for _, ifc := range ifcsArray {
+ ifcMap, err := ifc.GetMap()
+ if err != nil {
+ return err
+ }
+
+ // Iterate over the links assocated with the interface, looking for
+ // links with a subnect as well as a mode of "auto"
+ links, ok := ifcMap["links"]
+ if ok {
+ linkArray, err := links.GetArray()
+ if err != nil {
+ return err
+ }
+
+ for _, link := range linkArray {
+ linkMap, err := link.GetMap()
+ if err != nil {
+ return err
+ }
+ subnet, ok := linkMap["subnet"]
+ if ok {
+ subnetMap, err := subnet.GetMap()
+ if err != nil {
+ return err
+ }
+
+ val, err := linkMap["mode"].GetString()
+ if err != nil {
+ return err
+ }
+
+ if val == "auto" {
+ // Found one we like, so grab the subnet from the data and
+ // then relink this as DHCP
+ cidr, err := subnetMap["cidr"].GetString()
+ if err != nil {
+ return err
+ }
+
+ fifcID, err := ifcMap["id"].GetFloat64()
+ if err != nil {
+ return err
+ }
+ ifcID := strconv.Itoa(int(fifcID))
+
+ flID, err := linkMap["id"].GetFloat64()
+ if err != nil {
+ return err
+ }
+ lID := strconv.Itoa(int(flID))
+
+ ifcObj := ifcsObj.GetSubObject(ifcID)
+ _, err = ifcObj.CallPost("unlink_subnet", url.Values{"id": []string{lID}})
+ if err != nil {
+ return err
+ }
+ _, err = ifcObj.CallPost("link_subnet", url.Values{"mode": []string{"DHCP"}, "subnet": []string{cidr}})
+ if err != nil {
+ return err
+ }
+ }
+ }
+ }
+ }
+ }
+ _, err = nodesObj.CallPost("acquire",
+ url.Values{"name": []string{node.Hostname()}})
+ if err != nil {
+ log.Printf("ERROR: AQUIRE '%s' : '%s'", node.Hostname(), err)
+ return err
+ }
+ }
+ return nil
+}
+
+// Commission cause a node to be commissioned
+var Commission = func(client *maas.MAASObject, node MaasNode, options ProcessingOptions) error {
+ updateNodeName(client, node, options)
+
+ // Need to understand the power state of the node. We only want to move to "Commissioning" if the node
+ // power is off. If the node power is not off, then turn it off.
+ state := node.PowerState()
+ switch state {
+ case "on":
+ // Attempt to turn the node off
+ log.Printf("POWER DOWN: %s", node.Hostname())
+ if !options.Preview {
+ //POST /api/1.0/nodes/{system_id}/ op=stop
+ nodesObj := client.GetSubObject("nodes")
+ nodeObj := nodesObj.GetSubObject(node.ID())
+ _, err := nodeObj.CallPost("stop", url.Values{"stop_mode" : []string{"soft"}})
+ if err != nil {
+ log.Printf("ERROR: Commission '%s' : changing power start to off : '%s'", node.Hostname(), err)
+ }
+ return err
+ }
+ break
+ case "off":
+ // We are off so move to commissioning
+ log.Printf("COMISSION: %s", node.Hostname())
+ if !options.Preview {
+ nodesObj := client.GetSubObject("nodes")
+ nodeObj := nodesObj.GetSubObject(node.ID())
+
+ updateNodeName(client, node, options)
+
+ _, err := nodeObj.CallPost("commission", url.Values{})
+ if err != nil {
+ log.Printf("ERROR: Commission '%s' : '%s'", node.Hostname(), err)
+ }
+ return err
+ }
+ break
+ default:
+ // We are in a state from which we can't move forward.
+ log.Printf("ERROR: %s has invalid power state '%s'", node.Hostname(), state)
+ break
+ }
+ return nil
+}
+
+// Wait a do nothing state, while work is being done
+var Wait = func(client *maas.MAASObject, node MaasNode, options ProcessingOptions) error {
+ log.Printf("WAIT: %s", node.Hostname())
+ return nil
+}
+
+// Fail a state from which we cannot, currently, automatically recover
+var Fail = func(client *maas.MAASObject, node MaasNode, options ProcessingOptions) error {
+ log.Printf("FAIL: %s", node.Hostname())
+ return nil
+}
+
+// AdminState an administrative state from which we should make no automatic transition
+var AdminState = func(client *maas.MAASObject, node MaasNode, options ProcessingOptions) error {
+ log.Printf("ADMIN: %s", node.Hostname())
+ return nil
+}
+
+func findAction(target string, current string) (Action, error) {
+ targets, ok := Transitions[target]
+ if !ok {
+ log.Printf("[warn] unable to find transitions to target state '%s'", target)
+ return nil, fmt.Errorf("Could not find transition to target state '%s'", target)
+ }
+
+ action, ok := targets[current]
+ if !ok {
+ log.Printf("[warn] unable to find transition from current state '%s' to target state '%s'",
+ current, target)
+ return nil, fmt.Errorf("Could not find transition from current state '%s' to target state '%s'",
+ current, target)
+ }
+
+ return action, nil
+}
+
+// ProcessNode something
+func ProcessNode(client *maas.MAASObject, node MaasNode, options ProcessingOptions) error {
+ substatus, err := node.GetInteger("substatus")
+ if err != nil {
+ return err
+ }
+ action, err := findAction("Deployed", MaasNodeStatus(substatus).String())
+ if err != nil {
+ return err
+ }
+
+ if options.Preview {
+ action(client, node, options)
+ } else {
+ go action(client, node, options)
+ }
+ return nil
+}
+
+func buildFilter(filter []string) ([]*regexp.Regexp, error) {
+
+ results := make([]*regexp.Regexp, len(filter))
+ for i, v := range filter {
+ r, err := regexp.Compile(v)
+ if err != nil {
+ return nil, err
+ }
+ results[i] = r
+ }
+ return results, nil
+}
+
+func matchedFilter(include []*regexp.Regexp, target string) bool {
+ for _, e := range include {
+ if e.MatchString(target) {
+ return true
+ }
+ }
+ return false
+}
+
+// ProcessAll something
+func ProcessAll(client *maas.MAASObject, nodes []MaasNode, options ProcessingOptions) []error {
+ errors := make([]error, len(nodes))
+ includeHosts, err := buildFilter(options.Filter.Hosts.Include)
+ if err != nil {
+ log.Fatalf("[error] invalid regular expression for include filter '%s' : %s", options.Filter.Hosts.Include, err)
+ }
+
+ includeZones, err := buildFilter(options.Filter.Zones.Include)
+ if err != nil {
+ log.Fatalf("[error] invalid regular expression for include filter '%v' : %s", options.Filter.Zones.Include, err)
+ }
+
+ for i, node := range nodes {
+ // For hostnames we always match on an empty filter
+ if len(includeHosts) >= 0 && matchedFilter(includeHosts, node.Hostname()) {
+
+ // For zones we don't match on an empty filter
+ if len(includeZones) >= 0 && matchedFilter(includeZones, node.Zone()) {
+ err := ProcessNode(client, node, options)
+ if err != nil {
+ errors[i] = err
+ } else {
+ errors[i] = nil
+ }
+ } else {
+ if options.Verbose {
+ log.Printf("[info] ignoring node '%s' as its zone '%s' didn't match include zone name filter '%v'",
+ node.Hostname(), node.Zone(), options.Filter.Zones.Include)
+ }
+ }
+ } else {
+ if options.Verbose {
+ log.Printf("[info] ignoring node '%s' as it didn't match include hostname filter '%v'",
+ node.Hostname(), options.Filter.Hosts.Include)
+ }
+ }
+ }
+ return errors
+}