-
Notifications
You must be signed in to change notification settings - Fork 224
/
update.go
216 lines (164 loc) · 5.13 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
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
package postgres
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/internal/app"
"github.com/superfly/flyctl/internal/client"
"github.com/superfly/flyctl/internal/command"
machines "github.com/superfly/flyctl/internal/command/machine"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/pkg/agent"
"github.com/superfly/flyctl/pkg/flaps"
"github.com/superfly/flyctl/pkg/flypg"
"github.com/superfly/flyctl/pkg/iostreams"
)
func newUpdate() (cmd *cobra.Command) {
const (
long = `Performs a rolling upgrade against the target Postgres cluster.
`
short = "Updates the Postgres cluster to the latest eligible version"
usage = "update"
)
cmd = command.New(usage, short, long, runUpdate,
command.RequireSession,
command.RequireAppName,
)
flag.Add(cmd,
flag.App(),
flag.AppConfig(),
)
return
}
func runUpdate(ctx context.Context) error {
appName := app.NameFromContext(ctx)
client := client.FromContext(ctx).API()
io := iostreams.FromContext(ctx)
app, err := client.GetAppCompact(ctx, appName)
if err != nil {
return fmt.Errorf("get app: %w", err)
}
agentclient, err := agent.Establish(ctx, client)
if err != nil {
return fmt.Errorf("can't establish agent %w", err)
}
dialer, err := agentclient.Dialer(ctx, app.Organization.Slug)
if err != nil {
return fmt.Errorf("can't build tunnel for %s: %s", app.Organization.Slug, err)
}
flapsClient, err := flaps.New(ctx, app)
if err != nil {
return fmt.Errorf("list of machines could not be retrieved: %w", err)
}
// map of machine lease to machine
var machines = make(map[string]*api.V1Machine)
out, err := flapsClient.List(ctx, "started")
if err != nil {
return fmt.Errorf("machines could not be retrieved %w", err)
}
if len(out) == 0 {
return fmt.Errorf("no machines found")
}
fmt.Fprintf(io.Out, "Acquiring lease on postgres cluster\n")
for _, machine := range out {
lease, err := flapsClient.GetLease(ctx, machine.ID, api.IntPointer(40))
if err != nil {
return fmt.Errorf("failed to obtain lease: %w", err)
}
machines[lease.Data.Nonce] = machine
}
var (
leader *api.V1Machine
replicas []*api.V1Machine
)
fmt.Fprintf(io.Out, "Resolving cluster roles\n")
for _, machine := range machines {
address := formatAddress(machine)
pgclient := flypg.NewFromInstance(address, dialer)
if err != nil {
return fmt.Errorf("can't connect to %s: %w", machine.Name, err)
}
role, err := pgclient.NodeRole(ctx)
if err != nil {
return fmt.Errorf("can't get role for %s: %w", machine.Name, err)
}
switch role {
case "leader":
leader = machine
case "replica":
replicas = append(replicas, machine)
}
fmt.Fprintf(io.Out, " %s: %s\n", machine.ID, role)
}
if leader == nil {
return fmt.Errorf("this cluster has no leader")
}
image := fmt.Sprintf("%s:%s", leader.Config.ImageRef.Repository, leader.Config.ImageRef.Tag)
latest, err := client.GetLatestImageDetails(ctx, image)
if err != nil {
return fmt.Errorf("can't get latest image details for %s: %w", image, err)
}
fmt.Fprintf(io.Out, "Updating replicas\n")
for _, replica := range replicas {
current := replica.Config.ImageRef
if current.Labels["fly.version"] == latest.Version {
fmt.Fprintf(io.Out, " %s: already up to date\n", replica.ID)
continue
}
ref := fmt.Sprintf("%s:%s", latest.Repository, latest.Tag)
if err := updateMachine(ctx, app, replica, ref, latest.Version); err != nil {
return fmt.Errorf("can't update %s: %w", replica.ID, err)
}
}
current := leader.Config.ImageRef
if current.Labels["fly.version"] == latest.Version {
fmt.Fprintf(io.Out, "%s(leader): already up to date\n", leader.ID)
return nil
}
ref := fmt.Sprintf("%s:%s", latest.Repository, latest.Tag)
pgclient := flypg.New(app.Name, dialer)
fmt.Fprintf(io.Out, "Failing over to a new leader\n")
if err := pgclient.Failover(ctx); err != nil {
return fmt.Errorf("failed to trigger failover %w", err)
}
fmt.Fprintf(io.Out, "Updating leader\n")
if err := updateMachine(ctx, app, leader, ref, latest.Version); err != nil {
return err
}
for lease, machine := range machines {
if err := flapsClient.ReleaseLease(ctx, machine.ID, lease); err != nil {
return fmt.Errorf("failed to release lease: %w", err)
}
}
fmt.Fprintf(io.Out, "Successfully updated Postgres cluster\n")
return nil
}
func updateMachine(ctx context.Context, app *api.AppCompact, machine *api.V1Machine, image, version string) error {
var io = iostreams.FromContext(ctx)
flaps, err := flaps.New(ctx, app)
if err != nil {
return err
}
fmt.Fprintf(io.Out, "Updating machine %s with image %s %s\n", machine.ID, image, version)
machineConf := machine.Config
machineConf.Image = image
input := api.LaunchMachineInput{
ID: machine.ID,
AppID: app.Name,
OrgSlug: app.Organization.Slug,
Region: machine.Region,
Config: machineConf,
}
updated, err := flaps.Update(ctx, input)
if err != nil {
return err
}
if err := machines.WaitForStart(ctx, flaps, updated); err != nil {
return err
}
return nil
}
func formatAddress(machine *api.V1Machine) string {
return fmt.Sprintf("[%s]", machine.PrivateIP)
}