forked from juju/juju
/
bootstrap.go
408 lines (372 loc) · 12.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
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package common
import (
"fmt"
"io"
"os"
"path"
"strings"
"time"
"github.com/juju/loggo"
"github.com/juju/utils"
"github.com/juju/utils/parallel"
"github.com/juju/utils/shell"
coreCloudinit "github.com/juju/juju/cloudinit"
"github.com/juju/juju/cloudinit/sshinit"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/bootstrap"
"github.com/juju/juju/environs/cloudinit"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/instance"
"github.com/juju/juju/network"
coretools "github.com/juju/juju/tools"
"github.com/juju/juju/utils/ssh"
)
var logger = loggo.GetLogger("juju.provider.common")
// Bootstrap is a common implementation of the Bootstrap method defined on
// environs.Environ; we strongly recommend that this implementation be used
// when writing a new provider.
func Bootstrap(ctx environs.BootstrapContext, env environs.Environ, args environs.BootstrapParams) (err error) {
// TODO make safe in the case of racing Bootstraps
// If two Bootstraps are called concurrently, there's
// no way to make sure that only one succeeds.
var inst instance.Instance
defer func() { handleBootstrapError(err, ctx, inst, env) }()
// First thing, ensure we have tools otherwise there's no point.
selectedTools, err := EnsureBootstrapTools(ctx, env, config.PreferredSeries(env.Config()), args.Constraints.Arch)
if err != nil {
return err
}
// Get the bootstrap SSH client. Do this early, so we know
// not to bother with any of the below if we can't finish the job.
client := ssh.DefaultClient
if client == nil {
// This should never happen: if we don't have OpenSSH, then
// go.crypto/ssh should be used with an auto-generated key.
return fmt.Errorf("no SSH client available")
}
privateKey, err := GenerateSystemSSHKey(env)
if err != nil {
return err
}
machineConfig := environs.NewBootstrapMachineConfig(privateKey)
fmt.Fprintln(ctx.GetStderr(), "Launching instance")
inst, hw, _, err := env.StartInstance(environs.StartInstanceParams{
Constraints: args.Constraints,
Tools: selectedTools,
MachineConfig: machineConfig,
Placement: args.Placement,
})
if err != nil {
return fmt.Errorf("cannot start bootstrap instance: %v", err)
}
fmt.Fprintf(ctx.GetStderr(), " - %s\n", inst.Id())
machineConfig.InstanceId = inst.Id()
machineConfig.HardwareCharacteristics = hw
err = bootstrap.SaveState(
env.Storage(),
&bootstrap.BootstrapState{
StateInstances: []instance.Id{inst.Id()},
})
if err != nil {
return fmt.Errorf("cannot save state: %v", err)
}
return FinishBootstrap(ctx, client, inst, machineConfig)
}
// GenerateSystemSSHKey creates a new key for the system identity. The
// authorized_keys in the environment config is updated to include the public
// key for the generated key.
func GenerateSystemSSHKey(env environs.Environ) (privateKey string, err error) {
logger.Debugf("generate a system ssh key")
// Create a new system ssh key and add that to the authorized keys.
privateKey, publicKey, err := ssh.GenerateKey(config.JujuSystemKey)
if err != nil {
return "", fmt.Errorf("failed to create system key: %v", err)
}
authorized_keys := config.ConcatAuthKeys(env.Config().AuthorizedKeys(), publicKey)
newConfig, err := env.Config().Apply(map[string]interface{}{
config.AuthKeysConfig: authorized_keys,
})
if err != nil {
return "", fmt.Errorf("failed to create new config: %v", err)
}
if err = env.SetConfig(newConfig); err != nil {
return "", fmt.Errorf("failed to set new config: %v", err)
}
return privateKey, nil
}
// handleBootstrapError cleans up after a failed bootstrap.
func handleBootstrapError(err error, ctx environs.BootstrapContext, inst instance.Instance, env environs.Environ) {
if err == nil {
return
}
logger.Errorf("bootstrap failed: %v", err)
ch := make(chan os.Signal, 1)
ctx.InterruptNotify(ch)
defer ctx.StopInterruptNotify(ch)
defer close(ch)
go func() {
for _ = range ch {
fmt.Fprintln(ctx.GetStderr(), "Cleaning up failed bootstrap")
}
}()
if inst != nil {
fmt.Fprintln(ctx.GetStderr(), "Stopping instance...")
if stoperr := env.StopInstances(inst.Id()); stoperr != nil {
logger.Errorf("cannot stop failed bootstrap instance %q: %v", inst.Id(), stoperr)
} else {
// set to nil so we know we can safely delete the state file
inst = nil
}
}
// We only delete the bootstrap state file if either we didn't
// start an instance, or we managed to cleanly stop it.
if inst == nil {
if rmerr := bootstrap.DeleteStateFile(env.Storage()); rmerr != nil {
logger.Errorf("cannot delete bootstrap state file: %v", rmerr)
}
}
}
// FinishBootstrap completes the bootstrap process by connecting
// to the instance via SSH and carrying out the cloud-config.
//
// Note: FinishBootstrap is exposed so it can be replaced for testing.
var FinishBootstrap = func(ctx environs.BootstrapContext, client ssh.Client, inst instance.Instance, machineConfig *cloudinit.MachineConfig) error {
interrupted := make(chan os.Signal, 1)
ctx.InterruptNotify(interrupted)
defer ctx.StopInterruptNotify(interrupted)
// Each attempt to connect to an address must verify the machine is the
// bootstrap machine by checking its nonce file exists and contains the
// nonce in the MachineConfig. This also blocks sshinit from proceeding
// until cloud-init has completed, which is necessary to ensure apt
// invocations don't trample each other.
nonceFile := utils.ShQuote(path.Join(machineConfig.DataDir, cloudinit.NonceFile))
checkNonceCommand := fmt.Sprintf(`
noncefile=%s
if [ ! -e "$noncefile" ]; then
echo "$noncefile does not exist" >&2
exit 1
fi
content=$(cat $noncefile)
if [ "$content" != %s ]; then
echo "$noncefile contents do not match machine nonce" >&2
exit 1
fi
`, nonceFile, utils.ShQuote(machineConfig.MachineNonce))
addr, err := waitSSH(
ctx,
interrupted,
client,
checkNonceCommand,
inst,
machineConfig.Config.BootstrapSSHOpts(),
)
if err != nil {
return err
}
// Bootstrap is synchronous, and will spawn a subprocess
// to complete the procedure. If the user hits Ctrl-C,
// SIGINT is sent to the foreground process attached to
// the terminal, which will be the ssh subprocess at this
// point. For that reason, we do not call StopInterruptNotify
// until this function completes.
cloudcfg := coreCloudinit.New()
if err := cloudinit.ConfigureJuju(machineConfig, cloudcfg); err != nil {
return err
}
configScript, err := sshinit.ConfigureScript(cloudcfg)
if err != nil {
return err
}
script := shell.DumpFileOnErrorScript(machineConfig.CloudInitOutputLog) + configScript
return sshinit.RunConfigureScript(script, sshinit.ConfigureParams{
Host: "ubuntu@" + addr,
Client: client,
Config: cloudcfg,
ProgressWriter: ctx.GetStderr(),
})
}
type addresser interface {
// Refresh refreshes the addresses for the instance.
Refresh() error
// Addresses returns the addresses for the instance.
// To ensure that the results are up to date, call
// Refresh first.
Addresses() ([]network.Address, error)
}
type hostChecker struct {
addr network.Address
client ssh.Client
// checkDelay is the amount of time to wait between retries.
checkDelay time.Duration
// checkHostScript is executed on the host via SSH.
// hostChecker.loop will return once the script
// runs without error.
checkHostScript string
// closed is closed to indicate that the host checker should
// return, without waiting for the result of any ongoing
// attempts.
closed <-chan struct{}
}
// Close implements io.Closer, as required by parallel.Try.
func (*hostChecker) Close() error {
return nil
}
func (hc *hostChecker) loop(dying <-chan struct{}) (io.Closer, error) {
// The value of connectSSH is taken outside the goroutine that may outlive
// hostChecker.loop, or we evoke the wrath of the race detector.
connectSSH := connectSSH
done := make(chan error, 1)
var lastErr error
for {
go func() {
done <- connectSSH(hc.client, hc.addr.Value, hc.checkHostScript)
}()
select {
case <-hc.closed:
return hc, lastErr
case <-dying:
return hc, lastErr
case lastErr = <-done:
if lastErr == nil {
return hc, nil
}
}
select {
case <-hc.closed:
case <-dying:
case <-time.After(hc.checkDelay):
}
}
}
type parallelHostChecker struct {
*parallel.Try
client ssh.Client
stderr io.Writer
// active is a map of adresses to channels for addresses actively
// being tested. The goroutine testing the address will continue
// to attempt connecting to the address until it succeeds, the Try
// is killed, or the corresponding channel in this map is closed.
active map[network.Address]chan struct{}
// checkDelay is how long each hostChecker waits between attempts.
checkDelay time.Duration
// checkHostScript is the script to run on each host to check that
// it is the host we expect.
checkHostScript string
}
func (p *parallelHostChecker) UpdateAddresses(addrs []network.Address) {
for _, addr := range addrs {
if _, ok := p.active[addr]; ok {
continue
}
fmt.Fprintf(p.stderr, "Attempting to connect to %s:22\n", addr.Value)
closed := make(chan struct{})
hc := &hostChecker{
addr: addr,
client: p.client,
checkDelay: p.checkDelay,
checkHostScript: p.checkHostScript,
closed: closed,
}
p.active[addr] = closed
p.Start(hc.loop)
}
}
// Close prevents additional functions from being added to
// the Try, and tells each active hostChecker to exit.
func (p *parallelHostChecker) Close() error {
// We signal each checker to stop and wait for them
// each to complete; this allows us to get the error,
// as opposed to when using try.Kill which does not
// wait for the functions to complete.
p.Try.Close()
for _, ch := range p.active {
close(ch)
}
return nil
}
// connectSSH is called to connect to the specified host and
// execute the "checkHostScript" bash script on it.
var connectSSH = func(client ssh.Client, host, checkHostScript string) error {
cmd := client.Command("ubuntu@"+host, []string{"/bin/bash"}, nil)
cmd.Stdin = strings.NewReader(checkHostScript)
output, err := cmd.CombinedOutput()
if err != nil && len(output) > 0 {
err = fmt.Errorf("%s", strings.TrimSpace(string(output)))
}
return err
}
// waitSSH waits for the instance to be assigned a routable
// address, then waits until we can connect to it via SSH.
//
// waitSSH attempts on all addresses returned by the instance
// in parallel; the first succeeding one wins. We ensure that
// private addresses are for the correct machine by checking
// the presence of a file on the machine that contains the
// machine's nonce. The "checkHostScript" is a bash script
// that performs this file check.
func waitSSH(ctx environs.BootstrapContext, interrupted <-chan os.Signal, client ssh.Client, checkHostScript string, inst addresser, timeout config.SSHTimeoutOpts) (addr string, err error) {
globalTimeout := time.After(timeout.Timeout)
pollAddresses := time.NewTimer(0)
// checker checks each address in a loop, in parallel,
// until one succeeds, the global timeout is reached,
// or the tomb is killed.
checker := parallelHostChecker{
Try: parallel.NewTry(0, nil),
client: client,
stderr: ctx.GetStderr(),
active: make(map[network.Address]chan struct{}),
checkDelay: timeout.RetryDelay,
checkHostScript: checkHostScript,
}
defer checker.Kill()
fmt.Fprintln(ctx.GetStderr(), "Waiting for address")
for {
select {
case <-pollAddresses.C:
pollAddresses.Reset(timeout.AddressesDelay)
if err := inst.Refresh(); err != nil {
return "", fmt.Errorf("refreshing addresses: %v", err)
}
addresses, err := inst.Addresses()
if err != nil {
return "", fmt.Errorf("getting addresses: %v", err)
}
checker.UpdateAddresses(addresses)
case <-globalTimeout:
checker.Close()
lastErr := checker.Wait()
format := "waited for %v "
args := []interface{}{timeout.Timeout}
if len(checker.active) == 0 {
format += "without getting any addresses"
} else {
format += "without being able to connect"
}
if lastErr != nil && lastErr != parallel.ErrStopped {
format += ": %v"
args = append(args, lastErr)
}
return "", fmt.Errorf(format, args...)
case <-interrupted:
return "", fmt.Errorf("interrupted")
case <-checker.Dead():
result, err := checker.Result()
if err != nil {
return "", err
}
return result.(*hostChecker).addr.Value, nil
}
}
}
// EnsureBootstrapTools finds tools, syncing with an external tools source as
// necessary; it then selects the newest tools to bootstrap with, and sets
// agent-version.
func EnsureBootstrapTools(ctx environs.BootstrapContext, env environs.Environ, series string, arch *string) (coretools.List, error) {
possibleTools, err := bootstrap.EnsureToolsAvailability(ctx, env, series, arch)
if err != nil {
return nil, err
}
return bootstrap.SetBootstrapTools(env, possibleTools)
}