github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/cloudstack/step_shutdown_instance.go (about)

     1  package cloudstack
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/packer/packer"
     7  	"github.com/mitchellh/multistep"
     8  	"github.com/xanzy/go-cloudstack/cloudstack"
     9  )
    10  
    11  type stepShutdownInstance struct{}
    12  
    13  func (s *stepShutdownInstance) Run(state multistep.StateBag) multistep.StepAction {
    14  	client := state.Get("client").(*cloudstack.CloudStackClient)
    15  	config := state.Get("config").(*Config)
    16  	ui := state.Get("ui").(packer.Ui)
    17  
    18  	ui.Say("Shutting down instance...")
    19  
    20  	// Retrieve the instance ID from the previously saved state.
    21  	instanceID, ok := state.Get("instance_id").(string)
    22  	if !ok || instanceID == "" {
    23  		state.Put("error", fmt.Errorf("Could not retrieve instance_id from state!"))
    24  		return multistep.ActionHalt
    25  	}
    26  
    27  	// Create a new parameter struct.
    28  	p := client.VirtualMachine.NewStopVirtualMachineParams(instanceID)
    29  
    30  	// Shutdown the virtual machine.
    31  	_, err := client.VirtualMachine.StopVirtualMachine(p)
    32  	if err != nil {
    33  		err := fmt.Errorf("Error shutting down instance %s: %s", config.InstanceName, err)
    34  		state.Put("error", err)
    35  		ui.Error(err.Error())
    36  		return multistep.ActionHalt
    37  	}
    38  
    39  	ui.Message("Instance has been shutdown!")
    40  	return multistep.ActionContinue
    41  }
    42  
    43  // Cleanup any resources that may have been created during the Run phase.
    44  func (s *stepShutdownInstance) Cleanup(state multistep.StateBag) {
    45  	// Nothing to cleanup for this step.
    46  }