step_register_ami.go 1.44 KB
Newer Older
1 2 3 4
package instance

import (
	"fmt"
5
	"github.com/mitchellh/goamz/ec2"
6
	"github.com/mitchellh/multistep"
7
	awscommon "github.com/mitchellh/packer/builder/amazon/common"
8 9 10 11 12 13 14
	"github.com/mitchellh/packer/packer"
)

type StepRegisterAMI struct{}

func (s *StepRegisterAMI) Run(state map[string]interface{}) multistep.StepAction {
	config := state["config"].(*Config)
15
	ec2conn := state["ec2"].(*ec2.EC2)
16 17 18 19
	manifestPath := state["remote_manifest_path"].(string)
	ui := state["ui"].(packer.Ui)

	ui.Say("Registering the AMI...")
20 21
	registerOpts := &ec2.RegisterImage{
		ImageLocation: manifestPath,
22
		Name:          config.AMIName,
23
		BlockDevices:  config.BlockDevices.BuildAMIDevices(),
24
	}
25 26 27

	registerResp, err := ec2conn.RegisterImage(registerOpts)
	if err != nil {
28 29 30 31 32
		state["error"] = fmt.Errorf("Error registering AMI: %s", err)
		ui.Error(state["error"].(error).Error())
		return multistep.ActionHalt
	}

33 34 35
	// Set the AMI ID in the state
	ui.Say(fmt.Sprintf("AMI: %s", registerResp.ImageId))
	amis := make(map[string]string)
36
	amis[ec2conn.Region.Name] = registerResp.ImageId
37
	state["amis"] = amis
38

39 40 41 42 43 44 45 46 47
	// Wait for the image to become ready
	ui.Say("Waiting for AMI to become ready...")
	if err := awscommon.WaitForAMI(ec2conn, registerResp.ImageId); err != nil {
		err := fmt.Errorf("Error waiting for AMI: %s", err)
		state["error"] = err
		ui.Error(err.Error())
		return multistep.ActionHalt
	}

48 49 50 51
	return multistep.ActionContinue
}

func (s *StepRegisterAMI) Cleanup(map[string]interface{}) {}