From 12e7042c5b5af84124d752a0d475d6118286303d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 29 Jul 2013 18:55:11 -0700 Subject: [PATCH] builder/amazon/chroot: wait for volume to beecome ready --- builder/amazon/chroot/builder.go | 1 + builder/amazon/chroot/step_create_volume.go | 26 +++++++++++++++++++ builder/amazon/common/instance.go | 2 +- .../amazon/common/step_run_source_instance.go | 10 +++---- builder/amazon/ebs/step_stop_instance.go | 2 +- 5 files changed, 34 insertions(+), 7 deletions(-) diff --git a/builder/amazon/chroot/builder.go b/builder/amazon/chroot/builder.go index 4ab739bbb..15928a220 100644 --- a/builder/amazon/chroot/builder.go +++ b/builder/amazon/chroot/builder.go @@ -75,6 +75,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe &StepInstanceInfo{}, &StepSourceAMIInfo{}, &StepCreateVolume{}, + //&StepAttachVolume{}, } // Run! diff --git a/builder/amazon/chroot/step_create_volume.go b/builder/amazon/chroot/step_create_volume.go index 21e924916..9d7e0a1cb 100644 --- a/builder/amazon/chroot/step_create_volume.go +++ b/builder/amazon/chroot/step_create_volume.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/mitchellh/goamz/ec2" "github.com/mitchellh/multistep" + awscommon "github.com/mitchellh/packer/builder/amazon/common" "github.com/mitchellh/packer/packer" "log" ) @@ -59,6 +60,31 @@ func (s *StepCreateVolume) Run(state map[string]interface{}) multistep.StepActio // Set the volume ID so we remember to delete it later s.volumeId = createVolumeResp.VolumeId + 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) + state["error"] = err + ui.Error(err.Error()) + return multistep.ActionHalt + } return multistep.ActionContinue } diff --git a/builder/amazon/common/instance.go b/builder/amazon/common/instance.go index 5411916b0..12c033d66 100644 --- a/builder/amazon/common/instance.go +++ b/builder/amazon/common/instance.go @@ -12,7 +12,7 @@ import ( type StateChangeConf struct { Conn *ec2.EC2 Pending []string - Refresh func() (interface{}, string, error) + Refresh func() (interface{}, string, error) StepState map[string]interface{} Target string } diff --git a/builder/amazon/common/step_run_source_instance.go b/builder/amazon/common/step_run_source_instance.go index ec823ffc6..d0767d4a8 100644 --- a/builder/amazon/common/step_run_source_instance.go +++ b/builder/amazon/common/step_run_source_instance.go @@ -64,7 +64,7 @@ func (s *StepRunSourceInstance) Run(state map[string]interface{}) multistep.Step Conn: ec2conn, Pending: []string{"pending"}, Target: "running", - Refresh: InstanceStateRefreshFunc(ec2conn, s.instance), + Refresh: InstanceStateRefreshFunc(ec2conn, s.instance), StepState: state, } latestInstance, err := WaitForState(&stateChange) @@ -96,10 +96,10 @@ func (s *StepRunSourceInstance) Cleanup(state map[string]interface{}) { } stateChange := StateChangeConf{ - Conn: ec2conn, - Pending: []string{"pending", "running", "shutting-down", "stopped", "stopping"}, - Refresh: InstanceStateRefreshFunc(ec2conn, s.instance), - Target: "running", + Conn: ec2conn, + Pending: []string{"pending", "running", "shutting-down", "stopped", "stopping"}, + Refresh: InstanceStateRefreshFunc(ec2conn, s.instance), + Target: "running", } WaitForState(&stateChange) diff --git a/builder/amazon/ebs/step_stop_instance.go b/builder/amazon/ebs/step_stop_instance.go index 616e91b29..6f6add6c8 100644 --- a/builder/amazon/ebs/step_stop_instance.go +++ b/builder/amazon/ebs/step_stop_instance.go @@ -31,7 +31,7 @@ func (s *stepStopInstance) Run(state map[string]interface{}) multistep.StepActio Conn: ec2conn, Pending: []string{"running", "stopping"}, Target: "stopped", - Refresh: awscommon.InstanceStateRefreshFunc(ec2conn, instance), + Refresh: awscommon.InstanceStateRefreshFunc(ec2conn, instance), StepState: state, } instanceRaw, err := awscommon.WaitForState(&stateChange)