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..9bc6125c1 --- /dev/null +++ b/builder/scaleway/step_remove_volume.go @@ -0,0 +1,44 @@ +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) { + if _, ok := state.GetOk("snapshot_name"); !ok { + // volume will be detached from server only after snapshotting ... 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) + 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)) + } +} 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