/
undertaker.go
59 lines (50 loc) · 1.34 KB
/
undertaker.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state
import (
"github.com/juju/errors"
"gopkg.in/mgo.v2/bson"
"gopkg.in/mgo.v2/txn"
)
var ErrModelNotDying = errors.New("model is not dying")
// ProcessDyingModel checks if there are any machines or services left in
// state. If there are none, the model's life is changed from dying to dead.
func (st *State) ProcessDyingModel() (err error) {
buildTxn := func(attempt int) ([]txn.Op, error) {
model, err := st.Model()
if err != nil {
return nil, errors.Trace(err)
}
if model.Life() != Dying {
return nil, errors.Trace(ErrModelNotDying)
}
if st.IsController() {
models, err := st.AllModels()
if err != nil {
return nil, errors.Trace(err)
}
for _, model := range models {
if model.UUID() != st.ModelUUID() && model.Life() != Dead {
return nil, errors.Errorf("one or more hosted models are not yet dead")
}
}
}
if err := model.checkEmpty(); err != nil {
return nil, errors.Trace(err)
}
ops := []txn.Op{{
C: modelsC,
Id: st.ModelUUID(),
Assert: isDyingDoc,
Update: bson.M{"$set": bson.M{
"life": Dead,
"time-of-death": st.NowToTheSecond(),
}},
}}
return ops, nil
}
if err = st.run(buildTxn); err != nil {
return errors.Trace(err)
}
return nil
}