~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/cmd/juju/block/block.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2014 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package block
 
5
 
 
6
import (
 
7
        "github.com/juju/cmd"
 
8
        "github.com/juju/errors"
 
9
        "launchpad.net/gnuflag"
 
10
 
 
11
        "github.com/juju/juju/cmd/modelcmd"
 
12
)
 
13
 
 
14
// BaseBlockCommand is base command for all
 
15
// commands that enable blocks.
 
16
type BaseBlockCommand struct {
 
17
        modelcmd.ModelCommandBase
 
18
        desc string
 
19
}
 
20
 
 
21
// Init initializes the command.
 
22
// Satisfying Command interface.
 
23
func (c *BaseBlockCommand) Init(args []string) error {
 
24
        if len(args) > 1 {
 
25
                return errors.Trace(errors.New("can only specify block message"))
 
26
        }
 
27
 
 
28
        if len(args) == 1 {
 
29
                c.desc = args[0]
 
30
        }
 
31
        return nil
 
32
}
 
33
 
 
34
// internalRun blocks commands from running successfully.
 
35
func (c *BaseBlockCommand) internalRun(operation string) error {
 
36
        client, err := getBlockClientAPI(c)
 
37
        if err != nil {
 
38
                return errors.Trace(err)
 
39
        }
 
40
        defer client.Close()
 
41
 
 
42
        return client.SwitchBlockOn(TypeFromOperation(operation), c.desc)
 
43
}
 
44
 
 
45
// SetFlags implements Command.SetFlags.
 
46
func (c *BaseBlockCommand) SetFlags(f *gnuflag.FlagSet) {
 
47
        c.ModelCommandBase.SetFlags(f)
 
48
}
 
49
 
 
50
// BlockClientAPI defines the client API methods that block command uses.
 
51
type BlockClientAPI interface {
 
52
        Close() error
 
53
        SwitchBlockOn(blockType, msg string) error
 
54
}
 
55
 
 
56
var getBlockClientAPI = func(p *BaseBlockCommand) (BlockClientAPI, error) {
 
57
        return getBlockAPI(&p.ModelCommandBase)
 
58
}
 
59
 
 
60
func newDestroyCommand() cmd.Command {
 
61
        return modelcmd.Wrap(&destroyCommand{})
 
62
}
 
63
 
 
64
// destroyCommand blocks destroy environment.
 
65
type destroyCommand struct {
 
66
        BaseBlockCommand
 
67
}
 
68
 
 
69
var destroyBlockDoc = `
 
70
This command allows to block model destruction.
 
71
 
 
72
To disable the block, run unblock command - see "juju help unblock". 
 
73
To by-pass the block, run destroy-model with --force option.
 
74
 
 
75
"juju block destroy-model" only blocks destroy-model command.
 
76
   
 
77
Examples:
 
78
    # To prevent the model from being destroyed:
 
79
    juju block destroy-model
 
80
 
 
81
`
 
82
 
 
83
// Info provides information about command.
 
84
// Satisfying Command interface.
 
85
func (c *destroyCommand) Info() *cmd.Info {
 
86
        return &cmd.Info{
 
87
                Name:    "destroy-model",
 
88
                Purpose: "Block an operation that would destroy Juju model.",
 
89
                Doc:     destroyBlockDoc,
 
90
        }
 
91
}
 
92
 
 
93
// Satisfying Command interface.
 
94
func (c *destroyCommand) Run(_ *cmd.Context) error {
 
95
        return c.internalRun(c.Info().Name)
 
96
}
 
97
 
 
98
func newRemoveCommand() cmd.Command {
 
99
        return modelcmd.Wrap(&removeCommand{})
 
100
}
 
101
 
 
102
// removeCommand blocks commands that remove juju objects.
 
103
type removeCommand struct {
 
104
        BaseBlockCommand
 
105
}
 
106
 
 
107
var removeBlockDoc = `
 
108
This command allows to block all operations that would remove an object 
 
109
from Juju model.
 
110
 
 
111
To disable the block, run unblock command - see "juju help unblock". 
 
112
To by-pass the block, where available, run desired remove command with --force option.
 
113
 
 
114
"juju block remove-object" blocks these commands:
 
115
    destroy-model
 
116
    remove-machine
 
117
    remove-relation
 
118
    remove-application
 
119
    remove-unit
 
120
   
 
121
Examples:
 
122
    # To prevent the machines, applications, units and relations from being removed:
 
123
    juju block remove-object
 
124
 
 
125
`
 
126
 
 
127
// Info provides information about command.
 
128
// Satisfying Command interface.
 
129
func (c *removeCommand) Info() *cmd.Info {
 
130
        return &cmd.Info{
 
131
                Name:    "remove-object",
 
132
                Purpose: "Block an operation that would remove an object.",
 
133
                Doc:     removeBlockDoc,
 
134
        }
 
135
}
 
136
 
 
137
// Satisfying Command interface.
 
138
func (c *removeCommand) Run(_ *cmd.Context) error {
 
139
        return c.internalRun(c.Info().Name)
 
140
}
 
141
 
 
142
func newChangeCommand() cmd.Command {
 
143
        return modelcmd.Wrap(&changeCommand{})
 
144
}
 
145
 
 
146
// changeCommand blocks commands that may change environment.
 
147
type changeCommand struct {
 
148
        BaseBlockCommand
 
149
}
 
150
 
 
151
var changeBlockDoc = `
 
152
This command allows to block all operations that would alter
 
153
Juju model.
 
154
 
 
155
To disable the block, run unblock command - see "juju help unblock". 
 
156
To by-pass the block, where available, run desired remove command with --force option.
 
157
 
 
158
"juju block all-changes" blocks these commands:
 
159
    add-machine
 
160
    add-relation
 
161
    add-unit
 
162
    authorised-keys add
 
163
    authorised-keys delete
 
164
    authorised-keys import
 
165
    deploy
 
166
    destroy-model
 
167
    enable-ha
 
168
    expose
 
169
    remove-machine
 
170
    remove-relation
 
171
    remove-application
 
172
    remove-unit
 
173
    resolved
 
174
    retry-provisioning
 
175
    run
 
176
    set
 
177
    set-constraints
 
178
    set-model-config
 
179
    sync-tools
 
180
    unexpose
 
181
    unset
 
182
    unset-model-config
 
183
    upgrade-charm
 
184
    upgrade-juju
 
185
    add-user
 
186
    change-user-password
 
187
    disable-user
 
188
    enable-user
 
189
   
 
190
Examples:
 
191
    # To prevent changes to the model:
 
192
    juju block all-changes
 
193
 
 
194
`
 
195
 
 
196
// Info provides information about command.
 
197
// Satisfying Command interface.
 
198
func (c *changeCommand) Info() *cmd.Info {
 
199
        return &cmd.Info{
 
200
                Name:    "all-changes",
 
201
                Purpose: "Block operations that could change Juju model.",
 
202
                Doc:     changeBlockDoc,
 
203
        }
 
204
}
 
205
 
 
206
// Satisfying Command interface.
 
207
func (c *changeCommand) Run(_ *cmd.Context) error {
 
208
        return c.internalRun(c.Info().Name)
 
209
}