github.com/supr/packer@v0.3.10-0.20131015195147-7b09e24ac3c1/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 Conn: ec2conn, 57 Pending: []string{"pending"}, 58 Target: "available", 59 Refresh: awscommon.AMIStateRefreshFunc(ec2conn, registerResp.ImageId), 60 StepState: state, 61 } 62 63 ui.Say("Waiting for AMI to become ready...") 64 if _, err := awscommon.WaitForState(&stateChange); err != nil { 65 err := fmt.Errorf("Error waiting for AMI: %s", err) 66 state.Put("error", err) 67 ui.Error(err.Error()) 68 return multistep.ActionHalt 69 } 70 71 return multistep.ActionContinue 72 } 73 74 func (s *StepRegisterAMI) Cleanup(state multistep.StateBag) {}