2019-04-15 17:22:25 -04:00
|
|
|
package linode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2019-04-15 17:22:25 -04:00
|
|
|
"github.com/linode/linodego"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stepShutdownLinode struct {
|
|
|
|
client linodego.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepShutdownLinode) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2020-10-19 17:40:37 -04:00
|
|
|
c := state.Get("config").(*Config)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2019-04-15 17:22:25 -04:00
|
|
|
instance := state.Get("instance").(*linodego.Instance)
|
|
|
|
|
|
|
|
ui.Say("Shutting down Linode...")
|
|
|
|
if err := s.client.ShutdownInstance(ctx, instance.ID); err != nil {
|
|
|
|
err = errors.New("Error shutting down Linode: " + err.Error())
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2020-10-19 17:40:37 -04:00
|
|
|
_, err := s.client.WaitForInstanceStatus(ctx, instance.ID, linodego.InstanceOffline, int(c.StateTimeout.Seconds()))
|
2019-04-15 17:22:25 -04:00
|
|
|
if err != nil {
|
|
|
|
err = errors.New("Error shutting down Linode: " + err.Error())
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepShutdownLinode) Cleanup(state multistep.StateBag) {}
|