OLD | NEW |
(Empty) | |
| 1 package environs |
| 2 |
| 3 import ( |
| 4 "fmt" |
| 5 "time" |
| 6 |
| 7 "launchpad.net/juju-core/cert" |
| 8 "launchpad.net/juju-core/constraints" |
| 9 "launchpad.net/juju-core/environs/cloudinit" |
| 10 "launchpad.net/juju-core/environs/config" |
| 11 "launchpad.net/juju-core/state" |
| 12 "launchpad.net/juju-core/state/api" |
| 13 "launchpad.net/juju-core/utils" |
| 14 ) |
| 15 |
| 16 // FinishMachineConfig sets fields on a MachineConfig that can be determined by |
| 17 // inspecting a plain config.Config and the machine constraints at the last |
| 18 // moment before bootstrapping. It assumes that the supplied Config comes from |
| 19 // an environment that has passed through all the validation checks in the |
| 20 // Bootstrap func, and that has set an agent-version (via FindBootstrapTools, |
| 21 // or otherwise). |
| 22 // TODO(fwereade) This function is not meant to be "good" in any serious way: |
| 23 // it is better that this functionality be collected in one place here than |
| 24 // that it be spread out across 3 or 4 providers, but this is its only |
| 25 // redeeming feature. |
| 26 func FinishMachineConfig(mcfg *cloudinit.MachineConfig, cfg *config.Config, cons
constraints.Value) (err error) { |
| 27 defer utils.ErrorContextf(&err, "cannot complete machine configuration") |
| 28 |
| 29 // Everything needs the environment's authorized keys. |
| 30 authKeys := cfg.AuthorizedKeys() |
| 31 if authKeys == "" { |
| 32 return fmt.Errorf("environment configuration has no authorized-k
eys") |
| 33 } |
| 34 mcfg.AuthorizedKeys = authKeys |
| 35 if !mcfg.StateServer { |
| 36 return nil |
| 37 } |
| 38 |
| 39 // These settings are only appropriate at bootstrap time. At the |
| 40 // moment, the only state server is the bootstrap node, but this |
| 41 // will probably change. |
| 42 if mcfg.APIInfo != nil || mcfg.StateInfo != nil { |
| 43 return fmt.Errorf("machine configuration already has api/state i
nfo") |
| 44 } |
| 45 caCert, hasCACert := cfg.CACert() |
| 46 if !hasCACert { |
| 47 return fmt.Errorf("environment configuration has no ca-cert") |
| 48 } |
| 49 password := cfg.AdminSecret() |
| 50 if password == "" { |
| 51 return fmt.Errorf("environment configuration has no admin-secret
") |
| 52 } |
| 53 passwordHash := utils.PasswordHash(password) |
| 54 mcfg.APIInfo = &api.Info{Password: passwordHash, CACert: caCert} |
| 55 mcfg.StateInfo = &state.Info{Password: passwordHash, CACert: caCert} |
| 56 mcfg.Constraints = cons |
| 57 if mcfg.Config, err = BootstrapConfig(cfg); err != nil { |
| 58 return err |
| 59 } |
| 60 |
| 61 // These really are directly relevant to running a state server. |
| 62 caKey, hasCAKey := cfg.CAPrivateKey() |
| 63 if !hasCAKey { |
| 64 return fmt.Errorf("environment configuration has no ca-private-k
ey") |
| 65 } |
| 66 cert, key, err := cert.NewServer(cfg.Name(), caCert, caKey, time.Now().U
TC().AddDate(10, 0, 0)) |
| 67 if err != nil { |
| 68 return fmt.Errorf("cannot generate state server certificate: %v"
, err) |
| 69 } |
| 70 mcfg.StateServerCert = cert |
| 71 mcfg.StateServerKey = key |
| 72 return nil |
| 73 } |
OLD | NEW |