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"
|
|
|
|
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2020-09-09 04:27:48 -04:00
|
|
|
"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{}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepShutdown) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2020-09-09 04:27:48 -04:00
|
|
|
instanceAPI := instance.NewAPI(state.Get("client").(*scw.Client))
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2017-04-11 06:19:28 -04:00
|
|
|
serverID := state.Get("server_id").(string)
|
2017-04-06 05:19:17 -04:00
|
|
|
|
|
|
|
ui.Say("Shutting down server...")
|
|
|
|
|
2020-09-09 04:27:48 -04:00
|
|
|
_, err := instanceAPI.ServerAction(&instance.ServerActionRequest{
|
|
|
|
Action: instance.ServerActionPoweroff,
|
|
|
|
ServerID: serverID,
|
2020-10-28 12:46:40 -04:00
|
|
|
}, scw.WithContext(ctx))
|
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
|
|
|
|
}
|
|
|
|
|
2020-09-09 04:27:48 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-09-09 04:27:48 -04:00
|
|
|
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
|
|
|
|
}
|