packer-cn/builder/scaleway/step_shutdown.go

56 lines
1.4 KiB
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"
"github.com/hashicorp/packer/packer"
2020-11-17 19:31:03 -05:00
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
2017-04-06 05:19:17 -04:00
)
type stepShutdown struct{}
func (s *stepShutdown) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
instanceAPI := instance.NewAPI(state.Get("client").(*scw.Client))
2017-04-06 05:19:17 -04:00
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 := instanceAPI.ServerAction(&instance.ServerActionRequest{
Action: instance.ServerActionPoweroff,
ServerID: serverID,
})
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
}
instanceResp, err := instanceAPI.WaitForServer(&instance.WaitForServerRequest{
ServerID: serverID,
})
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
}
if instanceResp.State != instance.ServerStateStopped {
err := fmt.Errorf("Server is in state %s instead of stopped", instanceResp.State.String())
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
2017-04-06 05:19:17 -04:00
return multistep.ActionContinue
}
func (s *stepShutdown) Cleanup(state multistep.StateBag) {
// no cleanup
}