-
Notifications
You must be signed in to change notification settings - Fork 218
/
machines.go
439 lines (383 loc) · 11.9 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
package appconfig
import (
"fmt"
"github.com/docker/go-units"
"github.com/google/shlex"
"github.com/jinzhu/copier"
"github.com/samber/lo"
fly "github.com/superfly/fly-go"
"github.com/superfly/flyctl/helpers"
"github.com/superfly/flyctl/internal/buildinfo"
)
func (c *Config) ToMachineConfig(processGroup string, src *fly.MachineConfig) (*fly.MachineConfig, error) {
fc, err := c.Flatten(processGroup)
if err != nil {
return nil, err
}
return fc.updateMachineConfig(src)
}
func (c *Config) ToReleaseMachineConfig() (*fly.MachineConfig, error) {
releaseCmd, err := shlex.Split(c.Deploy.ReleaseCommand)
if err != nil {
return nil, err
}
mConfig := &fly.MachineConfig{
Init: fly.MachineInit{
Cmd: releaseCmd,
SwapSizeMB: c.SwapSizeMB,
},
Restart: &fly.MachineRestart{
Policy: fly.MachineRestartPolicyNo,
},
AutoDestroy: true,
DNS: &fly.DNSConfig{
SkipRegistration: true,
},
Metadata: map[string]string{
fly.MachineConfigMetadataKeyFlyctlVersion: buildinfo.Version().String(),
fly.MachineConfigMetadataKeyFlyPlatformVersion: fly.MachineFlyPlatformVersion2,
fly.MachineConfigMetadataKeyFlyProcessGroup: fly.MachineProcessGroupFlyAppReleaseCommand,
},
Env: lo.Assign(c.Env),
}
if c.Experimental != nil {
mConfig.Init.Entrypoint = c.Experimental.Entrypoint
}
mConfig.Env["RELEASE_COMMAND"] = "1"
mConfig.Env["FLY_PROCESS_GROUP"] = fly.MachineProcessGroupFlyAppReleaseCommand
if c.PrimaryRegion != "" {
mConfig.Env["PRIMARY_REGION"] = c.PrimaryRegion
}
// StopConfig
c.tomachineSetStopConfig(mConfig)
return mConfig, nil
}
type TestMachineConfigErr int
const (
MissingCommand TestMachineConfigErr = iota
MissingImage
)
func (e TestMachineConfigErr) Error() string {
switch e {
case MissingCommand:
return "missing command for test machine"
case MissingImage:
return "missing image for test machine"
default:
return "unknown error creating test machine config"
}
}
func (e TestMachineConfigErr) Suggestion() string {
switch e {
case MissingCommand:
return "Add a `command` field to the `[[services.machine_checks]]` or `[[http_service.machine_checks]]` section of your fly.toml"
case MissingImage:
return "Add an `image` field to the `[[services.machine_checks]]` or `[[http_service.machine_checks]]` section of your fly.toml"
default:
return ""
}
}
func (c *Config) ToTestMachineConfig(svc *ServiceMachineCheck, origMachine *fly.Machine) (*fly.MachineConfig, error) {
var machineEntrypoint []string
if svc.Entrypoint != nil {
machineEntrypoint = svc.Entrypoint
} else {
machineEntrypoint = origMachine.Config.Init.Entrypoint
}
var machineCommand []string
if len(svc.Command) > 0 {
machineCommand = svc.Command
} else {
return nil, MissingCommand
}
var machineImage string
if svc.Image != "" {
machineImage = svc.Image
} else if origMachine != nil && origMachine.Config != nil && origMachine.Config.Image != "" {
machineImage = origMachine.Config.Image
} else {
return nil, MissingImage
}
var origMachineEnv map[string]string
if origMachine != nil && origMachine.Config != nil {
origMachineEnv = origMachine.Config.Env
}
mConfig := &fly.MachineConfig{
Init: fly.MachineInit{
Cmd: machineCommand,
SwapSizeMB: c.SwapSizeMB,
Entrypoint: machineEntrypoint,
},
Image: machineImage,
Restart: &fly.MachineRestart{
Policy: fly.MachineRestartPolicyNo,
},
AutoDestroy: true,
DNS: &fly.DNSConfig{
SkipRegistration: true,
},
Metadata: map[string]string{
fly.MachineConfigMetadataKeyFlyctlVersion: buildinfo.Version().String(),
fly.MachineConfigMetadataKeyFlyPlatformVersion: fly.MachineFlyPlatformVersion2,
fly.MachineConfigMetadataKeyFlyProcessGroup: fly.MachineProcessGroupFlyAppTestMachineCommand,
},
Env: lo.Assign(c.Env, origMachineEnv),
}
if c.Experimental != nil {
mConfig.Init.Entrypoint = c.Experimental.Entrypoint
}
mConfig.Env["FLY_TEST_COMMAND"] = "1"
mConfig.Env["FLY_PROCESS_GROUP"] = fly.MachineProcessGroupFlyAppTestMachineCommand
if c.PrimaryRegion != "" {
mConfig.Env["PRIMARY_REGION"] = c.PrimaryRegion
}
mConfig.Env["FLY_TEST_MACHINE_IP"] = lo.Ternary(origMachine == nil, "", origMachine.PrivateIP)
// Use the stop config from the app config by default
c.tomachineSetStopConfig(mConfig)
var killTimeout *fly.Duration
var killSignal *string
// We use the image's default killsignal/timeout if it isn't set by the user
if svc.Image != "" {
killTimeout = lo.Ternary(svc.KillTimeout != nil, svc.KillTimeout, nil)
killSignal = lo.Ternary(svc.KillSignal != nil, svc.KillSignal, nil)
} else {
if svc.KillTimeout != nil {
killTimeout = svc.KillTimeout
} else if c.KillTimeout != nil {
killTimeout = c.KillTimeout
}
if svc.KillSignal != nil {
killSignal = svc.KillSignal
} else if c.KillSignal != nil {
killSignal = c.KillSignal
}
}
mConfig.StopConfig = &fly.StopConfig{
Timeout: killTimeout,
Signal: killSignal,
}
return mConfig, nil
}
func (c *Config) ToConsoleMachineConfig() (*fly.MachineConfig, error) {
mConfig := &fly.MachineConfig{
Init: fly.MachineInit{
// TODO: it would be better to configure init to run no
// command at all. That way we don't rely on /bin/sleep
// being available and working right. However, there's no
// way to do that yet.
Exec: []string{"/bin/sleep", "inf"},
SwapSizeMB: c.SwapSizeMB,
},
Restart: &fly.MachineRestart{
Policy: fly.MachineRestartPolicyNo,
},
AutoDestroy: true,
DNS: &fly.DNSConfig{
SkipRegistration: true,
},
Metadata: map[string]string{
fly.MachineConfigMetadataKeyFlyctlVersion: buildinfo.Version().String(),
fly.MachineConfigMetadataKeyFlyPlatformVersion: fly.MachineFlyPlatformVersion2,
fly.MachineConfigMetadataKeyFlyProcessGroup: fly.MachineProcessGroupFlyAppConsole,
},
Env: lo.Assign(c.Env),
}
mConfig.Env["FLY_PROCESS_GROUP"] = fly.MachineProcessGroupFlyAppConsole
if c.PrimaryRegion != "" {
mConfig.Env["PRIMARY_REGION"] = c.PrimaryRegion
}
return mConfig, nil
}
// updateMachineConfig applies configuration options from the optional MachineConfig passed in, then the base config, into a new MachineConfig
func (c *Config) updateMachineConfig(src *fly.MachineConfig) (*fly.MachineConfig, error) {
// For flattened app configs there is only one proces name and it is the group it was flattened for
processGroup := c.DefaultProcessName()
mConfig := &fly.MachineConfig{}
if src != nil {
mConfig = helpers.Clone(src)
}
// Metrics
mConfig.Metrics = nil
if len(c.Metrics) > 0 {
mConfig.Metrics = c.Metrics[0].MachineMetrics
}
// Init
cmd, err := c.InitCmd(processGroup)
if err != nil {
return nil, err
}
if c.Experimental != nil {
if cmd == nil {
cmd = c.Experimental.Cmd
}
mConfig.Init.Entrypoint = c.Experimental.Entrypoint
mConfig.Init.Exec = c.Experimental.Exec
} else {
mConfig.Init.Entrypoint = nil
mConfig.Init.Exec = nil
}
mConfig.Init.Cmd = cmd
mConfig.Init.SwapSizeMB = c.SwapSizeMB
// Metadata
mConfig.Metadata = lo.Assign(mConfig.Metadata, map[string]string{
fly.MachineConfigMetadataKeyFlyctlVersion: buildinfo.Version().String(),
fly.MachineConfigMetadataKeyFlyPlatformVersion: fly.MachineFlyPlatformVersion2,
fly.MachineConfigMetadataKeyFlyProcessGroup: processGroup,
})
// Services
mConfig.Services = nil
if services := c.AllServices(); len(services) > 0 {
mConfig.Services = lo.Map(services, func(s Service, _ int) fly.MachineService {
return *s.toMachineService()
})
}
// Checks
mConfig.Checks = nil
if len(c.Checks) > 0 {
mConfig.Checks = map[string]fly.MachineCheck{}
for checkName, check := range c.Checks {
machineCheck, err := check.toMachineCheck()
if err != nil {
return nil, err
}
if machineCheck.Port == nil {
if c.HTTPService == nil {
return nil, fmt.Errorf(
"Check '%s' for process group '%s' has no port set and the group has no http_service to take it from",
checkName, processGroup,
)
}
machineCheck.Port = &c.HTTPService.InternalPort
}
mConfig.Checks[checkName] = *machineCheck
}
}
// Env
mConfig.Env = lo.Assign(c.Env)
mConfig.Env["FLY_PROCESS_GROUP"] = processGroup
if c.PrimaryRegion != "" {
mConfig.Env["PRIMARY_REGION"] = c.PrimaryRegion
}
// Statics
mConfig.Statics = nil
for _, s := range c.Statics {
mConfig.Statics = append(mConfig.Statics, &fly.Static{
GuestPath: s.GuestPath,
UrlPrefix: s.UrlPrefix,
TigrisBucket: s.TigrisBucket,
})
}
// Mounts
mConfig.Mounts = nil
for _, m := range c.Mounts {
var extendSizeIncrement, extendSizeLimit int
if m.AutoExtendSizeIncrement != "" {
// Ignore the error because invalid values are caught at config validation time
extendSizeIncrement, _ = helpers.ParseSize(m.AutoExtendSizeIncrement, units.FromHumanSize, units.GB)
}
if m.AutoExtendSizeLimit != "" {
// Ignore the error because invalid values are caught at config validation time
extendSizeLimit, _ = helpers.ParseSize(m.AutoExtendSizeLimit, units.FromHumanSize, units.GB)
}
mConfig.Mounts = append(mConfig.Mounts, fly.MachineMount{
Path: m.Destination,
Name: m.Source,
ExtendThresholdPercent: m.AutoExtendSizeThreshold,
AddSizeGb: extendSizeIncrement,
SizeGbLimit: extendSizeLimit,
})
}
// StopConfig
c.tomachineSetStopConfig(mConfig)
// Files
mConfig.Files = nil
fly.MergeFiles(mConfig, c.MergedFiles)
// Guest
if guest, err := c.toMachineGuest(); err != nil {
return nil, err
} else if guest != nil {
// Only override machine's Guest if app config knows what to set
mConfig.Guest = guest
}
// Restart Policy
mConfig.Restart = nil
for _, restart := range c.Restart {
policy, err := parseRestartPolicy(restart.Policy)
if err != nil {
return nil, err
}
mConfig.Restart = &fly.MachineRestart{
Policy: policy,
MaxRetries: restart.MaxRetries,
}
}
return mConfig, nil
}
func parseRestartPolicy(policy RestartPolicy) (fly.MachineRestartPolicy, error) {
switch policy {
case RestartPolicyAlways:
return fly.MachineRestartPolicyAlways, nil
case RestartPolicyOnFailure:
return fly.MachineRestartPolicyOnFailure, nil
case RestartPolicyNever:
return fly.MachineRestartPolicyNo, nil
default:
return "", fmt.Errorf("invalid restart policy: %s", policy)
}
}
func (c *Config) tomachineSetStopConfig(mConfig *fly.MachineConfig) error {
mConfig.StopConfig = nil
if c.KillSignal == nil && c.KillTimeout == nil {
return nil
}
mConfig.StopConfig = &fly.StopConfig{
Timeout: c.KillTimeout,
Signal: c.KillSignal,
}
return nil
}
func (c *Config) toMachineGuest() (*fly.MachineGuest, error) {
// XXX: Don't be extra smart here, keep it backwards compatible with apps that don't have a [[compute]] section.
// Think about apps that counts on `fly deploy` to respect whatever was set by `fly scale` or the --vm-* family flags.
// It is important to return a `nil` guest when fly.toml doesn't contain a [[compute]] section for the process group.
if len(c.Compute) == 0 {
return nil, nil
} else if len(c.Compute) > 2 {
return nil, fmt.Errorf("2+ compute sections for group %s", c.DefaultProcessName())
}
// At most one compute after group flattening
compute := c.Compute[0]
size := fly.DefaultVMSize
switch {
case compute.Size != "":
size = compute.Size
case compute.MachineGuest != nil && compute.MachineGuest.GPUKind != "":
size = fly.DefaultGPUVMSize
}
guest := &fly.MachineGuest{}
if err := guest.SetSize(size); err != nil {
return nil, err
}
if c.HostDedicationID != "" {
guest.HostDedicationID = c.HostDedicationID
}
if compute.Memory != "" {
mb, err := helpers.ParseSize(compute.Memory, units.RAMInBytes, units.MiB)
switch {
case err != nil:
return nil, err
case mb == 0:
return nil, fmt.Errorf("memory cannot be zero")
default:
guest.MemoryMB = mb
}
}
if compute.MachineGuest != nil {
opts := copier.Option{IgnoreEmpty: true, DeepCopy: true}
err := copier.CopyWithOption(guest, compute.MachineGuest, opts)
if err != nil {
return nil, err
}
}
return guest, nil
}