1
// Copyright 2013 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
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"
17
// Default data directory.
18
// Tests can override this where needed, so they don't need to mess with global
20
var DataDir = "/var/lib/juju"
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{
33
MachineNonce: machineNonce,
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,
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
49
func FinishMachineConfig(mcfg *cloudinit.MachineConfig, cfg *config.Config, cons constraints.Value) (err error) {
50
defer utils.ErrorContextf(&err, "cannot complete machine configuration")
52
// Everything needs the environment's authorized keys.
53
authKeys := cfg.AuthorizedKeys()
55
return fmt.Errorf("environment configuration has no authorized-keys")
57
mcfg.AuthorizedKeys = authKeys
58
if !mcfg.StateServer {
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")
68
caCert, hasCACert := cfg.CACert()
70
return fmt.Errorf("environment configuration has no ca-cert")
72
password := cfg.AdminSecret()
74
return fmt.Errorf("environment configuration has no admin-secret")
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 {
86
// These really are directly relevant to running a state server.
87
cert, key, err := cfg.GenerateStateServerCertAndKey()
89
return fmt.Errorf("cannot generate state server certificate: %v", err)
91
mcfg.StateServerCert = cert
92
mcfg.StateServerKey = key