/
debug.go
448 lines (399 loc) · 11.9 KB
/
debug.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
package migrate_to_v2
import (
"context"
"errors"
"fmt"
"time"
"github.com/AlecAivazis/survey/v2"
"github.com/jpillora/backoff"
"github.com/samber/lo"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/flaps"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/command/apps"
"github.com/superfly/flyctl/internal/command/deploy"
"github.com/superfly/flyctl/internal/command/machine"
"github.com/superfly/flyctl/internal/command/status"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/render"
"github.com/superfly/flyctl/iostreams"
)
func newDebug() *cobra.Command {
const (
usage = `debug`
long = `Debug an app that has been migrated to Apps V2`
short = long
)
cmd := command.New(
usage, short, long, runDebug,
command.RequireSession,
command.RequireAppName,
)
cmd.Args = cobra.NoArgs
flag.Add(cmd,
flag.App(),
flag.AppConfig(),
)
return cmd
}
func hasMigrated(ctx context.Context, app *api.AppCompact, machines []*api.Machine) bool {
client := client.FromContext(ctx).API()
if app.PlatformVersion == appconfig.DetachedPlatform {
return true
}
// Check that the app is not currently on nomad
if app.PlatformVersion == appconfig.NomadPlatform {
return false
}
// Look for a machine tied to a previous alloc
for _, machine := range machines {
if machine.Config != nil && machine.Config.Metadata != nil {
if _, ok := machine.Config.Metadata[api.MachineConfigMetadataKeyFlyPreviousAlloc]; ok {
return true
}
}
}
// Look for a release created by admin-bot@fly.io
releases, err := client.GetAppReleasesMachines(ctx, app.Name, "", 25)
if err != nil {
return false
}
for _, release := range releases {
// Technically, I don't think this is the only time we could use admin-bot@fly.io,
// but we use it infrequently and soon we'll be done dealing with this,
// so it's probably an acceptable way to determine this for now.
if release.User.Email == "admin-bot@fly.io" {
return true
}
}
return false
}
const timedOutErr = "timed out"
func backoffWait(ctx context.Context, cutoff time.Duration, cb func() (bool, error)) error {
ctx, cancelFn := context.WithTimeout(ctx, cutoff)
defer cancelFn()
b := &backoff.Backoff{
Min: 1 * time.Second,
Max: 10 * time.Second,
Factor: 1.2,
Jitter: true,
}
for {
// Check for deadline
select {
case <-ctx.Done():
return errors.New(timedOutErr)
default:
}
done, err := cb()
if err != nil {
return err
}
if done {
return nil
}
time.Sleep(b.Duration())
}
}
func unsuspend(ctx context.Context, app *api.AppCompact) error {
if app.Status != "suspended" {
return nil
}
client := client.FromContext(ctx).API()
if app.Status == "suspended" {
_, err := client.ResumeApp(ctx, app.Name)
if err != nil {
return err
}
}
err := backoffWait(ctx, 1*time.Minute, func() (bool, error) {
app, err := client.GetAppCompact(ctx, app.Name)
if err != nil {
return false, err
}
if app.Status != "suspended" {
return true, nil
}
return false, nil
})
if err != nil {
if err.Error() == timedOutErr {
return errors.New("timed out waiting for app to unsuspend")
}
return err
}
return nil
}
func runDebug(ctx context.Context) error {
io := iostreams.FromContext(ctx)
appName := appconfig.NameFromContext(ctx)
client := client.FromContext(ctx).API()
app, err := client.GetAppCompact(ctx, appName)
if err != nil {
return err
}
flapsClient, err := flaps.NewFromAppName(ctx, app.Name)
if err != nil {
return fmt.Errorf("could not create flaps client: %w", err)
}
ctx = flaps.NewContext(ctx, flapsClient)
// Grab the list of machines
// Useful to have, but also used to determine if the app has been migrated
var machines []*api.Machine
if app.PlatformVersion != appconfig.NomadPlatform {
machines, err = flapsClient.List(ctx, "")
if err != nil {
return fmt.Errorf("could not list machines: %w", err)
}
}
if !hasMigrated(ctx, app, machines) {
return fmt.Errorf("app has not been migrated to Apps V2")
}
if app.PlatformVersion != appconfig.MachinesPlatform {
err := unsuspend(ctx, app)
if err != nil {
return fmt.Errorf("could not unsuspend app: %w", err)
}
}
// Grab nomad allocs now that we know the app has been migrated
allocs, err := client.GetAllocations(ctx, app.Name, false)
if err != nil {
return fmt.Errorf("could not list Nomad VMs: %w", err)
}
if app.PlatformVersion == appconfig.DetachedPlatform {
fmt.Fprintf(io.Out, `The app's platform version is 'detached'
This means that the app is stuck in a half-migrated state, and wasn't able to
be fully recovered during the migration error rollback process.
Fixing this depends on how far the app got in the migration process.
Please use these tools to troubleshoot and attempt to repair the app.
`)
return fixDetachedApp(ctx, app, machines, allocs)
}
if app.PlatformVersion == appconfig.MachinesPlatform {
if len(allocs) != 0 {
fmt.Fprintf(io.Out, "Detected Nomad VMs running on V2 app, cleaning up.\n")
return zeroNomadUseMachines(ctx, app, allocs)
}
}
fmt.Fprintf(io.Out, "No issues detected.\n")
return nil
}
func zeroNomadUseMachines(ctx context.Context, app *api.AppCompact, allocs []*api.AllocationStatus) error {
io := iostreams.FromContext(ctx)
if app.PlatformVersion != appconfig.DetachedPlatform {
err := setPlatformVersion(ctx, appconfig.DetachedPlatform)
if err != nil {
return err
}
}
fmt.Fprintf(io.Out, "Destroying Nomad VMs and setting platform version to machines/Apps V2.\n")
vmGroups := lo.Uniq(lo.Map(allocs, func(alloc *api.AllocationStatus, _ int) string {
return alloc.TaskName
}))
err := scaleNomadToZero(ctx, app, "", vmGroups)
if err != nil {
return err
}
err = setPlatformVersion(ctx, appconfig.MachinesPlatform)
if err != nil {
return err
}
fmt.Fprint(io.Out, "Done!\n")
return nil
}
func setPlatformVersion(ctx context.Context, ver string) error {
return apps.UpdateAppPlatformVersion(ctx, appconfig.NameFromContext(ctx), ver)
}
func fixDetachedApp(
ctx context.Context,
app *api.AppCompact,
machines []*api.Machine,
allocs []*api.AllocationStatus,
) error {
io := iostreams.FromContext(ctx)
client := client.FromContext(ctx).API()
flapsClient := flaps.FromContext(ctx)
if len(machines) == 0 && len(allocs) == 0 {
fmt.Fprintf(io.Out, "No machines or Nomad VMs found. Setting platform version to machines/Apps V2.\n")
return setPlatformVersion(ctx, appconfig.MachinesPlatform)
}
if len(machines) == 0 {
fmt.Fprintf(io.Out, "No Apps v2 machines found. Setting platform version to Nomad.\n")
setPlatformVersion(ctx, appconfig.NomadPlatform)
}
if len(allocs) == 0 {
fmt.Fprintf(io.Out, "No legacy Nomad VMs found. Setting platform version to machines/Apps V2.\n")
return setPlatformVersion(ctx, appconfig.MachinesPlatform)
}
autodiagnose := func() string {
nomadTaskGroups := make(map[string]bool)
for _, alloc := range allocs {
nomadTaskGroups[alloc.TaskName] = false
}
for _, machine := range machines {
nomadTaskGroups[machine.ProcessGroup()] = true
}
missingProcessGroup := false
errorStr := "\nProcess group issues:\n"
for taskGroup, hasMachine := range nomadTaskGroups {
if !hasMachine {
errorStr += fmt.Sprintf(" * '%s' has no machines\n", taskGroup)
missingProcessGroup = true
}
}
if !missingProcessGroup {
errorStr += " * none found\n"
}
errorStr += "VM count issues:\n"
if len(allocs) > len(machines) {
errorStr += fmt.Sprintf(" * %d more Nomad VMs than machines\n", len(allocs)-len(machines))
} else {
errorStr += " * none found\n"
}
errorStr += "\nTo fix this, you can try:\n"
if missingProcessGroup {
errorStr += " * running `fly deploy` to create missing process groups,\n then removing existing Nomad VMs and switching to V2\n"
} else {
errorStr += " * removing existing Nomad VMs and switching to V2, then using `fly scale count` to scale your app as needed\n"
}
return errorStr + "\n"
}
const (
Autodiagnose = "Autodiagnose issues"
PrintNomad = "List Nomad allocs"
PrintMachines = "List Machines"
Deploy = "Deploy Machines (equivalent to 'fly deploy', might help with process groups)"
DestroyNomadUseMachines = "Destroy remaining Nomad allocs and use Apps V2"
DestroyMachinesUseNomad = "Destroy existing machines and use Nomad"
Exit = "Exit"
)
// Lifted from command/status/status.go
var appStatus *api.AppStatus
var err error
if appStatus, err = client.GetAppStatus(ctx, app.Name, false); err != nil {
return fmt.Errorf("failed retrieving app %s: %w", app.Name, err)
}
var backupRegions []api.Region
if appStatus.Deployed {
if _, backupRegions, err = client.ListAppRegions(ctx, app.Name); err != nil {
return fmt.Errorf("failed retrieving backup regions for %s: %w", app.Name, err)
}
}
for {
var opt struct{ Choice string }
err := survey.Ask([]*survey.Question{{
Name: "choice",
Prompt: &survey.Select{
Message: "What would you like to do?",
Options: []string{
Autodiagnose,
PrintNomad,
PrintMachines,
Deploy,
DestroyNomadUseMachines,
DestroyMachinesUseNomad,
Exit,
},
Default: Autodiagnose,
},
}}, &opt)
if err != nil {
return err
}
switch opt.Choice {
case Autodiagnose:
fmt.Fprint(io.Out, autodiagnose())
case PrintNomad:
fmt.Fprint(io.Out, "Nomad VMs:\n")
err = render.AllocationStatuses(io.Out, "Nomad VMs", backupRegions, appStatus.Allocations...)
if err != nil {
return err
}
case PrintMachines:
if err := status.RenderMachineStatus(ctx, app, io.Out); err != nil {
return err
}
case Deploy:
if err := deployMachines(ctx); err != nil {
fmt.Fprintf(io.ErrOut, "Error deploying machines: %s\n", err)
}
machines, err = flapsClient.List(ctx, "")
if err != nil {
return fmt.Errorf("could not list machines: %w", err)
}
case DestroyNomadUseMachines:
confirm := false
if err := survey.AskOne(&survey.Confirm{
Message: "Are you sure you want to remove existing Nomad VMs and switch to V2?",
}, &confirm); err != nil {
return err
}
if !confirm {
continue
}
return zeroNomadUseMachines(ctx, app, allocs)
case DestroyMachinesUseNomad:
confirm := false
if err := survey.AskOne(&survey.Confirm{
Message: "Are you sure you want to remove all Machines and switch back to Nomad?",
}, &confirm); err != nil {
return err
}
if !confirm {
continue
}
fmt.Fprint(io.Out, "Destroying machines and setting platform version to nomad.\n")
for _, mach := range machines {
err := machine.Destroy(ctx, app, mach, true)
if err != nil {
return fmt.Errorf("could not destroy machine: %w", err)
}
}
err = setPlatformVersion(ctx, appconfig.NomadPlatform)
if err != nil {
return err
}
fmt.Fprint(io.Out, "Done!\n")
return nil
case Exit:
return nil
}
}
}
func deployMachines(ctx context.Context) (err error) {
io := iostreams.FromContext(ctx)
tb := render.NewTextBlock(ctx, "Verifying app config")
appName := appconfig.NameFromContext(ctx)
var cfg *appconfig.Config
if cfg = appconfig.ConfigFromContext(ctx); cfg == nil {
cfg, err = appconfig.FromRemoteApp(ctx, appName)
if err != nil {
return err
}
}
// Always prefer the app name passed via --app
if appName != "" {
cfg.AppName = appName
}
err, extraInfo := cfg.Validate(ctx)
if extraInfo != "" {
fmt.Fprintf(io.Out, extraInfo)
}
if err != nil {
return err
}
tb.Done("Verified app config")
// Hack!
newDeployCmd := deploy.New()
fakeFlagCtx := flag.NewContext(ctx, newDeployCmd.Flags())
if err := flag.SetString(fakeFlagCtx, flag.AppConfigFilePathName, flag.GetAppConfigFilePath(ctx)); err != nil {
return err
}
if err := flag.SetString(fakeFlagCtx, flag.AppName, flag.GetApp(ctx)); err != nil {
return err
}
return deploy.DeployWithConfig(fakeFlagCtx, cfg, false)
}