/
update.go
112 lines (82 loc) · 3.77 KB
/
update.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
package machine
import (
"context"
"fmt"
"time"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/flaps"
"github.com/superfly/flyctl/internal/watch"
"github.com/superfly/flyctl/iostreams"
)
func Update(ctx context.Context, m *api.Machine, input *api.LaunchMachineInput) error {
var (
flapsClient = flaps.FromContext(ctx)
io = iostreams.FromContext(ctx)
colorize = io.ColorScheme()
updatedMachine *api.Machine
err error
)
if input != nil && input.Config != nil && input.Config.Guest != nil {
// Check that there's a valid number of CPUs
var validNumCpus []int
if input.Config.Guest.CPUKind == "shared" {
validNumCpus = append(validNumCpus, 1, 2, 4, 6, 8)
} else if input.Config.Guest.CPUKind == "performance" {
validNumCpus = append(validNumCpus, 1, 2, 4, 6, 8, 10, 12, 14, 16)
}
validCpuNum := false
for _, num := range validNumCpus {
if num == input.Config.Guest.CPUs {
validCpuNum = true
break
}
}
if !validCpuNum {
return fmt.Errorf("invalid config: invalid number of CPUs for %s guest. Valid numbers are %v\nView more information here: https://fly.io/docs/about/pricing/#machines", input.Config.Guest.CPUKind, validNumCpus)
}
if input.Config.Guest.CPUKind == "shared" && input.Config.Guest.MemoryMB%256 != 0 {
return fmt.Errorf("invalid config: invalid memory size %d; must be in 256 MiB increment (%d would work)\nView more information here: https://fly.io/docs/about/pricing/#machines", input.Config.Guest.MemoryMB, input.Config.Guest.MemoryMB-(input.Config.Guest.MemoryMB%256))
} else if input.Config.Guest.CPUKind == "performance" && input.Config.Guest.MemoryMB%1024 != 0 {
return fmt.Errorf("invalid config: invalid memory size %d; must be in 1024 MiB increment (%d would work)\nView more information here: https://fly.io/docs/about/pricing/#machines", input.Config.Guest.MemoryMB, input.Config.Guest.MemoryMB-(input.Config.Guest.MemoryMB%1024))
}
// Check memory sizes
var min_memory_size int
if input.Config.Guest.CPUKind == "shared" {
min_memory_size = api.MIN_MEMORY_MB_PER_SHARED_CPU * input.Config.Guest.CPUs
} else if input.Config.Guest.CPUKind == "performance" {
min_memory_size = api.MIN_MEMORY_MB_PER_CPU * input.Config.Guest.CPUs
}
if min_memory_size > input.Config.Guest.MemoryMB {
return fmt.Errorf("invalid config: for machines with %d CPUs, the minimum amount of memory is %d MiB\nView more information here: https://fly.io/docs/about/pricing/#machines", input.Config.Guest.CPUs, min_memory_size)
}
var maxMemory int
if input.Config.Guest.CPUKind == "shared" {
maxMemory = input.Config.Guest.CPUs * api.MAX_MEMORY_MB_PER_SHARED_CPU
} else if input.Config.Guest.CPUKind == "performance" {
maxMemory = input.Config.Guest.CPUs * api.MAX_MEMORY_MB_PER_CPU
}
if input.Config.Guest.MemoryMB > maxMemory {
return fmt.Errorf("invalid config: for machines with %d CPUs, the maximum amount of memory is %d MiB\nView more information here: https://fly.io/docs/about/pricing/#machines", input.Config.Guest.CPUs, maxMemory)
}
}
fmt.Fprintf(io.Out, "Updating machine %s\n", colorize.Bold(m.ID))
input.ID = m.ID
updatedMachine, err = flapsClient.Update(ctx, *input, m.LeaseNonce)
if err != nil {
return fmt.Errorf("could not stop machine %s: %w", input.ID, err)
}
waitForAction := "start"
if m.Config.Schedule != "" {
waitForAction = "stop"
}
if err := WaitForStartOrStop(ctx, updatedMachine, waitForAction, time.Minute*5); err != nil {
return err
}
if !input.SkipHealthChecks {
if err := watch.MachinesChecks(ctx, []*api.Machine{updatedMachine}); err != nil {
return fmt.Errorf("failed to wait for health checks to pass: %w", err)
}
}
fmt.Fprintf(io.Out, "Machine %s updated successfully!\n", colorize.Bold(m.ID))
return nil
}