github.com/sneal/packer@v0.5.2/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 multistep.StateBag) multistep.StepAction {
    14  	ec2conn := state.Get("ec2").(*ec2.EC2)
    15  	instance := state.Get("instance").(*ec2.Instance)
    16  	ui := state.Get("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.Put("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  		Pending:   []string{"running", "stopping"},
    32  		Target:    "stopped",
    33  		Refresh:   awscommon.InstanceStateRefreshFunc(ec2conn, instance),
    34  		StepState: state,
    35  	}
    36  	_, err = awscommon.WaitForState(&stateChange)
    37  	if err != nil {
    38  		err := fmt.Errorf("Error waiting for instance to stop: %s", err)
    39  		state.Put("error", err)
    40  		ui.Error(err.Error())
    41  		return multistep.ActionHalt
    42  	}
    43  
    44  	return multistep.ActionContinue
    45  }
    46  
    47  func (s *stepStopInstance) Cleanup(multistep.StateBag) {
    48  	// No cleanup...
    49  }