-
Notifications
You must be signed in to change notification settings - Fork 937
/
Copy pathdelete.go
177 lines (144 loc) · 4.14 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"bufio"
"errors"
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/canonical/lxd/client"
"github.com/canonical/lxd/shared"
"github.com/canonical/lxd/shared/api"
cli "github.com/canonical/lxd/shared/cmd"
"github.com/canonical/lxd/shared/i18n"
)
type cmdDelete struct {
global *cmdGlobal
flagForce bool
flagForceProtected bool
flagInteractive bool
}
func (c *cmdDelete) command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = usage("delete", i18n.G("[<remote>:]<instance>[/<snapshot>] [[<remote>:]<instance>[/<snapshot>]...]"))
cmd.Aliases = []string{"rm"}
cmd.Short = i18n.G("Delete instances and snapshots")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
`Delete instances and snapshots`))
cmd.RunE = c.run
cmd.Flags().BoolVarP(&c.flagForce, "force", "f", false, i18n.G("Force the removal of running instances"))
cmd.Flags().BoolVarP(&c.flagInteractive, "interactive", "i", false, i18n.G("Require user confirmation"))
cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return c.global.cmpInstancesAction(toComplete, "delete", c.flagForce)
}
return cmd
}
func (c *cmdDelete) 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.ValueInSlice(strings.ToLower(input), []string{i18n.G("yes")}) {
return errors.New(i18n.G("User aborted delete operation"))
}
return nil
}
func (c *cmdDelete) doDelete(d lxd.InstanceServer, name string) error {
var op lxd.Operation
var err error
if shared.IsSnapshot(name) {
// Snapshot delete
fields := strings.SplitN(name, shared.SnapshotDelimiter, 2)
op, err = d.DeleteInstanceSnapshot(fields[0], fields[1])
} else {
// Instance delete
op, err = d.DeleteInstance(name)
}
if err != nil {
return err
}
return op.Wait()
}
func (c *cmdDelete) run(cmd *cobra.Command, args []string) error {
// Quick checks.
exit, err := c.global.CheckArgs(cmd, args, 1, -1)
if exit {
return err
}
// Parse remote
resources, err := c.global.ParseServers(args...)
if err != nil {
return err
}
// Check that everything exists.
err = instancesExist(resources)
if err != nil {
return err
}
// Process with deletion.
for _, resource := range resources {
connInfo, err := resource.server.GetConnectionInfo()
if err != nil {
return err
}
if c.flagInteractive {
err := c.promptDelete(resource.name)
if err != nil {
return err
}
}
if shared.IsSnapshot(resource.name) {
err := c.doDelete(resource.server, resource.name)
if err != nil {
return fmt.Errorf(i18n.G("Failed deleting instance snapshot %q in project %q: %w"), resource.name, connInfo.Project, err)
}
continue
}
ct, _, err := resource.server.GetInstance(resource.name)
if err != nil {
return err
}
if ct.StatusCode != 0 && ct.StatusCode != api.Stopped {
if !c.flagForce {
return errors.New(i18n.G("The instance is currently running, stop it first or pass --force"))
}
req := api.InstanceStatePut{
Action: "stop",
Timeout: -1,
Force: true,
}
op, err := resource.server.UpdateInstanceState(resource.name, req, "")
if err != nil {
return err
}
err = op.Wait()
if err != nil {
return fmt.Errorf(i18n.G("Stopping the instance failed: %s"), err)
}
if ct.Ephemeral {
continue
}
}
if c.flagForceProtected && shared.IsTrue(ct.ExpandedConfig["security.protection.delete"]) {
// Refresh in case we had to stop it above.
ct, etag, err := resource.server.GetInstance(resource.name)
if err != nil {
return err
}
ct.Config["security.protection.delete"] = "false"
op, err := resource.server.UpdateInstance(resource.name, ct.Writable(), etag)
if err != nil {
return err
}
err = op.Wait()
if err != nil {
return err
}
}
err = c.doDelete(resource.server, resource.name)
if err != nil {
return fmt.Errorf(i18n.G("Failed deleting instance %q in project %q: %w"), resource.name, connInfo.Project, err)
}
}
return nil
}