-
Notifications
You must be signed in to change notification settings - Fork 224
/
config.go
457 lines (383 loc) · 11.1 KB
/
config.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
package postgres
import (
"context"
"fmt"
"strconv"
"strings"
"github.com/pkg/errors"
"github.com/r3labs/diff"
"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"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/prompt"
"github.com/superfly/flyctl/internal/render"
"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 newConfig() (cmd *cobra.Command) {
// TODO - Add better top level docs.
const (
long = `View and manage Postgres configuration.
`
short = "View and manage Postgres configuration."
)
cmd = command.New("config", short, long, nil)
cmd.AddCommand(
newConfigView(),
newConfigUpdate(),
)
return
}
// pgSettingMap maps the command-line argument to the actual pgParameter.
// This also acts as a whitelist as far as what's configurable via flyctl and
// can be expanded on as needed.
var pgSettingMap = map[string]string{
"wal-level": "wal_level",
"max-connections": "max_connections",
"log-statement": "log_statement",
"log-min-duration-statement": "log_min_duration_statement",
}
func newConfigView() (cmd *cobra.Command) {
const (
long = `View your Postgres configuration
`
short = "View your Postgres configuration"
usage = "view"
)
cmd = command.New(usage, short, long, runConfigView,
command.RequireSession,
command.RequireAppName,
)
flag.Add(cmd,
flag.App(),
flag.AppConfig(),
)
return
}
func runConfigView(ctx context.Context) error {
client := client.FromContext(ctx).API()
appName := app.NameFromContext(ctx)
io := iostreams.FromContext(ctx)
colorize := io.ColorScheme()
app, err := client.GetAppBasic(ctx, appName)
if err != nil {
return fmt.Errorf("get app: %w", err)
}
agentclient, err := agent.Establish(ctx, client)
if err != nil {
return errors.Wrap(err, "can't establish agent")
}
dialer, err := agentclient.Dialer(ctx, app.Organization.Slug)
if err != nil {
return fmt.Errorf("ssh: can't build tunnel for %s: %s", app.Organization.Slug, err)
}
pgclient := flypg.New(app.Name, dialer)
var settings []string
for _, k := range pgSettingMap {
settings = append(settings, k)
}
resp, err := pgclient.SettingsView(ctx, settings)
if err != nil {
return err
}
pendingRestart := false
rows := make([][]string, 0, len(resp.Settings))
for _, setting := range resp.Settings {
desc := setting.Desc
switch setting.VarType {
case "enum":
e := strings.Join(setting.EnumVals, ", ")
desc = fmt.Sprintf("%s [%s]", desc, e)
case "integer":
desc = fmt.Sprintf("%s (%s, %s)", desc, setting.MinVal, setting.MaxVal)
case "real":
min, err := strconv.ParseFloat(setting.MinVal, 32)
if err != nil {
return nil
}
max, err := strconv.ParseFloat(setting.MaxVal, 32)
if err != nil {
return err
}
desc = fmt.Sprintf("%s (%.1f, %.1f)", desc, min, max)
case "bool":
desc = fmt.Sprintf("%s [on, off]", desc)
}
value := setting.Setting
restart := fmt.Sprint(setting.PendingRestart)
if setting.PendingRestart {
pendingRestart = true
restart = colorize.Bold(restart)
}
if setting.PendingChange != "" {
p := colorize.Bold(fmt.Sprintf("(%s)", setting.PendingChange))
value = fmt.Sprintf("%s -> %s", value, p)
}
rows = append(rows, []string{
strings.Replace(setting.Name, "_", "-", -1),
value,
desc,
restart,
})
}
_ = render.Table(io.Out, "", rows, "Name", "Value", "Description", "Pending Restart")
if pendingRestart {
fmt.Fprintln(io.Out, colorize.Yellow("Some changes are awaiting a restart!"))
fmt.Fprintln(io.Out, colorize.Yellow(fmt.Sprintf("To apply changes, run: `DEV=1 fly services postgres restart --app %s`", appName)))
}
return nil
}
func newConfigUpdate() (cmd *cobra.Command) {
const (
long = `Update Postgres configuration.
`
short = "Update Postgres configuration."
usage = "update"
)
cmd = command.New(usage, short, long, runConfigUpdate,
command.RequireSession,
command.RequireAppName,
)
flag.Add(cmd,
flag.App(),
flag.AppConfig(),
flag.String{
Name: "max-connections",
Description: "Sets the maximum number of concurrent connections.",
},
flag.String{
Name: "wal-level",
Description: "Sets the level of information written to the WAL. (minimal, replica, logical).",
},
flag.String{
Name: "log-statement",
Description: "Sets the type of statements logged. (none, ddl, mod, all)",
},
flag.String{
Name: "log-min-duration-statement",
Description: "Sets the minimum execution time above which all statements will be logged. (ms)",
},
flag.Bool{
Name: "auto-confirm",
Description: "Will automatically confirm changes without an interactive prompt.",
},
)
return
}
func runConfigUpdate(ctx context.Context) error {
client := client.FromContext(ctx).API()
appName := app.NameFromContext(ctx)
app, err := client.GetAppCompact(ctx, appName)
if err != nil {
return fmt.Errorf("get app: %w", err)
}
pgCmd, err := newPostgresCmd(ctx, app)
if err != nil {
return err
}
io := iostreams.FromContext(ctx)
colorize := io.ColorScheme()
// Identify requested configuration changes.
rChanges := map[string]string{}
keys := []string{}
for key := range pgSettingMap {
val := flag.GetString(ctx, key)
if val != "" {
rChanges[pgSettingMap[key]] = val
keys = append(keys, pgSettingMap[key])
}
}
if len(rChanges) == 0 {
return fmt.Errorf("no changes were specified")
}
agentclient, err := agent.Establish(ctx, client)
if err != nil {
return errors.Wrap(err, "can't establish agent")
}
dialer, err := agentclient.Dialer(ctx, app.Organization.Slug)
if err != nil {
return fmt.Errorf("ssh: can't build tunnel for %s: %s", app.Organization.Slug, err)
}
pgclient := flypg.New(app.Name, dialer)
settings, err := pgclient.SettingsView(ctx, keys)
if err != nil {
return err
}
// // Pull existing configuration
// settings, err := pgCmd.viewSettings(keys)
// if err != nil {
// return err
// }
// Verfiy that input values are within acceptible ranges.
// Stolon does not verify this, so we need to do it here.
for k, v := range rChanges {
for _, setting := range settings.Settings {
if setting.Name == k {
if err = validateConfigValue(setting, k, v); err != nil {
return err
}
}
}
}
// Construct a map of the active configuration settings so we can compare.
oValues := map[string]string{}
for _, setting := range settings.Settings {
oValues[setting.Name] = setting.Setting
}
// Calculate diff
changelog, _ := diff.Diff(oValues, rChanges)
if len(changelog) == 0 {
return fmt.Errorf("no changes to apply")
}
restartRequired := false
rows := make([][]string, 0, len(changelog))
for _, change := range changelog {
requiresRestart := isRestartRequired(settings, change.Path[len(change.Path)-1])
if requiresRestart {
restartRequired = true
}
name := strings.Replace(change.Path[len(change.Path)-1], "_", "-", -1)
rows = append(rows, []string{
name,
fmt.Sprint(change.From),
fmt.Sprint(change.To),
fmt.Sprint(requiresRestart),
})
}
_ = render.Table(io.Out, "", rows, "Name", "Value", "Target value", "Restart Required")
if !flag.GetBool(ctx, "auto-confirm") {
const msg = "Are you sure you want to apply these changes?"
switch confirmed, err := prompt.Confirmf(ctx, msg); {
case err == nil:
if !confirmed {
return nil
}
case prompt.IsNonInteractive(err):
return prompt.NonInteractiveError("auto-confirm flag must be specified when not running interactively")
default:
return err
}
}
flapsClient, err := flaps.New(ctx, app)
if err != nil {
return fmt.Errorf("list of machines could not be retrieved: %w", err)
}
machines, err := flapsClient.List(ctx, "started")
if err != nil {
return fmt.Errorf("machines could not be retrieved")
}
var leader *api.V1Machine
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)
}
if role == "leader" {
leader = machine
break
} else if role == "replica" {
continue
}
}
if leader == nil {
return fmt.Errorf("no leader found")
}
info, err := client.GetAppCompact(ctx, appName)
if err != nil {
return fmt.Errorf("get app: %w", err)
}
// obtain lease on leader
flaps, err := flaps.New(ctx, info)
if err != nil {
return err
}
// get lease on machine
lease, err := flaps.GetLease(ctx, leader.ID, api.IntPointer(40))
if err != nil {
return fmt.Errorf("failed to obtain lease: %w", err)
}
fmt.Fprintf(io.Out, "Acquired lease %s on machine: %s\n", lease.Data.Nonce, leader.ID)
fmt.Fprintln(io.Out, "Performing update...")
err = pgCmd.updateSettings(rChanges)
if err != nil {
return err
}
err = flaps.ReleaseLease(ctx, leader.ID, lease.Data.Nonce)
if err != nil {
return fmt.Errorf("failed to release lease: %w", err)
}
fmt.Fprintln(io.Out, "Update complete!")
if restartRequired {
fmt.Fprintln(io.Out, colorize.Yellow("Please note that some of your changes will require a cluster restart before they will be applied."))
fmt.Fprintln(io.Out, colorize.Yellow("To review the state of your changes, run: `DEV=1 fly services postgres config view`"))
}
return nil
}
func isRestartRequired(pgSettings *flypg.PGSettings, name string) bool {
for _, s := range pgSettings.Settings {
if s.Name == name {
if s.Context == "postmaster" {
return true
}
}
}
return false
}
func validateConfigValue(setting flypg.PGSetting, key, val string) error {
switch setting.VarType {
case "enum":
for _, enumVal := range setting.EnumVals {
if enumVal == val {
return nil
}
}
return fmt.Errorf("invalid value specified for %s. Received: %s, Accepted values: [%s]", key, val, strings.Join(setting.EnumVals, ", "))
case "integer":
min, err := strconv.Atoi(setting.MinVal)
if err != nil {
return err
}
max, err := strconv.Atoi(setting.MaxVal)
if err != nil {
return err
}
v, err := strconv.Atoi(val)
if err != nil {
return err
}
if v < min || v > max {
return fmt.Errorf("invalid value specified for %s. (Received: %s, Accepted range: (%s, %s)", key, val, setting.MinVal, setting.MaxVal)
}
case "real":
min, err := strconv.ParseFloat(setting.MinVal, 32)
if err != nil {
return err
}
max, err := strconv.ParseFloat(setting.MaxVal, 32)
if err != nil {
return err
}
v, err := strconv.ParseFloat(val, 32)
if err != nil {
return err
}
if v < min || v > max {
return fmt.Errorf("invalid value specified for %s. (Received: %s, Accepted range: (%.1f, %.1f)", key, val, min, max)
}
case "bool":
if val != "on" && val != "off" {
return fmt.Errorf("invalid value specified for %s. (Received: %s, Accepted values: [on, off]", key, val)
}
}
return nil
}