-
Notifications
You must be signed in to change notification settings - Fork 218
/
machines.go
841 lines (764 loc) · 28.2 KB
/
machines.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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
package deploy
import (
"context"
"fmt"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/prompt"
"strconv"
"strings"
"time"
"github.com/Khan/genqlient/graphql"
"github.com/google/shlex"
"github.com/morikuni/aec"
"github.com/samber/lo"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/flaps"
"github.com/superfly/flyctl/gql"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/build/imgsrc"
"github.com/superfly/flyctl/internal/cmdutil"
machcmd "github.com/superfly/flyctl/internal/command/machine"
"github.com/superfly/flyctl/internal/logger"
"github.com/superfly/flyctl/internal/machine"
"github.com/superfly/flyctl/iostreams"
"github.com/superfly/flyctl/terminal"
)
const (
DefaultWaitTimeout = 120 * time.Second
DefaultLeaseTtl = 13 * time.Second
)
type MachineDeployment interface {
DeployMachinesApp(context.Context) error
}
type ProcessGroupMachineDiff struct {
machinesToRemove []machine.LeasableMachine
groupsToRemove map[string]int
groupsNeedingMachines map[string]*appconfig.ProcessConfig
}
type MachineDeploymentArgs struct {
AppCompact *api.AppCompact
DeploymentImage *imgsrc.DeploymentImage
Strategy string
EnvFromFlags []string
PrimaryRegionFlag string
BuildOnly bool
SkipHealthChecks bool
RestartOnly bool
WaitTimeout time.Duration
LeaseTimeout time.Duration
}
type machineDeployment struct {
apiClient *api.Client
gqlClient graphql.Client
flapsClient *flaps.Client
io *iostreams.IOStreams
colorize *iostreams.ColorScheme
app *api.AppCompact
appConfig *appconfig.Config
processConfigs map[string]*appconfig.ProcessConfig
img *imgsrc.DeploymentImage
machineSet machine.MachineSet
releaseCommandMachine machine.MachineSet
releaseCommand []string
volumeDestination string
volumes []api.Volume
strategy string
releaseId string
releaseVersion int
skipHealthChecks bool
restartOnly bool
waitTimeout time.Duration
leaseTimeout time.Duration
leaseDelayBetween time.Duration
}
func NewMachineDeployment(ctx context.Context, args MachineDeploymentArgs) (MachineDeployment, error) {
if !args.RestartOnly && args.DeploymentImage == nil {
return nil, fmt.Errorf("BUG: machines deployment created without specifying the image")
}
if args.RestartOnly && args.DeploymentImage != nil {
return nil, fmt.Errorf("BUG: restartOnly machines deployment created and specified an image")
}
appConfig, err := determineAppConfigForMachines(ctx, args.EnvFromFlags, args.PrimaryRegionFlag)
if err != nil {
return nil, err
}
err, _ = appConfig.Validate(ctx)
if err != nil {
return nil, err
}
if args.AppCompact == nil {
return nil, fmt.Errorf("BUG: args.AppCompact should be set when calling this method")
}
flapsClient, err := flaps.New(ctx, args.AppCompact)
if err != nil {
return nil, err
}
var releaseCmd []string
if appConfig.Deploy != nil {
releaseCmd, err = shlex.Split(appConfig.Deploy.ReleaseCommand)
if err != nil {
return nil, err
}
}
waitTimeout := args.WaitTimeout
if waitTimeout == 0 {
waitTimeout = DefaultWaitTimeout
}
leaseTimeout := args.LeaseTimeout
if leaseTimeout == 0 {
leaseTimeout = DefaultLeaseTtl
}
leaseDelayBetween := (leaseTimeout - 1*time.Second) / 3
if waitTimeout != DefaultWaitTimeout || leaseTimeout != DefaultLeaseTtl || args.WaitTimeout == 0 || args.LeaseTimeout == 0 {
terminal.Infof("Using wait timeout: %s lease timeout: %s delay between lease refreshes: %s\n", waitTimeout, leaseTimeout, leaseDelayBetween)
}
processConfigs, err := appConfig.GetProcessConfigs()
if err != nil {
return nil, err
}
io := iostreams.FromContext(ctx)
apiClient := client.FromContext(ctx).API()
md := &machineDeployment{
apiClient: apiClient,
gqlClient: apiClient.GenqClient,
flapsClient: flapsClient,
io: io,
colorize: io.ColorScheme(),
app: args.AppCompact,
appConfig: appConfig,
processConfigs: processConfigs,
img: args.DeploymentImage,
skipHealthChecks: args.SkipHealthChecks,
restartOnly: args.RestartOnly,
waitTimeout: waitTimeout,
leaseTimeout: leaseTimeout,
leaseDelayBetween: leaseDelayBetween,
releaseCommand: releaseCmd,
}
err = md.setStrategy(args.Strategy)
if err != nil {
return nil, err
}
err = md.setMachinesForDeployment(ctx)
if err != nil {
return nil, err
}
err = md.setVolumeConfig(ctx)
if err != nil {
return nil, err
}
/* // TODO(ali): Remove this
err = md.validateProcessesConfig()
if err != nil {
return nil, err
}
*/
err = md.validateVolumeConfig()
if err != nil {
return nil, err
}
err = md.provisionIpsOnFirstDeploy(ctx)
if err != nil {
return nil, err
}
err = md.createReleaseInBackend(ctx)
if err != nil {
return nil, err
}
return md, nil
}
func (md *machineDeployment) runReleaseCommand(ctx context.Context) error {
if len(md.releaseCommand) == 0 || md.restartOnly {
return nil
}
io := iostreams.FromContext(ctx)
fmt.Fprintf(io.ErrOut, "Running %s release_command: %s\n",
md.colorize.Bold(md.app.Name),
md.appConfig.Deploy.ReleaseCommand,
)
err := md.createOrUpdateReleaseCmdMachine(ctx)
if err != nil {
return fmt.Errorf("error running release_command machine: %w", err)
}
releaseCmdMachine := md.releaseCommandMachine.GetMachines()[0]
// FIXME: consolidate this wait stuff with deploy waits? Especially once we improve the outpu
err = releaseCmdMachine.WaitForState(ctx, api.MachineStateStarted, md.waitTimeout)
if err != nil {
return fmt.Errorf("error waiting for release_command machine %s to start: %w", releaseCmdMachine.Machine().ID, err)
}
err = releaseCmdMachine.WaitForState(ctx, api.MachineStateDestroyed, md.waitTimeout)
if err != nil {
return fmt.Errorf("error waiting for release_command machine %s to finish running: %w", releaseCmdMachine.Machine().ID, err)
}
lastExitEvent, err := releaseCmdMachine.WaitForEventTypeAfterType(ctx, "exit", "start", md.waitTimeout)
if err != nil {
return fmt.Errorf("error finding the release_command machine %s exit event: %w", releaseCmdMachine.Machine().ID, err)
}
exitCode, err := lastExitEvent.Request.GetExitCode()
if err != nil {
return fmt.Errorf("error get release_command machine %s exit code: %w", releaseCmdMachine.Machine().ID, err)
}
if exitCode != 0 {
fmt.Fprintf(md.io.ErrOut, "Error release_command failed running on machine %s with exit code %s. Check the logs at: https://fly.io/apps/%s/monitoring\n",
md.colorize.Bold(releaseCmdMachine.Machine().ID), md.colorize.Red(strconv.Itoa(exitCode)), md.app.Name)
return fmt.Errorf("error release_command machine %s exited with non-zero status of %d", releaseCmdMachine.Machine().ID, exitCode)
}
md.logClearLinesAbove(1)
fmt.Fprintf(md.io.ErrOut, " release_command %s completed successfully\n", md.colorize.Bold(releaseCmdMachine.Machine().ID))
return nil
}
func (md *machineDeployment) resolveProcessGroupMachineChanges() ProcessGroupMachineDiff {
output := ProcessGroupMachineDiff{
groupsToRemove: map[string]int{},
groupsNeedingMachines: map[string]*appconfig.ProcessConfig{},
}
groupHasMachine := map[string]bool{}
for _, leasableMachine := range md.machineSet.GetMachines() {
mach := leasableMachine.Machine()
machGroup := mach.ProcessGroup()
groupMatch := ""
for name := range md.processConfigs {
if machGroup == name {
groupMatch = machGroup
break
}
}
if groupMatch == "" {
output.groupsToRemove[groupMatch] += 1
output.machinesToRemove = append(output.machinesToRemove, leasableMachine)
} else {
groupHasMachine[groupMatch] = true
}
}
for name, val := range md.processConfigs {
if hasMach, ok := groupHasMachine[name]; !ok || !hasMach {
output.groupsNeedingMachines[name] = val
}
}
return output
}
func (md *machineDeployment) promptConfirmProcessGroupChanges(ctx context.Context, diff ProcessGroupMachineDiff) (bool, error) {
var (
io = iostreams.FromContext(ctx)
colorize = io.ColorScheme()
willAddMachines = len(diff.groupsNeedingMachines) != 0
willRemoveMachines = diff.machinesToRemove != nil
)
if willAddMachines || willRemoveMachines {
fmt.Fprintln(io.ErrOut, "Process groups have changed. This will:")
}
// TODO(ali): Figure out why the bullets aren't colored
if willRemoveMachines {
bullet := colorize.Red("*")
for grp, numMach := range diff.groupsToRemove {
pluralS := lo.Ternary(numMach == 1, "", "s")
fmt.Fprintf(io.ErrOut, " %s remove %d \"%s\" machine%s\n", bullet, numMach, grp, pluralS)
}
}
if willAddMachines {
bullet := colorize.Green("*")
for name := range diff.groupsNeedingMachines {
fmt.Fprintf(io.ErrOut, " %s create 1 \"%s\" machine\n", bullet, name)
}
}
if !flag.GetBool(ctx, flag.NowName) {
switch confirmed, err := prompt.Confirm(ctx, "Accept these changes? "); {
case err == nil:
return confirmed, nil
case prompt.IsNonInteractive(err):
return false, prompt.NonInteractiveError("yes flag must be specified when not running interactively")
default:
return false, err
}
}
return true, nil
}
func (md *machineDeployment) spawnMachineInGroup(ctx context.Context, groupName string) error {
fmt.Fprintf(md.io.Out, "No machines in group '%s', launching one new machine\n", md.colorize.Bold(groupName))
machBase := &api.Machine{
Region: md.appConfig.PrimaryRegion,
Config: &api.MachineConfig{
Metadata: map[string]string{
api.MachineConfigMetadataKeyFlyProcessGroup: groupName,
},
},
}
launchInput := md.resolveUpdatedMachineConfig(machBase, false)
newMachineRaw, err := md.flapsClient.Launch(ctx, *launchInput)
newMachine := machine.NewLeasableMachine(md.flapsClient, md.io, newMachineRaw)
if err != nil {
return fmt.Errorf("error creating a new machine machine: %w", err)
}
// FIXME: dry this up with release commands and non-empty update
fmt.Fprintf(md.io.ErrOut, " Created release_command machine %s\n", md.colorize.Bold(newMachineRaw.ID))
if md.strategy != "immediate" {
err := newMachine.WaitForState(ctx, api.MachineStateStarted, md.waitTimeout)
if err != nil {
return err
}
}
if md.strategy != "immediate" && !md.skipHealthChecks {
err := newMachine.WaitForHealthchecksToPass(ctx, md.waitTimeout)
// FIXME: combine this wait with the wait for start as one update line (or two per in noninteractive case)
if err != nil {
return err
} else {
md.logClearLinesAbove(1)
fmt.Fprintf(md.io.ErrOut, " Machine %s update finished: %s\n",
md.colorize.Bold(newMachine.FormattedMachineId()),
md.colorize.Green("success"),
)
}
}
fmt.Fprintf(md.io.ErrOut, " Finished deploying\n")
return nil
}
func (md *machineDeployment) DeployMachinesApp(ctx context.Context) error {
err := md.runReleaseCommand(ctx)
if err != nil {
return fmt.Errorf("release command failed - aborting deployment. %w", err)
}
if md.machineSet.IsEmpty() {
for name := range md.processConfigs {
if err := md.spawnMachineInGroup(ctx, name); err != nil {
return err
}
}
return nil
}
err = md.machineSet.AcquireLeases(ctx, md.leaseTimeout)
defer func() {
err := md.machineSet.ReleaseLeases(ctx)
if err != nil {
terminal.Warnf("error releasing leases on machines: %v\n", err)
}
}()
if err != nil {
return err
}
md.machineSet.StartBackgroundLeaseRefresh(ctx, md.leaseTimeout, md.leaseDelayBetween)
processGroupMachineDiff := md.resolveProcessGroupMachineChanges()
cont, err := md.promptConfirmProcessGroupChanges(ctx, processGroupMachineDiff)
if err != nil {
return err
}
if !cont {
// TODO(ali): is this happening too late to properly abort?
fmt.Fprintln(md.io.Out, "Aborted.")
return nil
}
// TODO(ali): Why do I have to do this?
ctx = flaps.NewContext(ctx, md.flapsClient)
// Destroy machines that don't fit the current process groups
if err := md.machineSet.RemoveMachines(ctx, processGroupMachineDiff.machinesToRemove); err != nil {
return err
}
for _, mach := range processGroupMachineDiff.machinesToRemove {
if err := machcmd.Destroy(ctx, md.app, mach.Machine().ID, true); err != nil {
return err
}
}
// Create machines for new process groups
for name := range processGroupMachineDiff.groupsNeedingMachines {
if err := md.spawnMachineInGroup(ctx, name); err != nil {
return err
}
}
// FIXME: handle deploy strategy: rolling, immediate, canary, bluegreen
fmt.Fprintf(md.io.Out, "Deploying %s app with %s strategy\n", md.colorize.Bold(md.app.Name), md.strategy)
for _, m := range md.machineSet.GetMachines() {
launchInput := md.resolveUpdatedMachineConfig(m.Machine(), false)
fmt.Fprintf(md.io.ErrOut, " Updating %s\n", md.colorize.Bold(m.FormattedMachineId()))
err := m.Update(ctx, *launchInput)
if err != nil {
if md.strategy != "immediate" {
return err
} else {
fmt.Printf("Continuing after error: %s\n", err)
}
}
if md.strategy != "immediate" {
err := m.WaitForState(ctx, api.MachineStateStarted, md.waitTimeout)
if err != nil {
return err
}
}
if md.strategy != "immediate" && !md.skipHealthChecks {
err := m.WaitForHealthchecksToPass(ctx, md.waitTimeout)
// FIXME: combine this wait with the wait for start as one update line (or two per in noninteractive case)
if err != nil {
return err
} else {
md.logClearLinesAbove(1)
fmt.Fprintf(md.io.ErrOut, " Machine %s update finished: %s\n",
md.colorize.Bold(m.FormattedMachineId()),
md.colorize.Green("success"),
)
}
}
}
fmt.Fprintf(md.io.ErrOut, " Finished deploying\n")
return nil
}
func (md *machineDeployment) setMachinesForDeployment(ctx context.Context) error {
machines, releaseCmdMachine, err := md.flapsClient.ListFlyAppsMachines(ctx)
if err != nil {
return err
}
// migrate non-platform machines into fly platform
if len(machines) == 0 {
terminal.Debug("Found no machines that are part of Fly Apps Platform. Checking for active machines...")
activeMachines, err := md.flapsClient.ListActive(ctx)
if err != nil {
return err
}
if len(activeMachines) > 0 {
return fmt.Errorf(
"found %d machines that are unmanaged. `fly deploy` only updates machines with %s=%s in their metadata. Use `fly machine list` to list machines and `fly machine update --metadata %s=%s` to update individual machines with the metadata. Once done, `fly deploy` will update machines with the metadata based on your %s app configuration",
len(activeMachines),
api.MachineConfigMetadataKeyFlyPlatformVersion,
api.MachineFlyPlatformVersion2,
api.MachineConfigMetadataKeyFlyPlatformVersion,
api.MachineFlyPlatformVersion2,
appconfig.DefaultConfigFileName,
)
}
}
md.machineSet = machine.NewMachineSet(md.flapsClient, md.io, machines)
var releaseCmdSet []*api.Machine
if releaseCmdMachine != nil {
releaseCmdSet = []*api.Machine{releaseCmdMachine}
}
md.releaseCommandMachine = machine.NewMachineSet(md.flapsClient, md.io, releaseCmdSet)
return nil
}
func (md *machineDeployment) createOrUpdateReleaseCmdMachine(ctx context.Context) error {
if md.releaseCommandMachine.IsEmpty() {
return md.createReleaseCommandMachine(ctx)
} else {
return md.updateReleaseCommandMachine(ctx)
}
}
func (md *machineDeployment) configureLaunchInputForReleaseCommand(launchInput *api.LaunchMachineInput) *api.LaunchMachineInput {
launchInput.Config.Init.Cmd = md.releaseCommand
launchInput.Config.Metadata[api.MachineConfigMetadataKeyFlyProcessGroup] = api.MachineProcessGroupFlyAppReleaseCommand
launchInput.Config.Restart = api.MachineRestart{
Policy: api.MachineRestartPolicyNo,
}
launchInput.Config.AutoDestroy = true
launchInput.Config.DNS = &api.DNSConfig{SkipRegistration: true}
if md.appConfig.PrimaryRegion != "" {
launchInput.Region = md.appConfig.PrimaryRegion
}
if _, present := launchInput.Config.Env["RELEASE_COMMAND"]; !present {
launchInput.Config.Env["RELEASE_COMMAND"] = "1"
}
return launchInput
}
func (md *machineDeployment) createReleaseCommandMachine(ctx context.Context) error {
if len(md.releaseCommand) == 0 || !md.releaseCommandMachine.IsEmpty() {
return nil
}
launchInput := md.resolveUpdatedMachineConfig(nil, true)
releaseCmdMachine, err := md.flapsClient.Launch(ctx, *launchInput)
if err != nil {
return fmt.Errorf("error creating a release_command machine: %w", err)
}
fmt.Fprintf(md.io.ErrOut, " Created release_command machine %s\n", md.colorize.Bold(releaseCmdMachine.ID))
md.releaseCommandMachine = machine.NewMachineSet(md.flapsClient, md.io, []*api.Machine{releaseCmdMachine})
return nil
}
func (md *machineDeployment) updateReleaseCommandMachine(ctx context.Context) error {
if len(md.releaseCommand) == 0 {
return nil
}
if md.releaseCommandMachine.IsEmpty() {
return fmt.Errorf("expected release_command machine to exist already, but it does not :-(")
}
releaseCmdMachine := md.releaseCommandMachine.GetMachines()[0]
fmt.Fprintf(md.io.ErrOut, " Updating release_command machine %s\n", md.colorize.Bold(releaseCmdMachine.Machine().ID))
err := releaseCmdMachine.WaitForState(ctx, api.MachineStateStopped, md.waitTimeout)
if err != nil {
return err
}
updatedConfig := md.resolveUpdatedMachineConfig(releaseCmdMachine.Machine(), true)
err = md.releaseCommandMachine.AcquireLeases(ctx, md.leaseTimeout)
defer func() {
_ = md.releaseCommandMachine.ReleaseLeases(ctx)
}()
if err != nil {
return err
}
err = releaseCmdMachine.Update(ctx, *updatedConfig)
if err != nil {
return fmt.Errorf("error updating release_command machine: %w", err)
}
return nil
}
func (md *machineDeployment) setVolumeConfig(ctx context.Context) error {
if md.appConfig.Mounts == nil {
return nil
}
md.volumeDestination = md.appConfig.Mounts.Destination
if volumes, err := md.apiClient.GetVolumes(ctx, md.app.Name); err != nil {
return fmt.Errorf("Error fetching application volumes: %w", err)
} else {
md.volumes = lo.Filter(volumes, func(v api.Volume, _ int) bool {
return v.Name == md.appConfig.Mounts.Source && v.AttachedAllocation == nil && v.AttachedMachine == nil
})
}
return nil
}
// TODO(ali): remove this
/*
func (md *machineDeployment) validateProcessesConfig() error {
appConfigProcessesExist := md.appConfig.Processes != nil && len(md.appConfig.Processes) > 0
appConfigProcessesStr := ""
first := true
for procGroupName := range md.appConfig.Processes {
if !first {
appConfigProcessesStr += ", "
} else {
first = false
}
appConfigProcessesStr += procGroupName
}
for _, m := range md.machineSet.GetMachines() {
mid := m.Machine().ID
machineProcGroup := m.Machine().Config.Metadata[api.MachineConfigMetadataKeyFlyProcessGroup]
machineProcGroupPresent := machineProcGroup != ""
if machineProcGroup == api.MachineProcessGroupFlyAppReleaseCommand {
continue
}
// we put the api.MachineProcessGroupApp process group on machine by default
if !appConfigProcessesExist && machineProcGroup == api.MachineProcessGroupApp {
continue
}
if !machineProcGroupPresent && appConfigProcessesExist {
return fmt.Errorf("error machine %s does not have a process group and should have one from app configuration: %s", mid, appConfigProcessesStr)
}
if machineProcGroupPresent && !appConfigProcessesExist {
return fmt.Errorf("error machine %s has process group '%s' and no processes are defined in app config; add [processes] to fly.toml or remove the process group from this machine", mid, machineProcGroup)
}
if machineProcGroupPresent {
_, appConfigProcGroupPresent := md.appConfig.Processes[machineProcGroup]
if !appConfigProcGroupPresent {
return fmt.Errorf("error machine %s has process group '%s', which is missing from the processes in the app config: %s", mid, machineProcGroup, appConfigProcessesStr)
}
}
}
return nil
}
*/
func (md *machineDeployment) validateVolumeConfig() error {
for _, m := range md.machineSet.GetMachines() {
mid := m.Machine().ID
if m.Machine().ProcessGroup() == api.MachineProcessGroupFlyAppReleaseCommand {
continue
}
mountsConfig := m.Machine().Config.Mounts
if len(mountsConfig) > 1 {
return fmt.Errorf("error machine %s has %d mounts and expected 1", mid, len(mountsConfig))
}
if md.volumeDestination == "" && len(mountsConfig) != 0 {
return fmt.Errorf("error machine %s has a volume mounted and app config does not specify a volume; remove the volume from the machine or add a [mounts] configuration to fly.toml", mid)
}
if md.volumeDestination != "" && len(mountsConfig) == 0 {
return fmt.Errorf("error machine %s does not have a volume configured and fly.toml expects one with destination %s; remove the [mounts] configuration in fly.toml or use the machines API to add a volume to this machine", mid, md.volumeDestination)
}
}
if md.machineSet.IsEmpty() && md.volumeDestination != "" && len(md.volumes) == 0 {
return fmt.Errorf("error new machine requires an unattached volume named '%s' on mount destination '%s'",
md.appConfig.Mounts.Source, md.volumeDestination)
}
return nil
}
func (md *machineDeployment) setStrategy(passedInStrategy string) error {
if passedInStrategy != "" {
md.strategy = passedInStrategy
} else if md.appConfig.Deploy != nil && md.appConfig.Deploy.Strategy != "" {
md.strategy = md.appConfig.Deploy.Strategy
} else {
md.strategy = "rolling"
}
if md.strategy != "rolling" && md.strategy != "immediate" {
return fmt.Errorf("error unsupported deployment strategy '%s'; fly deploy for machines supports rolling and immediate strategies", md.strategy)
}
return nil
}
func (md *machineDeployment) createReleaseInBackend(ctx context.Context) error {
_ = `# @genqlient
mutation MachinesCreateRelease($input:CreateReleaseInput!) {
createRelease(input:$input) {
release {
id
version
}
}
}
`
input := gql.CreateReleaseInput{
AppId: md.appConfig.AppName,
PlatformVersion: "machines",
Strategy: gql.DeploymentStrategy(strings.ToUpper(md.strategy)),
Definition: md.appConfig,
}
if !md.restartOnly {
input.Image = md.img.Tag
} else if !md.machineSet.IsEmpty() {
input.Image = md.machineSet.GetMachines()[0].Machine().Config.Image
}
resp, err := gql.MachinesCreateRelease(ctx, md.gqlClient, input)
if err != nil {
return err
}
md.releaseId = resp.CreateRelease.Release.Id
md.releaseVersion = resp.CreateRelease.Release.Version
return nil
}
func (md *machineDeployment) resolveUpdatedMachineConfig(origMachineRaw *api.Machine, forReleaseCommand bool) *api.LaunchMachineInput {
if origMachineRaw == nil {
origMachineRaw = &api.Machine{
Region: md.appConfig.PrimaryRegion,
Config: &api.MachineConfig{},
}
}
launchInput := &api.LaunchMachineInput{
ID: origMachineRaw.ID,
AppID: md.app.Name,
OrgSlug: md.app.Organization.ID,
Config: lo.Ternary(md.restartOnly, origMachineRaw.Config, &api.MachineConfig{}),
Region: origMachineRaw.Region,
}
launchInput.Config.Metadata = md.defaultMachineMetadata()
for k, v := range origMachineRaw.Config.Metadata {
if !isFlyAppsPlatformMetadata(k) {
launchInput.Config.Metadata[k] = v
}
}
// Stop here If the machine is restarting
if md.restartOnly {
return launchInput
}
launchInput.Config.DisableMachineAutostart = origMachineRaw.Config.DisableMachineAutostart
launchInput.Config.Image = md.img.Tag
launchInput.Config.Restart = origMachineRaw.Config.Restart
launchInput.Config.Guest = origMachineRaw.Config.Guest
launchInput.Config.Init = origMachineRaw.Config.Init
launchInput.Config.Env = lo.Assign(md.appConfig.Env)
if launchInput.Config.Env["PRIMARY_REGION"] == "" && origMachineRaw.Config.Env["PRIMARY_REGION"] != "" {
launchInput.Config.Env["PRIMARY_REGION"] = origMachineRaw.Config.Env["PRIMARY_REGION"]
}
// Stop here If the machine is for release command
if forReleaseCommand {
return md.configureLaunchInputForReleaseCommand(launchInput)
}
// Anything below this point doesn't apply to machines created to run ReleaseCommand
launchInput.Config.Metrics = md.appConfig.Metrics
for _, s := range md.appConfig.Statics {
launchInput.Config.Statics = append(launchInput.Config.Statics, &api.Static{
GuestPath: s.GuestPath,
UrlPrefix: s.UrlPrefix,
})
}
launchInput.Config.Mounts = origMachineRaw.Config.Mounts
if launchInput.Config.Mounts == nil && md.appConfig.Mounts != nil {
launchInput.Config.Mounts = []api.MachineMount{{
Path: md.volumeDestination,
Volume: md.volumes[0].ID,
}}
}
if len(launchInput.Config.Mounts) == 1 && launchInput.Config.Mounts[0].Path != md.volumeDestination {
currentMount := launchInput.Config.Mounts[0]
terminal.Warnf("Updating the mount path for volume %s on machine %s from %s to %s due to fly.toml [mounts] destination value\n", currentMount.Volume, origMachineRaw.ID, currentMount.Path, md.volumeDestination)
launchInput.Config.Mounts[0].Path = md.volumeDestination
}
processGroup := launchInput.Config.ProcessGroup()
if processConfig, ok := md.processConfigs[processGroup]; ok {
launchInput.Config.Services = processConfig.Services
launchInput.Config.Checks = processConfig.Checks
launchInput.Config.Init.Cmd = lo.Ternary(len(processConfig.Cmd) > 0, processConfig.Cmd, nil)
}
return launchInput
}
func (md *machineDeployment) defaultMachineMetadata() map[string]string {
res := map[string]string{
api.MachineConfigMetadataKeyFlyPlatformVersion: api.MachineFlyPlatformVersion2,
api.MachineConfigMetadataKeyFlyReleaseId: md.releaseId,
api.MachineConfigMetadataKeyFlyReleaseVersion: strconv.Itoa(md.releaseVersion),
api.MachineConfigMetadataKeyFlyProcessGroup: api.MachineProcessGroupApp,
}
if md.app.IsPostgresApp() {
res[api.MachineConfigMetadataKeyFlyManagedPostgres] = "true"
}
return res
}
func isFlyAppsPlatformMetadata(key string) bool {
return key == api.MachineConfigMetadataKeyFlyPlatformVersion ||
key == api.MachineConfigMetadataKeyFlyReleaseId ||
key == api.MachineConfigMetadataKeyFlyReleaseVersion ||
key == api.MachineConfigMetadataKeyFlyManagedPostgres
}
func (md *machineDeployment) provisionIpsOnFirstDeploy(ctx context.Context) error {
if md.app.Deployed || !md.machineSet.IsEmpty() {
return nil
}
if md.appConfig.HttpService != nil || len(md.appConfig.Services) > 0 {
ipAddrs, err := md.apiClient.GetIPAddresses(ctx, md.app.Name)
if err != nil {
return fmt.Errorf("error detecting ip addresses allocated to %s app: %w", md.app.Name, err)
}
if len(ipAddrs) > 0 {
return nil
}
fmt.Fprintf(md.io.Out, "Provisioning ips for %s\n", md.colorize.Bold(md.app.Name))
v6Addr, err := md.apiClient.AllocateIPAddress(ctx, md.app.Name, "v6", "", nil, "")
if err != nil {
return fmt.Errorf("error allocating ipv6 after detecting first deploy and presence of services: %w", err)
}
fmt.Fprintf(md.io.Out, " Dedicated ipv6: %s\n", v6Addr.Address)
v4Shared, err := md.apiClient.AllocateSharedIPAddress(ctx, md.app.Name)
if err != nil {
return fmt.Errorf("error allocating shared ipv4 after detecting first deploy and presence of services: %w", err)
}
fmt.Fprintf(md.io.Out, " Shared ipv4: %s\n", v4Shared)
fmt.Fprintf(md.io.Out, " Add a dedicated ipv4 with: fly ips allocate-v4\n")
}
return nil
}
func (md *machineDeployment) logClearLinesAbove(count int) {
if md.io.IsInteractive() {
builder := aec.EmptyBuilder
str := builder.Up(uint(count)).EraseLine(aec.EraseModes.All).ANSI
fmt.Fprint(md.io.ErrOut, str.String())
}
}
func determineAppConfigForMachines(ctx context.Context, envFromFlags []string, primaryRegion string) (cfg *appconfig.Config, err error) {
appNameFromContext := appconfig.NameFromContext(ctx)
if cfg = appconfig.ConfigFromContext(ctx); cfg == nil {
logger := logger.FromContext(ctx)
logger.Debug("no local app config detected for machines deploy; fetching from backend ...")
cfg, err = appconfig.FromRemoteApp(ctx, appNameFromContext)
if err != nil {
return nil, err
}
}
if len(envFromFlags) > 0 {
var parsedEnv map[string]string
if parsedEnv, err = cmdutil.ParseKVStringsToMap(envFromFlags); err != nil {
err = fmt.Errorf("failed parsing environment: %w", err)
return
}
cfg.SetEnvVariables(parsedEnv)
}
// deleting this block will result in machines not being deployed in the user selected region
if primaryRegion != "" {
cfg.PrimaryRegion = primaryRegion
}
// Always prefer the app name passed via --app
if appNameFromContext != "" {
cfg.AppName = appNameFromContext
}
return
}