github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/cmd/juju/application/removeunit.go (about) 1 // Copyright 2012, 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package application 5 6 import ( 7 "github.com/juju/cmd" 8 "github.com/juju/errors" 9 "gopkg.in/juju/names.v2" 10 11 "github.com/juju/juju/api/application" 12 "github.com/juju/juju/cmd/juju/block" 13 "github.com/juju/juju/cmd/modelcmd" 14 ) 15 16 // NewRemoveUnitCommand returns a command which removes an application's units. 17 func NewRemoveUnitCommand() cmd.Command { 18 return modelcmd.Wrap(&removeUnitCommand{}) 19 } 20 21 // removeUnitCommand is responsible for destroying application units. 22 type removeUnitCommand struct { 23 modelcmd.ModelCommandBase 24 UnitNames []string 25 } 26 27 const removeUnitDoc = ` 28 Remove application units from the model. 29 30 Units of a service are numbered in sequence upon creation. For example, the 31 fourth unit of wordpress will be designated "wordpress/3". These identifiers 32 can be supplied in a space delimited list to remove unwanted units from the 33 model. 34 35 Juju will also remove the machine if the removed unit was the only unit left 36 on that machine (including units in containers). 37 38 Removing all units of a service is not equivalent to removing the service 39 itself; for that, the ` + "`juju remove-service`" + ` command is used. 40 41 Examples: 42 43 juju remove-unit wordpress/2 wordpress/3 wordpress/4 44 45 See also: 46 remove-service 47 ` 48 49 func (c *removeUnitCommand) Info() *cmd.Info { 50 return &cmd.Info{ 51 Name: "remove-unit", 52 Args: "<unit> [...]", 53 Purpose: "Remove application units from the model.", 54 Doc: removeUnitDoc, 55 } 56 } 57 58 func (c *removeUnitCommand) Init(args []string) error { 59 c.UnitNames = args 60 if len(c.UnitNames) == 0 { 61 return errors.Errorf("no units specified") 62 } 63 for _, name := range c.UnitNames { 64 if !names.IsValidUnit(name) { 65 return errors.Errorf("invalid unit name %q", name) 66 } 67 } 68 return nil 69 } 70 71 func (c *removeUnitCommand) getAPI() (ServiceAPI, error) { 72 root, err := c.NewAPIRoot() 73 if err != nil { 74 return nil, errors.Trace(err) 75 } 76 return application.NewClient(root), nil 77 } 78 79 // Run connects to the environment specified on the command line and destroys 80 // units therein. 81 func (c *removeUnitCommand) Run(_ *cmd.Context) error { 82 client, err := c.getAPI() 83 if err != nil { 84 return err 85 } 86 defer client.Close() 87 return block.ProcessBlockedError(client.DestroyUnits(c.UnitNames...), block.BlockRemove) 88 }