packer-cn/builder/hcloud/step_shutdown_server.go

50 lines
1.1 KiB
Go
Raw Normal View History

2018-10-17 06:15:47 -04:00
package hcloud
import (
"context"
"fmt"
2020-12-17 16:29:25 -05:00
"github.com/hashicorp/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
2018-10-17 06:15:47 -04:00
"github.com/hetznercloud/hcloud-go/hcloud"
)
type stepShutdownServer struct{}
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").(packersdk.Ui)
2018-10-17 06:15:47 -04:00
serverID := state.Get("server_id").(int)
ui.Say("Shutting down server...")
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
}
_, 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
}