github.com/phobos182/packer@v0.2.3-0.20130819023704-c84d2aeffc68/builder/amazon/ebs/step_create_ami.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 stepCreateAMI struct{}
    12  
    13  func (s *stepCreateAMI) Run(state map[string]interface{}) multistep.StepAction {
    14  	config := state["config"].(config)
    15  	ec2conn := state["ec2"].(*ec2.EC2)
    16  	instance := state["instance"].(*ec2.Instance)
    17  	ui := state["ui"].(packer.Ui)
    18  
    19  	// Create the image
    20  	ui.Say(fmt.Sprintf("Creating the AMI: %s", config.AMIName))
    21  	createOpts := &ec2.CreateImage{
    22  		InstanceId:   instance.InstanceId,
    23  		Name:         config.AMIName,
    24  		BlockDevices: config.BlockDevices.BuildAMIDevices(),
    25  	}
    26  
    27  	createResp, err := ec2conn.CreateImage(createOpts)
    28  	if err != nil {
    29  		err := fmt.Errorf("Error creating AMI: %s", err)
    30  		state["error"] = err
    31  		ui.Error(err.Error())
    32  		return multistep.ActionHalt
    33  	}
    34  
    35  	// Set the AMI ID in the state
    36  	ui.Say(fmt.Sprintf("AMI: %s", createResp.ImageId))
    37  	amis := make(map[string]string)
    38  	amis[ec2conn.Region.Name] = createResp.ImageId
    39  	state["amis"] = amis
    40  
    41  	// Wait for the image to become ready
    42  	ui.Say("Waiting for AMI to become ready...")
    43  	if err := awscommon.WaitForAMI(ec2conn, createResp.ImageId); err != nil {
    44  		err := fmt.Errorf("Error waiting for AMI: %s", err)
    45  		state["error"] = err
    46  		ui.Error(err.Error())
    47  		return multistep.ActionHalt
    48  	}
    49  
    50  	return multistep.ActionContinue
    51  }
    52  
    53  func (s *stepCreateAMI) Cleanup(map[string]interface{}) {
    54  	// No cleanup...
    55  }