github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/cmd/juju/removemachine.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package main 5 6 import ( 7 "fmt" 8 9 "github.com/juju/names" 10 "launchpad.net/gnuflag" 11 12 "github.com/juju/juju/cmd" 13 "github.com/juju/juju/cmd/envcmd" 14 "github.com/juju/juju/juju" 15 ) 16 17 // RemoveMachineCommand causes an existing machine to be destroyed. 18 type RemoveMachineCommand struct { 19 envcmd.EnvCommandBase 20 MachineIds []string 21 Force bool 22 } 23 24 const destroyMachineDoc = ` 25 Machines that are responsible for the environment cannot be removed. Machines 26 running units or containers can only be removed with the --force flag; doing 27 so will also remove all those units and containers without giving them any 28 opportunity to shut down cleanly. 29 30 Examples: 31 # Remove machine number 5 which has no running units or containers 32 $ juju remove-machine 5 33 34 # Remove machine 6 and any running units or containers 35 $ juju remove-machine 6 --force 36 ` 37 38 func (c *RemoveMachineCommand) Info() *cmd.Info { 39 return &cmd.Info{ 40 Name: "remove-machine", 41 Args: "<machine> ...", 42 Purpose: "remove machines from the environment", 43 Doc: destroyMachineDoc, 44 Aliases: []string{"destroy-machine", "terminate-machine"}, 45 } 46 } 47 48 func (c *RemoveMachineCommand) SetFlags(f *gnuflag.FlagSet) { 49 f.BoolVar(&c.Force, "force", false, "completely remove machine and all dependencies") 50 } 51 52 func (c *RemoveMachineCommand) 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.IsMachine(id) { 58 return fmt.Errorf("invalid machine id %q", id) 59 } 60 } 61 c.MachineIds = args 62 return nil 63 } 64 65 func (c *RemoveMachineCommand) Run(_ *cmd.Context) error { 66 apiclient, err := juju.NewAPIClientFromName(c.EnvName) 67 if err != nil { 68 return err 69 } 70 defer apiclient.Close() 71 if c.Force { 72 return apiclient.ForceDestroyMachines(c.MachineIds...) 73 } 74 return apiclient.DestroyMachines(c.MachineIds...) 75 }