~rogpeppe/juju-core/azure

« back to all changes in this revision

Viewing changes to environs/maas/util.go

[r=jtv] Unify providers' user-data composition functions.

All the "physical" providers (EC2, OpenStack, MAAS, Azure) were generating
user data in essentially the same way. The MAAS one looked different but
actually just added an optional bit that was still completely generic. And so
this branch unifies the implementations.

Construction of the cloudinit.MachineConfig was lifted out of the function.
That may seem arbitrary, but it's part of a greater plan: in a later step of
the ongoing refactoring the construction can be lifted out of the providers'
internalStartInstance methods as well, doing away with the EC2 and OpenStack
providers' startInstanceParams structs. This actually shortens some of the
data flows and reduces cognitive load, which can only be good for maintenance.

After that step we can have a fresh look at sanitizing NewMachineConfig (which
arguably should take more parameters, or not exist at all, but is in an
intermediate state that facilitates the changes happening now). We may also
look into re-introducing some sort of dedicated Parameter Object for
internalStartInstance, but by then the cluster of methods Bootstrap /
StartInstance / internalStartInstance will look a bit different and we can
find the most maintainable solution for that new situation.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
import (
7
7
        "fmt"
8
8
        "launchpad.net/goyaml"
9
 
        cloudinit_core "launchpad.net/juju-core/cloudinit"
10
9
        "launchpad.net/juju-core/environs"
11
 
        "launchpad.net/juju-core/environs/cloudinit"
12
10
        "launchpad.net/juju-core/instance"
13
 
        "launchpad.net/juju-core/log"
14
11
        "launchpad.net/juju-core/utils"
15
12
        "net/url"
16
13
        "strings"
35
32
        return values
36
33
}
37
34
 
38
 
// userData returns a zipped cloudinit config.
39
 
// TODO(bug 1199847): Some of this work can be shared between providers.
40
 
func userData(cfg *cloudinit.MachineConfig, scripts ...string) ([]byte, error) {
41
 
        cloudcfg := cloudinit_core.New()
42
 
        for _, script := range scripts {
43
 
                cloudcfg.AddRunCmd(script)
44
 
        }
45
 
        cloudcfg, err := cloudinit.Configure(cfg, cloudcfg)
46
 
        if err != nil {
47
 
                return nil, err
48
 
        }
49
 
        data, err := cloudcfg.Render()
50
 
        if err != nil {
51
 
                return nil, err
52
 
        }
53
 
        cdata := utils.Gzip(data)
54
 
        log.Debugf("environs/maas: maas user data; %d bytes", len(cdata))
55
 
        return cdata, nil
56
 
}
57
 
 
58
35
// machineInfo is the structure used to pass information between the provider
59
36
// and the agent running on a node.
60
37
// When a node is started, the provider code creates a machineInfo object