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

     1  package chroot
     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  // StepRegisterAMI creates the AMI.
    12  type StepRegisterAMI struct{}
    13  
    14  func (s *StepRegisterAMI) Run(state map[string]interface{}) multistep.StepAction {
    15  	config := state["config"].(*Config)
    16  	ec2conn := state["ec2"].(*ec2.EC2)
    17  	image := state["source_image"].(*ec2.Image)
    18  	snapshotId := state["snapshot_id"].(string)
    19  	ui := state["ui"].(packer.Ui)
    20  
    21  	ui.Say("Registering the AMI...")
    22  	blockDevices := make([]ec2.BlockDeviceMapping, len(image.BlockDevices))
    23  	for i, device := range image.BlockDevices {
    24  		newDevice := device
    25  		if newDevice.DeviceName == image.RootDeviceName {
    26  			newDevice.SnapshotId = snapshotId
    27  		}
    28  
    29  		blockDevices[i] = newDevice
    30  	}
    31  
    32  	registerOpts := &ec2.RegisterImage{
    33  		Name:           config.AMIName,
    34  		Architecture:   image.Architecture,
    35  		KernelId:       image.KernelId,
    36  		RamdiskId:      image.RamdiskId,
    37  		RootDeviceName: image.RootDeviceName,
    38  		BlockDevices:   blockDevices,
    39  	}
    40  
    41  	registerResp, err := ec2conn.RegisterImage(registerOpts)
    42  	if err != nil {
    43  		state["error"] = fmt.Errorf("Error registering AMI: %s", err)
    44  		ui.Error(state["error"].(error).Error())
    45  		return multistep.ActionHalt
    46  	}
    47  
    48  	// Set the AMI ID in the state
    49  	ui.Say(fmt.Sprintf("AMI: %s", registerResp.ImageId))
    50  	amis := make(map[string]string)
    51  	amis[ec2conn.Region.Name] = registerResp.ImageId
    52  	state["amis"] = amis
    53  
    54  	// Wait for the image to become ready
    55  	ui.Say("Waiting for AMI to become ready...")
    56  	if err := awscommon.WaitForAMI(ec2conn, registerResp.ImageId); err != nil {
    57  		err := fmt.Errorf("Error waiting for AMI: %s", err)
    58  		state["error"] = err
    59  		ui.Error(err.Error())
    60  		return multistep.ActionHalt
    61  	}
    62  
    63  	return multistep.ActionContinue
    64  }
    65  
    66  func (s *StepRegisterAMI) Cleanup(state map[string]interface{}) {}