github.com/amanya/packer@v0.12.1-0.20161117214323-902ac5ab2eb6/builder/openstack/step_stop_server.go (about)

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