github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/service/removeunit.go (about) 1 // Copyright 2012, 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package service 5 6 import ( 7 "fmt" 8 9 "github.com/juju/cmd" 10 "github.com/juju/errors" 11 "github.com/juju/names" 12 13 apiservice "github.com/juju/juju/api/service" 14 "github.com/juju/juju/cmd/juju/block" 15 "github.com/juju/juju/cmd/modelcmd" 16 ) 17 18 // NewRemoveUnitCommand returns a command which removes a service's units. 19 func NewRemoveUnitCommand() cmd.Command { 20 return modelcmd.Wrap(&removeUnitCommand{}) 21 } 22 23 // removeUnitCommand is responsible for destroying service units. 24 type removeUnitCommand struct { 25 modelcmd.ModelCommandBase 26 UnitNames []string 27 } 28 29 const removeUnitDoc = ` 30 Remove service units from the model. 31 32 If this is the only unit running, the machine on which 33 the unit is hosted will also be destroyed, if possible. 34 The machine will be destroyed if: 35 - it is not a controller 36 - it is not hosting any Juju managed containers 37 ` 38 39 func (c *removeUnitCommand) Info() *cmd.Info { 40 return &cmd.Info{ 41 Name: "remove-unit", 42 Args: "<unit> [...]", 43 Purpose: "remove service units from the model", 44 Doc: removeUnitDoc, 45 Aliases: []string{"destroy-unit"}, 46 } 47 } 48 49 func (c *removeUnitCommand) Init(args []string) error { 50 c.UnitNames = args 51 if len(c.UnitNames) == 0 { 52 return fmt.Errorf("no units specified") 53 } 54 for _, name := range c.UnitNames { 55 if !names.IsValidUnit(name) { 56 return fmt.Errorf("invalid unit name %q", name) 57 } 58 } 59 return nil 60 } 61 62 func (c *removeUnitCommand) getAPI() (ServiceAPI, error) { 63 root, err := c.NewAPIRoot() 64 if err != nil { 65 return nil, errors.Trace(err) 66 } 67 return apiservice.NewClient(root), nil 68 } 69 70 // Run connects to the environment specified on the command line and destroys 71 // units therein. 72 func (c *removeUnitCommand) Run(_ *cmd.Context) error { 73 client, err := c.getAPI() 74 if err != nil { 75 return err 76 } 77 defer client.Close() 78 return block.ProcessBlockedError(client.DestroyUnits(c.UnitNames...), block.BlockRemove) 79 }