Merge pull request #7887 from xunleii/f-scaleway-autoremove-volume

Allow removing volume after image creation for Scaleway builder
This commit is contained in:
Megan Marsh 2019-07-25 11:56:23 -07:00 committed by GitHub
commit 166f1de5fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 1 deletions

View File

@ -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{

View File

@ -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
}

View File

@ -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))
}
}

View File

@ -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