-
Notifications
You must be signed in to change notification settings - Fork 240
/
update.go
164 lines (140 loc) · 3.89 KB
/
update.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
package machine
import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/iostreams"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/flyerr"
mach "github.com/superfly/flyctl/internal/machine"
"github.com/superfly/flyctl/internal/watch"
)
func newUpdate() *cobra.Command {
const (
short = "Update a machine"
long = short + "\n"
usage = "update [machine_id]"
)
cmd := command.New(usage, short, long, runUpdate,
command.RequireSession,
command.LoadAppNameIfPresent,
)
flag.Add(
cmd,
flag.Image(),
sharedFlags,
flag.Yes(),
selectFlag,
flag.Bool{
Name: "skip-health-checks",
Description: "Updates machine without waiting for health checks.",
Default: false,
},
flag.String{
Name: "command",
Shorthand: "C",
Description: "Command to run",
},
flag.String{
Name: "mount-point",
Description: "New volume mount point",
},
flag.Int{
Name: "wait-timeout",
Description: "Seconds to wait for individual machines to transition states and become healthy. (default 300)",
Default: 300,
},
)
cmd.Args = cobra.RangeArgs(0, 1)
return cmd
}
func runUpdate(ctx context.Context) (err error) {
var (
io = iostreams.FromContext(ctx)
colorize = io.ColorScheme()
autoConfirm = flag.GetBool(ctx, "yes")
skipHealthChecks = flag.GetBool(ctx, "skip-health-checks")
image = flag.GetString(ctx, "image")
dockerfile = flag.GetString(ctx, flag.Dockerfile().Name)
)
machineID := flag.FirstArg(ctx)
haveMachineID := len(flag.Args(ctx)) > 0
machine, ctx, err := selectOneMachine(ctx, nil, machineID, haveMachineID)
if err != nil {
return err
}
appName := appconfig.NameFromContext(ctx)
// Acquire lease
machine, releaseLeaseFunc, err := mach.AcquireLease(ctx, machine)
defer releaseLeaseFunc(ctx, machine)
if err != nil {
return err
}
var imageOrPath string
if image != "" {
imageOrPath = image
} else if dockerfile != "" {
imageOrPath = "."
}
// Identify configuration changes
machineConf, err := determineMachineConfig(ctx, &determineMachineConfigInput{
initialMachineConf: *machine.Config,
appName: appName,
imageOrPath: imageOrPath,
region: machine.Region,
updating: true,
})
if err != nil {
return err
}
if mp := flag.GetString(ctx, "mount-point"); mp != "" {
if len(machineConf.Mounts) != 1 {
return fmt.Errorf("Machine doesn't have a volume attached")
}
machineConf.Mounts[0].Path = mp
}
// Prompt user to confirm changes
if !autoConfirm {
confirmed, err := mach.ConfirmConfigChanges(ctx, machine, *machineConf, "")
if err != nil {
return err
}
if !confirmed {
fmt.Fprintf(io.Out, "No changes to apply\n")
return nil
}
}
// Perform update
input := &api.LaunchMachineInput{
Name: machine.Name,
Region: machine.Region,
Config: machineConf,
SkipLaunch: len(machineConf.Standbys) > 0,
SkipHealthChecks: skipHealthChecks,
Timeout: flag.GetInt(ctx, "wait-timeout"),
}
if err := mach.Update(ctx, machine, input); err != nil {
var timeoutErr mach.WaitTimeoutErr
if errors.As(err, &timeoutErr) {
return flyerr.GenericErr{
Err: timeoutErr.Error(),
Descript: timeoutErr.Description(),
Suggest: "Try increasing the --wait-timeout",
}
}
return err
}
if !(input.SkipLaunch || flag.GetDetach(ctx)) {
fmt.Fprintln(io.Out, colorize.Green("==> "+"Monitoring health checks"))
if err := watch.MachinesChecks(ctx, []*api.Machine{machine}); err != nil {
return err
}
fmt.Fprintln(io.Out)
}
fmt.Fprintf(io.Out, "\nMonitor machine status here:\nhttps://fly.io/apps/%s/machines/%s\n", appName, machine.ID)
return nil
}