-
Notifications
You must be signed in to change notification settings - Fork 221
/
restart.go
94 lines (79 loc) · 2.01 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
package machine
import (
"context"
"fmt"
"strings"
"time"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/flag"
mach "github.com/superfly/flyctl/internal/machine"
)
func newRestart() *cobra.Command {
const (
short = "Restart one or more Fly machines"
long = short + "\n"
usage = "restart <id> [<id>...]"
)
cmd := command.New(usage, short, long, runMachineRestart,
command.RequireSession,
command.LoadAppNameIfPresent,
)
cmd.Args = cobra.ArbitraryArgs
flag.Add(
cmd,
flag.App(),
flag.AppConfig(),
selectFlag,
flag.String{
Name: "signal",
Shorthand: "s",
Description: "Signal to stop the machine with (default: SIGINT)",
},
flag.Int{
Name: "time",
Description: "Seconds to wait before killing the machine",
},
flag.Bool{
Name: "force",
Description: "Force stop the machine(s)",
},
flag.Bool{
Name: "skip-health-checks",
Description: "Restarts app without waiting for health checks. ( Machines only )",
Default: false,
},
)
return cmd
}
func runMachineRestart(ctx context.Context) error {
var (
args = flag.Args(ctx)
timeout = flag.GetInt(ctx, "time")
)
// Resolve flags
input := &api.RestartMachineInput{
Timeout: time.Duration(timeout) * time.Second,
ForceStop: flag.GetBool(ctx, "force"),
SkipHealthChecks: flag.GetBool(ctx, "skip-health-checks"),
Signal: strings.ToUpper(flag.GetString(ctx, "signal")),
}
machines, ctx, err := selectManyMachines(ctx, args)
if err != nil {
return err
}
// Acquire leases
machines, releaseLeaseFunc, err := mach.AcquireLeases(ctx, machines)
defer releaseLeaseFunc(ctx, machines)
if err != nil {
return err
}
// Restart each machine
for _, machine := range machines {
if err := mach.Restart(ctx, machine, input, machine.LeaseNonce); err != nil {
return fmt.Errorf("failed to restart machine %s: %w", machine.ID, err)
}
}
return nil
}