/
cloudinit.go
190 lines (172 loc) · 6.81 KB
/
cloudinit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package environs
import (
"fmt"
"github.com/juju/errors"
"github.com/juju/names"
"github.com/juju/utils"
"github.com/juju/utils/proxy"
"github.com/juju/juju/agent"
coreCloudinit "github.com/juju/juju/cloudinit"
"github.com/juju/juju/constraints"
"github.com/juju/juju/environs/cloudinit"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/state"
"github.com/juju/juju/state/api"
"github.com/juju/juju/state/api/params"
)
// DataDir is the default data directory.
// Tests can override this where needed, so they don't need to mess with global
// system state.
var DataDir = agent.DefaultDataDir
// CloudInitOutputLog is the default cloud-init-output.log file path.
const CloudInitOutputLog = "/var/log/cloud-init-output.log"
// NewMachineConfig sets up a basic machine configuration, for a non-bootstrap
// node. You'll still need to supply more information, but this takes care of
// the fixed entries and the ones that are always needed.
func NewMachineConfig(
machineID, machineNonce string, networks []string,
stateInfo *state.Info, apiInfo *api.Info,
) *cloudinit.MachineConfig {
mcfg := &cloudinit.MachineConfig{
// Fixed entries.
DataDir: DataDir,
LogDir: agent.DefaultLogDir,
Jobs: []params.MachineJob{params.JobHostUnits},
CloudInitOutputLog: CloudInitOutputLog,
MachineAgentServiceName: "jujud-" + names.MachineTag(machineID),
// Parameter entries.
MachineId: machineID,
MachineNonce: machineNonce,
Networks: networks,
StateInfo: stateInfo,
APIInfo: apiInfo,
}
return mcfg
}
// NewBootstrapMachineConfig sets up a basic machine configuration for a
// bootstrap node. You'll still need to supply more information, but this
// takes care of the fixed entries and the ones that are always needed.
func NewBootstrapMachineConfig(privateSystemSSHKey string) *cloudinit.MachineConfig {
// For a bootstrap instance, FinishMachineConfig will provide the
// state.Info and the api.Info. The machine id must *always* be "0".
mcfg := NewMachineConfig("0", state.BootstrapNonce, nil, nil, nil)
mcfg.Bootstrap = true
mcfg.SystemPrivateSSHKey = privateSystemSSHKey
mcfg.Jobs = []params.MachineJob{params.JobManageEnviron, params.JobHostUnits}
return mcfg
}
// PopulateMachineConfig is called both from the FinishMachineConfig below,
// which does have access to the environment config, and from the container
// provisioners, which don't have access to the environment config. Everything
// that is needed to provision a container needs to be returned to the
// provisioner in the ContainerConfig structure. Those values are then used to
// call this function.
func PopulateMachineConfig(mcfg *cloudinit.MachineConfig,
providerType, authorizedKeys string,
sslHostnameVerification bool,
proxySettings, aptProxySettings proxy.Settings,
) error {
if authorizedKeys == "" {
return fmt.Errorf("environment configuration has no authorized-keys")
}
mcfg.AuthorizedKeys = authorizedKeys
if mcfg.AgentEnvironment == nil {
mcfg.AgentEnvironment = make(map[string]string)
}
mcfg.AgentEnvironment[agent.ProviderType] = providerType
mcfg.AgentEnvironment[agent.ContainerType] = string(mcfg.MachineContainerType)
mcfg.DisableSSLHostnameVerification = !sslHostnameVerification
mcfg.ProxySettings = proxySettings
mcfg.AptProxySettings = aptProxySettings
return nil
}
// FinishMachineConfig sets fields on a MachineConfig that can be determined by
// inspecting a plain config.Config and the machine constraints at the last
// moment before bootstrapping. It assumes that the supplied Config comes from
// an environment that has passed through all the validation checks in the
// Bootstrap func, and that has set an agent-version (via finding the tools to,
// use for bootstrap, or otherwise).
// TODO(fwereade) This function is not meant to be "good" in any serious way:
// it is better that this functionality be collected in one place here than
// that it be spread out across 3 or 4 providers, but this is its only
// redeeming feature.
func FinishMachineConfig(mcfg *cloudinit.MachineConfig, cfg *config.Config, cons constraints.Value) (err error) {
defer errors.Maskf(&err, "cannot complete machine configuration")
if err := PopulateMachineConfig(
mcfg,
cfg.Type(),
cfg.AuthorizedKeys(),
cfg.SSLHostnameVerification(),
cfg.ProxySettings(),
cfg.AptProxySettings(),
); err != nil {
return err
}
// The following settings are only appropriate at bootstrap time. At the
// moment, the only state server is the bootstrap node, but this
// will probably change.
if !mcfg.Bootstrap {
return nil
}
if mcfg.APIInfo != nil || mcfg.StateInfo != nil {
return fmt.Errorf("machine configuration already has api/state info")
}
caCert, hasCACert := cfg.CACert()
if !hasCACert {
return fmt.Errorf("environment configuration has no ca-cert")
}
password := cfg.AdminSecret()
if password == "" {
return fmt.Errorf("environment configuration has no admin-secret")
}
passwordHash := utils.UserPasswordHash(password, utils.CompatSalt)
mcfg.APIInfo = &api.Info{Password: passwordHash, CACert: caCert}
mcfg.StateInfo = &state.Info{Password: passwordHash, CACert: caCert}
// These really are directly relevant to running a state server.
cert, key, err := cfg.GenerateStateServerCertAndKey()
if err != nil {
return errors.Annotate(err, "cannot generate state server certificate")
}
srvInfo := params.StateServingInfo{
StatePort: cfg.StatePort(),
APIPort: cfg.APIPort(),
Cert: string(cert),
PrivateKey: string(key),
SystemIdentity: mcfg.SystemPrivateSSHKey,
}
mcfg.StateServingInfo = &srvInfo
mcfg.Constraints = cons
if mcfg.Config, err = BootstrapConfig(cfg); err != nil {
return err
}
return nil
}
func configureCloudinit(mcfg *cloudinit.MachineConfig, cloudcfg *coreCloudinit.Config) error {
// When bootstrapping, we only want to apt-get update/upgrade
// and setup the SSH keys. The rest we leave to cloudinit/sshinit.
if mcfg.Bootstrap {
return cloudinit.ConfigureBasic(mcfg, cloudcfg)
}
return cloudinit.Configure(mcfg, cloudcfg)
}
// ComposeUserData fills out the provided cloudinit configuration structure
// so it is suitable for initialising a machine with the given configuration,
// and then renders it and returns it as a binary (gzipped) blob of user data.
//
// If the provided cloudcfg is nil, a new one will be created internally.
func ComposeUserData(mcfg *cloudinit.MachineConfig, cloudcfg *coreCloudinit.Config) ([]byte, error) {
if cloudcfg == nil {
cloudcfg = coreCloudinit.New()
}
if err := configureCloudinit(mcfg, cloudcfg); err != nil {
return nil, err
}
data, err := cloudcfg.Render()
logger.Tracef("Generated cloud init:\n%s", string(data))
if err != nil {
return nil, err
}
return utils.Gzip(data), nil
}