From d9ff19919cde4781cf3d89e9b37e265567302cde Mon Sep 17 00:00:00 2001 From: Mathieu Tortuyaux Date: Tue, 23 Apr 2024 15:16:00 +0200 Subject: [PATCH 1/3] fix(instance): support snapshot based instance This PR allow users to create an instance from a snapshot directly without creating an image. It's already supported by the APIs. Signed-off-by: Mathieu Tortuyaux --- ...-usage-instance-server-create-usage.golden | 3 ++ .../instance/v1/custom_server_create.go | 44 +++++++++++++------ 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/cmd/scw/testdata/test-all-usage-instance-server-create-usage.golden b/cmd/scw/testdata/test-all-usage-instance-server-create-usage.golden index 1f814f1589..266a5c06c3 100644 --- a/cmd/scw/testdata/test-all-usage-instance-server-create-usage.golden +++ b/cmd/scw/testdata/test-all-usage-instance-server-create-usage.golden @@ -21,6 +21,9 @@ EXAMPLES: Create an instance with volumes from snapshots scw instance server create image=ubuntu_focal root-volume=local: additional-volumes.0=block: + Create and start an instance from a snapshot + scw instance server create image=none root-volume=local: + Use an existing IP ip=$(scw instance ip create | grep id | awk '{ print $2 }') scw instance server create image=ubuntu_focal ip=$ip diff --git a/internal/namespaces/instance/v1/custom_server_create.go b/internal/namespaces/instance/v1/custom_server_create.go index 1c7081b5b9..f84167f816 100644 --- a/internal/namespaces/instance/v1/custom_server_create.go +++ b/internal/namespaces/instance/v1/custom_server_create.go @@ -168,6 +168,10 @@ func serverCreateCommand() *core.Command { Short: "Create an instance with volumes from snapshots", ArgsJSON: `{"image":"ubuntu_focal","root_volume":"local:","additional_volumes":["block:"]}`, }, + { + Short: "Create and start an instance from a snapshot", + ArgsJSON: `{"image":"none","root_volume":"local:"}`, + }, { Short: "Use an existing IP", Raw: `ip=$(scw instance ip create | grep id | awk '{ print $2 }') @@ -231,6 +235,8 @@ func instanceServerCreateRun(ctx context.Context, argsI interface{}) (i interfac // - An image label // switch { + case args.Image == "none": + break case !validation.IsUUID(args.Image): // For retro-compatibility, we replace dashes with underscores imageLabel := strings.Replace(args.Image, "-", "_", -1) @@ -250,22 +256,32 @@ func instanceServerCreateRun(ctx context.Context, argsI interface{}) (i interfac serverReq.Image = args.Image } - getImageResponse, err := apiInstance.GetImage(&instance.GetImageRequest{ - Zone: args.Zone, - ImageID: serverReq.Image, - }) - if err != nil { - logger.Warningf("cannot get image %s: %s", serverReq.Image, err) - } + var ( + getImageResponse *instance.GetImageResponse + serverType *instance.ServerType + ) + if args.Image != "none" { + var err error + getImageResponse, err = apiInstance.GetImage(&instance.GetImageRequest{ + Zone: args.Zone, + ImageID: serverReq.Image, + }) + if err != nil { + logger.Warningf("cannot get image %s: %s", serverReq.Image, err) + } - serverType := getServerType(apiInstance, serverReq.Zone, serverReq.CommercialType) + serverType = getServerType(apiInstance, serverReq.Zone, serverReq.CommercialType) - if serverType != nil && getImageResponse != nil { - if err := validateImageServerTypeCompatibility(getImageResponse.Image, serverType, serverReq.CommercialType); err != nil { - return nil, err + if serverType != nil && getImageResponse != nil { + if err := validateImageServerTypeCompatibility(getImageResponse.Image, serverType, serverReq.CommercialType); err != nil { + return nil, err + } + } else { + logger.Warningf("skipping image server-type compatibility validation") } } else { - logger.Warningf("skipping image server-type compatibility validation") + getImageResponse = nil + serverType = nil } // @@ -315,7 +331,7 @@ func instanceServerCreateRun(ctx context.Context, argsI interface{}) (i interfac } // Validate root volume type and size. - if getImageResponse != nil { + if args.Image != "none" && getImageResponse != nil { if err := validateRootVolume(getImageResponse.Image.RootVolume.Size, volumes["0"]); err != nil { return nil, err } @@ -324,7 +340,7 @@ func instanceServerCreateRun(ctx context.Context, argsI interface{}) (i interfac } // Validate total local volume sizes. - if serverType != nil && getImageResponse != nil { + if args.Image != "none" && serverType != nil && getImageResponse != nil { if err := validateLocalVolumeSizes(volumes, serverType, serverReq.CommercialType, getImageResponse.Image.RootVolume.Size); err != nil { return nil, err } From 33be2961667cb615b6a8777429c9f84aa1a416b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20L=C3=A9one?= Date: Thu, 13 Jun 2024 18:47:57 +0200 Subject: [PATCH 2/3] update documentation --- docs/commands/instance.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/commands/instance.md b/docs/commands/instance.md index fec0f603ab..acb442f157 100644 --- a/docs/commands/instance.md +++ b/docs/commands/instance.md @@ -1754,6 +1754,11 @@ Create an instance with volumes from snapshots scw instance server create image=ubuntu_focal root-volume=local: additional-volumes.0=block: ``` +Create and start an instance from a snapshot +``` +scw instance server create image=none root-volume=local: +``` + Use an existing IP ``` ip=$(scw instance ip create | grep id | awk '{ print $2 }') From fc68b94cab3d1f7cfe484abd911e7b356bcfe5a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20L=C3=A9one?= Date: Thu, 13 Jun 2024 18:48:50 +0200 Subject: [PATCH 3/3] fix master issue --- ...st-all-usage-instance-security-group-update-rule-usage.golden | 1 + .../testdata/test-all-usage-instance-server-action-usage.golden | 1 + .../test-all-usage-instance-server-enable-routed-ip-usage.golden | 1 + .../test-all-usage-instance-server-get-rdp-password-usage.golden | 1 + .../testdata/test-all-usage-instance-volume-wait-usage.golden | 1 + 5 files changed, 5 insertions(+) diff --git a/cmd/scw/testdata/test-all-usage-instance-security-group-update-rule-usage.golden b/cmd/scw/testdata/test-all-usage-instance-security-group-update-rule-usage.golden index 6ce653732c..724eaa3344 100644 --- a/cmd/scw/testdata/test-all-usage-instance-security-group-update-rule-usage.golden +++ b/cmd/scw/testdata/test-all-usage-instance-security-group-update-rule-usage.golden @@ -25,3 +25,4 @@ GLOBAL FLAGS: -D, --debug Enable debug mode -o, --output string Output format: json or human, see 'scw help output' for more info (default "human") -p, --profile string The config profile to use + --web open console page for the current ressource diff --git a/cmd/scw/testdata/test-all-usage-instance-server-action-usage.golden b/cmd/scw/testdata/test-all-usage-instance-server-action-usage.golden index 68ada81d9d..df099b1cde 100644 --- a/cmd/scw/testdata/test-all-usage-instance-server-action-usage.golden +++ b/cmd/scw/testdata/test-all-usage-instance-server-action-usage.golden @@ -23,6 +23,7 @@ GLOBAL FLAGS: -D, --debug Enable debug mode -o, --output string Output format: json or human, see 'scw help output' for more info (default "human") -p, --profile string The config profile to use + --web open console page for the current ressource SEE ALSO: # List available actions for a server diff --git a/cmd/scw/testdata/test-all-usage-instance-server-enable-routed-ip-usage.golden b/cmd/scw/testdata/test-all-usage-instance-server-enable-routed-ip-usage.golden index c35a5d188d..507e72896f 100644 --- a/cmd/scw/testdata/test-all-usage-instance-server-enable-routed-ip-usage.golden +++ b/cmd/scw/testdata/test-all-usage-instance-server-enable-routed-ip-usage.golden @@ -24,3 +24,4 @@ GLOBAL FLAGS: -D, --debug Enable debug mode -o, --output string Output format: json or human, see 'scw help output' for more info (default "human") -p, --profile string The config profile to use + --web open console page for the current ressource diff --git a/cmd/scw/testdata/test-all-usage-instance-server-get-rdp-password-usage.golden b/cmd/scw/testdata/test-all-usage-instance-server-get-rdp-password-usage.golden index 843144cb95..53779e6716 100644 --- a/cmd/scw/testdata/test-all-usage-instance-server-get-rdp-password-usage.golden +++ b/cmd/scw/testdata/test-all-usage-instance-server-get-rdp-password-usage.golden @@ -19,3 +19,4 @@ GLOBAL FLAGS: -D, --debug Enable debug mode -o, --output string Output format: json or human, see 'scw help output' for more info (default "human") -p, --profile string The config profile to use + --web open console page for the current ressource diff --git a/cmd/scw/testdata/test-all-usage-instance-volume-wait-usage.golden b/cmd/scw/testdata/test-all-usage-instance-volume-wait-usage.golden index 25ef1bcc07..7e9b2a9c7e 100644 --- a/cmd/scw/testdata/test-all-usage-instance-volume-wait-usage.golden +++ b/cmd/scw/testdata/test-all-usage-instance-volume-wait-usage.golden @@ -22,3 +22,4 @@ GLOBAL FLAGS: -D, --debug Enable debug mode -o, --output string Output format: json or human, see 'scw help output' for more info (default "human") -p, --profile string The config profile to use + --web open console page for the current ressource