github.phpd.cn/hashicorp/packer@v1.3.2/builder/hcloud/step_shutdown_server.go (about)

     1  package hcloud
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/hashicorp/packer/helper/multistep"
     8  	"github.com/hashicorp/packer/packer"
     9  	"github.com/hetznercloud/hcloud-go/hcloud"
    10  )
    11  
    12  type stepShutdownServer struct{}
    13  
    14  func (s *stepShutdownServer) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    15  	client := state.Get("hcloudClient").(*hcloud.Client)
    16  	ui := state.Get("ui").(packer.Ui)
    17  	serverID := state.Get("server_id").(int)
    18  
    19  	ui.Say("Shutting down server...")
    20  
    21  	action, _, err := client.Server.Shutdown(context.TODO(), &hcloud.Server{ID: serverID})
    22  
    23  	if err != nil {
    24  		err := fmt.Errorf("Error stopping server: %s", err)
    25  		state.Put("error", err)
    26  		ui.Error(err.Error())
    27  		return multistep.ActionHalt
    28  	}
    29  
    30  	_, errCh := client.Action.WatchProgress(context.TODO(), action)
    31  	for {
    32  		select {
    33  		case err1 := <-errCh:
    34  			if err1 == nil {
    35  				return multistep.ActionContinue
    36  			} else {
    37  				err := fmt.Errorf("Error stopping server: %s", err)
    38  				state.Put("error", err)
    39  				ui.Error(err.Error())
    40  				return multistep.ActionHalt
    41  			}
    42  
    43  		}
    44  	}
    45  }
    46  
    47  func (s *stepShutdownServer) Cleanup(state multistep.StateBag) {
    48  	// no cleanup
    49  }