forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.go
330 lines (283 loc) · 8.28 KB
/
block.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state
import (
"fmt"
"github.com/juju/errors"
"gopkg.in/juju/names.v2"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"gopkg.in/mgo.v2/txn"
"github.com/juju/juju/state/multiwatcher"
)
// Customers and stakeholders want to be able to prevent accidental damage to their Juju deployments.
// To prevent running some operations, we want to have blocks that can be switched on/off.
type Block interface {
// Id returns this block's id.
Id() string
// ModelUUID returns the model UUID associated with this block.
ModelUUID() string
// Tag returns tag for the entity that is being blocked
Tag() (names.Tag, error)
// Type returns block type
Type() BlockType
// Message returns explanation that accompanies this block.
Message() string
updateMessageOp(string) ([]txn.Op, error)
}
// BlockType specifies block type for enum benefit.
type BlockType int8
const (
// DestroyBlock type identifies block that prevents model destruction.
DestroyBlock BlockType = iota
// RemoveBlock type identifies block that prevents
// removal of machines, applications, units or relations.
RemoveBlock
// ChangeBlock type identifies block that prevents model changes such
// as additions, modifications, removals of model entities.
ChangeBlock
)
var (
typeNames = map[BlockType]multiwatcher.BlockType{
DestroyBlock: multiwatcher.BlockDestroy,
RemoveBlock: multiwatcher.BlockRemove,
ChangeBlock: multiwatcher.BlockChange,
}
blockMigrationValue = map[BlockType]string{
DestroyBlock: "destroy-model",
RemoveBlock: "remove-object",
ChangeBlock: "all-changes",
}
)
// AllTypes returns all supported block types.
func AllTypes() []BlockType {
return []BlockType{
DestroyBlock,
RemoveBlock,
ChangeBlock,
}
}
// ToParams returns the type as multiwatcher.BlockType.
func (t BlockType) ToParams() multiwatcher.BlockType {
if jujuBlock, ok := typeNames[t]; ok {
return jujuBlock
}
panic(fmt.Sprintf("unknown block type %d", int(t)))
}
// String returns humanly readable type representation.
func (t BlockType) String() string {
return string(t.ToParams())
}
// MigrationValue converts the block type value into a useful human readable
// string for model migration.
func (t BlockType) MigrationValue() string {
if value, ok := blockMigrationValue[t]; ok {
return value
}
return "unknown"
}
// ParseBlockType returns BlockType from humanly readable type representation.
func ParseBlockType(str string) BlockType {
for _, one := range AllTypes() {
if one.String() == str {
return one
}
}
panic(fmt.Sprintf("unknown block type %v", str))
}
type block struct {
doc blockDoc
}
// blockDoc records information about an model block.
type blockDoc struct {
DocID string `bson:"_id"`
ModelUUID string `bson:"model-uuid"`
Tag string `bson:"tag"`
Type BlockType `bson:"type"`
Message string `bson:"message,omitempty"`
}
func (b *block) updateMessageOp(message string) ([]txn.Op, error) {
return []txn.Op{{
C: blocksC,
Id: b.doc.DocID,
Assert: txn.DocExists,
Update: bson.D{{"$set", bson.D{{"message", message}}}},
}}, nil
}
// Id is part of the state.Block interface.
func (b *block) Id() string {
return b.doc.DocID
}
// ModelUUID is part of the state.Block interface.
func (b *block) ModelUUID() string {
return b.doc.ModelUUID
}
// Message is part of the state.Block interface.
func (b *block) Message() string {
return b.doc.Message
}
// Tag is part of the state.Block interface.
func (b *block) Tag() (names.Tag, error) {
tag, err := names.ParseTag(b.doc.Tag)
if err != nil {
return nil, errors.Annotatef(err, "getting block information")
}
return tag, nil
}
// Type is part of the state.Block interface.
func (b *block) Type() BlockType {
return b.doc.Type
}
// SwitchBlockOn enables block of specified type for the
// current model.
func (st *State) SwitchBlockOn(t BlockType, msg string) error {
return setModelBlock(st, t, msg)
}
// SwitchBlockOff disables block of specified type for the
// current model.
func (st *State) SwitchBlockOff(t BlockType) error {
return RemoveModelBlock(st, t)
}
// GetBlockForType returns the Block of the specified type for the current model
// where
// not found -> nil, false, nil
// found -> block, true, nil
// error -> nil, false, err
func (st *State) GetBlockForType(t BlockType) (Block, bool, error) {
return getBlockForType(st, t)
}
func getBlockForType(mb modelBackend, t BlockType) (Block, bool, error) {
all, closer := mb.db().GetCollection(blocksC)
defer closer()
doc := blockDoc{}
err := all.Find(bson.D{{"type", t}}).One(&doc)
switch err {
case nil:
return &block{doc}, true, nil
case mgo.ErrNotFound:
return nil, false, nil
default:
return nil, false, errors.Annotatef(err, "cannot get block of type %v", t.String())
}
}
// AllBlocks returns all blocks in the model.
func (st *State) AllBlocks() ([]Block, error) {
blocksCollection, closer := st.db().GetCollection(blocksC)
defer closer()
var bdocs []blockDoc
err := blocksCollection.Find(nil).All(&bdocs)
if err != nil {
return nil, errors.Annotatef(err, "cannot get all blocks")
}
blocks := make([]Block, len(bdocs))
for i, doc := range bdocs {
blocks[i] = &block{doc}
}
return blocks, nil
}
// AllBlocksForController returns all blocks in any models on
// the controller.
func (st *State) AllBlocksForController() ([]Block, error) {
blocksCollection, closer := st.db().GetRawCollection(blocksC)
defer closer()
var bdocs []blockDoc
err := blocksCollection.Find(nil).All(&bdocs)
if err != nil {
return nil, errors.Annotate(err, "cannot get all blocks")
}
blocks := make([]Block, len(bdocs))
for i, doc := range bdocs {
blocks[i] = &block{doc}
}
return blocks, nil
}
// RemoveAllBlocksForController removes all the blocks for the controller.
// It does not prevent new blocks from being added during / after
// removal.
func (st *State) RemoveAllBlocksForController() error {
blocks, err := st.AllBlocksForController()
if err != nil {
return errors.Trace(err)
}
ops := []txn.Op{}
for _, blk := range blocks {
ops = append(ops, txn.Op{
C: blocksC,
Id: blk.Id(),
Remove: true,
})
}
// Use runRawTransaction as we might be removing docs across
// multiple models.
return st.runRawTransaction(ops)
}
// setModelBlock updates the blocks collection with the
// specified block.
// Only one instance of each block type can exist in model.
func setModelBlock(mb modelBackend, t BlockType, msg string) error {
buildTxn := func(attempt int) ([]txn.Op, error) {
block, exists, err := getBlockForType(mb, t)
if err != nil {
return nil, errors.Trace(err)
}
// Cannot create blocks of the same type more than once per model.
// Cannot update current blocks.
if exists {
return block.updateMessageOp(msg)
}
return createModelBlockOps(mb, t, msg)
}
return mb.db().Run(buildTxn)
}
// newBlockId returns a sequential block id for this model.
func newBlockId(mb modelBackend) (string, error) {
seq, err := sequence(mb, "block")
if err != nil {
return "", errors.Trace(err)
}
return fmt.Sprint(seq), nil
}
func createModelBlockOps(mb modelBackend, t BlockType, msg string) ([]txn.Op, error) {
id, err := newBlockId(mb)
if err != nil {
return nil, errors.Annotatef(err, "getting new block id")
}
// NOTE: if at any time in the future, we change blocks so that the
// Tag is different from the model, then the migration of blocks will
// need to change format.
newDoc := blockDoc{
DocID: mb.docID(id),
ModelUUID: mb.modelUUID(),
Tag: names.NewModelTag(mb.modelUUID()).String(),
Type: t,
Message: msg,
}
insertOp := txn.Op{
C: blocksC,
Id: newDoc.DocID,
Assert: txn.DocMissing,
Insert: &newDoc,
}
return []txn.Op{insertOp}, nil
}
func RemoveModelBlock(st *State, t BlockType) error {
buildTxn := func(attempt int) ([]txn.Op, error) {
return RemoveModelBlockOps(st, t)
}
return st.db().Run(buildTxn)
}
func RemoveModelBlockOps(st *State, t BlockType) ([]txn.Op, error) {
tBlock, exists, err := st.GetBlockForType(t)
if err != nil {
return nil, errors.Annotatef(err, "removing block %v", t.String())
}
if exists {
return []txn.Op{{
C: blocksC,
Id: tBlock.Id(),
Remove: true,
}}, nil
}
// If the block doesn't exist, we're all good.
return nil, nil
}