-
Notifications
You must be signed in to change notification settings - Fork 221
/
machines.go
90 lines (78 loc) · 2.21 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
package flag
import (
"context"
"fmt"
"slices"
"strings"
"github.com/superfly/flyctl/api"
)
var validGPUKinds = []string{"a100-pcie-40gb", "a100-sxm4-80gb"}
// Returns a MachineGuest based on the flags provided overwriting a default VM
func GetMachineGuest(ctx context.Context, guest *api.MachineGuest) (*api.MachineGuest, error) {
defaultVMSize := api.DefaultVMSize
if IsSpecified(ctx, "vm-gpu-kind") {
defaultVMSize = api.DefaultGPUVMSize
}
if guest == nil {
guest = &api.MachineGuest{}
guest.SetSize(defaultVMSize)
}
if IsSpecified(ctx, "vm-size") {
if err := guest.SetSize(GetString(ctx, "vm-size")); err != nil {
return nil, err
}
}
if IsSpecified(ctx, "vm-cpus") {
guest.CPUs = GetInt(ctx, "vm-cpus")
if guest.CPUs == 0 {
return nil, fmt.Errorf("--vm-cpus cannot be zero")
}
}
if IsSpecified(ctx, "vm-memory") {
guest.MemoryMB = GetInt(ctx, "vm-memory")
if guest.MemoryMB == 0 {
return nil, fmt.Errorf("--vm-memory cannot be zero")
}
}
if IsSpecified(ctx, "vm-cpu-kind") {
guest.CPUKind = GetString(ctx, "vm-cpu-kind")
if k := guest.CPUKind; k != "shared" && k != "performance" {
return nil, fmt.Errorf("--vm-cpu-kind must be set to 'shared' or 'performance'")
}
}
if IsSpecified(ctx, "vm-gpu-kind") {
m := GetString(ctx, "vm-gpu-kind")
if !slices.Contains(validGPUKinds, m) {
return nil, fmt.Errorf("--vm-gpu-kind must be set to one of: %v", strings.Join(validGPUKinds, ", "))
}
guest.GPUKind = m
}
return guest, nil
}
var VMSizeFlags = Set{
String{
Name: "vm-size",
Description: `The VM size to set machines to. See "fly platform vm-sizes" for valid values`,
},
Int{
Name: "vm-cpus",
Description: "Number of CPUs",
Aliases: []string{"cpus"},
},
String{
Name: "vm-cpu-kind",
Description: "The kind of CPU to use ('shared' or 'performance')",
Aliases: []string{"vm-cpukind"},
},
Int{
Name: "vm-memory",
Description: "Memory (in megabytes) to attribute to the VM",
Aliases: []string{"memory"},
},
String{
Name: "vm-gpu-kind",
Description: fmt.Sprintf("If set, the GPU model to attach (%v)", strings.Join(validGPUKinds, ", ")),
Aliases: []string{"vm-gpukind"},
Hidden: true,
},
}