github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/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 "path" 11 12 "launchpad.net/goyaml" 13 14 "launchpad.net/juju-core/environs" 15 "launchpad.net/juju-core/instance" 16 "launchpad.net/juju-core/utils" 17 "launchpad.net/juju-core/juju/osenv" 18 ) 19 20 // extractSystemId extracts the 'system_id' part from an InstanceId. 21 // "/MAAS/api/1.0/nodes/system_id/" => "system_id" 22 func extractSystemId(instanceId instance.Id) string { 23 trimmed := strings.TrimRight(string(instanceId), "/") 24 split := strings.Split(trimmed, "/") 25 return split[len(split)-1] 26 } 27 28 // getSystemIdValues returns a url.Values object with all the 'system_ids' 29 // from the given instanceIds stored under the key 'id'. This is used 30 // to filter out instances when listing the nodes objects. 31 func getSystemIdValues(instanceIds []instance.Id) url.Values { 32 values := url.Values{} 33 for _, instanceId := range instanceIds { 34 values.Add("id", extractSystemId(instanceId)) 35 } 36 return values 37 } 38 39 // machineInfo is the structure used to pass information between the provider 40 // and the agent running on a node. 41 // When a node is started, the provider code creates a machineInfo object 42 // containing information about the node being started and configures 43 // cloudinit to get a YAML representation of that object written on the node's 44 // filesystem during its first startup. That file is then read by the juju 45 // agent running on the node and converted back into a machineInfo object. 46 type machineInfo struct { 47 Hostname string `yaml:,omitempty` 48 } 49 50 var _MAASInstanceFilename = path.Join(environs.DataDir, "MAASmachine.txt") 51 52 // cloudinitRunCmd returns the shell command that, when run, will create the 53 // "machine info" file containing the hostname of a machine. 54 // That command is destined to be used by cloudinit. 55 func (info *machineInfo) cloudinitRunCmd() (string, error) { 56 yaml, err := goyaml.Marshal(info) 57 if err != nil { 58 return "", err 59 } 60 script := fmt.Sprintf(`mkdir -p %s; echo -n %s > %s`, utils.ShQuote(environs.DataDir), utils.ShQuote(string(yaml)), utils.ShQuote(_MAASInstanceFilename)) 61 return script, nil 62 } 63 64 // winCloudinitRunCmd returns the shell command that, when run, will create the 65 // "machine info" file containing the hostname of a machine on a winows system. 66 // That command is destined to be used by cloudinit. 67 func (info *machineInfo) winCloudinitRunCmd() (string, error) { 68 _MAASInstanceFilenameWin := path.Join(osenv.WinDataDir, "MAASmachine.txt") 69 yaml, err := goyaml.Marshal(info) 70 if err != nil { 71 return "", err 72 } 73 script := fmt.Sprintf("mkdir \"%s\"\r\n Set-Content \"%s\" @\"\n%s\n\"@", utils.PathToWindows(osenv.WinDataDir), utils.PathToWindows(_MAASInstanceFilenameWin), string(yaml)) 74 return script, nil 75 } 76 77 // load loads the "machine info" file and parse the content into the info 78 // object. 79 func (info *machineInfo) load() error { 80 return utils.ReadYaml(_MAASInstanceFilename, info) 81 }