Skip to content

Commit

Permalink
fix: update snapshots on step sweep (#181)
Browse files Browse the repository at this point in the history
* fix: update snapshots on step sweep

* remove setup

* add zone in requests
  • Loading branch information
yfodil authored Jul 12, 2024
1 parent 3a2f91b commit 28fd972
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion builder/scaleway/builder_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func testAccPreCheck(t *testing.T) bool {

const testBuilderAccBasic = `
source "scaleway" "basic" {
commercial_type = "DEV1-S"
commercial_type = "PRO2-XXS"
image = "ubuntu_focal"
image_name = "Acceptance test"
ssh_username = "root"
Expand Down
1 change: 1 addition & 0 deletions builder/scaleway/step_create_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func (s *stepCreateServer) Run(ctx context.Context, state multistep.StateBag) mu
Name: c.ServerName,
Image: c.Image,
Tags: tags,
Zone: scw.Zone(c.Zone),
}

if c.ImageSizeInGB != 0 {
Expand Down
1 change: 1 addition & 0 deletions builder/scaleway/step_remove_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (s *stepRemoveVolume) Cleanup(state multistep.StateBag) {

err := instanceAPI.DeleteVolume(&instance.DeleteVolumeRequest{
VolumeID: volumeID,
Zone: scw.Zone(c.Zone),
})
if err != nil {
err := fmt.Errorf("error removing volume: %s", err)
Expand Down
2 changes: 2 additions & 0 deletions builder/scaleway/step_server_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ type stepServerInfo struct{}

func (s *stepServerInfo) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
instanceAPI := instance.NewAPI(state.Get("client").(*scw.Client))
c := state.Get("config").(*Config)
ui := state.Get("ui").(packersdk.Ui)
serverID := state.Get("server_id").(string)

ui.Say("Waiting for server to become active...")

instanceResp, err := instanceAPI.WaitForServer(&instance.WaitForServerRequest{
ServerID: serverID,
Zone: scw.Zone(c.Zone),
}, scw.WithContext(ctx))
if err != nil {
err := fmt.Errorf("error waiting for server to become booted: %s", err)
Expand Down
1 change: 1 addition & 0 deletions builder/scaleway/step_shutdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func (s *stepShutdown) Run(ctx context.Context, state multistep.StateBag) multis
_, err := instanceAPI.ServerAction(&instance.ServerActionRequest{
Action: instance.ServerActionPoweroff,
ServerID: serverID,
Zone: scw.Zone(c.Zone),
}, scw.WithContext(ctx))
if err != nil {
err := fmt.Errorf("error stopping server: %s", err)
Expand Down
22 changes: 13 additions & 9 deletions builder/scaleway/step_sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ func (s *stepSweep) Run(_ context.Context, _ multistep.StateBag) multistep.StepA
func (s *stepSweep) Cleanup(state multistep.StateBag) {
instanceAPI := instance.NewAPI(state.Get("client").(*scw.Client))
ui := state.Get("ui").(packersdk.Ui)
snapshotID := state.Get("snapshot_id").(string)
c := state.Get("config").(*Config)
snapshots := state.Get("snapshots").([]ArtifactSnapshot)
imageID := state.Get("image_id").(string)

ui.Say("Deleting Image...")

err := instanceAPI.DeleteImage(&instance.DeleteImageRequest{
ImageID: imageID,
Zone: scw.Zone(c.Zone),
})
if err != nil {
err := fmt.Errorf("error deleting image: %s", err)
Expand All @@ -36,13 +38,15 @@ func (s *stepSweep) Cleanup(state multistep.StateBag) {

ui.Say("Deleting Snapshot...")

err = instanceAPI.DeleteSnapshot(&instance.DeleteSnapshotRequest{
SnapshotID: snapshotID,
})
if err != nil {
err := fmt.Errorf("error deleting snapshot: %s", err)
state.Put("error", err)
ui.Error(fmt.Sprintf("Error deleting snapshot: %s. (Ignored)", err))
for _, snapshot := range snapshots {
err = instanceAPI.DeleteSnapshot(&instance.DeleteSnapshotRequest{
SnapshotID: snapshot.ID,
Zone: scw.Zone(c.Zone),
})
if err != nil {
err := fmt.Errorf("error deleting snapshot: %s", err)
state.Put("error", err)
ui.Error(fmt.Sprintf("Error deleting snapshot: %s. (Ignored)", err))
}
}

}

0 comments on commit 28fd972

Please sign in to comment.