github.com/kaixiang/packer@v0.5.2-0.20140114230416-1f5786b0d7f1/builder/amazon/chroot/step_register_ami.go (about) 1 package chroot 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 // StepRegisterAMI creates the AMI. 12 type StepRegisterAMI struct{} 13 14 func (s *StepRegisterAMI) Run(state multistep.StateBag) multistep.StepAction { 15 config := state.Get("config").(*Config) 16 ec2conn := state.Get("ec2").(*ec2.EC2) 17 image := state.Get("source_image").(*ec2.Image) 18 snapshotId := state.Get("snapshot_id").(string) 19 ui := state.Get("ui").(packer.Ui) 20 21 ui.Say("Registering the AMI...") 22 blockDevices := make([]ec2.BlockDeviceMapping, len(image.BlockDevices)) 23 for i, device := range image.BlockDevices { 24 newDevice := device 25 if newDevice.DeviceName == image.RootDeviceName { 26 newDevice.SnapshotId = snapshotId 27 } 28 29 blockDevices[i] = newDevice 30 } 31 32 registerOpts := &ec2.RegisterImage{ 33 Name: config.AMIName, 34 Architecture: image.Architecture, 35 KernelId: image.KernelId, 36 RamdiskId: image.RamdiskId, 37 RootDeviceName: image.RootDeviceName, 38 BlockDevices: blockDevices, 39 } 40 41 registerResp, err := ec2conn.RegisterImage(registerOpts) 42 if err != nil { 43 state.Put("error", fmt.Errorf("Error registering AMI: %s", err)) 44 ui.Error(state.Get("error").(error).Error()) 45 return multistep.ActionHalt 46 } 47 48 // Set the AMI ID in the state 49 ui.Say(fmt.Sprintf("AMI: %s", registerResp.ImageId)) 50 amis := make(map[string]string) 51 amis[ec2conn.Region.Name] = registerResp.ImageId 52 state.Put("amis", amis) 53 54 // Wait for the image to become ready 55 stateChange := awscommon.StateChangeConf{ 56 Pending: []string{"pending"}, 57 Target: "available", 58 Refresh: awscommon.AMIStateRefreshFunc(ec2conn, registerResp.ImageId), 59 StepState: state, 60 } 61 62 ui.Say("Waiting for AMI to become ready...") 63 if _, err := awscommon.WaitForState(&stateChange); err != nil { 64 err := fmt.Errorf("Error waiting for AMI: %s", err) 65 state.Put("error", err) 66 ui.Error(err.Error()) 67 return multistep.ActionHalt 68 } 69 70 return multistep.ActionContinue 71 } 72 73 func (s *StepRegisterAMI) Cleanup(state multistep.StateBag) {}