From 03436a37459e10a070464703c42bdc8bb4931147 Mon Sep 17 00:00:00 2001 From: Dan Schaffer Date: Thu, 16 Apr 2015 12:55:59 -0400 Subject: [PATCH] builder/amazon/chroot: fix no attachments on volume error. This adds retry logic to the amazon/chroot builder. The builder intermittently fails when ec2conn shows the volume in the attached state but calling Volumes[0].Attachments return an empty array. The fix adds a retry logic for when Volumes[0].Attachments has len 0 sleep for 2 seconds and retry up to 30 times. When the Volumes[0].Attachments is empty I find within 5 seconds the Volumes[0].Attachments contains the correct value. The issue is reported in: https://github.com/mitchellh/packer/issues/1854 --- builder/amazon/chroot/step_attach_volume.go | 29 ++++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/builder/amazon/chroot/step_attach_volume.go b/builder/amazon/chroot/step_attach_volume.go index e67479550..ec3facc10 100644 --- a/builder/amazon/chroot/step_attach_volume.go +++ b/builder/amazon/chroot/step_attach_volume.go @@ -8,6 +8,7 @@ import ( awscommon "github.com/mitchellh/packer/builder/amazon/common" "github.com/mitchellh/packer/packer" "strings" + "time" ) // StepAttachVolume attaches the previously created volume to an @@ -50,17 +51,25 @@ func (s *StepAttachVolume) Run(state multistep.StateBag) multistep.StepAction { StepState: state, Target: "attached", Refresh: func() (interface{}, string, error) { - resp, err := ec2conn.Volumes([]string{volumeId}, ec2.NewFilter()) - if err != nil { - return nil, "", err + var attempts = 0 + for attempts < 30 { + resp, err := ec2conn.Volumes([]string{volumeId}, ec2.NewFilter()) + if err != nil { + return nil, "", err + } + if len(resp.Volumes[0].Attachments) > 0 { + a := resp.Volumes[0].Attachments[0] + return a, a.Status, nil + } + // When Attachment on volume is not present sleep for 2s and retry + attempts += 1 + ui.Say( + fmt.Sprintf("Warning volume %s show no attachments, Attempt %d/30, Sleeping for 2s and will retry.", + volumeId, attempts)) + time.Sleep(time.Duration(2) * time.Second) } - - if len(resp.Volumes[0].Attachments) == 0 { - return nil, "", errors.New("No attachments on volume.") - } - - a := resp.Volumes[0].Attachments[0] - return a, a.Status, nil + // Attachment on volume is not present after all attempts + return nil, "", errors.New("No attachments on volume.") }, }