-
Notifications
You must be signed in to change notification settings - Fork 239
/
restart.go
118 lines (94 loc) · 2.57 KB
/
restart.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
package apps
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/flaps"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/machine"
"github.com/superfly/flyctl/iostreams"
)
func newRestart() *cobra.Command {
const (
long = `The APPS RESTART command will perform a rolling restart against all running VMs`
short = "Restart an application"
usage = "restart <APPNAME>"
)
cmd := command.New(usage, short, long, runRestart,
command.RequireSession,
)
cmd.Args = cobra.ExactArgs(1)
// Note -
flag.Add(cmd,
flag.Bool{
Name: "force-stop",
Description: "Performs a force stop against the target Machine. ( Machines only )",
Default: false,
},
flag.Bool{
Name: "skip-health-checks",
Description: "Restarts app without waiting for health checks. ( Machines only )",
Default: false,
},
)
return cmd
}
func runRestart(ctx context.Context) error {
var (
appName = flag.FirstArg(ctx)
client = client.FromContext(ctx).API()
)
app, err := client.GetAppCompact(ctx, appName)
if err != nil {
return err
}
if app.IsPostgresApp() {
return fmt.Errorf("postgres apps should use `fly pg restart` instead")
}
ctx, err = BuildContext(ctx, app)
if err != nil {
return err
}
if app.PlatformVersion == "machines" {
return runMachinesRestart(ctx, app)
}
return runNomadRestart(ctx, app)
}
func runNomadRestart(ctx context.Context, app *api.AppCompact) error {
client := client.FromContext(ctx).API()
if _, err := client.RestartApp(ctx, app.Name); err != nil {
return fmt.Errorf("failed restarting app: %w", err)
}
io := iostreams.FromContext(ctx)
fmt.Fprintf(io.Out, "%s is being restarted\n", app.Name)
return nil
}
func runMachinesRestart(ctx context.Context, app *api.AppCompact) error {
input := &api.RestartMachineInput{
ForceStop: flag.GetBool(ctx, "force-stop"),
SkipHealthChecks: flag.GetBool(ctx, "skip-health-checks"),
}
// Rolling restart against exclusively the machines managed by the Apps platform
flapsClient, err := flaps.New(ctx, app)
if err != nil {
return err
}
machines, _, err := flapsClient.ListFlyAppsMachines(ctx)
if err != nil {
return err
}
machines, releaseFunc, err := machine.AcquireLeases(ctx, machines)
defer releaseFunc(ctx, machines)
if err != nil {
return err
}
for _, m := range machines {
if err := machine.Restart(ctx, m, input, m.LeaseNonce); err != nil {
return err
}
}
return nil
}