2013-07-29 22:07:51 -04:00
|
|
|
package chroot
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-07-29 22:07:51 -04:00
|
|
|
"fmt"
|
2015-05-28 11:19:53 -04:00
|
|
|
"strings"
|
|
|
|
|
2018-06-01 19:17:30 -04:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2015-06-03 17:13:52 -04:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2017-04-04 16:39:01 -04:00
|
|
|
awscommon "github.com/hashicorp/packer/builder/amazon/common"
|
2013-07-29 22:07:51 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
2020-08-17 11:09:19 -04:00
|
|
|
PollingConfig *awscommon.AWSPollingConfig
|
|
|
|
attached bool
|
|
|
|
volumeId string
|
2013-07-29 22:07:51 -04:00
|
|
|
}
|
|
|
|
|
2018-06-01 19:17:30 -04:00
|
|
|
func (s *StepAttachVolume) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2013-08-31 15:58:55 -04:00
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
|
|
|
device := state.Get("device").(string)
|
|
|
|
instance := state.Get("instance").(*ec2.Instance)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2013-08-31 15:58:55 -04:00
|
|
|
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{
|
2015-08-17 20:44:01 -04:00
|
|
|
InstanceId: instance.InstanceId,
|
|
|
|
VolumeId: &volumeId,
|
2015-04-05 17:58:48 -04:00
|
|
|
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
|
2020-08-17 11:09:19 -04:00
|
|
|
err = s.PollingConfig.WaitUntilVolumeAttached(ctx, ec2conn, s.volumeId)
|
2013-07-29 22:07:51 -04:00
|
|
|
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) {
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.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)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2013-07-29 22:07:51 -04:00
|
|
|
|
|
|
|
ui.Say("Detaching EBS volume...")
|
2015-08-17 20:44:01 -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
|
2020-08-17 11:09:19 -04:00
|
|
|
err = s.PollingConfig.WaitUntilVolumeDetached(aws.BackgroundContext(), ec2conn, 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 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
|
|
|
}
|