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

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