github.com/phobos182/packer@v0.2.3-0.20130819023704-c84d2aeffc68/builder/amazon/ebs/step_stop_instance.go (about)

     1  package ebs
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/mitchellh/goamz/ec2"
     6  	"github.com/mitchellh/multistep"
     7  	awscommon "github.com/mitchellh/packer/builder/amazon/common"
     8  	"github.com/mitchellh/packer/packer"
     9  )
    10  
    11  type stepStopInstance struct{}
    12  
    13  func (s *stepStopInstance) Run(state map[string]interface{}) multistep.StepAction {
    14  	ec2conn := state["ec2"].(*ec2.EC2)
    15  	instance := state["instance"].(*ec2.Instance)
    16  	ui := state["ui"].(packer.Ui)
    17  
    18  	// Stop the instance so we can create an AMI from it
    19  	ui.Say("Stopping the source instance...")
    20  	_, err := ec2conn.StopInstances(instance.InstanceId)
    21  	if err != nil {
    22  		err := fmt.Errorf("Error stopping instance: %s", err)
    23  		state["error"] = err
    24  		ui.Error(err.Error())
    25  		return multistep.ActionHalt
    26  	}
    27  
    28  	// Wait for the instance to actual stop
    29  	ui.Say("Waiting for the instance to stop...")
    30  	stateChange := awscommon.StateChangeConf{
    31  		Conn:      ec2conn,
    32  		Pending:   []string{"running", "stopping"},
    33  		Target:    "stopped",
    34  		Refresh:   awscommon.InstanceStateRefreshFunc(ec2conn, instance),
    35  		StepState: state,
    36  	}
    37  	_, err = awscommon.WaitForState(&stateChange)
    38  	if err != nil {
    39  		err := fmt.Errorf("Error waiting for instance to stop: %s", err)
    40  		state["error"] = err
    41  		ui.Error(err.Error())
    42  		return multistep.ActionHalt
    43  	}
    44  
    45  	return multistep.ActionContinue
    46  }
    47  
    48  func (s *stepStopInstance) Cleanup(map[string]interface{}) {
    49  	// No cleanup...
    50  }