-
Notifications
You must be signed in to change notification settings - Fork 224
/
postgres.go
441 lines (373 loc) · 12.2 KB
/
postgres.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
package cmd
import (
"fmt"
"os"
"strings"
"time"
"github.com/AlecAivazis/survey/v2"
"github.com/briandowns/spinner"
"github.com/hashicorp/go-version"
"github.com/logrusorgru/aurora"
"github.com/pkg/errors"
"github.com/superfly/flyctl/agent"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/cmdctx"
"github.com/superfly/flyctl/flypg"
"github.com/superfly/flyctl/helpers"
"github.com/superfly/flyctl/internal/flag"
)
type PostgresConfiguration struct {
Name string
Description string
ImageRef string
InitialClusterSize int
VmSize string
MemoryMb int
DiskGb int
}
func postgresConfigurations() []PostgresConfiguration {
return []PostgresConfiguration{
{
Description: "Development - Single node, 1x shared CPU, 256MB RAM, 1GB disk",
DiskGb: 1,
ImageRef: "flyio/postgres",
InitialClusterSize: 1,
MemoryMb: 256,
VmSize: "shared-cpu-1x",
},
{
Description: "Production - Highly available, 1x shared CPU, 256MB RAM, 10GB disk",
DiskGb: 10,
ImageRef: "flyio/postgres",
InitialClusterSize: 2,
MemoryMb: 256,
VmSize: "shared-cpu-1x",
},
{
Description: "Production - Highly available, 1x Dedicated CPU, 2GB RAM, 50GB disk",
DiskGb: 50,
ImageRef: "flyio/postgres",
InitialClusterSize: 2,
MemoryMb: 2048,
VmSize: "dedicated-cpu-1x",
},
{
Description: "Production - Highly available, 2x Dedicated CPU's, 4GB RAM, 100GB disk",
DiskGb: 100,
ImageRef: "flyio/postgres",
InitialClusterSize: 2,
MemoryMb: 4096,
VmSize: "dedicated-cpu-2x",
},
{
Description: "Specify custom configuration",
DiskGb: 0,
ImageRef: "flyio/postgres",
InitialClusterSize: 0,
MemoryMb: 0,
VmSize: "",
},
}
}
func runCreatePostgresCluster(cmdCtx *cmdctx.CmdContext) error {
ctx := cmdCtx.Command.Context()
name := cmdCtx.Config.GetString("name")
if name == "" {
n, err := inputAppName("", false)
if err != nil {
return err
}
name = n
}
orgSlug := cmdCtx.Config.GetString("organization")
org, err := selectOrganization(ctx, cmdCtx.Client.API(), orgSlug)
if err != nil {
return err
}
regionCode := cmdCtx.Config.GetString("region")
var region *api.Region
region, err = selectRegion(ctx, cmdCtx.Client.API(), regionCode)
if err != nil {
return err
}
input := api.CreatePostgresClusterInput{
OrganizationID: org.ID,
Name: name,
Region: api.StringPointer(region.Code),
ImageRef: api.StringPointer("flyio/postgres"),
}
volumeSize := cmdCtx.Config.GetInt("volume-size")
initialClusterSize := cmdCtx.Config.GetInt("initial-cluster-size")
vmSizeName := cmdCtx.Config.GetString("vm-size")
customConfig := volumeSize != 0 || vmSizeName != "" || initialClusterSize != 0
var pgConfig *PostgresConfiguration
var vmSize *api.VMSize
// If no custom configuration settings have been passed in, prompt user to select
// from a list of pre-defined configurations or opt into specifying a custom
// configuration.
if !customConfig {
fmt.Println(aurora.Yellow("For pricing information visit: https://fly.io/docs/about/pricing/#postgresql-clusters"))
selectedCfg := 0
options := []string{}
for _, cfg := range postgresConfigurations() {
options = append(options, cfg.Description)
}
prompt := &survey.Select{
Message: "Select configuration:",
Options: options,
PageSize: len(postgresConfigurations()),
}
if err := survey.AskOne(prompt, &selectedCfg); err != nil {
return err
}
pgConfig = &postgresConfigurations()[selectedCfg]
if pgConfig.VmSize == "" {
// User has opted into choosing a custom configuration.
customConfig = true
}
}
if customConfig {
// Resolve cluster size
if initialClusterSize == 0 {
initialClusterSize, err = initialClusterSizeInput(2)
if err != nil {
return err
}
}
input.Count = &initialClusterSize
// Resolve VM size
vmSize, err = selectVMSize(ctx, cmdCtx.Client.API(), vmSizeName)
if err != nil {
return err
}
input.VMSize = api.StringPointer(vmSize.Name)
// Resolve volume size
if volumeSize == 0 {
volumeSize, err = volumeSizeInput(10)
if err != nil {
return err
}
}
input.VolumeSizeGB = api.IntPointer(volumeSize)
} else {
// Resolve configuration from pre-defined configuration.
vmSize, err = selectVMSize(ctx, cmdCtx.Client.API(), pgConfig.VmSize)
if err != nil {
return err
}
input.VMSize = api.StringPointer(vmSize.Name)
input.VolumeSizeGB = api.IntPointer(pgConfig.DiskGb)
input.Count = api.IntPointer(pgConfig.InitialClusterSize)
if imageRef := cmdCtx.Config.GetString("image-ref"); imageRef != "" {
input.ImageRef = api.StringPointer(imageRef)
} else {
input.ImageRef = &pgConfig.ImageRef
}
}
if password := cmdCtx.Config.GetString("password"); password != "" {
input.Password = api.StringPointer(password)
}
snapshot := cmdCtx.Config.GetString("snapshot-id")
if snapshot != "" {
input.SnapshotID = api.StringPointer(snapshot)
}
fmt.Fprintf(cmdCtx.Out, "Creating postgres cluster %s in organization %s\n", name, org.Slug)
_, err = runApiCreatePostgresCluster(cmdCtx, org.Slug, &input)
return err
}
func runApiCreatePostgresCluster(cmdCtx *cmdctx.CmdContext, org string, input *api.CreatePostgresClusterInput) (*api.CreatePostgresClusterPayload, error) {
s := spinner.New(spinner.CharSets[11], 100*time.Millisecond)
s.Writer = os.Stderr
s.Prefix = "Launching..."
s.Start()
payload, err := cmdCtx.Client.API().CreatePostgresCluster(cmdCtx.Command.Context(), *input)
if err != nil {
return nil, err
}
s.FinalMSG = fmt.Sprintf("Postgres cluster %s created\n", payload.App.Name)
s.Stop()
fmt.Printf(" Username: %s\n", payload.Username)
fmt.Printf(" Password: %s\n", payload.Password)
fmt.Printf(" Hostname: %s.internal\n", payload.App.Name)
fmt.Printf(" Proxy Port: 5432\n")
fmt.Printf(" PG Port: 5433\n")
fmt.Println(aurora.Italic("Save your credentials in a secure place -- you won't be able to see them again!"))
fmt.Println()
cancelCtx := cmdCtx.Command.Context()
cmdCtx.AppName = payload.App.Name
err = watchDeployment(cancelCtx, cmdCtx, "")
if isCancelledError(err) {
err = nil
}
if err == nil {
fmt.Println()
fmt.Println(aurora.Bold("Connect to postgres"))
fmt.Printf("Any app within the %s organization can connect to postgres using the above credentials and the hostname \"%s.internal.\"\n", org, payload.App.Name)
fmt.Printf("For example: postgres://%s:%s@%s.internal:%d\n", payload.Username, payload.Password, payload.App.Name, 5432)
fmt.Println()
fmt.Println("Now that you've set up postgres, here's what you need to understand: https://fly.io/docs/reference/postgres-whats-next/")
}
return payload, err
}
func runAttachPostgresCluster(cmdCtx *cmdctx.CmdContext) error {
// Minimum image version requirements
MinPostgresHaVersion := "0.0.19"
ctx := client.NewContext(cmdCtx.Command.Context(), cmdCtx.Client)
postgresAppName := cmdCtx.Config.GetString("postgres-app")
appName := cmdCtx.AppName
dbName := cmdCtx.Config.GetString("database-name")
if dbName == "" {
dbName = appName
}
dbName = strings.ToLower(strings.ReplaceAll(dbName, "-", "_"))
varName := cmdCtx.Config.GetString("variable-name")
if varName == "" {
varName = "DATABASE_URL"
}
dbUser := cmdCtx.Config.GetString("database-user")
if dbUser == "" {
dbUser = appName
}
dbUser = strings.ToLower(strings.ReplaceAll(dbUser, "-", "_"))
input := api.AttachPostgresClusterInput{
AppID: appName,
PostgresClusterAppID: postgresAppName,
ManualEntry: true,
DatabaseName: api.StringPointer(dbName),
DatabaseUser: api.StringPointer(dbUser),
VariableName: api.StringPointer(varName),
}
client := cmdCtx.Client.API()
app, err := client.GetAppBasic(ctx, appName)
if err != nil {
return fmt.Errorf("get app: %w", err)
}
pgApp, err := client.GetAppPostgres(ctx, postgresAppName)
if err != nil {
return fmt.Errorf("get app: %w", err)
}
if err := hasRequiredVersion(pgApp, MinPostgresHaVersion, ""); err != nil {
return err
}
agentclient, err := agent.Establish(ctx, cmdCtx.Client.API())
if err != nil {
return errors.Wrap(err, "can't establish agent")
}
dialer, err := agentclient.Dialer(ctx, pgApp.Organization.Slug)
if err != nil {
return fmt.Errorf("ssh: can't build tunnel for %s: %s", pgApp.Organization.Slug, err)
}
instances, err := agentclient.Instances(ctx, pgApp.Organization.Slug, pgApp.Name)
if err != nil {
return err
}
if len(instances.Addresses) == 0 {
return fmt.Errorf("no instances found for org: %s app: %s", pgApp.Organization.Slug, pgApp.Name)
}
pgInstanceIp := instances.Addresses[0]
pgclient := flypg.NewFromInstance(pgInstanceIp, dialer)
secrets, err := client.GetAppSecrets(ctx, appName)
if err != nil {
return err
}
for _, secret := range secrets {
if secret.Name == *input.VariableName {
return fmt.Errorf("Consumer app %q already contains a secret named %s.", appName, *input.VariableName)
}
}
// Check to see if database exists
dbExists, err := pgclient.DatabaseExists(ctx, *input.DatabaseName)
if err != nil {
return err
}
if dbExists && !flag.GetBool(ctx, "force") {
confirm := false
prompt := &survey.Confirm{
Message: fmt.Sprintf("Database %q already exists. Continue with the attachment process?", *input.DatabaseName),
}
err = survey.AskOne(prompt, &confirm)
if err != nil {
return err
}
if !confirm {
return nil
}
}
// Check to see if user exists
usrExists, err := pgclient.UserExists(ctx, *input.DatabaseUser)
if err != nil {
return err
}
if usrExists {
return fmt.Errorf("Database user %q already exists. Please specify a new database user via --database-user", *input.DatabaseUser)
}
// Create attachment
_, err = client.AttachPostgresCluster(ctx, input)
if err != nil {
return err
}
// Create database if it doesn't already exist
if !dbExists {
err := pgclient.CreateDatabase(ctx, *input.DatabaseName)
if err != nil {
if flypg.ErrorStatus(err) >= 500 {
return err
}
return fmt.Errorf("failed executing database-create: %w", err)
}
}
// Create user
pwd, err := helpers.RandString(15)
if err != nil {
return err
}
if err := pgclient.CreateUser(ctx, *input.DatabaseUser, pwd, true); err != nil {
return fmt.Errorf("failed executing create-user: %w", err)
}
connectionString := fmt.Sprintf("postgres://%s:%s@top2.nearest.of.%s.internal:5432/%s", *input.DatabaseUser, pwd, postgresAppName, *input.DatabaseName)
s := map[string]string{}
s[*input.VariableName] = connectionString
_, err = client.SetSecrets(ctx, appName, s)
if err != nil {
return err
}
fmt.Printf("\nPostgres cluster %s is now attached to %s\n", postgresAppName, app.Name)
fmt.Printf("The following secret was added to %s:\n %s=%s\n", app.Name, *input.VariableName, connectionString)
return nil
}
func hasRequiredVersion(app *api.AppPostgres, cluster, standalone string) error {
// Validate image version to ensure it's compatible with this feature.
if app.ImageDetails.Version == "" || app.ImageDetails.Version == "unknown" {
return fmt.Errorf("Command is not compatible with this image.")
}
imageVersionStr := app.ImageDetails.Version[1:]
imageVersion, err := version.NewVersion(imageVersionStr)
if err != nil {
return err
}
// Specify compatible versions per repo.
requiredVersion := &version.Version{}
if app.ImageDetails.Repository == "flyio/postgres-standalone" {
requiredVersion, err = version.NewVersion(standalone)
if err != nil {
return err
}
}
if app.ImageDetails.Repository == "flyio/postgres" {
requiredVersion, err = version.NewVersion(cluster)
if err != nil {
return err
}
}
if requiredVersion == nil {
return fmt.Errorf("Unable to resolve image version...")
}
if imageVersion.LessThan(requiredVersion) {
return fmt.Errorf(
"Image version is not compatible. (Current: %s, Required: >= %s)\n"+
"Please run 'flyctl image show' and update to the latest available version.",
imageVersion, requiredVersion.String())
}
return nil
}