2013-07-29 22:07:51 -04:00
|
|
|
package chroot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/goamz/ec2"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
awscommon "github.com/mitchellh/packer/builder/amazon/common"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-07-31 00:19:57 -04:00
|
|
|
"strings"
|
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 {
|
|
|
|
attached bool
|
|
|
|
volumeId string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepAttachVolume) Run(state map[string]interface{}) multistep.StepAction {
|
|
|
|
ec2conn := state["ec2"].(*ec2.EC2)
|
2013-07-31 00:19:57 -04:00
|
|
|
device := state["device"].(string)
|
2013-07-29 22:07:51 -04:00
|
|
|
instance := state["instance"].(*ec2.Instance)
|
|
|
|
ui := state["ui"].(packer.Ui)
|
|
|
|
volumeId := state["volume_id"].(string)
|
|
|
|
|
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))
|
|
|
|
_, err := ec2conn.AttachVolume(volumeId, instance.InstanceId, attachVolume)
|
2013-07-29 22:07:51 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error attaching volume: %s", err)
|
|
|
|
state["error"] = err
|
|
|
|
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{
|
|
|
|
Conn: ec2conn,
|
|
|
|
Pending: []string{"attaching"},
|
|
|
|
StepState: state,
|
|
|
|
Target: "attached",
|
|
|
|
Refresh: func() (interface{}, string, error) {
|
|
|
|
resp, err := ec2conn.Volumes([]string{volumeId}, ec2.NewFilter())
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.Volumes[0].Attachments) == 0 {
|
|
|
|
return nil, "", errors.New("No attachments on volume.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, resp.Volumes[0].Attachments[0].Status, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = awscommon.WaitForState(&stateChange)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error waiting for volume: %s", err)
|
|
|
|
state["error"] = err
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-07-30 20:56:42 -04:00
|
|
|
state["attach_cleanup"] = s
|
2013-07-29 22:07:51 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepAttachVolume) Cleanup(state map[string]interface{}) {
|
2013-07-30 19:41:29 -04:00
|
|
|
ui := state["ui"].(packer.Ui)
|
|
|
|
if err := s.CleanupFunc(state); err != nil {
|
|
|
|
ui.Error(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepAttachVolume) CleanupFunc(state map[string]interface{}) 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
|
|
|
}
|
|
|
|
|
|
|
|
ec2conn := state["ec2"].(*ec2.EC2)
|
|
|
|
ui := state["ui"].(packer.Ui)
|
|
|
|
|
|
|
|
ui.Say("Detaching EBS volume...")
|
|
|
|
_, err := ec2conn.DetachVolume(s.volumeId)
|
|
|
|
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{
|
|
|
|
Conn: ec2conn,
|
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) {
|
|
|
|
resp, err := ec2conn.Volumes([]string{s.volumeId}, ec2.NewFilter())
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
state := "detached"
|
|
|
|
if len(resp.Volumes[0].Attachments) > 0 {
|
|
|
|
state = resp.Volumes[0].Attachments[0].Status
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, state, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, 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
|
|
|
}
|