From 557bffc94a080d3513af5ae96dcc5bcab26de6fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20L=C3=A9one?= Date: Wed, 20 Jan 2021 21:41:39 +0100 Subject: [PATCH 1/5] scaleway: add support for timeout in shutdown step --- builder/scaleway/config.go | 2 ++ builder/scaleway/step_shutdown.go | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/builder/scaleway/config.go b/builder/scaleway/config.go index 3f5277c24..a00897635 100644 --- a/builder/scaleway/config.go +++ b/builder/scaleway/config.go @@ -98,6 +98,8 @@ type Config struct { // available. // Deprecated, use Zone instead Region string `mapstructure:"region" required:"false"` + + Timeout string `mapstructure:"timeout" required:"false"` } func (c *Config) Prepare(raws ...interface{}) ([]string, error) { diff --git a/builder/scaleway/step_shutdown.go b/builder/scaleway/step_shutdown.go index 12365a482..0b96926a5 100644 --- a/builder/scaleway/step_shutdown.go +++ b/builder/scaleway/step_shutdown.go @@ -3,6 +3,7 @@ package scaleway import ( "context" "fmt" + "time" "github.com/hashicorp/packer-plugin-sdk/multistep" packersdk "github.com/hashicorp/packer-plugin-sdk/packer" @@ -30,9 +31,22 @@ func (s *stepShutdown) Run(ctx context.Context, state multistep.StateBag) multis return multistep.ActionHalt } - instanceResp, err := instanceAPI.WaitForServer(&instance.WaitForServerRequest{ + waitRequest := &instance.WaitForServerRequest{ ServerID: serverID, - }) + } + timeout := state.Get("timeout").(string) + duration, err := time.ParseDuration(timeout) + if err != nil { + err := fmt.Errorf("error: %s could not parse string %s as a duration", err, timeout) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } + if timeout != "" { + waitRequest.Timeout = scw.TimeDurationPtr(duration) + } + + instanceResp, err := instanceAPI.WaitForServer(waitRequest) if err != nil { err := fmt.Errorf("Error shutting down server: %s", err) state.Put("error", err) From 44d19f160a683aee4f3e4d528eb4d7bb46dc2754 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20L=C3=A9one?= Date: Fri, 22 Jan 2021 11:29:19 +0100 Subject: [PATCH 2/5] fix --- builder/scaleway/config.go | 4 ++++ builder/scaleway/config.hcl2spec.go | 2 ++ builder/scaleway/step_shutdown.go | 3 ++- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/builder/scaleway/config.go b/builder/scaleway/config.go index a00897635..9ee2256ec 100644 --- a/builder/scaleway/config.go +++ b/builder/scaleway/config.go @@ -257,6 +257,10 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) { errs, errors.New("image is required")) } + if c.Timeout == "" { + c.Timeout = "5m" + } + if errs != nil && len(errs.Errors) > 0 { return warnings, errs } diff --git a/builder/scaleway/config.hcl2spec.go b/builder/scaleway/config.hcl2spec.go index 32a04f1eb..aeedbc01a 100644 --- a/builder/scaleway/config.hcl2spec.go +++ b/builder/scaleway/config.hcl2spec.go @@ -83,6 +83,7 @@ type FlatConfig struct { Token *string `mapstructure:"api_token" required:"false" cty:"api_token" hcl:"api_token"` Organization *string `mapstructure:"organization_id" required:"false" cty:"organization_id" hcl:"organization_id"` Region *string `mapstructure:"region" required:"false" cty:"region" hcl:"region"` + Timeout *string `mapstructure:"timeout" required:"false" cty:"timeout" hcl:"timeout"` } // FlatMapstructure returns a new FlatConfig. @@ -170,6 +171,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "api_token": &hcldec.AttrSpec{Name: "api_token", Type: cty.String, Required: false}, "organization_id": &hcldec.AttrSpec{Name: "organization_id", Type: cty.String, Required: false}, "region": &hcldec.AttrSpec{Name: "region", Type: cty.String, Required: false}, + "timeout": &hcldec.AttrSpec{Name: "timeout", Type: cty.String, Required: false}, } return s } diff --git a/builder/scaleway/step_shutdown.go b/builder/scaleway/step_shutdown.go index 0b96926a5..3bc874e2a 100644 --- a/builder/scaleway/step_shutdown.go +++ b/builder/scaleway/step_shutdown.go @@ -34,7 +34,8 @@ func (s *stepShutdown) Run(ctx context.Context, state multistep.StateBag) multis waitRequest := &instance.WaitForServerRequest{ ServerID: serverID, } - timeout := state.Get("timeout").(string) + c := state.Get("config").(*Config) + timeout := c.Timeout duration, err := time.ParseDuration(timeout) if err != nil { err := fmt.Errorf("error: %s could not parse string %s as a duration", err, timeout) From 1f4971f5accfc081dca7c9ad821e9e6bece74ce1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20L=C3=A9one?= Date: Thu, 11 Feb 2021 14:23:43 +0100 Subject: [PATCH 3/5] Fix --- .../content/partials/builder/scaleway/Config-not-required.mdx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/website/content/partials/builder/scaleway/Config-not-required.mdx b/website/content/partials/builder/scaleway/Config-not-required.mdx index ef73b13c1..2f301c0a6 100644 --- a/website/content/partials/builder/scaleway/Config-not-required.mdx +++ b/website/content/partials/builder/scaleway/Config-not-required.mdx @@ -39,3 +39,5 @@ or ams1). Consequently, this is the region where the snapshot will be available. Deprecated, use Zone instead + +- `timeout` (string) - Timeout From 95e8263280e1ef2305ab2b30613635c6a8e1af09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20L=C3=A9one?= Date: Mon, 22 Feb 2021 12:17:16 +0100 Subject: [PATCH 4/5] Fix --- builder/scaleway/config.go | 12 ++++++------ builder/scaleway/step_shutdown.go | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/builder/scaleway/config.go b/builder/scaleway/config.go index 9ee2256ec..878f5be1d 100644 --- a/builder/scaleway/config.go +++ b/builder/scaleway/config.go @@ -74,8 +74,10 @@ type Config struct { RemoveVolume bool `mapstructure:"remove_volume"` - UserAgent string `mapstructure-to-hcl2:",skip"` - ctx interpolate.Context + // Shutdown timeout. Default to 5m + ShutdownTimeout string `mapstructure:"shutdown_timeout" required:"false"` + UserAgent string `mapstructure-to-hcl2:",skip"` + ctx interpolate.Context // Deprecated configs @@ -98,8 +100,6 @@ type Config struct { // available. // Deprecated, use Zone instead Region string `mapstructure:"region" required:"false"` - - Timeout string `mapstructure:"timeout" required:"false"` } func (c *Config) Prepare(raws ...interface{}) ([]string, error) { @@ -257,8 +257,8 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) { errs, errors.New("image is required")) } - if c.Timeout == "" { - c.Timeout = "5m" + if c.ShutdownTimeout == "" { + c.ShutdownTimeout = "5m" } if errs != nil && len(errs.Errors) > 0 { diff --git a/builder/scaleway/step_shutdown.go b/builder/scaleway/step_shutdown.go index 3bc874e2a..164acc607 100644 --- a/builder/scaleway/step_shutdown.go +++ b/builder/scaleway/step_shutdown.go @@ -35,7 +35,7 @@ func (s *stepShutdown) Run(ctx context.Context, state multistep.StateBag) multis ServerID: serverID, } c := state.Get("config").(*Config) - timeout := c.Timeout + timeout := c.ShutdownTimeout duration, err := time.ParseDuration(timeout) if err != nil { err := fmt.Errorf("error: %s could not parse string %s as a duration", err, timeout) From 2967fccfd7e7580ee38b7861a0ae29b050821c73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20L=C3=A9one?= Date: Mon, 22 Feb 2021 14:10:31 +0100 Subject: [PATCH 5/5] Fix --- builder/scaleway/config.hcl2spec.go | 4 ++-- .../content/partials/builder/scaleway/Config-not-required.mdx | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/builder/scaleway/config.hcl2spec.go b/builder/scaleway/config.hcl2spec.go index aeedbc01a..720e02dae 100644 --- a/builder/scaleway/config.hcl2spec.go +++ b/builder/scaleway/config.hcl2spec.go @@ -80,10 +80,10 @@ type FlatConfig struct { Bootscript *string `mapstructure:"bootscript" required:"false" cty:"bootscript" hcl:"bootscript"` BootType *string `mapstructure:"boottype" required:"false" cty:"boottype" hcl:"boottype"` RemoveVolume *bool `mapstructure:"remove_volume" cty:"remove_volume" hcl:"remove_volume"` + ShutdownTimeout *string `mapstructure:"shutdown_timeout" required:"false" cty:"shutdown_timeout" hcl:"shutdown_timeout"` Token *string `mapstructure:"api_token" required:"false" cty:"api_token" hcl:"api_token"` Organization *string `mapstructure:"organization_id" required:"false" cty:"organization_id" hcl:"organization_id"` Region *string `mapstructure:"region" required:"false" cty:"region" hcl:"region"` - Timeout *string `mapstructure:"timeout" required:"false" cty:"timeout" hcl:"timeout"` } // FlatMapstructure returns a new FlatConfig. @@ -168,10 +168,10 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "bootscript": &hcldec.AttrSpec{Name: "bootscript", Type: cty.String, Required: false}, "boottype": &hcldec.AttrSpec{Name: "boottype", Type: cty.String, Required: false}, "remove_volume": &hcldec.AttrSpec{Name: "remove_volume", Type: cty.Bool, Required: false}, + "shutdown_timeout": &hcldec.AttrSpec{Name: "shutdown_timeout", Type: cty.String, Required: false}, "api_token": &hcldec.AttrSpec{Name: "api_token", Type: cty.String, Required: false}, "organization_id": &hcldec.AttrSpec{Name: "organization_id", Type: cty.String, Required: false}, "region": &hcldec.AttrSpec{Name: "region", Type: cty.String, Required: false}, - "timeout": &hcldec.AttrSpec{Name: "timeout", Type: cty.String, Required: false}, } return s } diff --git a/website/content/partials/builder/scaleway/Config-not-required.mdx b/website/content/partials/builder/scaleway/Config-not-required.mdx index 2f301c0a6..0e5560984 100644 --- a/website/content/partials/builder/scaleway/Config-not-required.mdx +++ b/website/content/partials/builder/scaleway/Config-not-required.mdx @@ -21,6 +21,8 @@ - `remove_volume` (bool) - Remove Volume +- `shutdown_timeout` (string) - Shutdown timeout. Default to 5m + - `api_token` (string) - The token to use to authenticate with your account. It can also be specified via environment variable SCALEWAY_API_TOKEN. You can see and generate tokens in the "Credentials" @@ -39,5 +41,3 @@ or ams1). Consequently, this is the region where the snapshot will be available. Deprecated, use Zone instead - -- `timeout` (string) - Timeout