github.com/homburg/packer@v0.6.1-0.20140528012651-1dcaf1716848/builder/amazon/instance/step_register_ami.go (about)

     1  package instance
     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 StepRegisterAMI struct{}
    12  
    13  func (s *StepRegisterAMI) Run(state multistep.StateBag) multistep.StepAction {
    14  	config := state.Get("config").(*Config)
    15  	ec2conn := state.Get("ec2").(*ec2.EC2)
    16  	manifestPath := state.Get("remote_manifest_path").(string)
    17  	ui := state.Get("ui").(packer.Ui)
    18  
    19  	ui.Say("Registering the AMI...")
    20  	registerOpts := &ec2.RegisterImage{
    21  		ImageLocation: manifestPath,
    22  		Name:          config.AMIName,
    23  		BlockDevices:  config.BlockDevices.BuildAMIDevices(),
    24  		VirtType:      config.AMIVirtType,
    25  	}
    26  
    27  	registerResp, err := ec2conn.RegisterImage(registerOpts)
    28  	if err != nil {
    29  		state.Put("error", fmt.Errorf("Error registering AMI: %s", err))
    30  		ui.Error(state.Get("error").(error).Error())
    31  		return multistep.ActionHalt
    32  	}
    33  
    34  	// Set the AMI ID in the state
    35  	ui.Say(fmt.Sprintf("AMI: %s", registerResp.ImageId))
    36  	amis := make(map[string]string)
    37  	amis[ec2conn.Region.Name] = registerResp.ImageId
    38  	state.Put("amis", amis)
    39  
    40  	// Wait for the image to become ready
    41  	stateChange := awscommon.StateChangeConf{
    42  		Pending:   []string{"pending"},
    43  		Target:    "available",
    44  		Refresh:   awscommon.AMIStateRefreshFunc(ec2conn, registerResp.ImageId),
    45  		StepState: state,
    46  	}
    47  
    48  	ui.Say("Waiting for AMI to become ready...")
    49  	if _, err := awscommon.WaitForState(&stateChange); err != nil {
    50  		err := fmt.Errorf("Error waiting for AMI: %s", err)
    51  		state.Put("error", err)
    52  		ui.Error(err.Error())
    53  		return multistep.ActionHalt
    54  	}
    55  
    56  	return multistep.ActionContinue
    57  }
    58  
    59  func (s *StepRegisterAMI) Cleanup(multistep.StateBag) {}