forked from canonical/lxd
/
delete.go
119 lines (95 loc) · 2.58 KB
/
delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/lxc/lxd"
"github.com/lxc/lxd/shared"
"github.com/lxc/lxd/shared/gnuflag"
"github.com/lxc/lxd/shared/i18n"
)
type deleteCmd struct {
force bool
interactive bool
}
func (c *deleteCmd) showByDefault() bool {
return true
}
func (c *deleteCmd) usage() string {
return i18n.G(
`Delete containers or container snapshots.
lxc delete [remote:]<container>[/<snapshot>] [remote:][<container>[/<snapshot>]...]
Destroy containers or snapshots with any attached data (configuration, snapshots, ...).`)
}
func (c *deleteCmd) flags() {
gnuflag.BoolVar(&c.force, "f", false, i18n.G("Force the removal of stopped containers."))
gnuflag.BoolVar(&c.force, "force", false, i18n.G("Force the removal of stopped containers."))
gnuflag.BoolVar(&c.interactive, "i", false, i18n.G("Require user confirmation."))
gnuflag.BoolVar(&c.interactive, "interactive", false, i18n.G("Require user confirmation."))
}
func (c *deleteCmd) promptDelete(name string) error {
reader := bufio.NewReader(os.Stdin)
fmt.Printf(i18n.G("Remove %s (yes/no): "), name)
input, _ := reader.ReadString('\n')
input = strings.TrimSuffix(input, "\n")
if !shared.StringInSlice(strings.ToLower(input), []string{i18n.G("yes")}) {
return fmt.Errorf(i18n.G("User aborted delete operation."))
}
return nil
}
func (c *deleteCmd) doDelete(d *lxd.Client, name string) error {
resp, err := d.Delete(name)
if err != nil {
return err
}
return d.WaitForSuccess(resp.Operation)
}
func (c *deleteCmd) run(config *lxd.Config, args []string) error {
if len(args) == 0 {
return errArgs
}
for _, nameArg := range args {
remote, name := config.ParseRemoteAndContainer(nameArg)
d, err := lxd.NewClient(config, remote)
if err != nil {
return err
}
if c.interactive {
err := c.promptDelete(name)
if err != nil {
return err
}
}
if shared.IsSnapshot(name) {
return c.doDelete(d, name)
}
ct, err := d.ContainerInfo(name)
if err != nil {
return err
}
if ct.StatusCode != 0 && ct.StatusCode != shared.Stopped {
if !c.force {
return fmt.Errorf(i18n.G("The container is currently running, stop it first or pass --force."))
}
resp, err := d.Action(name, shared.Stop, -1, true, false)
if err != nil {
return err
}
op, err := d.WaitFor(resp.Operation)
if err != nil {
return err
}
if op.StatusCode == shared.Failure {
return fmt.Errorf(i18n.G("Stopping container failed!"))
}
if ct.Ephemeral == true {
return nil
}
}
if err := c.doDelete(d, name); err != nil {
return err
}
}
return nil
}