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