-
Notifications
You must be signed in to change notification settings - Fork 223
/
destroy.go
206 lines (173 loc) · 4.83 KB
/
destroy.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package machine
import (
"context"
"fmt"
"strings"
"github.com/spf13/cobra"
fly "github.com/superfly/fly-go"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/flapsutil"
"github.com/superfly/flyctl/internal/flyutil"
mach "github.com/superfly/flyctl/internal/machine"
"github.com/superfly/flyctl/internal/prompt"
"github.com/superfly/flyctl/iostreams"
)
func newDestroy() *cobra.Command {
const (
short = "Destroy Fly machines"
long = `Destroy one or more Fly machines.
This command requires a machine to be in a stopped state unless the force flag is used.
`
usage = "destroy [flags] ID ID ..."
)
cmd := command.New(usage, short, long, runMachineDestroy,
command.RequireSession,
command.LoadAppNameIfPresent,
)
cmd.Aliases = []string{"remove", "rm"}
flag.Add(
cmd,
flag.App(),
flag.AppConfig(),
selectFlag,
flag.Bool{
Name: "force",
Shorthand: "f",
Description: "force kill machine regardless of current state",
},
flag.String{
Name: "image",
Shorthand: "i",
Description: "remove all machines with the specified image hash",
},
)
cmd.Args = cobra.ArbitraryArgs
return cmd
}
func runMachineDestroy(ctx context.Context) (err error) {
ctx, err = buildContextFromAppName(ctx, appconfig.NameFromContext(ctx))
image := strings.TrimSpace(flag.GetString(ctx, "image"))
ids := []string{}
if image != "" {
machines, err := flapsutil.ClientFromContext(ctx).ListActive(ctx)
if err != nil {
return err
}
machinesToBeDeleted := []*fly.Machine{}
for _, machine := range machines {
if machine.ImageRefWithVersion() == image {
machinesToBeDeleted = append(machinesToBeDeleted, machine)
ids = append(ids, machine.ID)
}
}
if len(machinesToBeDeleted) == 0 {
fmt.Fprint(iostreams.FromContext(ctx).Out, "No machine to destroy, exiting\n")
return nil
}
machines, release, err := mach.AcquireLeases(ctx, machinesToBeDeleted)
if err != nil {
return err
}
defer release()
confirmed, err := prompt.Confirm(ctx, fmt.Sprintf("%d Machines (%s) will be destroyed, continue?", len(machines), strings.Join(ids, ",")))
if err != nil {
return err
}
if !confirmed {
return nil
}
for _, machine := range machines {
err = singleDestroyRun(ctx, machine)
if err != nil {
return err
}
}
} else if len(flag.Args(ctx)) == 0 {
machine, ctx, err := selectOneMachine(ctx, "", "", false)
if err != nil {
return err
}
machine, release, err := mach.AcquireLease(ctx, machine)
if err != nil {
return err
}
defer release()
err = singleDestroyRun(ctx, machine)
if err != nil {
return err
}
} else {
machines, ctx, err := selectManyMachines(ctx, flag.Args(ctx))
if err != nil {
return err
}
machines, release, err := mach.AcquireLeases(ctx, machines)
if err != nil {
return err
}
defer release()
for _, machine := range machines {
err = singleDestroyRun(ctx, machine)
if err != nil {
return err
}
}
}
return nil
}
func singleDestroyRun(ctx context.Context, machine *fly.Machine) error {
var (
out = iostreams.FromContext(ctx).Out
force = flag.GetBool(ctx, "force")
)
appName := appconfig.NameFromContext(ctx)
// This is used for the deletion hook below.
client := flyutil.ClientFromContext(ctx)
app, err := client.GetAppCompact(ctx, appName)
if err != nil {
return fmt.Errorf("could not get app '%s': %w", appName, err)
}
err = Destroy(ctx, app, machine, force)
if err != nil {
return err
}
fmt.Fprintf(out, "%s has been destroyed\n", machine.ID)
return nil
}
func Destroy(ctx context.Context, app *fly.AppCompact, machine *fly.Machine, force bool) error {
var (
out = iostreams.FromContext(ctx).Out
flapsClient = flapsutil.ClientFromContext(ctx)
input = fly.RemoveMachineInput{
ID: machine.ID,
Kill: force,
}
)
switch machine.State {
case "stopped":
break
case "destroyed":
return fmt.Errorf("machine %s has already been destroyed", machine.ID)
case "started":
if !force {
return fmt.Errorf("machine %s currently started, either stop first or use --force flag", machine.ID)
}
default:
if !force {
return fmt.Errorf("machine %s is in a %s state and cannot be destroyed since it is not stopped, either stop first or use --force flag", machine.ID, machine.State)
}
}
fmt.Fprintf(out, "machine %s was found and is currently in %s state, attempting to destroy...\n", machine.ID, machine.State)
err := flapsClient.Destroy(ctx, input, machine.LeaseNonce)
if err != nil {
if err := rewriteMachineNotFoundErrors(ctx, err, machine.ID); err != nil {
return err
}
return fmt.Errorf("could not destroy machine %s: %w", machine.ID, err)
}
// Best effort post-deletion hook.
runOnDeletionHook(ctx, app, machine)
return nil
}