10
"launchpad.net/gnuflag"
9
12
"launchpad.net/juju-core/cmd"
13
"launchpad.net/juju-core/errors"
10
14
"launchpad.net/juju-core/juju"
11
15
"launchpad.net/juju-core/names"
16
"launchpad.net/juju-core/rpc"
17
"launchpad.net/juju-core/state"
14
20
// DestroyMachineCommand causes an existing machine to be destroyed.
15
21
type DestroyMachineCommand struct {
17
23
MachineIds []string
27
const destroyMachineDoc = `
28
Machines that are responsible for the environment cannot be destroyed. Machines
29
running units or containers can only be destroyed with the --force flag; doing
30
so will also destroy all those units and containers without giving them any
31
opportunity to shut down cleanly.
20
34
func (c *DestroyMachineCommand) Info() *cmd.Info {
22
36
Name: "destroy-machine",
23
37
Args: "<machine> ...",
24
38
Purpose: "destroy machines",
25
Doc: "Machines that have assigned units, or are responsible for the environment, cannot be destroyed.",
39
Doc: destroyMachineDoc,
26
40
Aliases: []string{"terminate-machine"},
44
func (c *DestroyMachineCommand) SetFlags(f *gnuflag.FlagSet) {
45
c.EnvCommandBase.SetFlags(f)
46
f.BoolVar(&c.Force, "force", false, "completely remove machine and all dependencies")
30
49
func (c *DestroyMachineCommand) Init(args []string) error {
31
50
if len(args) == 0 {
32
51
return fmt.Errorf("no machines specified")
43
func (c *DestroyMachineCommand) Run(_ *cmd.Context) error {
62
// destroyMachines destroys the machines with the specified ids.
63
// This is copied from the 1.16.3 code to enable compatibility. It should be
64
// removed when we release a version that goes via the API only (whatever is
66
func destroyMachines(st *state.State, ids ...string) (err error) {
68
for _, id := range ids {
69
machine, err := st.Machine(id)
71
case errors.IsNotFoundError(err):
72
err = fmt.Errorf("machine %s does not exist", id)
74
case machine.Life() != state.Alive:
77
err = machine.Destroy()
80
errs = append(errs, err.Error())
86
msg := "some machines were not destroyed"
87
if len(errs) == len(ids) {
88
msg = "no machines were destroyed"
90
return fmt.Errorf("%s: %s", msg, strings.Join(errs, "; "))
93
func (c *DestroyMachineCommand) run1dot16() error {
95
return fmt.Errorf("destroy-machine --force is not supported in Juju servers older than 1.16.4")
44
97
conn, err := juju.NewConnFromName(c.EnvName)
48
101
defer conn.Close()
49
return conn.State.DestroyMachines(c.MachineIds...)
102
return destroyMachines(conn.State, c.MachineIds...)
105
func (c *DestroyMachineCommand) Run(_ *cmd.Context) error {
106
apiclient, err := juju.NewAPIClientFromName(c.EnvName)
110
defer apiclient.Close()
112
err = apiclient.ForceDestroyMachines(c.MachineIds...)
114
err = apiclient.DestroyMachines(c.MachineIds...)
116
// Juju 1.16.3 and older did not have DestroyMachines as an API command.
117
if rpc.IsNoSuchRequest(err) {
118
logger.Infof("DestroyMachines not supported by the API server, " +
119
"falling back to <=1.16.3 compatibility")