Commit 03436a37 authored by Dan Schaffer's avatar Dan Schaffer

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
parent b49d74d9
...@@ -8,6 +8,7 @@ import ( ...@@ -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 { ...@@ -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.")
}, },
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment