-
Notifications
You must be signed in to change notification settings - Fork 224
/
stop.go
90 lines (76 loc) · 1.92 KB
/
stop.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
package machine
import (
"context"
"fmt"
"strconv"
"syscall"
"time"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/flaps"
"github.com/superfly/flyctl/internal/app"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/iostreams"
)
func newStop() *cobra.Command {
const (
short = "Stop a Fly machine"
long = short + "\n"
usage = "stop <id>"
)
cmd := command.New(usage, short, long, runMachineStop,
command.RequireSession,
command.LoadAppNameIfPresent,
)
cmd.Args = cobra.MaximumNArgs(1)
flag.Add(
cmd,
flag.App(),
flag.AppConfig(),
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",
},
)
return cmd
}
func runMachineStop(ctx context.Context) (err error) {
var (
args = flag.Args(ctx)
out = iostreams.FromContext(ctx).Out
appName = app.NameFromContext(ctx)
)
for _, arg := range args {
signal := api.Signal{}
if flag.GetString(ctx, "signal") != "" {
s, err := strconv.Atoi(flag.GetString(ctx, "signal"))
if err != nil {
return fmt.Errorf("could not get signal %s", err)
}
signal.Signal = syscall.Signal(s)
}
machineStopInput := api.StopMachineInput{
ID: arg,
Signal: signal,
Timeout: time.Duration(flag.GetInt(ctx, "time")),
Filters: &api.Filters{},
}
app, err := appFromMachineOrName(ctx, arg, appName)
flapsClient, err := flaps.New(ctx, app)
if err != nil {
return fmt.Errorf("could not make flaps client: %w", err)
}
err = flapsClient.Stop(ctx, machineStopInput)
if err != nil {
return fmt.Errorf("could not stop machine %s: %w", machineStopInput.ID, err)
}
fmt.Fprintf(out, "%s has been successfully stopped\n", machineStopInput.ID)
}
return
}