/
custom_server_action.go
537 lines (494 loc) · 15.4 KB
/
custom_server_action.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
package instance
import (
"context"
"errors"
"fmt"
"reflect"
"strings"
"github.com/scaleway/scaleway-cli/v2/internal/core"
"github.com/scaleway/scaleway-cli/v2/internal/interactive"
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
)
func serverStartCommand() *core.Command {
return &core.Command{
Short: `Power on server`,
Namespace: "instance",
Resource: "server",
Verb: "start",
ArgsType: reflect.TypeOf(instanceUniqueActionRequest{}),
Run: getRunServerAction(instance.ServerActionPoweron),
WaitFunc: waitForServerFunc(),
ArgSpecs: serverActionArgSpecs,
Examples: []*core.Example{
{
Short: "Start a server in the default zone with a given id",
ArgsJSON: `{"server_id": "11111111-1111-1111-1111-111111111111"}`,
},
{
Short: "Start a server in fr-par-1 zone with a given id",
ArgsJSON: `{"zone":"fr-par-1", "server_id": "11111111-1111-1111-1111-111111111111"}`,
},
},
}
}
func serverStopCommand() *core.Command {
return &core.Command{
Short: `Power off server`,
Namespace: "instance",
Resource: "server",
Verb: "stop",
ArgsType: reflect.TypeOf(instanceUniqueActionRequest{}),
Run: getRunServerAction(instance.ServerActionPoweroff),
WaitFunc: waitForServerFunc(),
ArgSpecs: serverActionArgSpecs,
Examples: []*core.Example{
{
Short: "Stop a server in the default zone with a given id",
ArgsJSON: `{"server_id": "11111111-1111-1111-1111-111111111111"}`,
},
{
Short: "Stop a server in fr-par-1 zone with a given id",
ArgsJSON: `{"zone":"fr-par-1", "server_id": "11111111-1111-1111-1111-111111111111"}`,
},
},
}
}
func serverStandbyCommand() *core.Command {
return &core.Command{
Short: `Put server in standby mode`,
Namespace: "instance",
Resource: "server",
Verb: "standby",
ArgsType: reflect.TypeOf(instanceUniqueActionRequest{}),
Run: getRunServerAction(instance.ServerActionStopInPlace),
WaitFunc: waitForServerFunc(),
ArgSpecs: serverActionArgSpecs,
Examples: []*core.Example{
{
Short: "Put in standby a server in the default zone with a given id",
ArgsJSON: `{"server_id": "11111111-1111-1111-1111-111111111111"}`,
},
{
Short: "Put in standby a server in fr-par-1 zone with a given id",
ArgsJSON: `{"zone":"fr-par-1", "server_id": "11111111-1111-1111-1111-111111111111"}`,
},
},
}
}
func serverRebootCommand() *core.Command {
return &core.Command{
Short: `Reboot server`,
Namespace: "instance",
Resource: "server",
Verb: "reboot",
ArgsType: reflect.TypeOf(instanceUniqueActionRequest{}),
Run: getRunServerAction(instance.ServerActionReboot),
WaitFunc: waitForServerFunc(),
ArgSpecs: serverActionArgSpecs,
Examples: []*core.Example{
{
Short: "Reboot a server in the default zone with a given id",
ArgsJSON: `{"server_id": "11111111-1111-1111-1111-111111111111"}`,
},
{
Short: "Reboot a server in fr-par-1 zone with a given id",
ArgsJSON: `{"zone":"fr-par-1", "server_id": "11111111-1111-1111-1111-111111111111"}`,
},
},
}
}
func serverEnableRoutedIPCommand() *core.Command {
return &core.Command{
Short: `Migrate server to IP mobility`,
Long: `Enable routed IP for this server and migrate the nat public IP to routed
Server will reboot !
https://www.scaleway.com/en/docs/compute/instances/api-cli/using-ip-mobility/
`,
Namespace: "instance",
Resource: "server",
Verb: "enable-routed-ip",
ArgsType: reflect.TypeOf(instanceUniqueActionRequest{}),
Run: getRunServerAction("enable_routed_ip"),
WaitFunc: waitForServerFunc(),
ArgSpecs: serverActionArgSpecs,
Examples: []*core.Example{
{
Short: "Migrate a server with legacy network to IP mobility",
ArgsJSON: `{"server_id": "11111111-1111-1111-1111-111111111111"}`,
},
},
}
}
func serverBackupCommand() *core.Command {
type instanceBackupRequest struct {
Zone scw.Zone
ServerID string
Name string
Unified bool
}
return &core.Command{
Short: `Backup server`,
Long: `Create a new image based on the server.
This command:
- creates a snapshot of all attached volumes.
- creates an image based on all these snapshots.
Once your image is ready you will be able to create a new server based on this image.
`,
Namespace: "instance",
Resource: "server",
Verb: "backup",
ArgsType: reflect.TypeOf(instanceBackupRequest{}),
Run: func(ctx context.Context, argsI interface{}) (i interface{}, err error) {
args := argsI.(*instanceBackupRequest)
client := core.ExtractClient(ctx)
api := instance.NewAPI(client)
server, err := api.GetServer(&instance.GetServerRequest{
ServerID: args.ServerID,
Zone: args.Zone,
})
if err != nil {
return nil, err
}
req := &instance.ServerActionRequest{
Zone: args.Zone,
ServerID: args.ServerID,
Action: instance.ServerActionBackup,
Name: &args.Name,
Volumes: map[string]*instance.ServerActionRequestVolumeBackupTemplate{},
}
for _, v := range server.Server.Volumes {
var template *instance.ServerActionRequestVolumeBackupTemplate
if args.Unified {
template = &instance.ServerActionRequestVolumeBackupTemplate{
VolumeType: instance.SnapshotVolumeTypeUnified,
}
} else {
template = &instance.ServerActionRequestVolumeBackupTemplate{
VolumeType: instance.SnapshotVolumeType(v.VolumeType),
}
}
req.Volumes[v.ID] = template
}
res, err := api.ServerAction(req)
if err != nil {
return nil, err
}
tmp := strings.Split(res.Task.HrefResult, "/")
if len(tmp) != 3 {
return nil, fmt.Errorf("cannot extract image id from task")
}
return api.GetImage(&instance.GetImageRequest{Zone: args.Zone, ImageID: tmp[2]})
},
WaitFunc: func(ctx context.Context, _, respI interface{}) (i interface{}, err error) {
resp := respI.(*instance.GetImageResponse)
api := instance.NewAPI(core.ExtractClient(ctx))
return api.WaitForImage(&instance.WaitForImageRequest{
ImageID: resp.Image.ID,
Zone: resp.Image.Zone,
Timeout: scw.TimeDurationPtr(serverActionTimeout),
RetryInterval: core.DefaultRetryInterval,
})
},
ArgSpecs: core.ArgSpecs{
{
Name: "server-id",
Short: `ID of the server to backup.`,
Required: true,
Positional: true,
},
{
Name: "name",
Short: `Name of your backup.`,
Default: core.RandomValueGenerator("backup"),
},
{
Name: "unified",
Short: "Whether or not the type of the snapshot is unified.",
},
core.ZoneArgSpec(),
},
Examples: []*core.Example{
{
Short: "Create a new image based on a server",
ArgsJSON: `{"server_id": "11111111-1111-1111-1111-111111111111"}`,
},
},
}
}
type customTerminateServerRequest struct {
Zone scw.Zone
ServerID string
WithIP bool
WithBlock withBlock
}
type withBlock string
const (
withBlockPrompt = withBlock("prompt")
withBlockTrue = withBlock("true")
withBlockFalse = withBlock("false")
)
func serverTerminateCommand() *core.Command {
return &core.Command{
Short: `Terminate server`,
Long: `Terminates a server with the given ID and all of its volumes.`,
Namespace: "instance",
Verb: "terminate",
Resource: "server",
ArgsType: reflect.TypeOf(customTerminateServerRequest{}),
ArgSpecs: core.ArgSpecs{
{
Name: "server-id",
Required: true,
Positional: true,
},
{
Name: "with-ip",
Short: "Delete the IP attached to the server",
},
{
Name: "with-block",
Short: "Delete the Block Storage volumes attached to the server",
Default: core.DefaultValueSetter("prompt"),
EnumValues: []string{
string(withBlockPrompt),
string(withBlockTrue),
string(withBlockFalse),
},
},
core.ZoneArgSpec(),
},
Examples: []*core.Example{
{
Short: "Terminate a server in the default zone with a given id",
ArgsJSON: `{"server_id": "11111111-1111-1111-1111-111111111111"}`,
},
{
Short: "Terminate a server in fr-par-1 zone with a given id",
ArgsJSON: `{"zone":"fr-par-1", "server_id": "11111111-1111-1111-1111-111111111111"}`,
},
{
Short: "Terminate a server and also delete its flexible IPs",
ArgsJSON: `{"with_ip":true, "server_id": "11111111-1111-1111-1111-111111111111"}`,
},
},
SeeAlsos: []*core.SeeAlso{
{
Command: "scw instance server delete",
Short: "delete a running server",
},
{
Command: "scw instance server stop",
Short: "Stop a running server",
},
},
WaitUsage: "wait until the server and its resources are deleted",
WaitFunc: func(ctx context.Context, argsI, respI interface{}) (interface{}, error) {
terminateServerArgs := argsI.(*customTerminateServerRequest)
server := respI.(*core.SuccessResult).TargetResource.(*instance.Server)
client := core.ExtractClient(ctx)
api := instance.NewAPI(client)
notFoundErr := &scw.ResourceNotFoundError{}
_, err := api.WaitForServer(&instance.WaitForServerRequest{
Zone: server.Zone,
ServerID: server.ID,
Timeout: scw.TimeDurationPtr(serverActionTimeout),
RetryInterval: core.DefaultRetryInterval,
})
if err != nil {
err = errors.Unwrap(err)
if !errors.As(err, ¬FoundErr) {
return nil, err
}
}
if terminateServerArgs.WithBlock == withBlockTrue {
for _, volume := range server.Volumes {
if volume.VolumeType != instance.VolumeServerVolumeTypeBSSD {
continue
}
_, err := api.WaitForVolume(&instance.WaitForVolumeRequest{
VolumeID: volume.ID,
Zone: volume.Zone,
})
if err != nil {
if errors.As(err, ¬FoundErr) {
continue
}
return nil, err
}
}
}
return respI, nil
},
Run: func(ctx context.Context, argsI interface{}) (interface{}, error) {
terminateServerArgs := argsI.(*customTerminateServerRequest)
client := core.ExtractClient(ctx)
api := instance.NewAPI(client)
server, err := api.GetServer(&instance.GetServerRequest{
Zone: terminateServerArgs.Zone,
ServerID: terminateServerArgs.ServerID,
})
if err != nil {
return nil, err
}
deleteBlockVolumes, err := shouldDeleteBlockVolumes(ctx, server, terminateServerArgs.WithBlock)
if err != nil {
return nil, err
}
if !deleteBlockVolumes {
// detach block storage volumes before terminating the instance to preserve them
for _, volume := range server.Server.Volumes {
if volume.VolumeType != instance.VolumeServerVolumeTypeBSSD {
continue
}
if _, err := api.DetachVolume(&instance.DetachVolumeRequest{
Zone: terminateServerArgs.Zone,
VolumeID: volume.ID,
}); err != nil {
return nil, err
}
_, _ = interactive.Printf("successfully detached volume %s\n", volume.Name)
}
}
if _, err := api.ServerAction(&instance.ServerActionRequest{
Zone: terminateServerArgs.Zone,
ServerID: terminateServerArgs.ServerID,
Action: instance.ServerActionTerminate,
}); err != nil {
return nil, err
}
if terminateServerArgs.WithIP && server.Server.PublicIP != nil && !server.Server.PublicIP.Dynamic {
err = api.DeleteIP(&instance.DeleteIPRequest{
Zone: terminateServerArgs.Zone,
IP: server.Server.PublicIP.ID,
})
if err != nil {
return nil, err
}
_, _ = interactive.Printf("successfully deleted ip %s\n", server.Server.PublicIP.Address.String())
}
return &core.SuccessResult{
TargetResource: server.Server,
}, err
},
}
}
func shouldDeleteBlockVolumes(ctx context.Context, server *instance.GetServerResponse, terminateWithBlock withBlock) (bool, error) {
switch terminateWithBlock {
case withBlockTrue:
return true, nil
case withBlockFalse:
return false, nil
case withBlockPrompt:
// Only prompt user if at least one block volume is attached to the instance
for _, volume := range server.Server.Volumes {
if volume.VolumeType != instance.VolumeServerVolumeTypeBSSD {
continue
}
return interactive.PromptBoolWithConfig(&interactive.PromptBoolConfig{
Prompt: "Do you also want to delete block volumes attached to this instance ?",
DefaultValue: false,
Ctx: ctx,
})
}
return false, nil
default:
return false, fmt.Errorf("unsupported with-block value %v", terminateWithBlock)
}
}
type instanceUniqueActionRequest struct {
Zone scw.Zone
ServerID string
}
var serverActionArgSpecs = core.ArgSpecs{
{
Name: "server-id",
Short: `ID of the server affected by the action.`,
Required: true,
Positional: true,
},
core.ZoneArgSpec(),
}
func getRunServerAction(action instance.ServerAction) core.CommandRunner {
return func(ctx context.Context, argsI interface{}) (i interface{}, e error) {
args := argsI.(*instanceUniqueActionRequest)
client := core.ExtractClient(ctx)
api := instance.NewAPI(client)
_, err := api.ServerAction(&instance.ServerActionRequest{
Zone: args.Zone,
ServerID: args.ServerID,
Action: action,
})
return &core.SuccessResult{Message: fmt.Sprintf("%s successfully started for the server", action)}, err
}
}
func waitForServerFunc() core.WaitFunc {
return func(ctx context.Context, argsI, _ interface{}) (interface{}, error) {
return instance.NewAPI(core.ExtractClient(ctx)).WaitForServer(&instance.WaitForServerRequest{
Zone: argsI.(*instanceUniqueActionRequest).Zone,
ServerID: argsI.(*instanceUniqueActionRequest).ServerID,
Timeout: scw.TimeDurationPtr(serverActionTimeout),
RetryInterval: core.DefaultRetryInterval,
})
}
}
type instanceActionRequest struct {
Zone scw.Zone
ServerID string
Action string
}
func serverActionCommand() *core.Command {
argSpecs := serverActionArgSpecs
argSpecs.AddBefore("server-id", &core.ArgSpec{
Name: "action",
Short: "The raw API action to perform, as listed with 'scw instance server list-actions'",
Required: true,
})
return &core.Command{
Short: `Perform a raw API action on a server`,
Namespace: "instance",
Resource: "server",
Verb: "action",
ArgsType: reflect.TypeOf(instanceActionRequest{}),
Run: func(ctx context.Context, argsI interface{}) (interface{}, error) {
args := argsI.(*instanceActionRequest)
return getRunServerAction(instance.ServerAction(args.Action))(ctx, &instanceUniqueActionRequest{
Zone: args.Zone,
ServerID: args.ServerID,
})
},
WaitFunc: func(ctx context.Context, argsI, _ interface{}) (interface{}, error) {
return instance.NewAPI(core.ExtractClient(ctx)).WaitForServer(&instance.WaitForServerRequest{
Zone: argsI.(*instanceActionRequest).Zone,
ServerID: argsI.(*instanceActionRequest).ServerID,
Timeout: scw.TimeDurationPtr(serverActionTimeout),
RetryInterval: core.DefaultRetryInterval,
})
},
ArgSpecs: argSpecs,
Examples: []*core.Example{
{
Short: "Start a server in the default zone with a given id",
ArgsJSON: `{"action":"poweron", "server_id": "11111111-1111-1111-1111-111111111111"}`,
},
},
SeeAlsos: []*core.SeeAlso{
{
Command: "scw instance server list-actions",
Short: "List available actions for a server",
},
{
Command: "scw instance server reboot",
Short: "Perform reboot action",
},
{
Command: "scw instance server start",
Short: "Perform start action",
},
{
Command: "scw instance server stop",
Short: "Perform stop action",
},
},
}
}