github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/block/disablecommand.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  	"strings"
     8  
     9  	"github.com/juju/cmd"
    10  	"github.com/juju/errors"
    11  
    12  	"github.com/juju/juju/cmd/modelcmd"
    13  )
    14  
    15  // NewDisableCommand returns a disable-command command instance
    16  // that will use the default API.
    17  func NewDisableCommand() cmd.Command {
    18  	return modelcmd.Wrap(&disableCommand{
    19  		apiFunc: func(c newAPIRoot) (blockClientAPI, error) {
    20  			return getBlockAPI(c)
    21  		},
    22  	})
    23  }
    24  
    25  type disableCommand struct {
    26  	modelcmd.ModelCommandBase
    27  	apiFunc func(newAPIRoot) (blockClientAPI, error)
    28  	target  string
    29  	message string
    30  }
    31  
    32  // Init implements Command.
    33  func (c *disableCommand) Init(args []string) error {
    34  	if len(args) < 1 {
    35  		return errors.Errorf("missing command set (%s)", validTargets)
    36  	}
    37  	c.target, args = args[0], args[1:]
    38  	target, ok := toAPIValue[c.target]
    39  	if !ok {
    40  		return errors.Errorf("bad command set, valid options: %s", validTargets)
    41  	}
    42  	c.target = target
    43  	c.message = strings.Join(args, " ")
    44  	return nil
    45  }
    46  
    47  // Info implements Command.
    48  func (c *disableCommand) Info() *cmd.Info {
    49  	return &cmd.Info{
    50  		Name:    "disable-command",
    51  		Args:    "<command set> [message...]",
    52  		Purpose: "Disable commands for the model.",
    53  		Doc:     disableCommandDoc,
    54  	}
    55  }
    56  
    57  type blockClientAPI interface {
    58  	Close() error
    59  	SwitchBlockOn(blockType, msg string) error
    60  }
    61  
    62  // Run implements Command.Run
    63  func (c *disableCommand) Run(ctx *cmd.Context) error {
    64  	api, err := c.apiFunc(c)
    65  	if err != nil {
    66  		return errors.Annotate(err, "cannot connect to the API")
    67  	}
    68  	defer api.Close()
    69  
    70  	return api.SwitchBlockOn(c.target, c.message)
    71  }
    72  
    73  var disableCommandDoc = `
    74  Juju allows to safeguard deployed models from unintentional damage by preventing
    75  execution of operations that could alter model.
    76  
    77  This is done by disabling certain sets of commands from successful execution.
    78  Disabled commands must be manually enabled to proceed.
    79  
    80  Some commands offer a --force option that can be used to bypass the disabling.
    81  ` + commandSets + `
    82  Examples:
    83      # To prevent the model from being destroyed:
    84      juju disable-command destroy-model "Check with SA before destruction."
    85  
    86      # To prevent the machines, applications, units and relations from being removed:
    87      juju disable-command remove-object
    88  
    89      # To prevent changes to the model:
    90      juju disable-command all "Model locked down"
    91  
    92  See also:
    93      disabled-commands
    94      enable-command
    95  `