forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addmachine.go
606 lines (556 loc) · 20 KB
/
addmachine.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state
import (
"fmt"
"strconv"
"github.com/juju/errors"
"github.com/juju/mgo/v2/txn"
"github.com/juju/names/v4"
"github.com/juju/juju/core/constraints"
"github.com/juju/juju/core/instance"
"github.com/juju/juju/core/network"
"github.com/juju/juju/core/status"
"github.com/juju/juju/storage"
)
// MachineTemplate holds attributes that are to be associated
// with a newly created machine.
type MachineTemplate struct {
// Series is the series to be associated with the new machine.
Series string
// Constraints are the constraints to be used when finding
// an instance for the machine.
Constraints constraints.Value
// Jobs holds the jobs to run on the machine's instance.
// A machine must have at least one job to do.
// JobManageModel can only be part of the jobs
// when the first (bootstrap) machine is added.
Jobs []MachineJob
// Addresses holds the addresses to be associated with the
// new machine.
//
// TODO(dimitern): This should be removed once all addresses
// come from link-layer device addresses.
Addresses network.SpaceAddresses
// InstanceId holds the instance id to associate with the machine.
// If this is empty, the provisioner will try to provision the machine.
// If this is non-empty, the HardwareCharacteristics and Nonce
// fields must be set appropriately.
InstanceId instance.Id
// HardwareCharacteristics holds the h/w characteristics to
// be associated with the machine.
HardwareCharacteristics instance.HardwareCharacteristics
// LinkLayerDevices holds a list of arguments for setting link-layer devices
// on the machine.
LinkLayerDevices []LinkLayerDeviceArgs
// Volumes holds the parameters for volumes that are to be created
// and attached to the machine.
Volumes []HostVolumeParams
// VolumeAttachments holds the parameters for attaching existing
// volumes to the machine.
VolumeAttachments map[names.VolumeTag]VolumeAttachmentParams
// Filesystems holds the parameters for filesystems that are to be
// created and attached to the machine.
Filesystems []HostFilesystemParams
// FilesystemAttachments holds the parameters for attaching existing
// filesystems to the machine.
FilesystemAttachments map[names.FilesystemTag]FilesystemAttachmentParams
// Nonce holds a unique value that can be used to check
// if a new instance was really started for this machine.
// See Machine.SetProvisioned. This must be set if InstanceId is set.
Nonce string
// Dirty signifies whether the new machine will be treated
// as unclean for unit-assignment purposes.
Dirty bool
// Placement holds the placement directive that will be associated
// with the machine.
Placement string
// principals holds the principal units that will
// associated with the machine.
principals []string
}
// HostVolumeParams holds the parameters for creating a volume and
// attaching it to a new host.
type HostVolumeParams struct {
Volume VolumeParams
Attachment VolumeAttachmentParams
}
// HostFilesystemParams holds the parameters for creating a filesystem
// and attaching it to a new host.
type HostFilesystemParams struct {
Filesystem FilesystemParams
Attachment FilesystemAttachmentParams
}
// AddMachineInsideNewMachine creates a new machine within a container
// of the given type inside another new machine. The two given templates
// specify the form of the child and parent respectively.
func (st *State) AddMachineInsideNewMachine(template, parentTemplate MachineTemplate, containerType instance.ContainerType) (*Machine, error) {
mdoc, ops, err := st.addMachineInsideNewMachineOps(template, parentTemplate, containerType)
if err != nil {
return nil, errors.Annotate(err, "cannot add a new machine")
}
return st.addMachine(mdoc, ops)
}
// AddMachineInsideMachine adds a machine inside a container of the
// given type on the existing machine with id=parentId.
func (st *State) AddMachineInsideMachine(template MachineTemplate, parentId string, containerType instance.ContainerType) (*Machine, error) {
mdoc, ops, err := st.addMachineInsideMachineOps(template, parentId, containerType)
if err != nil {
return nil, errors.Annotate(err, "cannot add a new machine")
}
return st.addMachine(mdoc, ops)
}
// AddMachine adds a machine with the given series and jobs.
// It is deprecated and around for testing purposes only.
func (st *State) AddMachine(series string, jobs ...MachineJob) (*Machine, error) {
ms, err := st.AddMachines(MachineTemplate{
Series: series,
Jobs: jobs,
})
if err != nil {
return nil, err
}
return ms[0], nil
}
// AddOneMachine machine adds a new machine configured according to the
// given template.
func (st *State) AddOneMachine(template MachineTemplate) (*Machine, error) {
ms, err := st.AddMachines(template)
if err != nil {
return nil, err
}
return ms[0], nil
}
// AddMachines adds new machines configured according to the
// given templates.
func (st *State) AddMachines(templates ...MachineTemplate) (_ []*Machine, err error) {
defer errors.DeferredAnnotatef(&err, "cannot add a new machine")
var ms []*Machine
var ops []txn.Op
var controllerIds []string
for _, template := range templates {
mdoc, addOps, err := st.addMachineOps(template)
if err != nil {
return nil, errors.Trace(err)
}
if isController(mdoc) {
controllerIds = append(controllerIds, mdoc.Id)
}
ms = append(ms, newMachine(st, mdoc))
ops = append(ops, addOps...)
}
ssOps, err := st.maintainControllersOps(controllerIds, true)
if err != nil {
return nil, errors.Trace(err)
}
ops = append(ops, ssOps...)
ops = append(ops, assertModelActiveOp(st.ModelUUID()))
if err := st.db().RunTransaction(ops); err != nil {
if errors.Cause(err) == txn.ErrAborted {
if err := checkModelActive(st); err != nil {
return nil, errors.Trace(err)
}
}
return nil, errors.Trace(err)
}
return ms, nil
}
func (st *State) addMachine(mdoc *machineDoc, ops []txn.Op) (*Machine, error) {
ops = append([]txn.Op{assertModelActiveOp(st.ModelUUID())}, ops...)
if err := st.db().RunTransaction(ops); err != nil {
if errors.Cause(err) == txn.ErrAborted {
if err := checkModelActive(st); err != nil {
return nil, errors.Trace(err)
}
}
return nil, errors.Trace(err)
}
return newMachine(st, mdoc), nil
}
func (st *State) resolveMachineConstraints(cons constraints.Value) (constraints.Value, error) {
mcons, err := st.ResolveConstraints(cons)
if err != nil {
return constraints.Value{}, err
}
// Machine constraints do not use a container constraint value.
// Both provisioning and deployment constraints use the same
// constraints.Value struct so here we clear the container
// value. Provisioning ignores the container value but clearing
// it avoids potential confusion.
mcons.Container = nil
return mcons, nil
}
// effectiveMachineTemplate verifies that the given template is
// valid and combines it with values from the state
// to produce a resulting template that more accurately
// represents the data that will be inserted into the state.
func (st *State) effectiveMachineTemplate(p MachineTemplate, allowController bool) (tmpl MachineTemplate, err error) {
// First check for obvious errors.
if p.Series == "" {
return tmpl, errors.New("no series specified")
}
if p.InstanceId != "" {
if p.Nonce == "" {
return tmpl, errors.New("cannot add a machine with an instance id and no nonce")
}
} else if p.Nonce != "" {
return tmpl, errors.New("cannot specify a nonce without an instance id")
}
// We ignore all constraints if there's a placement directive.
if p.Placement == "" {
p.Constraints, err = st.resolveMachineConstraints(p.Constraints)
if err != nil {
return tmpl, err
}
}
if len(p.Jobs) == 0 {
return tmpl, errors.New("no jobs specified")
}
jset := make(map[MachineJob]bool)
for _, j := range p.Jobs {
if jset[j] {
return MachineTemplate{}, errors.Errorf("duplicate job: %s", j)
}
jset[j] = true
}
if jset[JobManageModel] {
if !allowController {
return tmpl, errControllerNotAllowed
}
}
return p, nil
}
// addMachineOps returns operations to add a new top level machine
// based on the given template. It also returns the machine document
// that will be inserted.
func (st *State) addMachineOps(template MachineTemplate) (*machineDoc, []txn.Op, error) {
template, err := st.effectiveMachineTemplate(template, st.IsController())
if err != nil {
return nil, nil, err
}
if template.InstanceId == "" {
volumeAttachments, err := st.machineTemplateVolumeAttachmentParams(template)
if err != nil {
return nil, nil, err
}
if err := st.precheckInstance(
template.Series,
template.Constraints,
template.Placement,
volumeAttachments,
); err != nil {
return nil, nil, err
}
}
seq, err := sequence(st, "machine")
if err != nil {
return nil, nil, err
}
mdoc := st.machineDocForTemplate(template, strconv.Itoa(seq))
prereqOps, machineOp, err := st.insertNewMachineOps(mdoc, template)
if err != nil {
return nil, nil, errors.Trace(err)
}
prereqOps = append(prereqOps, assertModelActiveOp(st.ModelUUID()))
prereqOps = append(prereqOps, insertNewContainerRefOp(st, mdoc.Id))
if template.InstanceId != "" {
prereqOps = append(prereqOps, txn.Op{
C: instanceDataC,
Id: mdoc.DocID,
Assert: txn.DocMissing,
Insert: &instanceData{
DocID: mdoc.DocID,
MachineId: mdoc.Id,
InstanceId: template.InstanceId,
ModelUUID: mdoc.ModelUUID,
Arch: template.HardwareCharacteristics.Arch,
Mem: template.HardwareCharacteristics.Mem,
RootDisk: template.HardwareCharacteristics.RootDisk,
RootDiskSource: template.HardwareCharacteristics.RootDiskSource,
CpuCores: template.HardwareCharacteristics.CpuCores,
CpuPower: template.HardwareCharacteristics.CpuPower,
Tags: template.HardwareCharacteristics.Tags,
AvailZone: template.HardwareCharacteristics.AvailabilityZone,
},
})
}
if isController(mdoc) {
prereqOps = append(prereqOps, addControllerNodeOp(st, mdoc.Id, false))
}
return mdoc, append(prereqOps, machineOp), nil
}
// supportsContainerType reports whether the machine supports the given
// container type. If the machine's supportedContainers attribute is
// set, this decision can be made right here, otherwise we assume that
// everything will be ok and later on put the container into an error
// state if necessary.
func (m *Machine) supportsContainerType(ctype instance.ContainerType) bool {
supportedContainers, ok := m.SupportedContainers()
if !ok {
// We don't know yet, so we report that we support the container.
return true
}
for _, ct := range supportedContainers {
if ct == ctype {
return true
}
}
return false
}
// addMachineInsideMachineOps returns operations to add a machine inside
// a container of the given type on an existing machine.
func (st *State) addMachineInsideMachineOps(template MachineTemplate, parentId string, containerType instance.ContainerType) (*machineDoc, []txn.Op, error) {
if template.InstanceId != "" {
return nil, nil, errors.New("cannot specify instance id for a new container")
}
template, err := st.effectiveMachineTemplate(template, false)
if err != nil {
return nil, nil, err
}
if containerType == "" {
return nil, nil, errors.New("no container type specified")
}
// If a parent machine is specified, make sure it exists
// and can support the requested container type.
parent, err := st.Machine(parentId)
if err != nil {
return nil, nil, err
}
if !parent.supportsContainerType(containerType) {
return nil, nil, errors.Errorf("machine %s cannot host %s containers", parentId, containerType)
}
// Ensure that the machine is not locked for series-upgrade.
locked, err := parent.IsLockedForSeriesUpgrade()
if err != nil {
return nil, nil, err
}
if locked {
return nil, nil, errors.Errorf("machine %s is locked for series upgrade", parentId)
}
newId, err := st.newContainerId(parentId, containerType)
if err != nil {
return nil, nil, err
}
mdoc := st.machineDocForTemplate(template, newId)
mdoc.ContainerType = string(containerType)
prereqOps, machineOp, err := st.insertNewMachineOps(mdoc, template)
if err != nil {
return nil, nil, errors.Trace(err)
}
prereqOps = append(prereqOps,
// Update containers record for host machine.
addChildToContainerRefOp(st, parentId, mdoc.Id),
// Create a containers reference document for the container itself.
insertNewContainerRefOp(st, mdoc.Id),
)
return mdoc, append(prereqOps, machineOp), nil
}
// newContainerId returns a new id for a machine within the machine
// with id parentId and the given container type.
func (st *State) newContainerId(parentId string, containerType instance.ContainerType) (string, error) {
name := fmt.Sprintf("machine%s%sContainer", parentId, containerType)
seq, err := sequence(st, name)
if err != nil {
return "", err
}
return fmt.Sprintf("%s/%s/%d", parentId, containerType, seq), nil
}
// addMachineInsideNewMachineOps returns operations to create a new
// machine within a container of the given type inside another
// new machine. The two given templates specify the form
// of the child and parent respectively.
func (st *State) addMachineInsideNewMachineOps(template, parentTemplate MachineTemplate, containerType instance.ContainerType) (*machineDoc, []txn.Op, error) {
if template.InstanceId != "" || parentTemplate.InstanceId != "" {
return nil, nil, errors.New("cannot specify instance id for a new container")
}
seq, err := sequence(st, "machine")
if err != nil {
return nil, nil, err
}
parentTemplate, err = st.effectiveMachineTemplate(parentTemplate, false)
if err != nil {
return nil, nil, err
}
if containerType == "" {
return nil, nil, errors.New("no container type specified")
}
if parentTemplate.InstanceId == "" {
volumeAttachments, err := st.machineTemplateVolumeAttachmentParams(parentTemplate)
if err != nil {
return nil, nil, err
}
if err := st.precheckInstance(
parentTemplate.Series,
parentTemplate.Constraints,
parentTemplate.Placement,
volumeAttachments,
); err != nil {
return nil, nil, err
}
}
parentDoc := st.machineDocForTemplate(parentTemplate, strconv.Itoa(seq))
newId, err := st.newContainerId(parentDoc.Id, containerType)
if err != nil {
return nil, nil, err
}
template, err = st.effectiveMachineTemplate(template, false)
if err != nil {
return nil, nil, err
}
mdoc := st.machineDocForTemplate(template, newId)
mdoc.ContainerType = string(containerType)
parentPrereqOps, parentOp, err := st.insertNewMachineOps(parentDoc, parentTemplate)
if err != nil {
return nil, nil, errors.Trace(err)
}
prereqOps, machineOp, err := st.insertNewMachineOps(mdoc, template)
if err != nil {
return nil, nil, errors.Trace(err)
}
prereqOps = append(prereqOps, parentPrereqOps...)
prereqOps = append(prereqOps,
// The host machine doesn't exist yet, create a new containers record.
insertNewContainerRefOp(st, mdoc.Id),
// Create a containers reference document for the container itself.
insertNewContainerRefOp(st, parentDoc.Id, mdoc.Id),
)
return mdoc, append(prereqOps, parentOp, machineOp), nil
}
func (st *State) machineTemplateVolumeAttachmentParams(t MachineTemplate) ([]storage.VolumeAttachmentParams, error) {
sb, err := NewStorageBackend(st)
if err != nil {
return nil, errors.Trace(err)
}
out := make([]storage.VolumeAttachmentParams, 0, len(t.VolumeAttachments))
for volumeTag, a := range t.VolumeAttachments {
v, err := sb.Volume(volumeTag)
if err != nil {
return nil, errors.Trace(err)
}
volumeInfo, err := v.Info()
if err != nil {
return nil, errors.Trace(err)
}
providerType, _, _, err := poolStorageProvider(sb, volumeInfo.Pool)
if err != nil {
return nil, errors.Trace(err)
}
out = append(out, storage.VolumeAttachmentParams{
AttachmentParams: storage.AttachmentParams{
Provider: providerType,
ReadOnly: a.ReadOnly,
},
Volume: volumeTag,
VolumeId: volumeInfo.VolumeId,
})
}
return out, nil
}
func (st *State) machineDocForTemplate(template MachineTemplate, id string) *machineDoc {
// We ignore the error from Select*Address as an error indicates
// no address is available, in which case the empty address is returned
// and setting the preferred address to an empty one is the correct
// thing to do when none is available.
privateAddr, _ := template.Addresses.OneMatchingScope(network.ScopeMatchCloudLocal)
publicAddr, _ := template.Addresses.OneMatchingScope(network.ScopeMatchPublic)
logger.Infof(
"new machine %q has preferred addresses: private %q, public %q",
id, privateAddr, publicAddr,
)
return &machineDoc{
DocID: st.docID(id),
Id: id,
ModelUUID: st.ModelUUID(),
Series: template.Series,
Jobs: template.Jobs,
Clean: !template.Dirty,
Principals: template.principals,
Life: Alive,
Nonce: template.Nonce,
Addresses: fromNetworkAddresses(template.Addresses, network.OriginMachine),
PreferredPrivateAddress: fromNetworkAddress(privateAddr, network.OriginMachine),
PreferredPublicAddress: fromNetworkAddress(publicAddr, network.OriginMachine),
Placement: template.Placement,
}
}
// insertNewMachineOps returns operations to insert the given machine document
// into the database, based on the given template. Only the constraints are
// taken from the template.
func (st *State) insertNewMachineOps(mdoc *machineDoc, template MachineTemplate) (prereqOps []txn.Op, machineOp txn.Op, err error) {
now := st.clock().Now()
machineStatusDoc := statusDoc{
Status: status.Pending,
ModelUUID: st.ModelUUID(),
Updated: now.UnixNano(),
}
instanceStatusDoc := statusDoc{
Status: status.Pending,
ModelUUID: st.ModelUUID(),
Updated: now.UnixNano(),
}
modificationStatusDoc := statusDoc{
Status: status.Idle,
ModelUUID: st.ModelUUID(),
Updated: now.UnixNano(),
}
prereqOps, machineOp = st.baseNewMachineOps(
mdoc,
machineStatusDoc,
instanceStatusDoc,
modificationStatusDoc,
template.Constraints,
)
sb, err := NewStorageBackend(st)
if err != nil {
return nil, txn.Op{}, errors.Trace(err)
}
storageOps, volumeAttachments, filesystemAttachments, err := sb.hostStorageOps(
mdoc.Id, &storageParams{
filesystems: template.Filesystems,
filesystemAttachments: template.FilesystemAttachments,
volumes: template.Volumes,
volumeAttachments: template.VolumeAttachments,
},
)
if err != nil {
return nil, txn.Op{}, errors.Trace(err)
}
for _, a := range volumeAttachments {
mdoc.Volumes = append(mdoc.Volumes, a.tag.Id())
}
for _, a := range filesystemAttachments {
mdoc.Filesystems = append(mdoc.Filesystems, a.tag.Id())
}
prereqOps = append(prereqOps, storageOps...)
// At the last moment we still have statusDoc in scope, set the initial
// history entry. This is risky, and may lead to extra entries, but that's
// an intrinsic problem with mixing txn and non-txn ops -- we can't sync
// them cleanly.
_, _ = probablyUpdateStatusHistory(st.db(), machineGlobalKey(mdoc.Id), machineStatusDoc)
_, _ = probablyUpdateStatusHistory(st.db(), machineGlobalInstanceKey(mdoc.Id), instanceStatusDoc)
_, _ = probablyUpdateStatusHistory(st.db(), machineGlobalModificationKey(mdoc.Id), modificationStatusDoc)
return prereqOps, machineOp, nil
}
func (st *State) baseNewMachineOps(mdoc *machineDoc,
machineStatusDoc, instanceStatusDoc, modificationStatusDoc statusDoc,
cons constraints.Value,
) (prereqOps []txn.Op, machineOp txn.Op) {
machineOp = txn.Op{
C: machinesC,
Id: mdoc.DocID,
Assert: txn.DocMissing,
Insert: mdoc,
}
globalKey := machineGlobalKey(mdoc.Id)
globalInstanceKey := machineGlobalInstanceKey(mdoc.Id)
globalModificationKey := machineGlobalModificationKey(mdoc.Id)
prereqOps = []txn.Op{
createConstraintsOp(globalKey, cons),
createStatusOp(st, globalKey, machineStatusDoc),
createStatusOp(st, globalInstanceKey, instanceStatusDoc),
createStatusOp(st, globalModificationKey, modificationStatusDoc),
createMachineBlockDevicesOp(mdoc.Id),
addModelMachineRefOp(st, mdoc.Id),
}
return prereqOps, machineOp
}