github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/cmd/juju/machine/remove.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package machine 5 6 import ( 7 "fmt" 8 9 "github.com/juju/cmd" 10 "github.com/juju/names" 11 "launchpad.net/gnuflag" 12 13 "github.com/juju/juju/cmd/envcmd" 14 "github.com/juju/juju/cmd/juju/block" 15 ) 16 17 // RemoveCommand causes an existing machine to be destroyed. 18 type RemoveCommand struct { 19 envcmd.EnvCommandBase 20 api RemoveMachineAPI 21 MachineIds []string 22 Force bool 23 } 24 25 const destroyMachineDoc = ` 26 Machines that are responsible for the environment cannot be removed. Machines 27 running units or containers can only be removed with the --force flag; doing 28 so will also remove all those units and containers without giving them any 29 opportunity to shut down cleanly. 30 31 Examples: 32 # Remove machine number 5 which has no running units or containers 33 $ juju machine remove 5 34 35 # Remove machine 6 and any running units or containers 36 $ juju machine remove 6 --force 37 ` 38 39 func (c *RemoveCommand) Info() *cmd.Info { 40 return &cmd.Info{ 41 Name: "remove", 42 Args: "<machine> ...", 43 Purpose: "remove machines from the environment", 44 Doc: destroyMachineDoc, 45 } 46 } 47 48 func (c *RemoveCommand) SetFlags(f *gnuflag.FlagSet) { 49 f.BoolVar(&c.Force, "force", false, "completely remove machine and all dependencies") 50 } 51 52 func (c *RemoveCommand) Init(args []string) error { 53 if len(args) == 0 { 54 return fmt.Errorf("no machines specified") 55 } 56 for _, id := range args { 57 if !names.IsValidMachine(id) { 58 return fmt.Errorf("invalid machine id %q", id) 59 } 60 } 61 c.MachineIds = args 62 return nil 63 } 64 65 type RemoveMachineAPI interface { 66 DestroyMachines(machines ...string) error 67 ForceDestroyMachines(machines ...string) error 68 Close() error 69 } 70 71 func (c *RemoveCommand) getRemoveMachineAPI() (RemoveMachineAPI, error) { 72 if c.api != nil { 73 return c.api, nil 74 } 75 return c.NewAPIClient() 76 } 77 78 func (c *RemoveCommand) Run(_ *cmd.Context) error { 79 client, err := c.getRemoveMachineAPI() 80 if err != nil { 81 return err 82 } 83 defer client.Close() 84 if c.Force { 85 err = client.ForceDestroyMachines(c.MachineIds...) 86 } else { 87 err = client.DestroyMachines(c.MachineIds...) 88 } 89 return block.ProcessBlockedError(err, block.BlockRemove) 90 }