github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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  	"net/url"
     8  	"path"
     9  	"strings"
    10  
    11  	"github.com/juju/errors"
    12  	"github.com/juju/utils"
    13  	goyaml "gopkg.in/yaml.v2"
    14  
    15  	"github.com/juju/juju/cloudconfig/cloudinit"
    16  	"github.com/juju/juju/environs/config"
    17  	"github.com/juju/juju/instance"
    18  	"github.com/juju/juju/juju/paths"
    19  )
    20  
    21  // extractSystemId extracts the 'system_id' part from an InstanceId.
    22  // "/MAAS/api/1.0/nodes/system_id/" => "system_id"
    23  func extractSystemId(instanceId instance.Id) string {
    24  	trimmed := strings.TrimRight(string(instanceId), "/")
    25  	split := strings.Split(trimmed, "/")
    26  	return split[len(split)-1]
    27  }
    28  
    29  // getSystemIdValues returns a url.Values object with all the 'system_ids'
    30  // from the given instanceIds stored under the specified key.  This is used
    31  // to filter out instances when listing the nodes objects.
    32  func getSystemIdValues(key string, instanceIds []instance.Id) url.Values {
    33  	values := url.Values{}
    34  	for _, instanceId := range instanceIds {
    35  		values.Add(key, extractSystemId(instanceId))
    36  	}
    37  	return values
    38  }
    39  
    40  // machineInfo is the structure used to pass information between the provider
    41  // and the agent running on a node.
    42  // When a node is started, the provider code creates a machineInfo object
    43  // containing information about the node being started and configures
    44  // cloudinit to get a YAML representation of that object written on the node's
    45  // filesystem during its first startup.  That file is then read by the juju
    46  // agent running on the node and converted back into a machineInfo object.
    47  type machineInfo struct {
    48  	Hostname string `yaml:",omitempty"`
    49  }
    50  
    51  var maasDataDir = paths.MustSucceed(paths.DataDir(config.LatestLtsSeries()))
    52  var _MAASInstanceFilename = path.Join(maasDataDir, "MAASmachine.txt")
    53  
    54  // cloudinitRunCmd returns the shell command that, when run, will create the
    55  // "machine info" file containing the hostname of a machine.
    56  // That command is destined to be used by cloudinit.
    57  func (info *machineInfo) cloudinitRunCmd(cloudcfg cloudinit.CloudConfig) (string, error) {
    58  	dataDir, err := paths.DataDir(cloudcfg.GetSeries())
    59  	if err != nil {
    60  		return "", errors.Trace(err)
    61  	}
    62  	yaml, err := goyaml.Marshal(info)
    63  	if err != nil {
    64  		return "", errors.Trace(err)
    65  	}
    66  	renderer := cloudcfg.ShellRenderer()
    67  	fileName := renderer.Join(renderer.FromSlash(dataDir), "MAASmachine.txt")
    68  	script := renderer.MkdirAll(dataDir)
    69  	contents := renderer.Quote(string(yaml))
    70  	script = append(script, renderer.WriteFile(fileName, []byte(contents))...)
    71  	script = append(script, renderer.Chmod(fileName, 0755)...)
    72  	return strings.Join(script, "\n"), nil
    73  }
    74  
    75  // load loads the "machine info" file and parse the content into the info
    76  // object.
    77  func (info *machineInfo) load() error {
    78  	return utils.ReadYaml(_MAASInstanceFilename, info)
    79  }