Retry fleet creating only on invalid IAM instance profile (#9946)

This commit is contained in:
Sylvia Moss 2020-09-18 17:05:36 +02:00 committed by GitHub
parent eca9b2f30d
commit d347bbec4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 13 deletions

View File

@ -10,7 +10,6 @@ import (
"time" "time"
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/ec2" "github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/packer/common/random" "github.com/hashicorp/packer/common/random"
"github.com/hashicorp/packer/common/retry" "github.com/hashicorp/packer/common/retry"
@ -281,7 +280,6 @@ func (s *StepRunSpotInstance) Run(ctx context.Context, state multistep.StateBag)
} }
var createOutput *ec2.CreateFleetOutput var createOutput *ec2.CreateFleetOutput
err = retry.Config{ err = retry.Config{
Tries: 11, Tries: 11,
ShouldRetry: func(err error) bool { ShouldRetry: func(err error) bool {
@ -291,40 +289,39 @@ func (s *StepRunSpotInstance) Run(ctx context.Context, state multistep.StateBag)
// we can wait on those operations, this can be removed. // we can wait on those operations, this can be removed.
return true return true
} }
return request.IsErrorRetryable(err) return false
}, },
RetryDelay: (&retry.Backoff{InitialBackoff: 500 * time.Millisecond, MaxBackoff: 30 * time.Second, Multiplier: 2}).Linear, RetryDelay: (&retry.Backoff{InitialBackoff: 500 * time.Millisecond, MaxBackoff: 30 * time.Second, Multiplier: 2}).Linear,
}.Run(ctx, func(ctx context.Context) error { }.Run(ctx, func(ctx context.Context) error {
createOutput, err = ec2conn.CreateFleet(createFleetInput) createOutput, err = ec2conn.CreateFleet(createFleetInput)
if err == nil && createOutput.Errors != nil { if err == nil && createOutput.Errors != nil {
err = fmt.Errorf("errors: %v", createOutput.Errors) err = fmt.Errorf("errors: %v", createOutput.Errors)
} }
// We can end up with errors because one of the allowed availability
// zones doesn't have one of the allowed instance types; as long as
// an instance is launched, these errors aren't important.
if len(createOutput.Instances) > 0 {
if err != nil {
log.Printf("create request failed for some instances %v", err.Error())
}
return nil
}
if err != nil { if err != nil {
log.Printf("create request failed %v", err) log.Printf("create request failed %v", err)
} }
return err return err
}) })
// Create the request for the spot instance.
if err != nil { if err != nil {
if createOutput.FleetId != nil { if createOutput.FleetId != nil {
err = fmt.Errorf("Error waiting for fleet request (%s): %s", *createOutput.FleetId, err) err = fmt.Errorf("Error waiting for fleet request (%s): %s", *createOutput.FleetId, err)
} else {
err = fmt.Errorf("Error waiting for fleet request: %s", err)
} }
// We can end up with errors because one of the allowed availability
// zones doesn't have one of the allowed instance types; as long as
// an instance is launched, these errors aren't important.
if len(createOutput.Errors) > 0 { if len(createOutput.Errors) > 0 {
errString := fmt.Sprintf("Error waiting for fleet request (%s) to become ready:", *createOutput.FleetId) errString := fmt.Sprintf("Error waiting for fleet request (%s) to become ready:", *createOutput.FleetId)
for _, outErr := range createOutput.Errors { for _, outErr := range createOutput.Errors {
errString = errString + aws.StringValue(outErr.ErrorMessage) errString = errString + aws.StringValue(outErr.ErrorMessage)
} }
err = fmt.Errorf(errString) err = fmt.Errorf(errString)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
} }
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error(err.Error())