github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/cmd/juju/commands/removeunit.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package commands
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/juju/cmd"
    10  	"github.com/juju/names"
    11  
    12  	"github.com/juju/juju/cmd/envcmd"
    13  	"github.com/juju/juju/cmd/juju/block"
    14  )
    15  
    16  // RemoveUnitCommand is responsible for destroying service units.
    17  type RemoveUnitCommand struct {
    18  	envcmd.EnvCommandBase
    19  	UnitNames []string
    20  }
    21  
    22  const removeUnitDoc = `
    23  Remove service units from the environment.
    24  
    25  If this is the only unit running, the machine on which
    26  the unit is hosted will also be destroyed, if possible.
    27  The machine will be destroyed if:
    28  - it is not a state server
    29  - it is not hosting any Juju managed containers
    30  `
    31  
    32  func (c *RemoveUnitCommand) Info() *cmd.Info {
    33  	return &cmd.Info{
    34  		Name:    "remove-unit",
    35  		Args:    "<unit> [...]",
    36  		Purpose: "remove service units from the environment",
    37  		Doc:     removeUnitDoc,
    38  		Aliases: []string{"destroy-unit"},
    39  	}
    40  }
    41  
    42  func (c *RemoveUnitCommand) Init(args []string) error {
    43  	c.UnitNames = args
    44  	if len(c.UnitNames) == 0 {
    45  		return fmt.Errorf("no units specified")
    46  	}
    47  	for _, name := range c.UnitNames {
    48  		if !names.IsValidUnit(name) {
    49  			return fmt.Errorf("invalid unit name %q", name)
    50  		}
    51  	}
    52  	return nil
    53  }
    54  
    55  // Run connects to the environment specified on the command line and destroys
    56  // units therein.
    57  func (c *RemoveUnitCommand) Run(_ *cmd.Context) error {
    58  	client, err := c.NewAPIClient()
    59  	if err != nil {
    60  		return err
    61  	}
    62  	defer client.Close()
    63  	return block.ProcessBlockedError(client.DestroyServiceUnits(c.UnitNames...), block.BlockRemove)
    64  }