-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.go
411 lines (369 loc) · 11.8 KB
/
bootstrap.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package main
import (
"bytes"
"encoding/base64"
"fmt"
"io/ioutil"
"net"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/juju/cmd"
"github.com/juju/errors"
"github.com/juju/names"
goyaml "gopkg.in/yaml.v1"
"launchpad.net/gnuflag"
"github.com/juju/juju/agent"
agenttools "github.com/juju/juju/agent/tools"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/constraints"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/instance"
"github.com/juju/juju/mongo"
"github.com/juju/juju/network"
"github.com/juju/juju/state"
"github.com/juju/juju/state/toolstorage"
"github.com/juju/juju/utils/ssh"
"github.com/juju/juju/version"
"github.com/juju/juju/worker/peergrouper"
)
var (
agentInitializeState = agent.InitializeState
sshGenerateKey = ssh.GenerateKey
stateStorage = (*state.State).Storage
minSocketTimeout = 1 * time.Minute
)
type BootstrapCommand struct {
cmd.CommandBase
AgentConf
EnvConfig map[string]interface{}
Constraints constraints.Value
Hardware instance.HardwareCharacteristics
InstanceId string
AdminUsername string
ImageMetadataDir string
}
// Info returns a decription of the command.
func (c *BootstrapCommand) Info() *cmd.Info {
return &cmd.Info{
Name: "bootstrap-state",
Purpose: "initialize juju state",
}
}
func (c *BootstrapCommand) SetFlags(f *gnuflag.FlagSet) {
c.AgentConf.AddFlags(f)
yamlBase64Var(f, &c.EnvConfig, "env-config", "", "initial environment configuration (yaml, base64 encoded)")
f.Var(constraints.ConstraintsValue{Target: &c.Constraints}, "constraints", "initial environment constraints (space-separated strings)")
f.Var(&c.Hardware, "hardware", "hardware characteristics (space-separated strings)")
f.StringVar(&c.InstanceId, "instance-id", "", "unique instance-id for bootstrap machine")
f.StringVar(&c.AdminUsername, "admin-user", "admin", "set the name for the juju admin user")
f.StringVar(&c.ImageMetadataDir, "image-metadata", "", "custom image metadata source dir")
}
// Init initializes the command for running.
func (c *BootstrapCommand) Init(args []string) error {
if len(c.EnvConfig) == 0 {
return requiredError("env-config")
}
if c.InstanceId == "" {
return requiredError("instance-id")
}
if !names.IsValidUser(c.AdminUsername) {
return errors.Errorf("%q is not a valid username", c.AdminUsername)
}
return c.AgentConf.CheckArgs(args)
}
// Run initializes state for an environment.
func (c *BootstrapCommand) Run(_ *cmd.Context) error {
envCfg, err := config.New(config.NoDefaults, c.EnvConfig)
if err != nil {
return err
}
err = c.ReadConfig("machine-0")
if err != nil {
return err
}
agentConfig := c.CurrentConfig()
network.InitializeFromConfig(agentConfig)
// agent.Jobs is an optional field in the agent config, and was
// introduced after 1.17.2. We default to allowing units on
// machine-0 if missing.
jobs := agentConfig.Jobs()
if len(jobs) == 0 {
jobs = []params.MachineJob{
params.JobManageEnviron,
params.JobHostUnits,
params.JobManageNetworking,
}
}
// Get the bootstrap machine's addresses from the provider.
env, err := environs.New(envCfg)
if err != nil {
return err
}
instanceId := instance.Id(c.InstanceId)
instances, err := env.Instances([]instance.Id{instanceId})
if err != nil {
return err
}
addrs, err := instances[0].Addresses()
if err != nil {
return err
}
// Generate a private SSH key for the state servers, and add
// the public key to the environment config. We'll add the
// private key to StateServingInfo below.
privateKey, publicKey, err := sshGenerateKey(config.JujuSystemKey)
if err != nil {
return errors.Annotate(err, "failed to generate system key")
}
authorizedKeys := config.ConcatAuthKeys(envCfg.AuthorizedKeys(), publicKey)
envCfg, err = env.Config().Apply(map[string]interface{}{
config.AuthKeysConfig: authorizedKeys,
})
if err != nil {
return errors.Annotate(err, "failed to add public key to environment config")
}
// Generate a shared secret for the Mongo replica set, and write it out.
sharedSecret, err := mongo.GenerateSharedSecret()
if err != nil {
return err
}
info, ok := agentConfig.StateServingInfo()
if !ok {
return fmt.Errorf("bootstrap machine config has no state serving info")
}
info.SharedSecret = sharedSecret
info.SystemIdentity = privateKey
err = c.ChangeConfig(func(agentConfig agent.ConfigSetter) error {
agentConfig.SetStateServingInfo(info)
return nil
})
if err != nil {
return fmt.Errorf("cannot write agent config: %v", err)
}
agentConfig = c.CurrentConfig()
// Create system-identity file
if err := agent.WriteSystemIdentityFile(agentConfig); err != nil {
return err
}
if err := c.startMongo(addrs, agentConfig); err != nil {
return err
}
logger.Infof("started mongo")
// Initialise state, and store any agent config (e.g. password) changes.
var st *state.State
var m *state.Machine
err = c.ChangeConfig(func(agentConfig agent.ConfigSetter) error {
var stateErr error
dialOpts := mongo.DefaultDialOpts()
// Set a longer socket timeout than usual, as the machine
// will be starting up and disk I/O slower than usual. This
// has been known to cause timeouts in queries.
timeouts := envCfg.BootstrapSSHOpts()
dialOpts.SocketTimeout = timeouts.Timeout
if dialOpts.SocketTimeout < minSocketTimeout {
dialOpts.SocketTimeout = minSocketTimeout
}
// We shouldn't attempt to dial peers until we have some.
dialOpts.Direct = true
adminTag := names.NewLocalUserTag(c.AdminUsername)
st, m, stateErr = agentInitializeState(
adminTag,
agentConfig,
envCfg,
agent.BootstrapMachineConfig{
Addresses: addrs,
Constraints: c.Constraints,
Jobs: jobs,
InstanceId: instanceId,
Characteristics: c.Hardware,
SharedSecret: sharedSecret,
},
dialOpts,
environs.NewStatePolicy(),
)
return stateErr
})
if err != nil {
return err
}
defer st.Close()
// Populate the tools catalogue.
if err := c.populateTools(st, env); err != nil {
return err
}
// Add custom image metadata to environment storage.
if c.ImageMetadataDir != "" {
if err := c.storeCustomImageMetadata(stateStorage(st)); err != nil {
return err
}
}
// bootstrap machine always gets the vote
return m.SetHasVote(true)
}
// newEnsureServerParams creates an EnsureServerParams from an agent configuration.
func newEnsureServerParams(agentConfig agent.Config) (mongo.EnsureServerParams, error) {
// If oplog size is specified in the agent configuration, use that.
// Otherwise leave the default zero value to indicate to EnsureServer
// that it should calculate the size.
var oplogSize int
if oplogSizeString := agentConfig.Value(agent.MongoOplogSize); oplogSizeString != "" {
var err error
if oplogSize, err = strconv.Atoi(oplogSizeString); err != nil {
return mongo.EnsureServerParams{}, fmt.Errorf("invalid oplog size: %q", oplogSizeString)
}
}
si, ok := agentConfig.StateServingInfo()
if !ok {
return mongo.EnsureServerParams{}, fmt.Errorf("agent config has no state serving info")
}
params := mongo.EnsureServerParams{
APIPort: si.APIPort,
StatePort: si.StatePort,
Cert: si.Cert,
PrivateKey: si.PrivateKey,
SharedSecret: si.SharedSecret,
SystemIdentity: si.SystemIdentity,
DataDir: agentConfig.DataDir(),
Namespace: agentConfig.Value(agent.Namespace),
OplogSize: oplogSize,
}
return params, nil
}
func (c *BootstrapCommand) startMongo(addrs []network.Address, agentConfig agent.Config) error {
logger.Debugf("starting mongo")
info, ok := agentConfig.MongoInfo()
if !ok {
return fmt.Errorf("no state info available")
}
// When bootstrapping, we need to allow enough time for mongo
// to start as there's no retry loop in place.
// 5 minutes should suffice.
bootstrapDialOpts := mongo.DialOpts{Timeout: 5 * time.Minute}
dialInfo, err := mongo.DialInfo(info.Info, bootstrapDialOpts)
if err != nil {
return err
}
servingInfo, ok := agentConfig.StateServingInfo()
if !ok {
return fmt.Errorf("agent config has no state serving info")
}
// Use localhost to dial the mongo server, because it's running in
// auth mode and will refuse to perform any operations unless
// we dial that address.
dialInfo.Addrs = []string{
net.JoinHostPort("127.0.0.1", fmt.Sprint(servingInfo.StatePort)),
}
logger.Debugf("calling ensureMongoServer")
ensureServerParams, err := newEnsureServerParams(agentConfig)
if err != nil {
return err
}
err = ensureMongoServer(ensureServerParams)
if err != nil {
return err
}
peerAddr := mongo.SelectPeerAddress(addrs)
if peerAddr == "" {
return fmt.Errorf("no appropriate peer address found in %q", addrs)
}
peerHostPort := net.JoinHostPort(peerAddr, fmt.Sprint(servingInfo.StatePort))
return maybeInitiateMongoServer(peergrouper.InitiateMongoParams{
DialInfo: dialInfo,
MemberHostPort: peerHostPort,
})
}
// populateTools stores uploaded tools in provider storage
// and updates the tools metadata.
func (c *BootstrapCommand) populateTools(st *state.State, env environs.Environ) error {
agentConfig := c.CurrentConfig()
dataDir := agentConfig.DataDir()
tools, err := agenttools.ReadTools(dataDir, version.Current)
if err != nil {
return err
}
data, err := ioutil.ReadFile(filepath.Join(
agenttools.SharedToolsDir(dataDir, version.Current),
"tools.tar.gz",
))
if err != nil {
return err
}
storage, err := st.ToolsStorage()
if err != nil {
return err
}
defer storage.Close()
var toolsVersions []version.Binary
if strings.HasPrefix(tools.URL, "file://") {
// Tools were uploaded: clone for each series of the same OS.
osSeries := version.OSSupportedSeries(tools.Version.OS)
for _, series := range osSeries {
toolsVersion := tools.Version
toolsVersion.Series = series
toolsVersions = append(toolsVersions, toolsVersion)
}
} else {
// Tools were downloaded from an external source: don't clone.
toolsVersions = []version.Binary{tools.Version}
}
for _, toolsVersion := range toolsVersions {
metadata := toolstorage.Metadata{
Version: toolsVersion,
Size: tools.Size,
SHA256: tools.SHA256,
}
logger.Debugf("Adding tools: %v", toolsVersion)
if err := storage.AddTools(bytes.NewReader(data), metadata); err != nil {
return err
}
}
return nil
}
// storeCustomImageMetadata reads the custom image metadata from disk,
// and stores the files in environment storage with the same relative
// paths.
func (c *BootstrapCommand) storeCustomImageMetadata(stor state.Storage) error {
logger.Debugf("storing custom image metadata from %q", c.ImageMetadataDir)
return filepath.Walk(c.ImageMetadataDir, func(abspath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.Mode().IsRegular() {
return nil
}
relpath, err := filepath.Rel(c.ImageMetadataDir, abspath)
if err != nil {
return err
}
f, err := os.Open(abspath)
if err != nil {
return err
}
defer f.Close()
logger.Debugf("storing %q in environment storage (%d bytes)", relpath, info.Size())
return stor.Put(relpath, f, info.Size())
})
}
// yamlBase64Value implements gnuflag.Value on a map[string]interface{}.
type yamlBase64Value map[string]interface{}
// Set decodes the base64 value into yaml then expands that into a map.
func (v *yamlBase64Value) Set(value string) error {
decoded, err := base64.StdEncoding.DecodeString(value)
if err != nil {
return err
}
return goyaml.Unmarshal(decoded, v)
}
func (v *yamlBase64Value) String() string {
return fmt.Sprintf("%v", *v)
}
// yamlBase64Var sets up a gnuflag flag analogous to the FlagSet.*Var methods.
func yamlBase64Var(fs *gnuflag.FlagSet, target *map[string]interface{}, name string, value string, usage string) {
fs.Var((*yamlBase64Value)(target), name, usage)
}