github.com/raghuse92/packer@v1.3.2/builder/openstack/step_stop_server.go (about)

     1  package openstack
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/startstop"
     8  	"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
     9  	"github.com/hashicorp/packer/helper/multistep"
    10  	"github.com/hashicorp/packer/packer"
    11  )
    12  
    13  type StepStopServer struct{}
    14  
    15  func (s *StepStopServer) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    16  	ui := state.Get("ui").(packer.Ui)
    17  	config := state.Get("config").(*Config)
    18  	server := state.Get("server").(*servers.Server)
    19  
    20  	// We need the v2 compute client
    21  	client, err := config.computeV2Client()
    22  	if err != nil {
    23  		err = fmt.Errorf("Error initializing compute client: %s", err)
    24  		state.Put("error", err)
    25  		return multistep.ActionHalt
    26  	}
    27  
    28  	ui.Say(fmt.Sprintf("Stopping server: %s ...", server.ID))
    29  	if err := startstop.Stop(client, server.ID).ExtractErr(); err != nil {
    30  		err = fmt.Errorf("Error stopping server: %s", err)
    31  		state.Put("error", err)
    32  		return multistep.ActionHalt
    33  	}
    34  
    35  	ui.Message(fmt.Sprintf("Waiting for server to stop: %s ...", server.ID))
    36  	stateChange := StateChangeConf{
    37  		Pending:   []string{"ACTIVE"},
    38  		Target:    []string{"SHUTOFF", "STOPPED"},
    39  		Refresh:   ServerStateRefreshFunc(client, server),
    40  		StepState: state,
    41  	}
    42  	if _, err := WaitForState(&stateChange); err != nil {
    43  		err := fmt.Errorf("Error waiting for server (%s) to stop: %s", server.ID, err)
    44  		state.Put("error", err)
    45  		ui.Error(err.Error())
    46  		return multistep.ActionHalt
    47  	}
    48  
    49  	return multistep.ActionContinue
    50  }
    51  
    52  func (s *StepStopServer) Cleanup(state multistep.StateBag) {}