builder/amazon/instance: register AMI using API

This commit is contained in:
Mitchell Hashimoto 2013-07-25 00:45:55 -05:00
parent 0552bc7306
commit b5fdab407f
1 changed files with 13 additions and 16 deletions

View File

@ -3,6 +3,7 @@ package instance
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"github.com/mitchellh/goamz/ec2"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"strconv" "strconv"
@ -17,8 +18,8 @@ type amiNameData struct {
type StepRegisterAMI struct{} type StepRegisterAMI struct{}
func (s *StepRegisterAMI) Run(state map[string]interface{}) multistep.StepAction { func (s *StepRegisterAMI) Run(state map[string]interface{}) multistep.StepAction {
comm := state["communicator"].(packer.Communicator)
config := state["config"].(*Config) config := state["config"].(*Config)
ec2conn := state["ec2"].(*ec2.EC2)
manifestPath := state["remote_manifest_path"].(string) manifestPath := state["remote_manifest_path"].(string)
ui := state["ui"].(packer.Ui) ui := state["ui"].(packer.Ui)
@ -33,27 +34,23 @@ func (s *StepRegisterAMI) Run(state map[string]interface{}) multistep.StepAction
amiName := amiNameBuf.String() amiName := amiNameBuf.String()
ui.Say("Registering the AMI...") ui.Say("Registering the AMI...")
cmd := &packer.RemoteCmd{ registerOpts := &ec2.RegisterImage{
Command: fmt.Sprintf( ImageLocation: manifestPath,
"ec2-register %s -n '%s' -O '%s' -W '%s'", Name: amiName,
manifestPath,
amiName,
config.AccessKey,
config.SecretKey),
} }
if err := cmd.StartWithUi(comm, ui); err != nil {
registerResp, err := ec2conn.RegisterImage(registerOpts)
if err != nil {
state["error"] = fmt.Errorf("Error registering AMI: %s", err) state["error"] = fmt.Errorf("Error registering AMI: %s", err)
ui.Error(state["error"].(error).Error()) ui.Error(state["error"].(error).Error())
return multistep.ActionHalt return multistep.ActionHalt
} }
if cmd.ExitStatus != 0 { // Set the AMI ID in the state
state["error"] = fmt.Errorf( ui.Say(fmt.Sprintf("AMI: %s", registerResp.ImageId))
"AMI registration failed. Please see the output above for more\n" + amis := make(map[string]string)
"details on what went wrong.") amis[config.Region] = registerResp.ImageId
ui.Error(state["error"].(error).Error()) state["amis"] = amis
return multistep.ActionHalt
}
return multistep.ActionContinue return multistep.ActionContinue
} }