/
deploy.go
489 lines (432 loc) · 15.1 KB
/
deploy.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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
package deploy
import (
"context"
"fmt"
"strconv"
"strings"
"time"
"github.com/logrusorgru/aurora"
"github.com/spf13/cobra"
fly "github.com/superfly/fly-go"
"github.com/superfly/fly-go/flaps"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/build/imgsrc"
"github.com/superfly/flyctl/internal/cmdutil"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/config"
"github.com/superfly/flyctl/internal/ctrlc"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/flapsutil"
"github.com/superfly/flyctl/internal/metrics"
"github.com/superfly/flyctl/internal/render"
"github.com/superfly/flyctl/internal/sentry"
"github.com/superfly/flyctl/internal/tracing"
"github.com/superfly/flyctl/iostreams"
"go.opentelemetry.io/otel/attribute"
)
var defaultMaxConcurrent = 16
var CommonFlags = flag.Set{
flag.Region(),
flag.Image(),
flag.Now(),
flag.RemoteOnly(false),
flag.LocalOnly(),
flag.Push(),
flag.Wireguard(),
flag.Detach(),
flag.Strategy(),
flag.Dockerfile(),
flag.Ignorefile(),
flag.ImageLabel(),
flag.BuildArg(),
flag.BuildSecret(),
flag.BuildTarget(),
flag.NoCache(),
flag.Nixpacks(),
flag.BuildOnly(),
flag.BpDockerHost(),
flag.BpVolume(),
flag.Yes(),
flag.VMSizeFlags,
flag.Bool{
Name: "provision-extensions",
Description: "Provision any extensions assigned as a default to first deployments",
},
flag.StringArray{
Name: "env",
Shorthand: "e",
Description: "Set of environment variables in the form of NAME=VALUE pairs. Can be specified multiple times.",
},
flag.String{
Name: "wait-timeout",
Description: "Time duration to wait for individual machines to transition states and become healthy.",
Default: DefaultWaitTimeout.String(),
},
flag.String{
Name: "release-command-timeout",
Description: "Time duration to wait for a release command finish running, or 'none' to disable.",
Default: DefaultReleaseCommandTimeout.String(),
},
flag.String{
Name: "lease-timeout",
Description: "Time duration to lease individual machines while running deployment." +
" All machines are leased at the beginning and released at the end." +
"The lease is refreshed periodically for this same time, which is why it is short." +
"flyctl releases leases in most cases.",
Default: DefaultLeaseTtl.String(),
},
flag.Bool{
Name: "force-machines",
Description: "Use the Apps v2 platform built with Machines",
Default: false,
Hidden: true,
},
flag.Bool{
Name: "ha",
Description: "Create spare machines that increases app availability",
Default: true,
},
flag.Bool{
Name: "smoke-checks",
Description: "Perform smoke checks during deployment",
Default: true,
},
flag.Bool{
Name: "dns-checks",
Description: "Perform DNS checks during deployment",
Default: true,
},
flag.Float64{
Name: "max-unavailable",
Description: "Max number of unavailable machines during rolling updates. A number between 0 and 1 means percent of total machines",
Default: DefaultMaxUnavailable,
},
flag.Bool{
Name: "no-public-ips",
Description: "Do not allocate any new public IP addresses",
},
flag.StringArray{
Name: "file-local",
Description: "Set of files in the form of /path/inside/machine=<local/path> pairs. Can be specified multiple times.",
},
flag.StringArray{
Name: "file-literal",
Description: "Set of literals in the form of /path/inside/machine=VALUE pairs where VALUE is the content. Can be specified multiple times.",
},
flag.StringArray{
Name: "file-secret",
Description: "Set of secrets in the form of /path/inside/machine=SECRET pairs where SECRET is the name of the secret. Can be specified multiple times.",
},
flag.StringSlice{
Name: "exclude-regions",
Description: "Deploy to all machines except machines in these regions. Multiple regions can be specified with comma separated values or by providing the flag multiple times. --exclude-regions iad,sea --exclude-regions syd will exclude all three iad, sea, and syd regions. Applied after --only-regions. V2 machines platform only.",
},
flag.StringSlice{
Name: "only-regions",
Description: "Deploy to machines only in these regions. Multiple regions can be specified with comma separated values or by providing the flag multiple times. --only-regions iad,sea --only-regions syd will deploy to all three iad, sea, and syd regions. Applied before --exclude-regions. V2 machines platform only.",
},
flag.StringArray{
Name: "label",
Description: "Add custom metadata to an image via docker labels",
},
flag.Int{
Name: "max-concurrent",
Description: "Maximum number of machines to operate on concurrently.",
Default: defaultMaxConcurrent,
},
flag.Int{
Name: "immediate-max-concurrent",
Description: "Maximum number of machines to update concurrently when using the immediate deployment strategy.",
Default: defaultMaxConcurrent,
Hidden: true,
},
flag.Int{
Name: "volume-initial-size",
Description: "The initial size in GB for volumes created on first deploy",
},
flag.StringSlice{
Name: "process-groups",
Description: "Deploy to machines only in these process groups",
},
flag.String{
Name: "signal",
Shorthand: "s",
Description: "Signal to stop the machine with for bluegreen strategy (default: SIGINT)",
},
}
func New() (cmd *cobra.Command) {
const (
long = `Deploy Fly applications from source or an image using a local or remote builder.
To disable colorized output and show full Docker build output, set the environment variable NO_COLOR=1.
`
short = "Deploy Fly applications"
)
cmd = command.New("deploy [WORKING_DIRECTORY]", short, long, run,
command.RequireSession,
command.ChangeWorkingDirectoryToFirstArgIfPresent,
command.RequireAppName,
)
cmd.Args = cobra.MaximumNArgs(1)
flag.Add(cmd,
CommonFlags,
flag.App(),
flag.AppConfig(),
// Not in CommonFlags because it's not relevant to a first deploy
flag.Bool{
Name: "update-only",
Description: "Do not create Machines for new process groups",
Default: false,
},
)
return
}
func run(ctx context.Context) error {
io := iostreams.FromContext(ctx)
appName := appconfig.NameFromContext(ctx)
hook := ctrlc.Hook(func() {
metrics.FlushMetrics(ctx)
})
defer hook.Done()
tp, err := tracing.InitTraceProvider(ctx, appName)
if err != nil {
fmt.Fprintf(io.ErrOut, "failed to initialize tracing library: =%v", err)
return err
}
defer tp.Shutdown(ctx)
flapsClient, err := flapsutil.NewClientWithOptions(ctx, flaps.NewClientOpts{
AppName: appName,
})
if err != nil {
return fmt.Errorf("could not create flaps client: %w", err)
}
ctx = flaps.NewContext(ctx, flapsClient)
client := fly.ClientFromContext(ctx)
ctx, span := tracing.CMDSpan(ctx, "cmd.deploy")
defer span.End()
user, err := client.GetCurrentUser(ctx)
if err != nil {
return fmt.Errorf("failed retrieving current user: %w", err)
}
span.SetAttributes(attribute.String("user.id", user.ID))
appConfig, err := determineAppConfig(ctx)
if err != nil {
if strings.Contains(err.Error(), "Could not find App") {
return fmt.Errorf("the app name %s could not be found, did you create the app or misspell it in the fly.toml file or via -a?", appName)
}
return err
}
var gpuKinds, cpuKinds []string
for _, compute := range appConfig.Compute {
if compute != nil && compute.MachineGuest != nil {
gpuKinds = append(gpuKinds, compute.MachineGuest.GPUKind)
cpuKinds = append(cpuKinds, compute.MachineGuest.CPUKind)
}
}
span.SetAttributes(attribute.StringSlice("gpu.kinds", gpuKinds))
span.SetAttributes(attribute.StringSlice("cpu.kinds", cpuKinds))
return DeployWithConfig(ctx, appConfig, flag.GetYes(ctx))
}
func DeployWithConfig(ctx context.Context, appConfig *appconfig.Config, forceYes bool) (err error) {
io := iostreams.FromContext(ctx)
appName := appconfig.NameFromContext(ctx)
apiClient := fly.ClientFromContext(ctx)
appCompact, err := apiClient.GetAppCompact(ctx, appName)
if err != nil {
return err
}
for env := range appConfig.Env {
if containsCommonSecretSubstring(env) {
warning := fmt.Sprintf("%s %s may be a potentially sensitive environment variable. Consider setting it as a secret, and removing it from the [env] section: https://fly.io/docs/reference/secrets/\n", aurora.Yellow("WARN"), env)
fmt.Fprintln(io.ErrOut, warning)
}
}
// Fetch an image ref or build from source to get the final image reference to deploy
img, err := determineImage(ctx, appConfig)
if err != nil {
return fmt.Errorf("failed to fetch an image or build from source: %w", err)
}
if flag.GetBuildOnly(ctx) {
return nil
}
fmt.Fprintf(io.Out, "\nWatch your deployment at https://fly.io/apps/%s/monitoring\n\n", appName)
if err := deployToMachines(ctx, appConfig, appCompact, img); err != nil {
return err
}
if appURL := appConfig.URL(); appURL != nil {
fmt.Fprintf(io.Out, "\nVisit your newly deployed app at %s\n", appURL)
}
return err
}
func parseDurationFlag(ctx context.Context, flagName string) (*time.Duration, error) {
if !flag.IsSpecified(ctx, flagName) {
return nil, nil
}
v := flag.GetString(ctx, flagName)
if v == "none" {
d := time.Duration(0)
return &d, nil
}
duration, err := time.ParseDuration(v)
if err == nil {
return &duration, nil
}
if strings.Contains(err.Error(), "missing unit in duration") {
asInt, err := strconv.Atoi(v)
if err == nil {
duration = time.Duration(asInt) * time.Second
return &duration, nil
}
}
return nil, fmt.Errorf("invalid duration value %v used for --%s flag: valid options are a number of seconds, number with time unit (i.e.: 5m, 180s) or 'none'", v, flagName)
}
// in a rare twist, the guest param takes precedence over CLI flags!
func deployToMachines(
ctx context.Context,
cfg *appconfig.Config,
app *fly.AppCompact,
img *imgsrc.DeploymentImage,
) (err error) {
// It's important to push appConfig into context because MachineDeployment will fetch it from there
ctx = appconfig.WithConfig(ctx, cfg)
metrics.Started(ctx, "deploy_machines")
defer func() {
metrics.Status(ctx, "deploy_machines", err == nil)
}()
releaseCmdTimeout, err := parseDurationFlag(ctx, "release-command-timeout")
if err != nil {
return err
}
waitTimeout, err := parseDurationFlag(ctx, "wait-timeout")
if err != nil {
return err
}
leaseTimeout, err := parseDurationFlag(ctx, "lease-timeout")
if err != nil {
return err
}
files, err := command.FilesFromCommand(ctx)
if err != nil {
return err
}
guest, err := flag.GetMachineGuest(ctx, nil)
if err != nil {
return err
}
excludeRegions := make(map[string]interface{})
for _, r := range flag.GetStringSlice(ctx, "exclude-regions") {
reg := strings.TrimSpace(r)
if reg != "" {
excludeRegions[reg] = struct{}{}
}
}
onlyRegions := make(map[string]interface{})
for _, r := range flag.GetStringSlice(ctx, "only-regions") {
reg := strings.TrimSpace(r)
if reg != "" {
onlyRegions[reg] = struct{}{}
}
}
// We default the flag to 0.33 so that --help can show the actual default value,
// but internally we want to differentiate between the flag being specified and not.
// We use 0.0 to denote unspecified, as that value is invalid for maxUnavailable.
var maxUnavailable *float64 = nil
if flag.IsSpecified(ctx, "max-unavailable") {
maxUnavailable = fly.Pointer(flag.GetFloat64(ctx, "max-unavailable"))
// Validation to ensure that 0.0 is *purely* the "unspecified" value
if *maxUnavailable <= 0 {
return fmt.Errorf("the value for --max-unavailable must be > 0")
}
}
processGroups := make(map[string]interface{})
for _, r := range flag.GetStringSlice(ctx, "process-groups") {
reg := strings.TrimSpace(r)
if reg != "" {
processGroups[reg] = struct{}{}
}
}
maxConcurrent := flag.GetInt(ctx, "max-concurrent")
immediateMaxConcurrent := flag.GetInt(ctx, "immediate-max-concurrent")
if maxConcurrent == defaultMaxConcurrent && immediateMaxConcurrent != defaultMaxConcurrent {
maxConcurrent = immediateMaxConcurrent
}
md, err := NewMachineDeployment(ctx, MachineDeploymentArgs{
AppCompact: app,
DeploymentImage: img.Tag,
Strategy: flag.GetString(ctx, "strategy"),
EnvFromFlags: flag.GetStringArray(ctx, "env"),
PrimaryRegionFlag: cfg.PrimaryRegion,
SkipSmokeChecks: flag.GetDetach(ctx) || !flag.GetBool(ctx, "smoke-checks"),
SkipHealthChecks: flag.GetDetach(ctx),
SkipDNSChecks: flag.GetDetach(ctx) || !flag.GetBool(ctx, "dns-checks"),
WaitTimeout: waitTimeout,
StopSignal: flag.GetString(ctx, "signal"),
ReleaseCmdTimeout: releaseCmdTimeout,
LeaseTimeout: leaseTimeout,
MaxUnavailable: maxUnavailable,
Guest: guest,
IncreasedAvailability: flag.GetBool(ctx, "ha"),
AllocPublicIP: !flag.GetBool(ctx, "no-public-ips"),
UpdateOnly: flag.GetBool(ctx, "update-only"),
Files: files,
ExcludeRegions: excludeRegions,
OnlyRegions: onlyRegions,
MaxConcurrent: maxConcurrent,
VolumeInitialSize: flag.GetInt(ctx, "volume-initial-size"),
ProcessGroups: processGroups,
})
if err != nil {
sentry.CaptureExceptionWithAppInfo(ctx, err, "deploy", app)
return err
}
err = md.DeployMachinesApp(ctx)
if err != nil {
sentry.CaptureExceptionWithAppInfo(ctx, err, "deploy", app)
}
return err
}
// determineAppConfig fetches the app config from a local file, or in its absence, from the API
func determineAppConfig(ctx context.Context) (cfg *appconfig.Config, err error) {
io := iostreams.FromContext(ctx)
tb := render.NewTextBlock(ctx, "Verifying app config")
appName := appconfig.NameFromContext(ctx)
ctx, span := tracing.GetTracer().Start(ctx, "get_app_config")
defer span.End()
if cfg = appconfig.ConfigFromContext(ctx); cfg == nil {
cfg, err = appconfig.FromRemoteApp(ctx, appName)
if err != nil {
tracing.RecordError(span, err, "get config from remote")
return nil, err
}
}
if env := flag.GetStringArray(ctx, "env"); len(env) > 0 {
parsedEnv, err := cmdutil.ParseKVStringsToMap(env)
if err != nil {
tracing.RecordError(span, err, "parse env")
return nil, fmt.Errorf("failed parsing environment: %w", err)
}
cfg.SetEnvVariables(parsedEnv)
}
// FIXME: this is a confusing flag; I thought it meant only update machines in the provided region, which resulted in a minor disaster :-)
if v := flag.GetRegion(ctx); v != "" {
cfg.PrimaryRegion = v
}
// Always prefer the app name passed via --app
if appName != "" {
cfg.AppName = appName
}
err, extraInfo := cfg.Validate(ctx)
if extraInfo != "" {
fmt.Fprint(io.Out, extraInfo)
}
if err != nil {
tracing.RecordError(span, err, "validate config")
return nil, err
}
if cfg.Deploy != nil && cfg.Deploy.Strategy != "rolling" && cfg.Deploy.Strategy != "canary" && cfg.Deploy.MaxUnavailable != nil {
if !config.FromContext(ctx).JSONOutput {
fmt.Fprintf(io.Out, "Warning: max-unavailable set for non-rolling strategy '%s', ignoring\n", cfg.Deploy.Strategy)
}
}
tb.Done("Verified app config")
return cfg, nil
}