2018-10-17 06:15:47 -04:00
|
|
|
package hcloud
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/hetznercloud/hcloud-go/hcloud"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stepShutdownServer struct{}
|
|
|
|
|
2019-03-07 05:41:39 -05:00
|
|
|
func (s *stepShutdownServer) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2018-10-17 06:15:47 -04:00
|
|
|
client := state.Get("hcloudClient").(*hcloud.Client)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
serverID := state.Get("server_id").(int)
|
|
|
|
|
|
|
|
ui.Say("Shutting down server...")
|
|
|
|
|
2019-03-07 05:41:39 -05:00
|
|
|
action, _, err := client.Server.Shutdown(ctx, &hcloud.Server{ID: serverID})
|
2018-10-17 06:15:47 -04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error stopping server: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2019-03-07 05:41:39 -05:00
|
|
|
_, errCh := client.Action.WatchProgress(ctx, action)
|
2018-10-17 06:15:47 -04:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case err1 := <-errCh:
|
|
|
|
if err1 == nil {
|
|
|
|
return multistep.ActionContinue
|
|
|
|
} else {
|
|
|
|
err := fmt.Errorf("Error stopping server: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepShutdownServer) Cleanup(state multistep.StateBag) {
|
|
|
|
// no cleanup
|
|
|
|
}
|