2013-07-29 22:07:51 -04:00
|
|
|
package chroot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2015-05-28 11:19:53 -04:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2015-04-05 17:58:48 -04:00
|
|
|
"github.com/awslabs/aws-sdk-go/service/ec2"
|
2013-07-29 22:07:51 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
awscommon "github.com/mitchellh/packer/builder/amazon/common"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StepAttachVolume attaches the previously created volume to an
|
|
|
|
// available device location.
|
|
|
|
//
|
|
|
|
// Produces:
|
2013-07-29 22:10:20 -04:00
|
|
|
// device string - The location where the volume was attached.
|
2013-07-30 19:41:29 -04:00
|
|
|
// attach_cleanup CleanupFunc
|
2013-07-29 22:07:51 -04:00
|
|
|
type StepAttachVolume struct {
|
|
|
|
attached bool
|
|
|
|
volumeId string
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
func (s *StepAttachVolume) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
|
|
|
device := state.Get("device").(string)
|
|
|
|
instance := state.Get("instance").(*ec2.Instance)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
volumeId := state.Get("volume_id").(string)
|
2013-07-29 22:07:51 -04:00
|
|
|
|
2013-07-31 00:19:57 -04:00
|
|
|
// For the API call, it expects "sd" prefixed devices.
|
|
|
|
attachVolume := strings.Replace(device, "/xvd", "/sd", 1)
|
2013-07-29 22:07:51 -04:00
|
|
|
|
2013-07-31 00:19:57 -04:00
|
|
|
ui.Say(fmt.Sprintf("Attaching the root volume to %s", attachVolume))
|
2015-04-05 17:58:48 -04:00
|
|
|
_, err := ec2conn.AttachVolume(&ec2.AttachVolumeInput{
|
|
|
|
InstanceID: instance.InstanceID,
|
|
|
|
VolumeID: &volumeId,
|
|
|
|
Device: &attachVolume,
|
|
|
|
})
|
2013-07-29 22:07:51 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error attaching volume: %s", err)
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("error", err)
|
2013-07-29 22:07:51 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark that we attached it so we can detach it later
|
|
|
|
s.attached = true
|
|
|
|
s.volumeId = volumeId
|
|
|
|
|
|
|
|
// Wait for the volume to become attached
|
|
|
|
stateChange := awscommon.StateChangeConf{
|
|
|
|
Pending: []string{"attaching"},
|
|
|
|
StepState: state,
|
|
|
|
Target: "attached",
|
|
|
|
Refresh: func() (interface{}, string, error) {
|
2015-05-28 11:19:53 -04:00
|
|
|
attempts := 0
|
2015-04-16 12:55:59 -04:00
|
|
|
for attempts < 30 {
|
2015-05-28 11:24:41 -04:00
|
|
|
resp, err := ec2conn.DescribeVolumes(&ec2.DescribeVolumesInput{VolumeIDs: []*string{&volumeId}})
|
2015-04-16 12:55:59 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
if len(resp.Volumes[0].Attachments) > 0 {
|
|
|
|
a := resp.Volumes[0].Attachments[0]
|
2015-05-28 11:24:41 -04:00
|
|
|
return a, *a.State, nil
|
2015-04-16 12:55:59 -04:00
|
|
|
}
|
|
|
|
// When Attachment on volume is not present sleep for 2s and retry
|
|
|
|
attempts += 1
|
2015-05-28 11:19:53 -04:00
|
|
|
ui.Say(fmt.Sprintf(
|
|
|
|
"Volume %s show no attachments. Attempt %d/30. Sleeping for 2s and will retry.",
|
|
|
|
volumeId, attempts))
|
|
|
|
time.Sleep(2 * time.Second)
|
2013-07-29 22:07:51 -04:00
|
|
|
}
|
2015-05-28 11:19:53 -04:00
|
|
|
|
2015-04-16 12:55:59 -04:00
|
|
|
// Attachment on volume is not present after all attempts
|
|
|
|
return nil, "", errors.New("No attachments on volume.")
|
2013-07-29 22:07:51 -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 22:07:51 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("attach_cleanup", s)
|
2013-07-29 22:07:51 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
func (s *StepAttachVolume) Cleanup(state multistep.StateBag) {
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-07-30 19:41:29 -04:00
|
|
|
if err := s.CleanupFunc(state); err != nil {
|
|
|
|
ui.Error(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
func (s *StepAttachVolume) CleanupFunc(state multistep.StateBag) error {
|
2013-07-29 22:07:51 -04:00
|
|
|
if !s.attached {
|
2013-07-30 19:41:29 -04:00
|
|
|
return nil
|
2013-07-29 22:07:51 -04:00
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-07-29 22:07:51 -04:00
|
|
|
|
|
|
|
ui.Say("Detaching EBS volume...")
|
2015-04-05 17:58:48 -04:00
|
|
|
_, err := ec2conn.DetachVolume(&ec2.DetachVolumeInput{VolumeID: &s.volumeId})
|
2013-07-29 22:07:51 -04:00
|
|
|
if err != nil {
|
2013-07-30 19:41:29 -04:00
|
|
|
return fmt.Errorf("Error detaching EBS volume: %s", err)
|
2013-07-29 22:07:51 -04:00
|
|
|
}
|
|
|
|
|
2013-07-30 19:41:29 -04:00
|
|
|
s.attached = false
|
|
|
|
|
2013-07-29 22:07:51 -04:00
|
|
|
// Wait for the volume to detach
|
|
|
|
stateChange := awscommon.StateChangeConf{
|
2013-07-29 22:08:22 -04:00
|
|
|
Pending: []string{"attaching", "attached", "detaching"},
|
2013-07-29 22:07:51 -04:00
|
|
|
StepState: state,
|
|
|
|
Target: "detached",
|
|
|
|
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 22:07:51 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
2013-09-25 00:44:03 -04:00
|
|
|
v := resp.Volumes[0]
|
|
|
|
if len(v.Attachments) > 0 {
|
2015-04-05 17:58:48 -04:00
|
|
|
return v, *v.Attachments[0].State, nil
|
2013-09-25 00:44:03 -04:00
|
|
|
} else {
|
|
|
|
return v, "detached", nil
|
2013-07-29 22:07:51 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = awscommon.WaitForState(&stateChange)
|
|
|
|
if err != nil {
|
2013-07-30 19:41:29 -04:00
|
|
|
return fmt.Errorf("Error waiting for volume: %s", err)
|
2013-07-29 22:07:51 -04:00
|
|
|
}
|
2013-07-30 19:41:29 -04:00
|
|
|
|
|
|
|
return nil
|
2013-07-29 22:07:51 -04:00
|
|
|
}
|