-
Notifications
You must be signed in to change notification settings - Fork 151
/
custom_instance.go
555 lines (501 loc) · 15.3 KB
/
custom_instance.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
package rdb
import (
"context"
"fmt"
"os"
"os/exec"
"path"
"reflect"
"runtime"
"strings"
"time"
"github.com/fatih/color"
"github.com/scaleway/scaleway-cli/v2/internal/core"
"github.com/scaleway/scaleway-cli/v2/internal/human"
"github.com/scaleway/scaleway-cli/v2/internal/interactive"
"github.com/scaleway/scaleway-sdk-go/api/rdb/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
)
const (
instanceActionTimeout = 20 * time.Minute
)
var (
instanceStatusMarshalSpecs = human.EnumMarshalSpecs{
rdb.InstanceStatusUnknown: &human.EnumMarshalSpec{Attribute: color.Faint, Value: "unknown"},
rdb.InstanceStatusReady: &human.EnumMarshalSpec{Attribute: color.FgGreen, Value: "ready"},
rdb.InstanceStatusProvisioning: &human.EnumMarshalSpec{Attribute: color.FgBlue, Value: "provisioning"},
rdb.InstanceStatusConfiguring: &human.EnumMarshalSpec{Attribute: color.FgBlue, Value: "configuring"},
rdb.InstanceStatusDeleting: &human.EnumMarshalSpec{Attribute: color.FgBlue, Value: "deleting"},
rdb.InstanceStatusError: &human.EnumMarshalSpec{Attribute: color.FgRed, Value: "error"},
rdb.InstanceStatusAutohealing: &human.EnumMarshalSpec{Attribute: color.FgBlue, Value: "auto-healing"},
rdb.InstanceStatusLocked: &human.EnumMarshalSpec{Attribute: color.FgRed, Value: "locked"},
rdb.InstanceStatusInitializing: &human.EnumMarshalSpec{Attribute: color.FgBlue, Value: "initialized"},
rdb.InstanceStatusDiskFull: &human.EnumMarshalSpec{Attribute: color.FgRed, Value: "disk_full"},
rdb.InstanceStatusBackuping: &human.EnumMarshalSpec{Attribute: color.FgBlue, Value: "backuping"},
}
)
type serverWaitRequest struct {
InstanceID string
Region scw.Region
}
func instanceMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error) {
// To avoid recursion of human.Marshal we create a dummy type
type tmp rdb.Instance
instance := tmp(i.(rdb.Instance))
// Sections
opt.Sections = []*human.MarshalSection{
{
FieldName: "Endpoint",
},
{
FieldName: "Volume",
},
{
FieldName: "BackupSchedule",
},
{
FieldName: "Settings",
},
}
str, err := human.Marshal(instance, opt)
if err != nil {
return "", err
}
return str, nil
}
func backupScheduleMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error) {
backupSchedule := i.(rdb.BackupSchedule)
if opt.TableCell {
if backupSchedule.Disabled {
return "Disabled", nil
}
return "Enabled", nil
}
// To avoid recursion of human.Marshal we create a dummy type
type LocalBackupSchedule rdb.BackupSchedule
type tmp struct {
LocalBackupSchedule
Frequency *scw.Duration `json:"frequency"`
Retention *scw.Duration `json:"retention"`
}
localBackupSchedule := tmp{
LocalBackupSchedule: LocalBackupSchedule(backupSchedule),
Frequency: &scw.Duration{
Seconds: int64(backupSchedule.Frequency) * 3600,
},
Retention: &scw.Duration{
Seconds: int64(backupSchedule.Retention) * 24 * 3600,
},
}
str, err := human.Marshal(localBackupSchedule, opt)
if err != nil {
return "", err
}
return str, nil
}
func instanceCloneBuilder(c *core.Command) *core.Command {
c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) {
api := rdb.NewAPI(core.ExtractClient(ctx))
return api.WaitForInstance(&rdb.WaitForInstanceRequest{
InstanceID: respI.(*rdb.Instance).ID,
Region: respI.(*rdb.Instance).Region,
Timeout: scw.TimeDurationPtr(instanceActionTimeout),
RetryInterval: core.DefaultRetryInterval,
})
}
return c
}
func instanceCreateBuilder(c *core.Command) *core.Command {
c.ArgSpecs.GetByName("node-type").Default = core.DefaultValueSetter("DB-DEV-S")
c.ArgSpecs.GetByName("node-type").EnumValues = nodeTypes
c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) {
api := rdb.NewAPI(core.ExtractClient(ctx))
return api.WaitForInstance(&rdb.WaitForInstanceRequest{
InstanceID: respI.(*rdb.Instance).ID,
Region: respI.(*rdb.Instance).Region,
Timeout: scw.TimeDurationPtr(instanceActionTimeout),
RetryInterval: core.DefaultRetryInterval,
})
}
// Waiting for API to accept uppercase node-type
c.Interceptor = func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) {
args := argsI.(*rdb.CreateInstanceRequest)
args.NodeType = strings.ToLower(args.NodeType)
return runner(ctx, args)
}
return c
}
func instanceUpgradeBuilder(c *core.Command) *core.Command {
c.ArgSpecs.GetByName("node-type").EnumValues = nodeTypes
c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) {
api := rdb.NewAPI(core.ExtractClient(ctx))
return api.WaitForInstance(&rdb.WaitForInstanceRequest{
InstanceID: respI.(*rdb.Instance).ID,
Region: respI.(*rdb.Instance).Region,
Timeout: scw.TimeDurationPtr(instanceActionTimeout),
RetryInterval: core.DefaultRetryInterval,
})
}
return c
}
func instanceUpdateBuilder(c *core.Command) *core.Command {
type rdbUpdateInstanceRequestCustom struct {
*rdb.UpdateInstanceRequest
Settings []*rdb.InstanceSetting
}
return &core.Command{
Short: `Update an instance`,
Long: `Update an instance.`,
Namespace: "rdb",
Resource: "instance",
Verb: "update",
ArgsType: reflect.TypeOf(rdbUpdateInstanceRequestCustom{}),
ArgSpecs: core.ArgSpecs{
{
Name: "backup-schedule-frequency",
Short: `In hours`,
Required: false,
Deprecated: false,
Positional: false,
},
{
Name: "backup-schedule-retention",
Short: `In days`,
Required: false,
Deprecated: false,
Positional: false,
},
{
Name: "is-backup-schedule-disabled",
Short: `Whether or not the backup schedule is disabled`,
Required: false,
Deprecated: false,
Positional: false,
},
{
Name: "name",
Short: `Name of the instance`,
Required: false,
Deprecated: false,
Positional: false,
},
{
Name: "instance-id",
Short: `UUID of the instance to update`,
Required: true,
Deprecated: false,
Positional: true,
},
{
Name: "tags.{index}",
Short: `Tags of a given instance`,
Required: false,
Deprecated: false,
Positional: false,
},
{
Name: "logs-policy.max-age-retention",
Short: `Max age (in day) of remote logs to keep on the database instance`,
Required: false,
Deprecated: false,
Positional: false,
},
{
Name: "logs-policy.total-disk-retention",
Short: `Max disk size of remote logs to keep on the database instance`,
Required: false,
Deprecated: false,
Positional: false,
},
{
Name: "backup-same-region",
Short: `Store logical backups in the same region as the database instance`,
Required: false,
Deprecated: false,
Positional: false,
},
{
Name: "settings.{index}.name",
Short: `Setting name of a given instance`,
Required: false,
Deprecated: false,
Positional: false,
},
{
Name: "settings.{index}.value",
Short: `Setting value of a given instance`,
Required: false,
Deprecated: false,
Positional: false,
},
core.RegionArgSpec(scw.RegionFrPar, scw.RegionNlAms, scw.RegionPlWaw),
},
Run: func(ctx context.Context, args interface{}) (i interface{}, e error) {
customRequest := args.(*rdbUpdateInstanceRequestCustom)
updateInstanceRequest := customRequest.UpdateInstanceRequest
client := core.ExtractClient(ctx)
api := rdb.NewAPI(client)
getResp, err := api.GetInstance(&rdb.GetInstanceRequest{
Region: customRequest.Region,
InstanceID: customRequest.InstanceID,
})
if err != nil {
return nil, err
}
if customRequest.Settings != nil {
settings := getResp.Settings
changes := customRequest.Settings
for _, change := range changes {
matched := false
for _, setting := range settings {
if change.Name == setting.Name {
setting.Value = change.Value
matched = true
break
}
}
if !matched {
settings = append(settings, change)
}
}
_, err = api.SetInstanceSettings(&rdb.SetInstanceSettingsRequest{
Region: updateInstanceRequest.Region,
InstanceID: updateInstanceRequest.InstanceID,
Settings: settings,
})
if err != nil {
return nil, err
}
}
updateInstanceResponse, err := api.UpdateInstance(updateInstanceRequest)
if err != nil {
return nil, err
}
return updateInstanceResponse, nil
},
WaitFunc: func(ctx context.Context, argsI, respI interface{}) (interface{}, error) {
api := rdb.NewAPI(core.ExtractClient(ctx))
return api.WaitForInstance(&rdb.WaitForInstanceRequest{
InstanceID: respI.(*rdb.Instance).ID,
Region: respI.(*rdb.Instance).Region,
Timeout: scw.TimeDurationPtr(instanceActionTimeout),
RetryInterval: core.DefaultRetryInterval,
})
},
Examples: []*core.Example{
{
Short: "Update instance name",
Raw: "scw rdb instance update 11111111-1111-1111-1111-111111111111 name=foo --wait",
},
{
Short: "Update instance tags",
Raw: "scw rdb instance update 11111111-1111-1111-1111-111111111111 tags.0=a --wait",
},
{
Short: "Set a timezone",
Raw: "scw rdb instance update 11111111-1111-1111-1111-111111111111 settings.0.name=timezone settings.0.value=UTC --wait",
},
},
}
}
func instanceWaitCommand() *core.Command {
return &core.Command{
Short: `Wait for an instance to reach a stable state`,
Long: `Wait for an instance to reach a stable state. This is similar to using --wait flag.`,
Namespace: "rdb",
Resource: "instance",
Verb: "wait",
ArgsType: reflect.TypeOf(serverWaitRequest{}),
Run: func(ctx context.Context, argsI interface{}) (i interface{}, err error) {
api := rdb.NewAPI(core.ExtractClient(ctx))
return api.WaitForInstance(&rdb.WaitForInstanceRequest{
Region: argsI.(*serverWaitRequest).Region,
InstanceID: argsI.(*serverWaitRequest).InstanceID,
Timeout: scw.TimeDurationPtr(instanceActionTimeout),
RetryInterval: core.DefaultRetryInterval,
})
},
ArgSpecs: core.ArgSpecs{
{
Name: "instance-id",
Short: `ID of the instance you want to wait for.`,
Required: true,
Positional: true,
},
core.RegionArgSpec(scw.RegionFrPar, scw.RegionNlAms),
},
Examples: []*core.Example{
{
Short: "Wait for an instance to reach a stable state",
ArgsJSON: `{"instance_id": "11111111-1111-1111-1111-111111111111"}`,
},
},
}
}
type instanceConnectArgs struct {
Region scw.Region
InstanceID string
Username string
Database *string
CliDB *string
}
type engineFamily string
const (
Unknown = engineFamily("Unknown")
PostgreSQL = engineFamily("PostgreSQL")
MySQL = engineFamily("MySQL")
postgreSQLHint = `
psql supports password file to avoid typing your password manually.
Learn more at: https://www.postgresql.org/docs/current/libpq-pgpass.html`
mySQLHint = `
mysql supports loading your password from a file to avoid typing them manually.
Learn more at: https://dev.mysql.com/doc/refman/8.0/en/option-files.html`
)
func passwordFileExist(ctx context.Context, family engineFamily) bool {
passwordFilePath := ""
switch family {
case PostgreSQL:
switch runtime.GOOS {
case "windows":
passwordFilePath = path.Join(core.ExtractUserHomeDir(ctx), core.ExtractEnv(ctx, "APPDATA"), "postgresql", "pgpass.conf")
default:
passwordFilePath = path.Join(core.ExtractUserHomeDir(ctx), ".pgpass")
}
case MySQL:
passwordFilePath = path.Join(core.ExtractUserHomeDir(ctx), ".my.cnf")
default:
return false
}
if passwordFilePath == "" {
return false
}
_, err := os.Stat(passwordFilePath)
return err == nil
}
func passwordFileHint(family engineFamily) string {
switch family {
case PostgreSQL:
return postgreSQLHint
case MySQL:
return mySQLHint
default:
return ""
}
}
func detectEngineFamily(instance *rdb.Instance) (engineFamily, error) {
if instance == nil {
return Unknown, fmt.Errorf("instance engine is nil")
}
if strings.HasPrefix(instance.Engine, string(PostgreSQL)) {
return PostgreSQL, nil
}
if strings.HasPrefix(instance.Engine, string(MySQL)) {
return MySQL, nil
}
return Unknown, fmt.Errorf("unknown engine: %s", instance.Engine)
}
func createConnectCommandLineArgs(instance *rdb.Instance, family engineFamily, args *instanceConnectArgs) ([]string, error) {
database := "rdb"
if args.Database != nil {
database = *args.Database
}
switch family {
case PostgreSQL:
clidb := "psql"
if args.CliDB != nil {
clidb = *args.CliDB
}
// psql -h 51.159.25.206 --port 13917 -d rdb -U username
return []string{
clidb,
"--host", instance.Endpoints[0].IP.String(),
"--port", fmt.Sprintf("%d", instance.Endpoints[0].Port),
"--username", args.Username,
"--dbname", database,
}, nil
case MySQL:
clidb := "mysql"
if args.CliDB != nil {
clidb = *args.CliDB
}
// mysql -h 195.154.69.163 --port 12210 -p -u username
return []string{
clidb,
"--host", instance.Endpoints[0].IP.String(),
"--port", fmt.Sprintf("%d", instance.Endpoints[0].Port),
"--database", database,
"--user", args.Username,
}, nil
}
return nil, fmt.Errorf("unrecognize database engine: %s", instance.Engine)
}
func instanceConnectCommand() *core.Command {
return &core.Command{
Namespace: "rdb",
Resource: "instance",
Verb: "connect",
Short: "Connect to an instance using locally installed CLI",
Long: "Connect to an instance using locally installed CLI such as psql or mysql.",
ArgsType: reflect.TypeOf(instanceConnectArgs{}),
ArgSpecs: core.ArgSpecs{
{
Name: "instance-id",
Short: `UUID of the instance`,
Required: true,
Positional: true,
},
{
Name: "username",
Short: "Name of the user to connect with to the database",
Required: true,
},
{
Name: "database",
Short: "Name of the database",
Default: core.DefaultValueSetter("rdb"),
},
{
Name: "cli-db",
Short: "Command line tool to use, default to psql/mysql",
},
core.RegionArgSpec(scw.RegionFrPar, scw.RegionNlAms),
},
Run: func(ctx context.Context, argsI interface{}) (interface{}, error) {
args := argsI.(*instanceConnectArgs)
client := core.ExtractClient(ctx)
api := rdb.NewAPI(client)
instance, err := api.GetInstance(&rdb.GetInstanceRequest{
Region: args.Region,
InstanceID: args.InstanceID,
})
if err != nil {
return nil, err
}
engineFamily, err := detectEngineFamily(instance)
if err != nil {
return nil, err
}
cmdArgs, err := createConnectCommandLineArgs(instance, engineFamily, args)
if err != nil {
return nil, err
}
if !passwordFileExist(ctx, engineFamily) {
interactive.Println(passwordFileHint(engineFamily))
}
// Run command
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...) //nolint:gosec
//cmd.Stdin = os.Stdin
core.ExtractLogger(ctx).Debugf("executing: %s\n", cmd.Args)
exitCode, err := core.ExecCmd(ctx, cmd)
if err != nil {
return nil, err
}
if exitCode != 0 {
return nil, &core.CliError{Empty: true, Code: exitCode}
}
return &core.SuccessResult{
Empty: true, // the program will output the success message
}, nil
},
}
}