~rogpeppe/juju-core/azure

« back to all changes in this revision

Viewing changes to environs/cloudinit.go

  • Committer: William Reade
  • Date: 2012-01-20 21:32:53 UTC
  • mto: This revision was merged to the branch mainline in revision 47.
  • Revision ID: fwereade@gmail.com-20120120213253-csks5e12opl8t1rq
hefty rearrangement, few actual changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright 2013 Canonical Ltd.
2
 
// Licensed under the AGPLv3, see LICENCE file for details.
3
 
 
4
 
package environs
5
 
 
6
 
import (
7
 
        "fmt"
8
 
 
9
 
        "launchpad.net/juju-core/constraints"
10
 
        "launchpad.net/juju-core/environs/cloudinit"
11
 
        "launchpad.net/juju-core/environs/config"
12
 
        "launchpad.net/juju-core/state"
13
 
        "launchpad.net/juju-core/state/api"
14
 
        "launchpad.net/juju-core/utils"
15
 
)
16
 
 
17
 
// Default data directory.
18
 
// Tests can override this where needed, so they don't need to mess with global
19
 
// system state.
20
 
var DataDir = "/var/lib/juju"
21
 
 
22
 
// NewMachineConfig sets up a basic machine configuration.  You'll still need
23
 
// to supply more information, but this takes care of the fixed entries and
24
 
// the ones that are always needed.
25
 
func NewMachineConfig(machineID, machineNonce string,
26
 
        stateInfo *state.Info, apiInfo *api.Info) *cloudinit.MachineConfig {
27
 
        return &cloudinit.MachineConfig{
28
 
                // Fixed entries.
29
 
                DataDir: DataDir,
30
 
 
31
 
                // Parameter entries.
32
 
                MachineId:    machineID,
33
 
                MachineNonce: machineNonce,
34
 
                StateInfo:    stateInfo,
35
 
                APIInfo:      apiInfo,
36
 
        }
37
 
}
38
 
 
39
 
// FinishMachineConfig sets fields on a MachineConfig that can be determined by
40
 
// inspecting a plain config.Config and the machine constraints at the last
41
 
// moment before bootstrapping. It assumes that the supplied Config comes from
42
 
// an environment that has passed through all the validation checks in the
43
 
// Bootstrap func, and that has set an agent-version (via FindBootstrapTools,
44
 
// or otherwise).
45
 
// TODO(fwereade) This function is not meant to be "good" in any serious way:
46
 
// it is better that this functionality be collected in one place here than
47
 
// that it be spread out across 3 or 4 providers, but this is its only
48
 
// redeeming feature.
49
 
func FinishMachineConfig(mcfg *cloudinit.MachineConfig, cfg *config.Config, cons constraints.Value) (err error) {
50
 
        defer utils.ErrorContextf(&err, "cannot complete machine configuration")
51
 
 
52
 
        // Everything needs the environment's authorized keys.
53
 
        authKeys := cfg.AuthorizedKeys()
54
 
        if authKeys == "" {
55
 
                return fmt.Errorf("environment configuration has no authorized-keys")
56
 
        }
57
 
        mcfg.AuthorizedKeys = authKeys
58
 
        if !mcfg.StateServer {
59
 
                return nil
60
 
        }
61
 
 
62
 
        // These settings are only appropriate at bootstrap time. At the
63
 
        // moment, the only state server is the bootstrap node, but this
64
 
        // will probably change.
65
 
        if mcfg.APIInfo != nil || mcfg.StateInfo != nil {
66
 
                return fmt.Errorf("machine configuration already has api/state info")
67
 
        }
68
 
        caCert, hasCACert := cfg.CACert()
69
 
        if !hasCACert {
70
 
                return fmt.Errorf("environment configuration has no ca-cert")
71
 
        }
72
 
        password := cfg.AdminSecret()
73
 
        if password == "" {
74
 
                return fmt.Errorf("environment configuration has no admin-secret")
75
 
        }
76
 
        passwordHash := utils.PasswordHash(password)
77
 
        mcfg.APIInfo = &api.Info{Password: passwordHash, CACert: caCert}
78
 
        mcfg.StateInfo = &state.Info{Password: passwordHash, CACert: caCert}
79
 
        mcfg.StatePort = cfg.StatePort()
80
 
        mcfg.APIPort = cfg.APIPort()
81
 
        mcfg.Constraints = cons
82
 
        if mcfg.Config, err = BootstrapConfig(cfg); err != nil {
83
 
                return err
84
 
        }
85
 
 
86
 
        // These really are directly relevant to running a state server.
87
 
        cert, key, err := cfg.GenerateStateServerCertAndKey()
88
 
        if err != nil {
89
 
                return fmt.Errorf("cannot generate state server certificate: %v", err)
90
 
        }
91
 
        mcfg.StateServerCert = cert
92
 
        mcfg.StateServerKey = key
93
 
        return nil
94
 
}