packer-cn/builder/scaleway/step_shutdown.go

45 lines
1014 B
Go
Raw Normal View History

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"
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 stepShutdown struct{}
2018-02-05 19:50:32 -05:00
func (s *stepShutdown) Run(_ 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)
serverID := state.Get("server_id").(string)
2017-04-06 05:19:17 -04:00
ui.Say("Shutting down server...")
err := client.PostServerAction(serverID, "poweroff")
2017-04-06 05:19:17 -04:00
if err != nil {
err := fmt.Errorf("Error stopping server: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
_, err = api.WaitForServerState(client, serverID, "stopped")
2017-04-06 05:19:17 -04:00
if err != nil {
err := fmt.Errorf("Error shutting down server: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
return multistep.ActionContinue
}
func (s *stepShutdown) Cleanup(state multistep.StateBag) {
// no cleanup
}