2017-04-06 05:19:17 -04:00
|
|
|
package scaleway
|
|
|
|
|
|
|
|
import (
|
2018-02-05 19:50:32 -05:00
|
|
|
"context"
|
2017-04-06 05:19:17 -04:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2018-02-05 19:50:32 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-06 05:19:17 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/scaleway/scaleway-cli/pkg/api"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stepSnapshot struct{}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepSnapshot) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2017-04-06 05:19:17 -04:00
|
|
|
client := state.Get("client").(*api.ScalewayAPI)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2018-09-27 06:04:04 -04:00
|
|
|
c := state.Get("config").(*Config)
|
2017-04-11 06:19:28 -04:00
|
|
|
volumeID := state.Get("root_volume_id").(string)
|
2017-04-06 05:19:17 -04:00
|
|
|
|
|
|
|
ui.Say(fmt.Sprintf("Creating snapshot: %v", c.SnapshotName))
|
2017-04-11 06:19:28 -04:00
|
|
|
snapshot, err := client.PostSnapshot(volumeID, c.SnapshotName)
|
2017-04-06 05:19:17 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating snapshot: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Snapshot ID: %s", snapshot)
|
|
|
|
state.Put("snapshot_id", snapshot)
|
|
|
|
state.Put("snapshot_name", c.SnapshotName)
|
|
|
|
state.Put("region", c.Region)
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepSnapshot) Cleanup(state multistep.StateBag) {
|
|
|
|
// no cleanup
|
|
|
|
}
|