From 39b8c0eccad91909c4c2f3fee0e17369d396c045 Mon Sep 17 00:00:00 2001 From: Alexandre NICOLAIE Date: Thu, 18 Jul 2019 22:42:18 +0200 Subject: [PATCH 1/4] feat: add 'remove volume' step to scaleway Signed-off-by: Alexandre NICOLAIE --- builder/scaleway/builder.go | 1 + builder/scaleway/config.go | 2 ++ builder/scaleway/step_remove_volume.go | 38 ++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 builder/scaleway/step_remove_volume.go diff --git a/builder/scaleway/builder.go b/builder/scaleway/builder.go index 83812c4a3..47ddc8376 100644 --- a/builder/scaleway/builder.go +++ b/builder/scaleway/builder.go @@ -52,6 +52,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack Debug: b.config.PackerDebug, DebugKeyPath: fmt.Sprintf("scw_%s.pem", b.config.PackerBuildName), }, + new(stepRemoveVolume), new(stepCreateServer), new(stepServerInfo), &communicator.StepConnect{ diff --git a/builder/scaleway/config.go b/builder/scaleway/config.go index 45a8217e8..53e8a9b0e 100644 --- a/builder/scaleway/config.go +++ b/builder/scaleway/config.go @@ -33,6 +33,8 @@ type Config struct { Bootscript string `mapstructure:"bootscript"` BootType string `mapstructure:"boottype"` + RemoveVolume bool `mapstructure:"remove_volume"` + UserAgent string ctx interpolate.Context } diff --git a/builder/scaleway/step_remove_volume.go b/builder/scaleway/step_remove_volume.go new file mode 100644 index 000000000..accffbc6f --- /dev/null +++ b/builder/scaleway/step_remove_volume.go @@ -0,0 +1,38 @@ +package scaleway + +import ( + "context" + "fmt" + + "github.com/scaleway/scaleway-cli/pkg/api" + + "github.com/hashicorp/packer/helper/multistep" + "github.com/hashicorp/packer/packer" +) + +type stepRemoveVolume struct{} + +func (s *stepRemoveVolume) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { + // nothing to do ... only cleanup interests us + return multistep.ActionContinue +} + +func (s *stepRemoveVolume) Cleanup(state multistep.StateBag) { + client := state.Get("client").(*api.ScalewayAPI) + ui := state.Get("ui").(packer.Ui) + c := state.Get("config").(*Config) + volumeID := state.Get("root_volume_id").(string) + + if !c.RemoveVolume { + return + } + + ui.Say("Removing Volume ...") + + err := client.DeleteVolume(volumeID) + if err != nil { + err := fmt.Errorf("Error removing volume: %s", err) + state.Put("error", err) + ui.Error(fmt.Sprintf("Error removing volume: %s. (Ignored)", err)) + } +} From b010442e07c0fcc92c2a556dde5de81171facccf Mon Sep 17 00:00:00 2001 From: Alexandre NICOLAIE Date: Sun, 21 Jul 2019 10:42:34 +0200 Subject: [PATCH 2/4] fix: delete volume only after snapshot step Signed-off-by: Alexandre NICOLAIE --- builder/scaleway/step_remove_volume.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/builder/scaleway/step_remove_volume.go b/builder/scaleway/step_remove_volume.go index accffbc6f..4767464a0 100644 --- a/builder/scaleway/step_remove_volume.go +++ b/builder/scaleway/step_remove_volume.go @@ -18,6 +18,12 @@ func (s *stepRemoveVolume) Run(ctx context.Context, state multistep.StateBag) mu } func (s *stepRemoveVolume) Cleanup(state multistep.StateBag) { + if _, ok := state.GetOk("snapshot_name"); !ok { + // volume will be detached to server only after sharpshooting ... so we don't + // need to remove volume before snapshot step. + return + } + client := state.Get("client").(*api.ScalewayAPI) ui := state.Get("ui").(packer.Ui) c := state.Get("config").(*Config) From 6835a12cb3b1412f29423700b38b4ec18d55df5b Mon Sep 17 00:00:00 2001 From: Alexandre NICOLAIE Date: Wed, 24 Jul 2019 21:19:53 +0200 Subject: [PATCH 3/4] fix: fix typo in comment Co-Authored-By: Megan Marsh --- builder/scaleway/step_remove_volume.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builder/scaleway/step_remove_volume.go b/builder/scaleway/step_remove_volume.go index 4767464a0..9bc6125c1 100644 --- a/builder/scaleway/step_remove_volume.go +++ b/builder/scaleway/step_remove_volume.go @@ -19,7 +19,7 @@ func (s *stepRemoveVolume) Run(ctx context.Context, state multistep.StateBag) mu func (s *stepRemoveVolume) Cleanup(state multistep.StateBag) { if _, ok := state.GetOk("snapshot_name"); !ok { - // volume will be detached to server only after sharpshooting ... so we don't + // volume will be detached from server only after snapshotting ... so we don't // need to remove volume before snapshot step. return } From a338dab8fc86067343caf22e41c0e8c512cdb8d0 Mon Sep 17 00:00:00 2001 From: Alexandre NICOLAIE Date: Wed, 24 Jul 2019 21:25:17 +0200 Subject: [PATCH 4/4] docs: add Scaleway documentation Signed-off-by: Alexandre NICOLAIE --- website/source/docs/builders/scaleway.html.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/website/source/docs/builders/scaleway.html.md b/website/source/docs/builders/scaleway.html.md index cdf975bc7..a97d3154a 100644 --- a/website/source/docs/builders/scaleway.html.md +++ b/website/source/docs/builders/scaleway.html.md @@ -74,11 +74,14 @@ builder. appear in your account. Default `packer-TIMESTAMP` - `boottype` (string) - The type of boot, can be either `local` or - `bootscript`, Default `bootscript` + `bootscript`. Default `bootscript` - `bootscript` (string) - The id of an existing bootscript to use when booting the server. +- `remove_volume` (boolean) - Force Packer to delete volume associated with + the resulting snapshot after the build. Default `false`. + ## Basic Example Here is a basic example. It is completely valid as soon as you enter your own