-
Notifications
You must be signed in to change notification settings - Fork 223
/
create.go
622 lines (542 loc) · 16.6 KB
/
create.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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
package postgres
import (
"context"
"errors"
"fmt"
"strings"
"github.com/spf13/cobra"
fly "github.com/superfly/fly-go"
"github.com/superfly/flyctl/flypg"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/command/apps"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/flapsutil"
"github.com/superfly/flyctl/internal/flyutil"
mach "github.com/superfly/flyctl/internal/machine"
"github.com/superfly/flyctl/internal/prompt"
"github.com/superfly/flyctl/iostreams"
)
func newCreate() *cobra.Command {
const (
short = "Create a new Postgres cluster"
long = short + "\n"
)
cmd := command.New("create", short, long, run,
command.RequireSession,
)
flag.Add(
cmd,
flag.Region(),
flag.Org(),
flag.Detach(),
flag.String{
Name: "name",
Shorthand: "n",
Description: "The name of your Postgres app",
},
flag.String{
Name: "password",
Shorthand: "p",
Description: "The superuser password. The password will be generated for you if you leave this blank",
},
flag.String{
Name: "vm-size",
Description: "the size of the VM",
},
flag.Int{
Name: "initial-cluster-size",
Description: "Initial cluster size",
},
flag.Int{
Name: "volume-size",
Description: "The volume size in GB",
},
flag.String{
Name: "consul-url",
Description: "Opt into using an existing consul as the backend store by specifying the target consul url.",
},
flag.String{
Name: "snapshot-id",
Description: "Creates the volume with the contents of the snapshot",
},
flag.String{
Name: "fork-from",
Description: "Specify a source Postgres application to fork from. Format: <app-name> or <app-name>:<volume-id>",
},
flag.String{
Name: "image-ref",
Description: "Specify a non-default base image for the Postgres app",
},
flag.Bool{
Name: "stolon",
Description: "Create a postgres cluster that's managed by Stolon",
Default: false,
},
flag.Bool{
Name: "flex",
Description: "Create a postgres cluster that's managed by Repmgr",
Default: true,
},
flag.Bool{
Name: "autostart",
Description: "Automatically start a stopped Postgres app when a network request is received",
Default: false,
},
)
return cmd
}
// Since we want to be able to create PG clusters from other commands,
// we pass the name, region and org to CreateCluster. Other flags that don't prompt may
// be safely passed through from other commands.
func run(ctx context.Context) (err error) {
var (
appName = flag.GetString(ctx, "name")
client = flyutil.ClientFromContext(ctx)
io = iostreams.FromContext(ctx)
colorize = io.ColorScheme()
)
// pre-fetch platform regions for later use
prompt.PlatformRegions(ctx)
if appName == "" {
if appName, err = prompt.SelectAppName(ctx); err != nil {
return
}
}
var org *fly.Organization
org, err = prompt.Org(ctx)
if err != nil {
return
}
params := &ClusterParams{
Password: flag.GetString(ctx, "password"),
SnapshotID: flag.GetString(ctx, "snapshot-id"),
Detach: flag.GetDetach(ctx),
Autostart: flag.GetBool(ctx, "autostart"),
}
pgConfig := &PostgresConfiguration{
Name: appName,
VMSize: flag.GetString(ctx, "vm-size"),
InitialClusterSize: flag.GetInt(ctx, "initial-cluster-size"),
ImageRef: flag.GetString(ctx, "image-ref"),
DiskGb: flag.GetInt(ctx, "volume-size"),
}
forkFrom := flag.GetString(ctx, "fork-from")
if forkFrom != "" {
// Snapshot ID may not be specified with fork-from
if params.SnapshotID != "" {
return fmt.Errorf("cannot specify both --fork-from and --snapshot-id")
}
// If initial-cluster-size is not specified, set it to 1
if pgConfig.InitialClusterSize == 0 {
pgConfig.InitialClusterSize = 1
}
// Initial cluster size may not be greater than 1 with fork-from
if pgConfig.InitialClusterSize > 1 {
fmt.Fprintf(io.Out, colorize.Yellow("Warning: --initial-cluster-size is ignored when specifying --fork-from\n"))
pgConfig.InitialClusterSize = 1
}
// Check to see whether the fork-from value includes a volume ID
forkSlice := strings.Split(forkFrom, ":")
if len(forkSlice) > 2 {
return fmt.Errorf("invalid --fork-from format. See `fly pg create --help` for more information")
}
// Resolve specified fork-from app
forkApp, err := client.GetAppCompact(ctx, forkSlice[0])
if err != nil {
return fmt.Errorf("Failed to resolve the specified fork-from app %s: %w", forkSlice[0], err)
}
// Confirm fork-app is a postgres app
if !forkApp.IsPostgresApp() {
return fmt.Errorf("The fork-from app %q must be a postgres app", forkApp.Name)
}
ctx, err := apps.BuildContext(ctx, forkApp)
if err != nil {
return err
}
machines, err := mach.ListActive(ctx)
if err != nil {
return fmt.Errorf("Failed to list machines associated with %s: %w", forkApp.Name, err)
}
// If the fork-from value includes a volume ID, use that. Otherwise, attempt to resolve the
// volume ID associated with the fork-from app's primary instance.
if len(forkSlice) == 2 {
params.ForkFrom = forkSlice[1]
} else {
volID, err := resolveForkFromVolume(ctx, machines)
if err != nil {
return err
}
params.ForkFrom = volID
}
flapsClient := flapsutil.ClientFromContext(ctx)
// Resolve the volume
vol, err := flapsClient.GetVolume(ctx, params.ForkFrom)
if err != nil {
return fmt.Errorf("Failed to resolve the specified fork-from volume %s: %w", params.ForkFrom, err)
}
appName, err := client.GetAppNameFromVolume(ctx, vol.ID)
if err != nil {
return err
}
// Confirm that the volume is associated with the fork-from app
if *appName != forkApp.Name {
return fmt.Errorf("The volume %q specified must be associated with the fork-from app %q", vol.ID, forkApp.Name)
}
// If the region isn't specified, set the region of the fork target
reg := flag.GetString(ctx, "region")
if reg == "" {
reg = vol.Region
flag.SetString(ctx, "region", vol.Region)
}
// Confirm that the region matches the region of the fork target
if reg != "" && reg != vol.Region {
return fmt.Errorf("Target region %q must match the region of the fork volume target: %q", reg, vol.Region)
}
// If the volume size isn't specified, set the volume size of the fork target
if pgConfig.DiskGb == 0 {
pgConfig.DiskGb = vol.SizeGb
}
// Confirm that the volume size is greater than or equal to the fork target
if pgConfig.DiskGb < vol.SizeGb {
return fmt.Errorf("The target volume size %dGB must be greater than or equal to the volume fork target: %dGB", pgConfig.DiskGb, vol.SizeGb)
}
// Attempt to resolve the image ref from the machine tied to the fork volume.
if pgConfig.ImageRef == "" {
pgConfig.ImageRef = resolveImageFromForkVolume(vol, machines)
}
// Resolve the fork-from app manager
params.Manager = resolveForkFromManager(ctx, machines)
params.ForkFrom = vol.ID
}
params.PostgresConfiguration = *pgConfig
var region *fly.Region
region, err = prompt.Region(ctx, !org.PaidPlan, prompt.RegionParams{
Message: "",
})
if err != nil {
return
}
// Set the default manager if not specified or already resolved
if params.Manager == "" {
params.Manager = flypg.ReplicationManager
if flag.GetBool(ctx, "stolon") {
params.Manager = flypg.StolonManager
}
}
return CreateCluster(ctx, org, region, params)
}
// CreateCluster creates a Postgres cluster with an optional name. The name will be prompted for if not supplied.
func CreateCluster(ctx context.Context, org *fly.Organization, region *fly.Region, params *ClusterParams) (err error) {
var (
client = flyutil.ClientFromContext(ctx)
io = iostreams.FromContext(ctx)
)
input := &flypg.CreateClusterInput{
AppName: params.Name,
Organization: org,
ImageRef: params.PostgresConfiguration.ImageRef,
Region: region.Code,
Manager: params.Manager,
Autostart: params.Autostart,
ForkFrom: params.ForkFrom,
}
customConfig := params.DiskGb != 0 || params.VMSize != "" || params.InitialClusterSize != 0 || params.ScaleToZero != nil
var config *PostgresConfiguration
if !customConfig {
fmt.Fprintf(io.Out, "For pricing information visit: https://fly.io/docs/about/pricing/#postgresql-clusters")
msg := "Select configuration:"
configurations := postgresConfigurations(input.Manager)
var selected int
options := []string{}
for i, cfg := range configurations {
options = append(options, cfg.Description)
if selected == 0 && !strings.HasPrefix(cfg.Description, "Dev") {
selected = i
}
}
if err := prompt.Select(ctx, &selected, msg, configurations[selected].Description, options...); err != nil {
return err
}
config = &postgresConfigurations(input.Manager)[selected]
if input.Manager == flypg.ReplicationManager && config.VMSize == "shared-cpu-1x" {
confirm, err := prompt.Confirm(ctx, "Scale single node pg to zero after one hour?")
if err != nil {
return err
}
input.ScaleToZero = confirm
}
if config.VMSize == "" {
// User has opted into choosing a custom configuration.
customConfig = true
}
}
if customConfig {
// Resolve cluster size
if params.PostgresConfiguration.InitialClusterSize == 0 {
clusterSizePrompt := "Initial cluster size"
defaultClusterSize := 2
if input.Manager == flypg.ReplicationManager {
defaultClusterSize = 3
clusterSizePrompt = "Initial cluster size - Specify at least 3 for HA"
}
err := prompt.Int(ctx, ¶ms.InitialClusterSize, clusterSizePrompt, defaultClusterSize, true)
if err != nil {
return err
}
}
input.InitialClusterSize = params.PostgresConfiguration.InitialClusterSize
// Resolve VM size
vmSize, err := resolveVMSize(ctx, params.VMSize)
if err != nil {
return err
}
input.VMSize = vmSize
if params.ScaleToZero != nil {
input.ScaleToZero = *params.ScaleToZero
if input.ScaleToZero && input.InitialClusterSize != 1 {
return errors.New("scale to zero is only supported for single node clusters")
}
}
// Resolve volume size
if params.DiskGb == 0 {
if err = prompt.Int(ctx, ¶ms.DiskGb, "Volume size", 10, false); err != nil {
return err
}
}
input.VolumeSize = fly.IntPointer(params.DiskGb)
input.Autostart = params.Autostart
} else {
// Resolve configuration from pre-defined configuration.
vmSize, err := resolveVMSize(ctx, config.VMSize)
if err != nil {
return err
}
input.VMSize = vmSize
input.VolumeSize = fly.IntPointer(config.DiskGb)
input.InitialClusterSize = config.InitialClusterSize
input.ImageRef = params.ImageRef
input.Autostart = params.Autostart
}
if params.Password != "" {
input.Password = params.Password
}
if params.SnapshotID != "" {
input.SnapshotID = ¶ms.SnapshotID
}
fmt.Fprintf(io.Out, "Creating postgres cluster in organization %s\n", org.Slug)
launcher := flypg.NewLauncher(client)
return launcher.LaunchMachinesPostgres(ctx, input, params.Detach)
}
func resolveVMSize(ctx context.Context, targetSize string) (*fly.VMSize, error) {
// verify the specified size
if targetSize != "" {
for _, size := range MachineVMSizes() {
if targetSize == size.Name {
return &size, nil
}
}
return nil, fmt.Errorf("VM size %q is not valid. For a full list of supported sizes use the command 'flyctl platform vm-sizes'", targetSize)
}
// prompt user to select machine specific size.
return prompt.SelectVMSize(ctx, MachineVMSizes())
}
type PostgresConfiguration struct {
Name string
Description string
ImageRef string
InitialClusterSize int
VMSize string
MemoryMb int
DiskGb int
}
type ClusterParams struct {
PostgresConfiguration
Password string
SnapshotID string
Detach bool
Manager string
ForkFrom string
Autostart bool
ScaleToZero *bool
}
func postgresConfigurations(manager string) []PostgresConfiguration {
if manager == flypg.StolonManager {
return stolonConfigurations()
}
return flexConfigurations()
}
func stolonConfigurations() []PostgresConfiguration {
return []PostgresConfiguration{
{
Description: "Development - Single node, 1x shared CPU, 256MB RAM, 1GB disk",
DiskGb: 1,
InitialClusterSize: 1,
MemoryMb: 256,
VMSize: "shared-cpu-1x",
},
{
Description: "Production - Highly available, 2x shared CPUs, 4GB RAM, 40GB disk",
DiskGb: 40,
InitialClusterSize: 2,
MemoryMb: 4096,
VMSize: "shared-cpu-2x",
},
{
Description: "Production - Highly available, 4x shared CPUs, 8GB RAM, 80GB disk",
DiskGb: 80,
InitialClusterSize: 2,
MemoryMb: 8192,
VMSize: "shared-cpu-4x",
},
{
Description: "Specify custom configuration",
DiskGb: 0,
InitialClusterSize: 0,
MemoryMb: 0,
VMSize: "",
},
}
}
func flexConfigurations() []PostgresConfiguration {
return []PostgresConfiguration{
{
Description: "Development - Single node, 1x shared CPU, 256MB RAM, 1GB disk",
DiskGb: 1,
InitialClusterSize: 1,
MemoryMb: 256,
VMSize: "shared-cpu-1x",
},
{
Description: "Production (High Availability) - 3 nodes, 2x shared CPUs, 4GB RAM, 40GB disk",
DiskGb: 40,
InitialClusterSize: 3,
MemoryMb: 4096,
VMSize: "shared-cpu-2x",
},
{
Description: "Production (High Availability) - 3 nodes, 4x shared CPUs, 8GB RAM, 80GB disk",
DiskGb: 80,
InitialClusterSize: 3,
MemoryMb: 8192,
VMSize: "shared-cpu-4x",
},
{
Description: "Specify custom configuration",
DiskGb: 0,
InitialClusterSize: 0,
MemoryMb: 0,
VMSize: "",
},
}
}
// machineVMSizes represents the available VM configurations for Machines.
func MachineVMSizes() []fly.VMSize {
// TODO - Eventually we will have a flaps endpoint for this.
return []fly.VMSize{
{
Name: "shared-cpu-1x",
CPUClass: "shared",
CPUCores: 1,
MemoryMB: 256,
MemoryGB: 0.25,
},
{
Name: "shared-cpu-2x",
CPUClass: "shared",
CPUCores: 2,
MemoryMB: 4096,
MemoryGB: 4,
},
{
Name: "shared-cpu-4x",
CPUClass: "shared",
CPUCores: 4,
MemoryMB: 8192,
MemoryGB: 8,
},
{
Name: "shared-cpu-8x",
CPUClass: "shared",
CPUCores: 8,
MemoryMB: 16384,
MemoryGB: 16,
},
{
Name: "performance-1x",
CPUClass: "performance",
CPUCores: 1,
MemoryMB: 2048,
MemoryGB: 2,
},
{
Name: "performance-2x",
CPUClass: "performance",
CPUCores: 2,
MemoryMB: 4096,
MemoryGB: 4,
},
{
Name: "performance-4x",
CPUClass: "performance",
CPUCores: 4,
MemoryMB: 8192,
MemoryGB: 8,
},
{
Name: "performance-8x",
CPUClass: "performance",
CPUCores: 8,
MemoryMB: 16384,
MemoryGB: 16,
},
{
Name: "performance-16x",
CPUClass: "performance",
CPUCores: 16,
MemoryMB: 32768,
MemoryGB: 32,
},
}
}
func resolveForkFromVolume(ctx context.Context, machines []*fly.Machine) (string, error) {
if len(machines) == 0 {
return "", fmt.Errorf("No machines associated with fork-from target. See `fly pg create --help` for more information")
}
primaryRegion := machines[0].Config.Env["PRIMARY_REGION"]
// Attempt to resolve the volume associated with the primary machine
for _, m := range machines {
// Exclude machines that are not in the primary region
if m.Config.Env["PRIMARY_REGION"] != primaryRegion {
continue
}
role := machineRole(m)
// Exclude machines that are not the primary (flex) or leader (stolon)
if role != "primary" && role != "leader" {
continue
}
return m.Config.Mounts[0].Volume, nil
}
return "", fmt.Errorf("Failed to resolve the volume associated with the primary instance. See `fly pg create --help` for more information")
}
func resolveForkFromManager(ctx context.Context, machines []*fly.Machine) string {
if flag.GetBool(ctx, "stolon") {
return flypg.StolonManager
}
// We can't resolve the manager type, so we'll default to repmgr
if len(machines) == 0 {
return flypg.ReplicationManager
}
if machines[0].ImageRef.Labels["fly.pg-manager"] == flypg.ReplicationManager {
return flypg.ReplicationManager
}
return flypg.StolonManager
}
func resolveImageFromForkVolume(vol *fly.Volume, machines []*fly.Machine) string {
// Attempt to resolve the machine image that's associated with the volume
for _, m := range machines {
if m.Config.Mounts[0].Volume == vol.ID {
return m.FullImageRef()
}
}
return ""
}