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

     1  package cloudstack
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/mitchellh/multistep"
     7  	"github.com/mitchellh/packer/packer"
     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  		ui.Error("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  		ui.Error(fmt.Sprintf("Error shutting down instance %s: %s", config.InstanceName, err))
    34  		return multistep.ActionHalt
    35  	}
    36  
    37  	ui.Message("Instance has been shutdown!")
    38  
    39  	return multistep.ActionContinue
    40  }
    41  
    42  // Cleanup any resources that may have been created during the Run phase.
    43  func (s *stepShutdownInstance) Cleanup(state multistep.StateBag) {
    44  	// Nothing to cleanup for this step.
    45  }