/
model.go
880 lines (774 loc) · 24.4 KB
/
model.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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state
import (
"fmt"
"strings"
"github.com/juju/errors"
"github.com/juju/names"
jujutxn "github.com/juju/txn"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"gopkg.in/mgo.v2/txn"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/mongo"
"github.com/juju/juju/status"
"github.com/juju/version"
)
// modelGlobalKey is the key for the model, its
// settings and constraints.
const modelGlobalKey = "e"
// MigrationMode specifies where the Model is with respect to migration.
type MigrationMode string
const (
// MigrationModeActive is the default mode for a model and reflects
// a model that is active within its controller.
MigrationModeActive MigrationMode = ""
// MigrationModeExporting reflects a model that is in the process of being
// exported from one controller to another.
MigrationModeExporting MigrationMode = "exporting"
// MigrationModeImporting reflects a model that is being imported into a
// controller, but is not yet fully active.
MigrationModeImporting MigrationMode = "importing"
)
// Model represents the state of a model.
type Model struct {
// st is not necessarily the state of this model. Though it is
// usually safe to assume that it is. The only times it isn't is when we
// get models other than the current one - which is mostly in
// controller api endpoints.
st *State
doc modelDoc
}
// modelDoc represents the internal state of the model in MongoDB.
type modelDoc struct {
UUID string `bson:"_id"`
Name string
Life Life
Owner string `bson:"owner"`
ServerUUID string `bson:"server-uuid"`
MigrationMode MigrationMode `bson:"migration-mode"`
// LatestAvailableTools is a string representing the newest version
// found while checking streams for new versions.
LatestAvailableTools string `bson:"available-tools,omitempty"`
}
// modelEntityRefsDoc records references to the top-level entities
// in the model.
type modelEntityRefsDoc struct {
UUID string `bson:"_id"`
// Machines contains the names of the top-level machines in the model.
Machines []string `bson:"machines"`
// Services contains the names of the services in the model.
Services []string `bson:"services"`
}
// ControllerModel returns the model that was bootstrapped.
// This is the only model that can have controller machines.
// The owner of this model is also considered "special", in that
// they are the only user that is able to create other users (until we
// have more fine grained permissions), and they cannot be disabled.
func (st *State) ControllerModel() (*Model, error) {
ssinfo, err := st.ControllerInfo()
if err != nil {
return nil, errors.Annotate(err, "could not get controller info")
}
models, closer := st.getCollection(modelsC)
defer closer()
env := &Model{st: st}
uuid := ssinfo.ModelTag.Id()
if err := env.refresh(models.FindId(uuid)); err != nil {
return nil, errors.Trace(err)
}
return env, nil
}
// Model returns the model entity.
func (st *State) Model() (*Model, error) {
models, closer := st.getCollection(modelsC)
defer closer()
env := &Model{st: st}
uuid := st.modelTag.Id()
if err := env.refresh(models.FindId(uuid)); err != nil {
return nil, errors.Trace(err)
}
return env, nil
}
// GetModel looks for the model identified by the uuid passed in.
func (st *State) GetModel(tag names.ModelTag) (*Model, error) {
models, closer := st.getCollection(modelsC)
defer closer()
env := &Model{st: st}
if err := env.refresh(models.FindId(tag.Id())); err != nil {
return nil, errors.Trace(err)
}
return env, nil
}
// AllModels returns all the models in the system.
func (st *State) AllModels() ([]*Model, error) {
models, closer := st.getCollection(modelsC)
defer closer()
var envDocs []modelDoc
err := models.Find(nil).All(&envDocs)
if err != nil {
return nil, err
}
result := make([]*Model, len(envDocs))
for i, doc := range envDocs {
result[i] = &Model{st: st, doc: doc}
}
return result, nil
}
// ModelArgs is a params struct for creating a new model.
type ModelArgs struct {
Config *config.Config
Owner names.UserTag
MigrationMode MigrationMode
}
// NewModel creates a new model with its own UUID and
// prepares it for use. Model and State instances for the new
// model are returned.
//
// The controller model's UUID is attached to the new
// model's document. Having the server UUIDs stored with each
// model document means that we have a way to represent external
// models, perhaps for future use around cross model
// relations.
func (st *State) NewModel(args ModelArgs) (_ *Model, _ *State, err error) {
owner := args.Owner
if owner.IsLocal() {
if _, err := st.User(owner); err != nil {
return nil, nil, errors.Annotate(err, "cannot create model")
}
}
ssEnv, err := st.ControllerModel()
if err != nil {
return nil, nil, errors.Annotate(err, "could not load controller model")
}
uuid := args.Config.UUID()
newState, err := st.ForModel(names.NewModelTag(uuid))
if err != nil {
return nil, nil, errors.Annotate(err, "could not create state for new model")
}
defer func() {
if err != nil {
newState.Close()
}
}()
ops, err := newState.modelSetupOps(args.Config, uuid, ssEnv.UUID(), owner, args.MigrationMode)
if err != nil {
return nil, nil, errors.Annotate(err, "failed to create new model")
}
err = newState.runTransaction(ops)
if err == txn.ErrAborted {
// We have a unique key restriction on the "owner" and "name" fields,
// which will cause the insert to fail if there is another record with
// the same "owner" and "name" in the collection. If the txn is
// aborted, check if it is due to the unique key restriction.
name := args.Config.Name()
models, closer := st.getCollection(modelsC)
defer closer()
envCount, countErr := models.Find(bson.D{
{"owner", owner.Canonical()},
{"name", name}},
).Count()
if countErr != nil {
err = errors.Trace(countErr)
} else if envCount > 0 {
err = errors.AlreadyExistsf("model %q for %s", name, owner.Canonical())
} else {
err = errors.New("model already exists")
}
}
if err != nil {
return nil, nil, errors.Trace(err)
}
newEnv, err := newState.Model()
if err != nil {
return nil, nil, errors.Trace(err)
}
return newEnv, newState, nil
}
// Tag returns a name identifying the model.
// The returned name will be different from other Tag values returned
// by any other entities from the same state.
func (m *Model) Tag() names.Tag {
return m.ModelTag()
}
// ModelTag is the concrete model tag for this model.
func (m *Model) ModelTag() names.ModelTag {
return names.NewModelTag(m.doc.UUID)
}
// ControllerTag is the model tag for the controller that the model is
// running within.
func (m *Model) ControllerTag() names.ModelTag {
return names.NewModelTag(m.doc.ServerUUID)
}
// UUID returns the universally unique identifier of the model.
func (m *Model) UUID() string {
return m.doc.UUID
}
// ControllerUUID returns the universally unique identifier of the controller
// in which the model is running.
func (m *Model) ControllerUUID() string {
return m.doc.ServerUUID
}
// Name returns the human friendly name of the model.
func (m *Model) Name() string {
return m.doc.Name
}
// MigrationMode returns whether the model is active or being migrated.
func (m *Model) MigrationMode() MigrationMode {
return m.doc.MigrationMode
}
// SetMigrationMode updates the migration mode of the model.
func (m *Model) SetMigrationMode(mode MigrationMode) error {
st, closeState, err := m.getState()
if err != nil {
return errors.Trace(err)
}
defer closeState()
ops := []txn.Op{{
C: modelsC,
Id: m.doc.UUID,
Assert: txn.DocExists,
Update: bson.D{{"$set", bson.D{{"migration-mode", mode}}}},
}}
if err := st.runTransaction(ops); err != nil {
return errors.Trace(err)
}
return m.Refresh()
}
// Life returns whether the model is Alive, Dying or Dead.
func (m *Model) Life() Life {
return m.doc.Life
}
// Owner returns tag representing the owner of the model.
// The owner is the user that created the model.
func (m *Model) Owner() names.UserTag {
return names.NewUserTag(m.doc.Owner)
}
// Status returns the status of the model.
func (m *Model) Status() (status.StatusInfo, error) {
st, closeState, err := m.getState()
if err != nil {
return status.StatusInfo{}, errors.Trace(err)
}
defer closeState()
status, err := getStatus(st, m.globalKey(), "model")
if err != nil {
return status, err
}
return status, nil
}
// SetStatus sets the status of the model.
func (m *Model) SetStatus(modelStatus status.Status, info string, data map[string]interface{}) error {
st, closeState, err := m.getState()
if err != nil {
return errors.Trace(err)
}
defer closeState()
if !status.ValidModelStatus(modelStatus) {
return errors.Errorf("cannot set invalid status %q", modelStatus)
}
return setStatus(st, setStatusParams{
badge: "model",
globalKey: m.globalKey(),
status: modelStatus,
message: info,
rawData: data,
})
}
// Config returns the config for the model.
func (m *Model) Config() (*config.Config, error) {
st, closeState, err := m.getState()
if err != nil {
return nil, errors.Trace(err)
}
defer closeState()
return st.ModelConfig()
}
// UpdateLatestToolsVersion looks up for the latest available version of
// juju tools and updates environementDoc with it.
func (m *Model) UpdateLatestToolsVersion(ver version.Number) error {
v := ver.String()
// TODO(perrito666): I need to assert here that there isn't a newer
// version in place.
ops := []txn.Op{{
C: modelsC,
Id: m.doc.UUID,
Update: bson.D{{"$set", bson.D{{"available-tools", v}}}},
}}
err := m.st.runTransaction(ops)
if err != nil {
return errors.Trace(err)
}
return m.Refresh()
}
// LatestToolsVersion returns the newest version found in the last
// check in the streams.
// Bear in mind that the check was performed filtering only
// new patches for the current major.minor. (major.minor.patch)
func (m *Model) LatestToolsVersion() version.Number {
ver := m.doc.LatestAvailableTools
if ver == "" {
return version.Zero
}
v, err := version.Parse(ver)
if err != nil {
// This is being stored from a valid version but
// in case this data would beacame corrupt It is not
// worth to fail because of it.
return version.Zero
}
return v
}
// globalKey returns the global database key for the model.
func (m *Model) globalKey() string {
return modelGlobalKey
}
func (m *Model) Refresh() error {
models, closer := m.st.getCollection(modelsC)
defer closer()
return m.refresh(models.FindId(m.UUID()))
}
func (m *Model) refresh(query mongo.Query) error {
err := query.One(&m.doc)
if err == mgo.ErrNotFound {
return errors.NotFoundf("model")
}
return err
}
// Users returns a slice of all users for this model.
func (m *Model) Users() ([]*ModelUser, error) {
if m.st.ModelUUID() != m.UUID() {
return nil, errors.New("cannot lookup model users outside the current model")
}
coll, closer := m.st.getCollection(modelUsersC)
defer closer()
var userDocs []modelUserDoc
err := coll.Find(nil).All(&userDocs)
if err != nil {
return nil, errors.Trace(err)
}
var modelUsers []*ModelUser
for _, doc := range userDocs {
modelUsers = append(modelUsers, &ModelUser{
st: m.st,
doc: doc,
})
}
return modelUsers, nil
}
// Destroy sets the models's lifecycle to Dying, preventing
// addition of services or machines to state. If called on
// an empty hosted model, the lifecycle will be advanced
// straight to Dead.
//
// If called on a controller model, and that controller is
// hosting any non-Dead models, this method will return an
// error satisfying IsHasHostedsError.
func (m *Model) Destroy() (err error) {
ensureNoHostedModels := false
if m.doc.UUID == m.doc.ServerUUID {
ensureNoHostedModels = true
}
return m.destroy(ensureNoHostedModels)
}
// DestroyIncludingHosted sets the model's lifecycle to Dying, preventing
// addition of services or machines to state. If this model is a controller
// hosting other models, they will also be destroyed.
func (m *Model) DestroyIncludingHosted() error {
ensureNoHostedModels := false
return m.destroy(ensureNoHostedModels)
}
func (m *Model) destroy(ensureNoHostedModels bool) (err error) {
defer errors.DeferredAnnotatef(&err, "failed to destroy model")
st, closeState, err := m.getState()
if err != nil {
return errors.Trace(err)
}
defer closeState()
buildTxn := func(attempt int) ([]txn.Op, error) {
// On the first attempt, we assume memory state is recent
// enough to try using...
if attempt != 0 {
// ...but on subsequent attempts, we read fresh environ
// state from the DB. Note that we do *not* refresh `e`
// itself, as detailed in doc/hacking-state.txt
if m, err = st.Model(); err != nil {
return nil, errors.Trace(err)
}
}
ops, err := m.destroyOps(ensureNoHostedModels, false)
if err == errModelNotAlive {
return nil, jujutxn.ErrNoOperations
} else if err != nil {
return nil, errors.Trace(err)
}
return ops, nil
}
return st.run(buildTxn)
}
// errModelNotAlive is a signal emitted from destroyOps to indicate
// that model destruction is already underway.
var errModelNotAlive = errors.New("model is no longer alive")
type hasHostedModelsError int
func (e hasHostedModelsError) Error() string {
return fmt.Sprintf("hosting %d other models", e)
}
func IsHasHostedModelsError(err error) bool {
_, ok := errors.Cause(err).(hasHostedModelsError)
return ok
}
// destroyOps returns the txn operations necessary to begin model
// destruction, or an error indicating why it can't.
//
// If ensureNoHostedModels is true, then destroyOps will
// fail if there are any non-Dead hosted models
func (m *Model) destroyOps(ensureNoHostedModels, ensureEmpty bool) ([]txn.Op, error) {
if m.Life() != Alive {
return nil, errModelNotAlive
}
// Ensure we're using the model's state.
st, closeState, err := m.getState()
if err != nil {
return nil, errors.Trace(err)
}
defer closeState()
if err := ensureDestroyable(st); err != nil {
return nil, errors.Trace(err)
}
// Check if the model is empty. If it is, we can advance the model's
// lifecycle state directly to Dead.
var prereqOps []txn.Op
checkEmptyErr := m.checkEmpty()
isEmpty := checkEmptyErr == nil
uuid := m.UUID()
if ensureEmpty && !isEmpty {
return nil, errors.Trace(checkEmptyErr)
}
if isEmpty {
prereqOps = append(prereqOps, txn.Op{
C: modelEntityRefsC,
Id: uuid,
Assert: bson.D{
{"machines", bson.D{{"$size", 0}}},
{"services", bson.D{{"$size", 0}}},
},
})
}
if ensureNoHostedModels {
// Check for any Dying or alive but non-empty models. If there
// are any, we return an error indicating that there are hosted
// models.
models, err := st.AllModels()
if err != nil {
return nil, errors.Trace(err)
}
var aliveEmpty, aliveNonEmpty, dying, dead int
for _, model := range models {
if model.UUID() == m.UUID() {
// Ignore the controller model.
continue
}
if model.Life() == Dead {
// Dead hosted models don't affect
// whether the controller can be
// destroyed or not, but they are
// still counted in the hosted models.
dead++
continue
}
// See if the model is empty, and if it is,
// get the ops required to destroy it.
ops, err := model.destroyOps(false, true)
switch err {
case errModelNotAlive:
dying++
case nil:
prereqOps = append(prereqOps, ops...)
aliveEmpty++
default:
aliveNonEmpty++
}
}
if dying > 0 || aliveNonEmpty > 0 {
// There are Dying, or Alive but non-empty models.
// We cannot destroy the controller without first
// destroying the models and waiting for them to
// become Dead.
return nil, errors.Trace(
hasHostedModelsError(dying + aliveNonEmpty + aliveEmpty),
)
}
// Ensure that the number of active models has not changed
// between the query and when the transaction is applied.
//
// Note that we assert that each empty model that we intend
// move to Dead is still Alive, so we're protected from an
// ABA style problem where an empty model is concurrently
// removed, and replaced with a non-empty model.
prereqOps = append(prereqOps, assertHostedModelsOp(aliveEmpty+dead))
}
life := Dying
if isEmpty && uuid != m.doc.ServerUUID {
// The model is empty, and is not the admin
// model, so we can move it straight to Dead.
life = Dead
}
timeOfDying := nowToTheSecond()
modelUpdateValues := bson.D{
{"life", life},
{"time-of-dying", timeOfDying},
}
if life == Dead {
modelUpdateValues = append(modelUpdateValues, bson.DocElem{
"time-of-death", timeOfDying,
})
}
ops := []txn.Op{{
C: modelsC,
Id: uuid,
Assert: isAliveDoc,
Update: bson.D{{"$set", modelUpdateValues}},
}}
// Because txn operations execute in order, and may encounter
// arbitrarily long delays, we need to make sure every op
// causes a state change that's still consistent; so we make
// sure the cleanup ops are the last thing that will execute.
if uuid == m.doc.ServerUUID {
cleanupOp := st.newCleanupOp(cleanupModelsForDyingController, uuid)
ops = append(ops, cleanupOp)
}
if !isEmpty {
// We only need to destroy machines and models if the model is
// non-empty. It wouldn't normally be harmful to enqueue the
// cleanups otherwise, except for when we're destroying an empty
// hosted model in the course of destroying the controller. In
// that case we'll get errors if we try to enqueue hosted-model
// cleanups, because the cleanups collection is non-global.
cleanupMachinesOp := st.newCleanupOp(cleanupMachinesForDyingModel, uuid)
ops = append(ops, cleanupMachinesOp)
cleanupServicesOp := st.newCleanupOp(cleanupServicesForDyingModel, uuid)
ops = append(ops, cleanupServicesOp)
}
return append(prereqOps, ops...), nil
}
// checkEmpty checks that the machine is empty of any entities that may
// require external resource cleanup. If the machine is not empty, then
// an error will be returned.
func (m *Model) checkEmpty() error {
st, closeState, err := m.getState()
if err != nil {
return errors.Trace(err)
}
defer closeState()
modelEntityRefs, closer := st.getCollection(modelEntityRefsC)
defer closer()
var doc modelEntityRefsDoc
if err := modelEntityRefs.FindId(m.UUID()).One(&doc); err != nil {
if err == mgo.ErrNotFound {
return errors.NotFoundf("entity references doc for model %s", m.UUID())
}
return errors.Annotatef(err, "getting entity references for model %s", m.UUID())
}
if n := len(doc.Machines); n > 0 {
return errors.Errorf("model not empty, found %d machine(s)", n)
}
if n := len(doc.Services); n > 0 {
return errors.Errorf("model not empty, found %d services(s)", n)
}
return nil
}
func addModelMachineRefOp(st *State, machineId string) txn.Op {
return txn.Op{
C: modelEntityRefsC,
Id: st.ModelUUID(),
Assert: txn.DocExists,
Update: bson.D{{"$addToSet", bson.D{{"machines", machineId}}}},
}
}
func removeModelMachineRefOp(st *State, machineId string) txn.Op {
return txn.Op{
C: modelEntityRefsC,
Id: st.ModelUUID(),
Update: bson.D{{"$pull", bson.D{{"machines", machineId}}}},
}
}
func addModelServiceRefOp(st *State, serviceName string) txn.Op {
return txn.Op{
C: modelEntityRefsC,
Id: st.ModelUUID(),
Assert: txn.DocExists,
Update: bson.D{{"$addToSet", bson.D{{"services", serviceName}}}},
}
}
func removeModelServiceRefOp(st *State, serviceName string) txn.Op {
return txn.Op{
C: modelEntityRefsC,
Id: st.ModelUUID(),
Update: bson.D{{"$pull", bson.D{{"services", serviceName}}}},
}
}
// checkManualMachines checks if any of the machines in the slice were
// manually provisioned, and are non-manager machines. These machines
// must (currently) be manually destroyed via destroy-machine before
// destroy-model can successfully complete.
func checkManualMachines(machines []*Machine) error {
var ids []string
for _, m := range machines {
if m.IsManager() {
continue
}
manual, err := m.IsManual()
if err != nil {
return errors.Trace(err)
}
if manual {
ids = append(ids, m.Id())
}
}
if len(ids) > 0 {
return errors.Errorf("manually provisioned machines must first be destroyed with `juju destroy-machine %s`", strings.Join(ids, " "))
}
return nil
}
// ensureDestroyable an error if any manual machines or persistent volumes are
// found.
func ensureDestroyable(st *State) error {
// TODO(waigani) bug #1475212: Model destroy can miss manual
// machines. We need to be able to assert the absence of these as
// part of the destroy txn, but in order to do this manual machines
// need to add refcounts to their models.
// Check for manual machines. We bail out if there are any,
// to stop the user from prematurely hobbling the model.
machines, err := st.AllMachines()
if err != nil {
return errors.Trace(err)
}
if err := checkManualMachines(machines); err != nil {
return errors.Trace(err)
}
return nil
}
// createModelOp returns the operation needed to create
// an model document with the given name and UUID.
func createModelOp(st *State, owner names.UserTag, name, uuid, server string, mode MigrationMode) txn.Op {
doc := &modelDoc{
UUID: uuid,
Name: name,
Life: Alive,
Owner: owner.Canonical(),
ServerUUID: server,
MigrationMode: mode,
}
return txn.Op{
C: modelsC,
Id: uuid,
Assert: txn.DocMissing,
Insert: doc,
}
}
func createModelEntityRefsOp(st *State, uuid string) txn.Op {
return txn.Op{
C: modelEntityRefsC,
Id: uuid,
Assert: txn.DocMissing,
Insert: &modelEntityRefsDoc{UUID: uuid},
}
}
const hostedModelCountKey = "hostedModelCount"
type hostedModelCountDoc struct {
// RefCount is the number of models in the Juju system.
// We do not count the system model.
RefCount int `bson:"refcount"`
}
func assertNoHostedModelsOp() txn.Op {
return assertHostedModelsOp(0)
}
func assertHostedModelsOp(n int) txn.Op {
return txn.Op{
C: controllersC,
Id: hostedModelCountKey,
Assert: bson.D{{"refcount", n}},
}
}
func incHostedModelCountOp() txn.Op {
return HostedModelCountOp(1)
}
func decHostedModelCountOp() txn.Op {
return HostedModelCountOp(-1)
}
func HostedModelCountOp(amount int) txn.Op {
return txn.Op{
C: controllersC,
Id: hostedModelCountKey,
Assert: txn.DocExists,
Update: bson.M{
"$inc": bson.M{"refcount": amount},
},
}
}
func hostedModelCount(st *State) (int, error) {
var doc hostedModelCountDoc
controllers, closer := st.getCollection(controllersC)
defer closer()
if err := controllers.Find(bson.D{{"_id", hostedModelCountKey}}).One(&doc); err != nil {
return 0, errors.Trace(err)
}
return doc.RefCount, nil
}
// createUniqueOwnerModelNameOp returns the operation needed to create
// an usermodelnameC document with the given owner and model name.
func createUniqueOwnerModelNameOp(owner names.UserTag, envName string) txn.Op {
return txn.Op{
C: usermodelnameC,
Id: userModelNameIndex(owner.Canonical(), envName),
Assert: txn.DocMissing,
Insert: bson.M{},
}
}
// assertAliveOp returns a txn.Op that asserts the model is alive.
func (m *Model) assertActiveOp() txn.Op {
return assertModelActiveOp(m.UUID())
}
// getState returns the State for the model, and a function to close
// it when done. If m.st is the model-specific state, then it will
// be returned and the close function will be a no-op.
//
// TODO(axw) 2016-04-14 #1570269
// find a way to guarantee that every Model is associated with the
// appropriate State. The current work-around is too easy to get wrong.
func (m *Model) getState() (*State, func(), error) {
if m.st.modelTag == m.ModelTag() {
return m.st, func() {}, nil
}
st, err := m.st.ForModel(m.ModelTag())
if err != nil {
return nil, nil, errors.Trace(err)
}
uuid := st.ModelUUID()
return st, func() {
if err := st.Close(); err != nil {
logger.Errorf("closing temporary state for model %s", uuid)
}
}, nil
}
// assertModelActiveOp returns a txn.Op that asserts the given
// model UUID refers to an Alive model.
func assertModelActiveOp(modelUUID string) txn.Op {
return txn.Op{
C: modelsC,
Id: modelUUID,
Assert: append(isAliveDoc, bson.DocElem{"migration-mode", MigrationModeActive}),
}
}
func checkModelActive(st *State) error {
model, err := st.Model()
if (err == nil && model.Life() != Alive) || errors.IsNotFound(err) {
return errors.Errorf("model %q is no longer alive", model.Name())
} else if err != nil {
return errors.Annotate(err, "unable to read model")
} else if mode := model.MigrationMode(); mode != MigrationModeActive {
return errors.Errorf("model %q is being migrated", model.Name())
}
return nil
}