github.com/kaixiang/packer@v0.5.2-0.20140114230416-1f5786b0d7f1/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 multistep.StateBag) multistep.StepAction { 14 config := state.Get("config").(*Config) 15 ec2conn := state.Get("ec2").(*ec2.EC2) 16 manifestPath := state.Get("remote_manifest_path").(string) 17 ui := state.Get("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.Put("error", fmt.Errorf("Error registering AMI: %s", err)) 29 ui.Error(state.Get("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.Put("amis", amis) 38 39 // Wait for the image to become ready 40 stateChange := awscommon.StateChangeConf{ 41 Pending: []string{"pending"}, 42 Target: "available", 43 Refresh: awscommon.AMIStateRefreshFunc(ec2conn, registerResp.ImageId), 44 StepState: state, 45 } 46 47 ui.Say("Waiting for AMI to become ready...") 48 if _, err := awscommon.WaitForState(&stateChange); err != nil { 49 err := fmt.Errorf("Error waiting for AMI: %s", err) 50 state.Put("error", err) 51 ui.Error(err.Error()) 52 return multistep.ActionHalt 53 } 54 55 return multistep.ActionContinue 56 } 57 58 func (s *StepRegisterAMI) Cleanup(multistep.StateBag) {}