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
This commit is contained in:
Dan Schaffer 2015-04-16 12:55:59 -04:00
parent b49d74d999
commit 03436a3745
1 changed files with 19 additions and 10 deletions

View File

@ -8,6 +8,7 @@ import (
awscommon "github.com/mitchellh/packer/builder/amazon/common" awscommon "github.com/mitchellh/packer/builder/amazon/common"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"strings" "strings"
"time"
) )
// StepAttachVolume attaches the previously created volume to an // StepAttachVolume attaches the previously created volume to an
@ -50,17 +51,25 @@ func (s *StepAttachVolume) Run(state multistep.StateBag) multistep.StepAction {
StepState: state, StepState: state,
Target: "attached", Target: "attached",
Refresh: func() (interface{}, string, error) { Refresh: func() (interface{}, string, error) {
var attempts = 0
for attempts < 30 {
resp, err := ec2conn.Volumes([]string{volumeId}, ec2.NewFilter()) resp, err := ec2conn.Volumes([]string{volumeId}, ec2.NewFilter())
if err != nil { if err != nil {
return nil, "", err return nil, "", err
} }
if len(resp.Volumes[0].Attachments) > 0 {
if len(resp.Volumes[0].Attachments) == 0 {
return nil, "", errors.New("No attachments on volume.")
}
a := resp.Volumes[0].Attachments[0] a := resp.Volumes[0].Attachments[0]
return a, a.Status, nil 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)
}
// Attachment on volume is not present after all attempts
return nil, "", errors.New("No attachments on volume.")
}, },
} }