github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/builder/amazon/chroot/step_register_ami.go (about)

     1  package chroot
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/mitchellh/goamz/ec2"
     7  	"github.com/mitchellh/multistep"
     8  	awscommon "github.com/mitchellh/packer/builder/amazon/common"
     9  	"github.com/mitchellh/packer/packer"
    10  )
    11  
    12  // StepRegisterAMI creates the AMI.
    13  type StepRegisterAMI struct{}
    14  
    15  func (s *StepRegisterAMI) Run(state multistep.StateBag) multistep.StepAction {
    16  	config := state.Get("config").(*Config)
    17  	ec2conn := state.Get("ec2").(*ec2.EC2)
    18  	image := state.Get("source_image").(*ec2.Image)
    19  	snapshotId := state.Get("snapshot_id").(string)
    20  	ui := state.Get("ui").(packer.Ui)
    21  
    22  	ui.Say("Registering the AMI...")
    23  	blockDevices := make([]ec2.BlockDeviceMapping, len(image.BlockDevices))
    24  	for i, device := range image.BlockDevices {
    25  		newDevice := device
    26  		if newDevice.DeviceName == image.RootDeviceName {
    27  			newDevice.SnapshotId = snapshotId
    28  		}
    29  
    30  		blockDevices[i] = newDevice
    31  	}
    32  
    33  	registerOpts := buildRegisterOpts(config, image, blockDevices)
    34  
    35  	// Set SriovNetSupport to "simple". See http://goo.gl/icuXh5
    36  	if config.AMIEnhancedNetworking {
    37  		registerOpts.SriovNetSupport = "simple"
    38  	}
    39  
    40  	registerResp, err := ec2conn.RegisterImage(registerOpts)
    41  	if err != nil {
    42  		state.Put("error", fmt.Errorf("Error registering AMI: %s", err))
    43  		ui.Error(state.Get("error").(error).Error())
    44  		return multistep.ActionHalt
    45  	}
    46  
    47  	// Set the AMI ID in the state
    48  	ui.Say(fmt.Sprintf("AMI: %s", registerResp.ImageId))
    49  	amis := make(map[string]string)
    50  	amis[ec2conn.Region.Name] = registerResp.ImageId
    51  	state.Put("amis", amis)
    52  
    53  	// Wait for the image to become ready
    54  	stateChange := awscommon.StateChangeConf{
    55  		Pending:   []string{"pending"},
    56  		Target:    "available",
    57  		Refresh:   awscommon.AMIStateRefreshFunc(ec2conn, registerResp.ImageId),
    58  		StepState: state,
    59  	}
    60  
    61  	ui.Say("Waiting for AMI to become ready...")
    62  	if _, err := awscommon.WaitForState(&stateChange); err != nil {
    63  		err := fmt.Errorf("Error waiting for AMI: %s", err)
    64  		state.Put("error", err)
    65  		ui.Error(err.Error())
    66  		return multistep.ActionHalt
    67  	}
    68  
    69  	return multistep.ActionContinue
    70  }
    71  
    72  func (s *StepRegisterAMI) Cleanup(state multistep.StateBag) {}
    73  
    74  func buildRegisterOpts(config *Config, image *ec2.Image, blockDevices []ec2.BlockDeviceMapping) *ec2.RegisterImage {
    75  	registerOpts := &ec2.RegisterImage{
    76  		Name:           config.AMIName,
    77  		Architecture:   image.Architecture,
    78  		RootDeviceName: image.RootDeviceName,
    79  		BlockDevices:   blockDevices,
    80  		VirtType:       config.AMIVirtType,
    81  	}
    82  
    83  	if config.AMIVirtType != "hvm" {
    84  		registerOpts.KernelId = image.KernelId
    85  		registerOpts.RamdiskId = image.RamdiskId
    86  	}
    87  
    88  	return registerOpts
    89  }