-
Notifications
You must be signed in to change notification settings - Fork 224
/
run.go
800 lines (688 loc) · 21 KB
/
run.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
package machine
import (
"context"
"fmt"
"path"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/briandowns/spinner"
"github.com/dustin/go-humanize"
"github.com/google/shlex"
"github.com/pkg/errors"
"github.com/samber/lo"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/flaps"
"github.com/superfly/flyctl/iostreams"
"github.com/superfly/flyctl/client"
"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/env"
"github.com/superfly/flyctl/internal/flag"
mach "github.com/superfly/flyctl/internal/machine"
"github.com/superfly/flyctl/internal/prompt"
"github.com/superfly/flyctl/internal/state"
"github.com/superfly/flyctl/internal/watch"
)
var sharedFlags = flag.Set{
flag.App(),
flag.AppConfig(),
flag.Detach(),
flag.StringSlice{
Name: "port",
Shorthand: "p",
Description: "Exposed port mappings (format: (edgePort|startPort-endPort)[:machinePort]/[protocol[:handler]])",
},
flag.String{
Name: "size",
Shorthand: "s",
Description: "Preset guest cpu and memory for a machine, defaults to shared-cpu-1x",
},
flag.Int{
Name: "cpus",
Description: "Number of CPUs",
},
flag.Int{
Name: "memory",
Description: "Memory (in megabytes) to attribute to the machine",
},
flag.StringSlice{
Name: "env",
Shorthand: "e",
Description: "Set of environment variables in the form of NAME=VALUE pairs. Can be specified multiple times.",
},
flag.String{
Name: "entrypoint",
Description: "ENTRYPOINT replacement",
},
flag.Bool{
Name: "build-only",
Description: "Only build the image without running the machine",
Hidden: true,
},
flag.Bool{
Name: "build-remote-only",
Description: "Perform builds remotely without using the local docker daemon",
Hidden: true,
},
flag.Bool{
Name: "build-local-only",
Description: "Only perform builds locally using the local docker daemon",
Hidden: true,
},
flag.Bool{
Name: "build-nixpacks",
Description: "Build your image with nixpacks",
},
flag.String{
Name: "dockerfile",
Description: "Path to a Dockerfile. Defaults to the Dockerfile in the working directory.",
},
flag.StringSlice{
Name: "build-arg",
Description: "Set of build time variables in the form of NAME=VALUE pairs. Can be specified multiple times.",
Hidden: true,
},
flag.String{
Name: "image-label",
Description: "Image label to use when tagging and pushing to the fly registry. Defaults to \"deployment-{timestamp}\".",
Hidden: true,
},
flag.String{
Name: "build-target",
Description: "Set the target build stage to build if the Dockerfile has more than one stage",
Hidden: true,
},
flag.Bool{
Name: "no-build-cache",
Description: "Do not use the cache when building the image",
Hidden: true,
},
flag.StringSlice{
Name: "kernel-arg",
Description: "List of kernel arguments to be provided to the init. Can be specified multiple times.",
},
flag.StringSlice{
Name: "metadata",
Shorthand: "m",
Description: "Metadata in the form of NAME=VALUE pairs. Can be specified multiple times.",
},
flag.String{
Name: "schedule",
Description: `Schedule a machine run at hourly, daily and monthly intervals`,
},
flag.Bool{
Name: "skip-dns-registration",
Description: "Do not register the machine's 6PN IP with the internal DNS system",
},
flag.Bool{
Name: "autostart",
Description: "Automatically start a stopped machine when a network request is received",
Default: true,
},
flag.Bool{
Name: "autostop",
Description: "Automatically stop a machine when there aren't network requests for it",
Default: true,
},
flag.String{
Name: "restart",
Description: "Configure restart policy, for a machine. Options include 'no', 'always' and 'on-fail'. Default is set to always",
},
flag.StringSlice{
Name: "standby-for",
Description: "Comma separated list of machine ids to watch for",
},
}
var s = spinner.New(spinner.CharSets[9], 100*time.Millisecond)
func newRun() *cobra.Command {
const (
short = "Run a machine"
long = short + "\n"
usage = "run <image> [command]"
)
cmd := command.New(usage, short, long, runMachineRun,
command.RequireSession,
command.LoadAppNameIfPresent,
)
flag.Add(
cmd,
flag.Region(),
// deprecated in favor of `flyctl machine update`
flag.String{
Name: "id",
Description: "Machine ID, if previously known",
},
flag.String{
Name: "name",
Shorthand: "n",
Description: "Machine name, will be generated if missing",
},
flag.String{
Name: "org",
Description: `The organization that will own the app`,
},
flag.Bool{
Name: "rm",
Description: "Automatically remove the machine when it exits",
},
flag.StringSlice{
Name: "volume",
Shorthand: "v",
Description: "Volumes to mount in the form of <volume_id_or_name>:/path/inside/machine[:<options>]",
},
sharedFlags,
)
cmd.Args = cobra.MinimumNArgs(1)
return cmd
}
func runMachineRun(ctx context.Context) error {
var (
appName = appconfig.NameFromContext(ctx)
client = client.FromContext(ctx).API()
io = iostreams.FromContext(ctx)
colorize = io.ColorScheme()
err error
app *api.AppCompact
)
if appName == "" {
app, err = createApp(ctx, "Running a machine without specifying an app will create one for you, is this what you want?", "", client)
if err != nil {
return err
}
if app == nil {
return nil
}
} else {
app, err = client.GetAppCompact(ctx, appName)
if err != nil && strings.Contains(err.Error(), "Could not find App") {
app, err = createApp(ctx, fmt.Sprintf("App '%s' does not exist, would you like to create it?", appName), appName, client)
if err != nil {
return err
}
if app == nil {
return nil
}
}
if err != nil {
return err
}
}
machineConf := &api.MachineConfig{
Guest: &api.MachineGuest{
CPUKind: "shared",
CPUs: 1,
MemoryMB: 256,
KernelArgs: flag.GetStringSlice(ctx, "kernel-arg"),
},
AutoDestroy: flag.GetBool(ctx, "rm"),
DNS: &api.DNSConfig{
SkipRegistration: flag.GetBool(ctx, "skip-dns-registration"),
},
}
input := api.LaunchMachineInput{
Name: flag.GetString(ctx, "name"),
Region: flag.GetString(ctx, "region"),
}
flapsClient, err := flaps.New(ctx, app)
if err != nil {
return fmt.Errorf("could not make API client: %w", err)
}
ctx = flaps.NewContext(ctx, flapsClient)
if app.PlatformVersion == "nomad" {
return fmt.Errorf("the app %s uses an earlier version of the platform that does not support machines", app.Name)
}
machineID := flag.GetString(ctx, "id")
if machineID != "" {
return fmt.Errorf("to update an existing machine, use 'flyctl machine update'")
}
machineConf, err = determineMachineConfig(ctx, &determineMachineConfigInput{
initialMachineConf: *machineConf,
appName: app.Name,
imageOrPath: flag.FirstArg(ctx),
region: input.Region,
updating: false,
})
if err != nil {
return err
}
if flag.GetBool(ctx, "build-only") {
return nil
}
input.SkipLaunch = len(machineConf.Standbys) > 0
input.Config = machineConf
machine, err := flapsClient.Launch(ctx, input)
if err != nil {
return fmt.Errorf("could not launch machine: %w", err)
}
id, instanceID, state, privateIP := machine.ID, machine.InstanceID, machine.State, machine.PrivateIP
fmt.Fprintf(io.Out, "Success! A machine has been successfully launched in app %s\n", appName)
fmt.Fprintf(io.Out, " Machine ID: %s\n", id)
fmt.Fprintf(io.Out, " Instance ID: %s\n", instanceID)
fmt.Fprintf(io.Out, " State: %s\n", state)
if input.SkipLaunch {
return nil
}
fmt.Fprintf(io.Out, "\n Attempting to start machine...\n\n")
s.Start()
// wait for machine to be started
err = mach.WaitForStartOrStop(ctx, machine, "start", time.Minute*5)
s.Stop()
if err != nil {
return err
}
if !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, "Machine started, you can connect via the following private ip\n")
fmt.Fprintf(io.Out, " %s\n", privateIP)
return nil
}
func createApp(ctx context.Context, message, name string, client *api.Client) (*api.AppCompact, error) {
confirm, err := prompt.Confirm(ctx, message)
if err != nil {
return nil, err
}
if !confirm {
return nil, nil
}
org, err := prompt.Org(ctx)
if err != nil {
return nil, err
}
if name == "" {
name, err = selectAppName(ctx)
if err != nil {
return nil, err
}
}
input := api.CreateAppInput{
Name: name,
OrganizationID: org.ID,
}
app, err := client.CreateApp(ctx, input)
if err != nil {
return nil, err
}
return &api.AppCompact{
ID: app.ID,
Name: app.Name,
Status: app.Status,
Deployed: app.Deployed,
Hostname: app.Hostname,
AppURL: app.AppURL,
Organization: &api.OrganizationBasic{
ID: app.Organization.ID,
Slug: app.Organization.Slug,
},
}, nil
}
func parseKVFlag(ctx context.Context, flagName string, initialMap map[string]string) (parsed map[string]string, err error) {
parsed = initialMap
if value := flag.GetStringSlice(ctx, flagName); len(value) > 0 {
parsed, err = cmdutil.ParseKVStringsToMap(value)
if err != nil {
return nil, fmt.Errorf("invalid key/value pairs specified for flag %s", flagName)
}
}
return parsed, nil
}
func determineImage(ctx context.Context, appName string, imageOrPath string) (img *imgsrc.DeploymentImage, err error) {
var (
client = client.FromContext(ctx).API()
io = iostreams.FromContext(ctx)
cfg = appconfig.ConfigFromContext(ctx)
)
daemonType := imgsrc.NewDockerDaemonType(!flag.GetBool(ctx, "build-remote-only"), !flag.GetBool(ctx, "build-local-only"), env.IsCI(), flag.GetBool(ctx, "build-nixpacks"))
resolver := imgsrc.NewResolver(daemonType, client, appName, io)
// build if relative or absolute path
if strings.HasPrefix(imageOrPath, ".") || strings.HasPrefix(imageOrPath, "/") {
opts := imgsrc.ImageOptions{
AppName: appName,
WorkingDir: path.Join(state.WorkingDirectory(ctx)),
Publish: !flag.GetBuildOnly(ctx),
ImageLabel: flag.GetString(ctx, "image-label"),
Target: flag.GetString(ctx, "build-target"),
NoCache: flag.GetBool(ctx, "no-build-cache"),
}
dockerfilePath := cfg.Dockerfile()
// dockerfile passed through flags takes precedence over the one set in config
if flag.GetString(ctx, "dockerfile") != "" {
dockerfilePath = flag.GetString(ctx, "dockerfile")
}
if dockerfilePath != "" {
dockerfilePath, err := filepath.Abs(dockerfilePath)
if err != nil {
return nil, err
}
opts.DockerfilePath = dockerfilePath
}
extraArgs, err := cmdutil.ParseKVStringsToMap(flag.GetStringSlice(ctx, "build-arg"))
if err != nil {
return nil, errors.Wrap(err, "invalid build-arg")
}
opts.BuildArgs = extraArgs
img, err = resolver.BuildImage(ctx, io, opts)
if err != nil {
return nil, err
}
if img == nil {
return nil, errors.New("could not find an image to deploy")
}
} else {
opts := imgsrc.RefOptions{
AppName: appName,
WorkingDir: state.WorkingDirectory(ctx),
Publish: !flag.GetBool(ctx, "build-only"),
ImageRef: imageOrPath,
ImageLabel: flag.GetString(ctx, "image-label"),
}
img, err = resolver.ResolveReference(ctx, io, opts)
if err != nil {
return nil, err
}
}
if img == nil {
return nil, errors.New("could not find an image to deploy")
}
fmt.Fprintf(io.Out, "Image: %s\n", img.Tag)
fmt.Fprintf(io.Out, "Image size: %s\n\n", humanize.Bytes(uint64(img.Size)))
return img, nil
}
func determineMounts(ctx context.Context, mounts []api.MachineMount, region string) ([]api.MachineMount, error) {
unattachedVolumes := make(map[string][]api.Volume)
pathIndex := make(map[string]int)
for idx, m := range mounts {
pathIndex[m.Path] = idx
}
for _, v := range flag.GetStringSlice(ctx, "volume") {
splittedIDDestOpts := strings.Split(v, ":")
if len(splittedIDDestOpts) < 2 {
return nil, fmt.Errorf("Can't infer volume and mount path from '%s'", v)
}
volID := splittedIDDestOpts[0]
mountPath := splittedIDDestOpts[1]
if !strings.HasPrefix(volID, "vol_") {
volName := volID
// Load app volumes the first time
if len(unattachedVolumes) == 0 {
var err error
unattachedVolumes, err = getUnattachedVolumes(ctx, region)
if err != nil {
return nil, err
}
}
if len(unattachedVolumes[volName]) == 0 {
return nil, fmt.Errorf("not enough unattached volumes for '%s'", volName)
}
volID = unattachedVolumes[volName][0].ID
unattachedVolumes[volName] = unattachedVolumes[volName][1:]
}
if idx, found := pathIndex[mountPath]; found {
mounts[idx].Volume = volID
} else {
mounts = append(mounts, api.MachineMount{
Volume: volID,
Path: mountPath,
})
}
}
return mounts, nil
}
func getUnattachedVolumes(ctx context.Context, regionCode string) (map[string][]api.Volume, error) {
appName := appconfig.NameFromContext(ctx)
apiclient := client.FromContext(ctx).API()
if regionCode == "" {
region, err := apiclient.GetNearestRegion(ctx)
if err != nil {
return nil, err
}
regionCode = region.Code
}
volumes, err := apiclient.GetVolumes(ctx, appName)
if err != nil {
return nil, fmt.Errorf("Error fetching application volumes: %w", err)
}
unattached := lo.Filter(volumes, func(v api.Volume, _ int) bool {
return !v.IsAttached() && (regionCode == v.Region)
})
if len(unattached) == 0 {
return nil, fmt.Errorf("No unattached volumes in region '%s'", regionCode)
}
unattachedMap := lo.GroupBy(unattached, func(v api.Volume) string { return v.Name })
return unattachedMap, nil
}
func determineServices(ctx context.Context) ([]api.MachineService, error) {
ports := flag.GetStringSlice(ctx, "port")
if len(ports) <= 0 {
return []api.MachineService{}, nil
}
machineServices := make([]api.MachineService, len(ports))
for i, p := range flag.GetStringSlice(ctx, "port") {
proto := "tcp"
handlers := []string{}
splittedPortsProto := strings.Split(p, "/")
if len(splittedPortsProto) == 2 {
splittedProtoHandlers := strings.Split(splittedPortsProto[1], ":")
proto = splittedProtoHandlers[0]
handlers = append(handlers, splittedProtoHandlers[1:]...)
} else if len(splittedPortsProto) > 2 {
return nil, errors.New("port must be at most two elements (ports/protocol:handler)")
}
edgePort, edgeStartPort, edgeEndPort, internalPort, err := parsePorts(splittedPortsProto[0])
if err != nil {
return nil, err
}
machineServices[i] = api.MachineService{
Protocol: proto,
InternalPort: internalPort,
Ports: []api.MachinePort{
{
Port: edgePort,
StartPort: edgeStartPort,
EndPort: edgeEndPort,
Handlers: handlers,
},
},
}
}
return machineServices, nil
}
func parsePorts(input string) (port, start_port, end_port *int, internal_port int, err error) {
split := strings.Split(input, ":")
if len(split) == 1 {
var external_port int
external_port, err = strconv.Atoi(split[0])
if err != nil {
err = errors.Wrap(err, "invalid port")
return
}
port = api.IntPointer(external_port)
} else if len(split) == 2 {
internal_port, err = strconv.Atoi(split[1])
if err != nil {
err = errors.Wrap(err, "invalid machine (internal) port")
return
}
external_split := strings.Split(split[0], "-")
if len(external_split) == 1 {
var external_port int
external_port, err = strconv.Atoi(external_split[0])
if err != nil {
err = errors.Wrap(err, "invalid external port")
return
}
port = api.IntPointer(external_port)
} else if len(external_split) == 2 {
var start int
start, err = strconv.Atoi(external_split[0])
if err != nil {
err = errors.Wrap(err, "invalid start port for port range")
return
}
start_port = api.IntPointer(start)
var end int
end, err = strconv.Atoi(external_split[0])
if err != nil {
err = errors.Wrap(err, "invalid end port for port range")
return
}
end_port = api.IntPointer(end)
} else {
err = errors.New("external port must be at most 2 elements (port, or range start-end)")
}
} else {
err = errors.New("port definition must be at most 2 elements (external:internal)")
}
return
}
func selectAppName(ctx context.Context) (name string, err error) {
const msg = "App Name:"
if err = prompt.String(ctx, &name, msg, "", false); prompt.IsNonInteractive(err) {
err = prompt.NonInteractiveError("name argument or flag must be specified when not running interactively")
}
return
}
type determineMachineConfigInput struct {
initialMachineConf api.MachineConfig
appName string
imageOrPath string
region string
updating bool
}
func determineMachineConfig(ctx context.Context, input *determineMachineConfigInput) (*api.MachineConfig, error) {
machineConf := mach.CloneConfig(&input.initialMachineConf)
if guestSize := flag.GetString(ctx, "size"); guestSize != "" {
err := machineConf.Guest.SetSize(guestSize)
if err != nil {
return nil, err
}
}
// Potential overrides for Guest
if cpus := flag.GetInt(ctx, "cpus"); cpus != 0 {
machineConf.Guest.CPUs = cpus
} else if flag.IsSpecified(ctx, "cpus") {
return nil, fmt.Errorf("cannot have zero cpus")
}
if memory := flag.GetInt(ctx, "memory"); memory != 0 {
machineConf.Guest.MemoryMB = memory
} else if flag.IsSpecified(ctx, "memory") {
return nil, fmt.Errorf("memory cannot be zero")
}
if len(flag.GetStringSlice(ctx, "kernel-arg")) != 0 {
machineConf.Guest.KernelArgs = flag.GetStringSlice(ctx, "kernel-arg")
}
parsedEnv, err := parseKVFlag(ctx, "env", machineConf.Env)
if err != nil {
return machineConf, err
}
if machineConf.Env == nil {
machineConf.Env = make(map[string]string)
}
for k, v := range parsedEnv {
machineConf.Env[k] = v
}
if flag.GetString(ctx, "schedule") != "" {
machineConf.Schedule = flag.GetString(ctx, "schedule")
}
if input.updating {
// Called from `update`. Command is specified by flag.
if command := flag.GetString(ctx, "command"); command != "" {
split, err := shlex.Split(command)
if err != nil {
return machineConf, errors.Wrap(err, "invalid command")
}
machineConf.Init.Cmd = split
}
} else {
// Called from `run`. Command is specified by arguments.
machineConf.Init.Cmd = flag.Args(ctx)[1:]
}
if machineConf.DNS == nil {
machineConf.DNS = &api.DNSConfig{
SkipRegistration: flag.GetBool(ctx, "skip-dns-registration"),
}
}
// Metadata
parsedMetadata, err := parseKVFlag(ctx, "metadata", machineConf.Metadata)
if err != nil {
return machineConf, err
}
if machineConf.Metadata == nil {
machineConf.Metadata = make(map[string]string)
}
for k, v := range parsedMetadata {
machineConf.Metadata[k] = v
}
services, err := determineServices(ctx)
if err != nil {
return machineConf, err
}
if len(services) > 0 {
machineConf.Services = services
}
if entrypoint := flag.GetString(ctx, "entrypoint"); entrypoint != "" {
splitted, err := shlex.Split(entrypoint)
if err != nil {
return machineConf, errors.Wrap(err, "invalid entrypoint")
}
machineConf.Init.Entrypoint = splitted
}
// default restart policy to always unless otherwise specified
switch flag.GetString(ctx, "restart") {
case "no":
machineConf.Restart.Policy = api.MachineRestartPolicyNo
case "on-fail":
machineConf.Restart.Policy = api.MachineRestartPolicyOnFailure
case "always":
machineConf.Restart.Policy = api.MachineRestartPolicyAlways
case "":
if flag.IsSpecified(ctx, "restart") {
// An empty policy was explicitly requested.
machineConf.Restart.Policy = ""
} else if !input.updating {
// This is a new machine; apply the default.
machineConf.Restart.Policy = api.MachineRestartPolicyAlways
}
default:
return machineConf, errors.New("invalid restart provided")
}
machineConf.Mounts, err = determineMounts(ctx, machineConf.Mounts, input.region)
if err != nil {
return machineConf, err
}
img, err := determineImage(ctx, input.appName, input.imageOrPath)
if err != nil {
return machineConf, err
}
machineConf.Image = img.Tag
// Service updates
for idx := range machineConf.Services {
s := &machineConf.Services[idx]
// Use the chance to port the deprecated field
if machineConf.DisableMachineAutostart != nil {
s.Autostart = api.Pointer(!(*machineConf.DisableMachineAutostart))
machineConf.DisableMachineAutostart = nil
}
if flag.IsSpecified(ctx, "autostop") {
s.Autostop = api.Pointer(flag.GetBool(ctx, "autostop"))
}
if flag.IsSpecified(ctx, "autostart") {
s.Autostart = api.Pointer(flag.GetBool(ctx, "autostart"))
}
}
// Standby machine
if flag.IsSpecified(ctx, "standby-for") {
standbys := flag.GetStringSlice(ctx, "standby-for")
machineConf.Standbys = lo.Ternary(len(standbys) > 0, standbys, nil)
}
return machineConf, nil
}