launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/provider/maas/util.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package maas 5 6 import ( 7 "fmt" 8 "net/url" 9 "strings" 10 11 "launchpad.net/goyaml" 12 13 "launchpad.net/juju-core/environs" 14 "launchpad.net/juju-core/instance" 15 "launchpad.net/juju-core/utils" 16 ) 17 18 // extractSystemId extracts the 'system_id' part from an InstanceId. 19 // "/MAAS/api/1.0/nodes/system_id/" => "system_id" 20 func extractSystemId(instanceId instance.Id) string { 21 trimmed := strings.TrimRight(string(instanceId), "/") 22 split := strings.Split(trimmed, "/") 23 return split[len(split)-1] 24 } 25 26 // getSystemIdValues returns a url.Values object with all the 'system_ids' 27 // from the given instanceIds stored under the key 'id'. This is used 28 // to filter out instances when listing the nodes objects. 29 func getSystemIdValues(instanceIds []instance.Id) url.Values { 30 values := url.Values{} 31 for _, instanceId := range instanceIds { 32 values.Add("id", extractSystemId(instanceId)) 33 } 34 return values 35 } 36 37 // machineInfo is the structure used to pass information between the provider 38 // and the agent running on a node. 39 // When a node is started, the provider code creates a machineInfo object 40 // containing information about the node being started and configures 41 // cloudinit to get a YAML representation of that object written on the node's 42 // filesystem during its first startup. That file is then read by the juju 43 // agent running on the node and converted back into a machineInfo object. 44 type machineInfo struct { 45 Hostname string `yaml:,omitempty` 46 } 47 48 var _MAASInstanceFilename = environs.DataDir + "/MAASmachine.txt" 49 50 // cloudinitRunCmd returns the shell command that, when run, will create the 51 // "machine info" file containing the hostname of a machine. 52 // That command is destined to be used by cloudinit. 53 func (info *machineInfo) cloudinitRunCmd() (string, error) { 54 yaml, err := goyaml.Marshal(info) 55 if err != nil { 56 return "", err 57 } 58 script := fmt.Sprintf(`mkdir -p %s; echo -n %s > %s`, utils.ShQuote(environs.DataDir), utils.ShQuote(string(yaml)), utils.ShQuote(_MAASInstanceFilename)) 59 return script, nil 60 } 61 62 // load loads the "machine info" file and parse the content into the info 63 // object. 64 func (info *machineInfo) load() error { 65 return utils.ReadYaml(_MAASInstanceFilename, info) 66 }