1
// Copyright 2014 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
8
"github.com/juju/errors"
9
"launchpad.net/gnuflag"
11
"github.com/juju/juju/cmd/modelcmd"
14
// BaseBlockCommand is base command for all
15
// commands that enable blocks.
16
type BaseBlockCommand struct {
17
modelcmd.ModelCommandBase
21
// Init initializes the command.
22
// Satisfying Command interface.
23
func (c *BaseBlockCommand) Init(args []string) error {
25
return errors.Trace(errors.New("can only specify block message"))
34
// internalRun blocks commands from running successfully.
35
func (c *BaseBlockCommand) internalRun(operation string) error {
36
client, err := getBlockClientAPI(c)
38
return errors.Trace(err)
42
return client.SwitchBlockOn(TypeFromOperation(operation), c.desc)
45
// SetFlags implements Command.SetFlags.
46
func (c *BaseBlockCommand) SetFlags(f *gnuflag.FlagSet) {
47
c.ModelCommandBase.SetFlags(f)
50
// BlockClientAPI defines the client API methods that block command uses.
51
type BlockClientAPI interface {
53
SwitchBlockOn(blockType, msg string) error
56
var getBlockClientAPI = func(p *BaseBlockCommand) (BlockClientAPI, error) {
57
return getBlockAPI(&p.ModelCommandBase)
60
func newDestroyCommand() cmd.Command {
61
return modelcmd.Wrap(&destroyCommand{})
64
// destroyCommand blocks destroy environment.
65
type destroyCommand struct {
69
var destroyBlockDoc = `
70
This command allows to block model destruction.
72
To disable the block, run unblock command - see "juju help unblock".
73
To by-pass the block, run destroy-model with --force option.
75
"juju block destroy-model" only blocks destroy-model command.
78
# To prevent the model from being destroyed:
79
juju block destroy-model
83
// Info provides information about command.
84
// Satisfying Command interface.
85
func (c *destroyCommand) Info() *cmd.Info {
87
Name: "destroy-model",
88
Purpose: "Block an operation that would destroy Juju model.",
93
// Satisfying Command interface.
94
func (c *destroyCommand) Run(_ *cmd.Context) error {
95
return c.internalRun(c.Info().Name)
98
func newRemoveCommand() cmd.Command {
99
return modelcmd.Wrap(&removeCommand{})
102
// removeCommand blocks commands that remove juju objects.
103
type removeCommand struct {
107
var removeBlockDoc = `
108
This command allows to block all operations that would remove an object
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.
114
"juju block remove-object" blocks these commands:
122
# To prevent the machines, applications, units and relations from being removed:
123
juju block remove-object
127
// Info provides information about command.
128
// Satisfying Command interface.
129
func (c *removeCommand) Info() *cmd.Info {
131
Name: "remove-object",
132
Purpose: "Block an operation that would remove an object.",
137
// Satisfying Command interface.
138
func (c *removeCommand) Run(_ *cmd.Context) error {
139
return c.internalRun(c.Info().Name)
142
func newChangeCommand() cmd.Command {
143
return modelcmd.Wrap(&changeCommand{})
146
// changeCommand blocks commands that may change environment.
147
type changeCommand struct {
151
var changeBlockDoc = `
152
This command allows to block all operations that would alter
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.
158
"juju block all-changes" blocks these commands:
163
authorised-keys delete
164
authorised-keys import
191
# To prevent changes to the model:
192
juju block all-changes
196
// Info provides information about command.
197
// Satisfying Command interface.
198
func (c *changeCommand) Info() *cmd.Info {
201
Purpose: "Block operations that could change Juju model.",
206
// Satisfying Command interface.
207
func (c *changeCommand) Run(_ *cmd.Context) error {
208
return c.internalRun(c.Info().Name)