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"
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]
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))
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)
40
cloudcfg, err := cloudinit.Configure(cfg, cloudcfg)
44
data, err := cloudcfg.Render()
48
cdata := utils.Gzip(data)
49
log.Debugf("environs/maas: maas user data; %d bytes", len(cdata))
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`
65
var _MAASInstanceFilename = jujuDataDir + "/MAASmachine.txt"
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)
75
script := fmt.Sprintf(`mkdir -p %s; echo -n %s > %s`, utils.ShQuote(jujuDataDir), utils.ShQuote(string(yaml)), utils.ShQuote(_MAASInstanceFilename))
79
// load loads the "machine info" file and parse the content into the info
81
func (info *machineInfo) load() error {
82
return utils.ReadYaml(_MAASInstanceFilename, info)