~juju-qa/ubuntu/yakkety/juju/2.0-rc3-again

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/environs/maas/util.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-04-24 22:34:47 UTC
  • Revision ID: package-import@ubuntu.com-20130424223447-f0qdji7ubnyo0s71
Tags: upstream-1.10.0.1
ImportĀ upstreamĀ versionĀ 1.10.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package maas
 
2
 
 
3
import (
 
4
        "fmt"
 
5
        "launchpad.net/goyaml"
 
6
        cloudinit_core "launchpad.net/juju-core/cloudinit"
 
7
        "launchpad.net/juju-core/environs/cloudinit"
 
8
        "launchpad.net/juju-core/log"
 
9
        "launchpad.net/juju-core/state"
 
10
        "launchpad.net/juju-core/utils"
 
11
        "net/url"
 
12
        "strings"
 
13
)
 
14
 
 
15
// extractSystemId extracts the 'system_id' part from an InstanceId.
 
16
// "/MAAS/api/1.0/nodes/system_id/" => "system_id"
 
17
func extractSystemId(instanceId state.InstanceId) string {
 
18
        trimmed := strings.TrimRight(string(instanceId), "/")
 
19
        split := strings.Split(trimmed, "/")
 
20
        return split[len(split)-1]
 
21
}
 
22
 
 
23
// getSystemIdValues returns a url.Values object with all the 'system_ids'
 
24
// from the given instanceIds stored under the key 'id'.  This is used
 
25
// to filter out instances when listing the nodes objects.
 
26
func getSystemIdValues(instanceIds []state.InstanceId) url.Values {
 
27
        values := url.Values{}
 
28
        for _, instanceId := range instanceIds {
 
29
                values.Add("id", extractSystemId(instanceId))
 
30
        }
 
31
        return values
 
32
}
 
33
 
 
34
// userData returns a zipped cloudinit config.
 
35
func userData(cfg *cloudinit.MachineConfig, scripts ...string) ([]byte, error) {
 
36
        cloudcfg := cloudinit_core.New()
 
37
        for _, script := range scripts {
 
38
                cloudcfg.AddRunCmd(script)
 
39
        }
 
40
        cloudcfg, err := cloudinit.Configure(cfg, cloudcfg)
 
41
        if err != nil {
 
42
                return nil, err
 
43
        }
 
44
        data, err := cloudcfg.Render()
 
45
        if err != nil {
 
46
                return nil, err
 
47
        }
 
48
        cdata := utils.Gzip(data)
 
49
        log.Debugf("environs/maas: maas user data; %d bytes", len(cdata))
 
50
        return cdata, nil
 
51
}
 
52
 
 
53
// machineInfo is the structure used to pass information between the provider
 
54
// and the agent running on a node.
 
55
// When a node is started, the provider code creates a machineInfo object
 
56
// containing information about the node being started and configures
 
57
// cloudinit to get a YAML representation of that object written on the node's
 
58
// filesystem during its first startup.  That file is then read by the juju
 
59
// agent running on the node and converted back into a machineInfo object.
 
60
type machineInfo struct {
 
61
        InstanceId string `yaml:,omitempty`
 
62
        Hostname   string `yaml:,omitempty`
 
63
}
 
64
 
 
65
var _MAASInstanceFilename = jujuDataDir + "/MAASmachine.txt"
 
66
 
 
67
// cloudinitRunCmd returns the shell command that, when run, will create the
 
68
// "machine info" file containing the instanceId and the hostname of a machine.
 
69
// That command is destined to be used by cloudinit.
 
70
func (info *machineInfo) cloudinitRunCmd() (string, error) {
 
71
        yaml, err := goyaml.Marshal(info)
 
72
        if err != nil {
 
73
                return "", err
 
74
        }
 
75
        script := fmt.Sprintf(`mkdir -p %s; echo -n %s > %s`, utils.ShQuote(jujuDataDir), utils.ShQuote(string(yaml)), utils.ShQuote(_MAASInstanceFilename))
 
76
        return script, nil
 
77
}
 
78
 
 
79
// load loads the "machine info" file and parse the content into the info
 
80
// object.
 
81
func (info *machineInfo) load() error {
 
82
        return utils.ReadYaml(_MAASInstanceFilename, info)
 
83
}