Merge pull request #8087 from hashicorp/fix_8048

add retry wrapper to query so it handles rate limiting
This commit is contained in:
Adrien Delorme 2019-09-16 10:25:12 +02:00 committed by GitHub
commit 4d4bd38afa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 26 additions and 16 deletions

View File

@ -12,6 +12,7 @@ import (
"time" "time"
"github.com/aws/aws-sdk-go/service/ec2" "github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/packer/common/retry"
commonhelper "github.com/hashicorp/packer/helper/common" commonhelper "github.com/hashicorp/packer/helper/common"
"github.com/hashicorp/packer/helper/communicator" "github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
@ -45,14 +46,13 @@ func (s *StepGetPassword) Run(ctx context.Context, state multistep.StateBag) mul
// Get the password // Get the password
var password string var password string
var err error var err error
cancel := make(chan struct{})
waitDone := make(chan bool, 1) waitDone := make(chan bool, 1)
go func() { go func() {
ui.Say("Waiting for auto-generated password for instance...") ui.Say("Waiting for auto-generated password for instance...")
ui.Message( ui.Message(
"It is normal for this process to take up to 15 minutes,\n" + "It is normal for this process to take up to 15 minutes,\n" +
"but it usually takes around 5. Please wait.") "but it usually takes around 5. Please wait.")
password, err = s.waitForPassword(state, cancel) password, err = s.waitForPassword(ctx, state)
waitDone <- true waitDone <- true
}() }()
@ -76,18 +76,14 @@ WaitLoop:
err := fmt.Errorf("Timeout waiting for password.") err := fmt.Errorf("Timeout waiting for password.")
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error(err.Error())
close(cancel)
return multistep.ActionHalt return multistep.ActionHalt
case <-time.After(1 * time.Second): case <-ctx.Done():
if _, ok := state.GetOk(multistep.StateCancelled); ok {
// The step sequence was cancelled, so cancel waiting for password // The step sequence was cancelled, so cancel waiting for password
// and just start the halting process. // and just start the halting process.
close(cancel)
log.Println("[WARN] Interrupt detected, quitting waiting for password.") log.Println("[WARN] Interrupt detected, quitting waiting for password.")
return multistep.ActionHalt return multistep.ActionHalt
} }
} }
}
// In debug-mode, we output the password // In debug-mode, we output the password
if s.Debug { if s.Debug {
@ -106,24 +102,38 @@ func (s *StepGetPassword) Cleanup(multistep.StateBag) {
commonhelper.RemoveSharedStateFile("winrm_password", s.BuildName) commonhelper.RemoveSharedStateFile("winrm_password", s.BuildName)
} }
func (s *StepGetPassword) waitForPassword(state multistep.StateBag, cancel <-chan struct{}) (string, error) { func (s *StepGetPassword) waitForPassword(ctx context.Context, state multistep.StateBag) (string, error) {
ec2conn := state.Get("ec2").(*ec2.EC2) ec2conn := state.Get("ec2").(*ec2.EC2)
instance := state.Get("instance").(*ec2.Instance) instance := state.Get("instance").(*ec2.Instance)
privateKey := s.Comm.SSHPrivateKey privateKey := s.Comm.SSHPrivateKey
for { for {
select { select {
case <-cancel: case <-ctx.Done():
log.Println("[INFO] Retrieve password wait cancelled. Exiting loop.") log.Println("[INFO] Retrieve password wait cancelled. Exiting loop.")
return "", errors.New("Retrieve password wait cancelled") return "", errors.New("Retrieve password wait cancelled")
case <-time.After(5 * time.Second): case <-time.After(5 * time.Second):
} }
resp, err := ec2conn.GetPasswordData(&ec2.GetPasswordDataInput{ // Wrap in a retry so that we don't fail on rate-limiting.
log.Printf("Retrieving auto-generated instance password...")
var resp *ec2.GetPasswordDataOutput
err := retry.Config{
Tries: 11,
RetryDelay: (&retry.Backoff{InitialBackoff: 200 * time.Millisecond, MaxBackoff: 30 * time.Second, Multiplier: 2}).Linear,
}.Run(ctx, func(ctx context.Context) error {
var err error
resp, err = ec2conn.GetPasswordData(&ec2.GetPasswordDataInput{
InstanceId: instance.InstanceId, InstanceId: instance.InstanceId,
}) })
if err != nil { if err != nil {
err := fmt.Errorf("Error retrieving auto-generated instance password: %s", err) err := fmt.Errorf("Error retrieving auto-generated instance password: %s", err)
return err
}
return nil
})
if err != nil {
return "", err return "", err
} }