github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/cmd/juju/block/block.go (about) 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/envcmd" 12 ) 13 14 // BaseBlockCommand is base command for all 15 // commands that enable blocks. 16 type BaseBlockCommand struct { 17 envcmd.EnvCommandBase 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.EnvCommandBase.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.EnvCommandBase) 58 } 59 60 // DestroyCommand blocks destroy environment. 61 type DestroyCommand struct { 62 BaseBlockCommand 63 } 64 65 var destroyBlockDoc = ` 66 67 This command allows to block environment destruction. 68 69 To disable the block, run unblock command - see "juju help unblock". 70 To by-pass the block, run destroy-enviornment with --force option. 71 72 "juju block destroy-environment" only blocks destroy-environment command. 73 74 Examples: 75 To prevent the environment from being destroyed: 76 juju block destroy-environment 77 78 ` 79 80 // Info provides information about command. 81 // Satisfying Command interface. 82 func (c *DestroyCommand) Info() *cmd.Info { 83 return &cmd.Info{ 84 Name: "destroy-environment", 85 Purpose: "block an operation that would destroy Juju environment", 86 Doc: destroyBlockDoc, 87 } 88 } 89 90 // Satisfying Command interface. 91 func (c *DestroyCommand) Run(_ *cmd.Context) error { 92 return c.internalRun(c.Info().Name) 93 } 94 95 // RemoveCommand blocks commands that remove juju objects. 96 type RemoveCommand struct { 97 BaseBlockCommand 98 } 99 100 var removeBlockDoc = ` 101 102 This command allows to block all operations that would remove an object 103 from Juju environment. 104 105 To disable the block, run unblock command - see "juju help unblock". 106 To by-pass the block, where available, run desired remove command with --force option. 107 108 "juju block remove-object" blocks these commands: 109 destroy-environment 110 remove-machine 111 remove-relation 112 remove-service 113 remove-unit 114 115 Examples: 116 To prevent the machines, services, units and relations from being removed: 117 juju block remove-object 118 119 ` 120 121 // Info provides information about command. 122 // Satisfying Command interface. 123 func (c *RemoveCommand) Info() *cmd.Info { 124 return &cmd.Info{ 125 Name: "remove-object", 126 Purpose: "block an operation that would remove an object", 127 Doc: removeBlockDoc, 128 } 129 } 130 131 // Satisfying Command interface. 132 func (c *RemoveCommand) Run(_ *cmd.Context) error { 133 return c.internalRun(c.Info().Name) 134 } 135 136 // ChangeCommand blocks commands that may change environment. 137 type ChangeCommand struct { 138 BaseBlockCommand 139 } 140 141 var changeBlockDoc = ` 142 143 This command allows to block all operations that would alter 144 Juju environment. 145 146 To disable the block, run unblock command - see "juju help unblock". 147 To by-pass the block, where available, run desired remove command with --force option. 148 149 "juju block all-changes" blocks these commands: 150 add-machine 151 add-relation 152 add-unit 153 authorised-keys add 154 authorised-keys delete 155 authorised-keys import 156 deploy 157 destroy-environment 158 ensure-availability 159 expose 160 remove-machine 161 remove-relation 162 remove-service 163 remove-unit 164 resolved 165 retry-provisioning 166 run 167 set 168 set-constraints 169 set-env 170 sync-tools 171 unexpose 172 unset 173 unset-env 174 upgrade-charm 175 upgrade-juju 176 user add 177 user change-password 178 user disable 179 user enable 180 181 Examples: 182 To prevent changes to the environment: 183 juju block all-changes 184 185 ` 186 187 // Info provides information about command. 188 // Satisfying Command interface. 189 func (c *ChangeCommand) Info() *cmd.Info { 190 return &cmd.Info{ 191 Name: "all-changes", 192 Purpose: "block operations that could change Juju environment", 193 Doc: changeBlockDoc, 194 } 195 } 196 197 // Satisfying Command interface. 198 func (c *ChangeCommand) Run(_ *cmd.Context) error { 199 return c.internalRun(c.Info().Name) 200 }