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