2013-07-29 21:07:07 -04:00
|
|
|
package chroot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-04-05 17:58:48 -04:00
|
|
|
"log"
|
|
|
|
|
2015-06-03 17:13:52 -04:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2013-07-29 21:07:07 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2013-07-29 21:55:11 -04:00
|
|
|
awscommon "github.com/mitchellh/packer/builder/amazon/common"
|
2013-07-29 21:07:07 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StepCreateVolume creates a new volume from the snapshot of the root
|
|
|
|
// device of the AMI.
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// volume_id string - The ID of the created volume
|
|
|
|
type StepCreateVolume struct {
|
|
|
|
volumeId string
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
func (s *StepCreateVolume) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
|
|
|
image := state.Get("source_image").(*ec2.Image)
|
|
|
|
instance := state.Get("instance").(*ec2.Instance)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-07-29 21:07:07 -04:00
|
|
|
|
|
|
|
// Determine the root device snapshot
|
2015-04-05 17:58:48 -04:00
|
|
|
log.Printf("Searching for root device of the image (%s)", *image.RootDeviceName)
|
2013-07-29 21:07:07 -04:00
|
|
|
var rootDevice *ec2.BlockDeviceMapping
|
2015-04-05 17:58:48 -04:00
|
|
|
for _, device := range image.BlockDeviceMappings {
|
2013-07-29 21:07:07 -04:00
|
|
|
if device.DeviceName == image.RootDeviceName {
|
2015-04-05 17:58:48 -04:00
|
|
|
rootDevice = device
|
2013-07-29 21:07:07 -04:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if rootDevice == nil {
|
|
|
|
err := fmt.Errorf("Couldn't find root device!")
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("error", err)
|
2013-07-29 21:07:07 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Creating the root volume...")
|
2015-04-05 17:58:48 -04:00
|
|
|
createVolume := &ec2.CreateVolumeInput{
|
|
|
|
AvailabilityZone: instance.Placement.AvailabilityZone,
|
|
|
|
Size: rootDevice.EBS.VolumeSize,
|
|
|
|
SnapshotID: rootDevice.EBS.SnapshotID,
|
|
|
|
VolumeType: rootDevice.EBS.VolumeType,
|
|
|
|
IOPS: rootDevice.EBS.IOPS,
|
2013-07-29 21:07:07 -04:00
|
|
|
}
|
2013-07-30 01:50:29 -04:00
|
|
|
log.Printf("Create args: %#v", createVolume)
|
2013-07-29 21:07:07 -04:00
|
|
|
|
|
|
|
createVolumeResp, err := ec2conn.CreateVolume(createVolume)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating root volume: %s", err)
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("error", err)
|
2013-07-29 21:07:07 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the volume ID so we remember to delete it later
|
2015-04-05 17:58:48 -04:00
|
|
|
s.volumeId = *createVolumeResp.VolumeID
|
2013-07-29 21:55:11 -04:00
|
|
|
log.Printf("Volume ID: %s", s.volumeId)
|
|
|
|
|
|
|
|
// Wait for the volume to become ready
|
|
|
|
stateChange := awscommon.StateChangeConf{
|
|
|
|
Pending: []string{"creating"},
|
|
|
|
StepState: state,
|
|
|
|
Target: "available",
|
|
|
|
Refresh: func() (interface{}, string, error) {
|
2015-04-05 17:58:48 -04:00
|
|
|
resp, err := ec2conn.DescribeVolumes(&ec2.DescribeVolumesInput{VolumeIDs: []*string{&s.volumeId}})
|
2013-07-29 21:55:11 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
2013-09-25 00:44:03 -04:00
|
|
|
v := resp.Volumes[0]
|
2015-04-05 17:58:48 -04:00
|
|
|
return v, *v.State, nil
|
2013-07-29 21:55:11 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = awscommon.WaitForState(&stateChange)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error waiting for volume: %s", err)
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("error", err)
|
2013-07-29 21:55:11 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-07-29 21:07:07 -04:00
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("volume_id", s.volumeId)
|
2013-07-29 21:07:07 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
func (s *StepCreateVolume) Cleanup(state multistep.StateBag) {
|
2013-07-29 21:17:27 -04:00
|
|
|
if s.volumeId == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-07-29 21:17:27 -04:00
|
|
|
|
|
|
|
ui.Say("Deleting the created EBS volume...")
|
2015-04-05 17:58:48 -04:00
|
|
|
_, err := ec2conn.DeleteVolume(&ec2.DeleteVolumeInput{VolumeID: &s.volumeId})
|
2013-07-29 21:17:27 -04:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error deleting EBS volume: %s", err))
|
|
|
|
}
|
|
|
|
}
|