2013-07-29 21:07:07 -04:00
|
|
|
package chroot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/goamz/ec2"
|
|
|
|
"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"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
log.Printf("Searching for root device of the image (%s)", image.RootDeviceName)
|
|
|
|
var rootDevice *ec2.BlockDeviceMapping
|
|
|
|
for _, device := range image.BlockDevices {
|
|
|
|
if device.DeviceName == image.RootDeviceName {
|
|
|
|
rootDevice = &device
|
|
|
|
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...")
|
|
|
|
createVolume := &ec2.CreateVolume{
|
|
|
|
AvailZone: instance.AvailZone,
|
|
|
|
Size: rootDevice.VolumeSize,
|
|
|
|
SnapshotId: rootDevice.SnapshotId,
|
|
|
|
VolumeType: rootDevice.VolumeType,
|
|
|
|
IOPS: rootDevice.IOPS,
|
|
|
|
}
|
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
|
|
|
|
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{
|
|
|
|
Conn: ec2conn,
|
|
|
|
Pending: []string{"creating"},
|
|
|
|
StepState: state,
|
|
|
|
Target: "available",
|
|
|
|
Refresh: func() (interface{}, string, error) {
|
|
|
|
resp, err := ec2conn.Volumes([]string{s.volumeId}, ec2.NewFilter())
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, resp.Volumes[0].Status, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, 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...")
|
|
|
|
_, err := ec2conn.DeleteVolume(s.volumeId)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error deleting EBS volume: %s", err))
|
|
|
|
}
|
|
|
|
}
|