Merge pull request #5029 from hashicorp/5007_instance_stop
add exponential backoff retry for stopping instance in amazon
This commit is contained in:
commit
ac15b33d2b
|
@ -3,7 +3,9 @@ package common
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
||||||
"github.com/aws/aws-sdk-go/service/ec2"
|
"github.com/aws/aws-sdk-go/service/ec2"
|
||||||
|
"github.com/hashicorp/packer/common"
|
||||||
"github.com/hashicorp/packer/packer"
|
"github.com/hashicorp/packer/packer"
|
||||||
"github.com/mitchellh/multistep"
|
"github.com/mitchellh/multistep"
|
||||||
)
|
)
|
||||||
|
@ -28,15 +30,47 @@ func (s *StepStopEBSBackedInstance) Run(state multistep.StateBag) multistep.Step
|
||||||
if !s.DisableStopInstance {
|
if !s.DisableStopInstance {
|
||||||
// Stop the instance so we can create an AMI from it
|
// Stop the instance so we can create an AMI from it
|
||||||
ui.Say("Stopping the source instance...")
|
ui.Say("Stopping the source instance...")
|
||||||
|
|
||||||
|
// Amazon EC2 API follows an eventual consistency model.
|
||||||
|
|
||||||
|
// This means that if you run a command to modify or describe a resource
|
||||||
|
// that you just created, its ID might not have propagated throughout
|
||||||
|
// the system, and you will get an error responding that the resource
|
||||||
|
// does not exist.
|
||||||
|
|
||||||
|
// Work around this by retrying a few times, up to about 5 minutes.
|
||||||
|
err := common.Retry(10, 60, 6, func(i uint) (bool, error) {
|
||||||
|
ui.Message(fmt.Sprintf("Stopping instance, attempt %d", i+1))
|
||||||
|
|
||||||
_, err = ec2conn.StopInstances(&ec2.StopInstancesInput{
|
_, err = ec2conn.StopInstances(&ec2.StopInstancesInput{
|
||||||
InstanceIds: []*string{instance.InstanceId},
|
InstanceIds: []*string{instance.InstanceId},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
// success
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if awsErr, ok := err.(awserr.Error); ok {
|
||||||
|
if awsErr.Code() == "InvalidInstanceID.NotFound" {
|
||||||
|
ui.Message(fmt.Sprintf(
|
||||||
|
"Error stopping instance; will retry ..."+
|
||||||
|
"Error: %s", err))
|
||||||
|
// retry
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// errored, but not in expected way. Don't want to retry
|
||||||
|
return true, err
|
||||||
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err := fmt.Errorf("Error stopping instance: %s", err)
|
err := fmt.Errorf("Error stopping instance: %s", err)
|
||||||
state.Put("error", err)
|
state.Put("error", err)
|
||||||
ui.Error(err.Error())
|
ui.Error(err.Error())
|
||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
ui.Say("Automatic instance stop disabled. Please stop instance manually.")
|
ui.Say("Automatic instance stop disabled. Please stop instance manually.")
|
||||||
}
|
}
|
||||||
|
@ -44,7 +78,7 @@ func (s *StepStopEBSBackedInstance) Run(state multistep.StateBag) multistep.Step
|
||||||
// Wait for the instance to actual stop
|
// Wait for the instance to actual stop
|
||||||
ui.Say("Waiting for the instance to stop...")
|
ui.Say("Waiting for the instance to stop...")
|
||||||
stateChange := StateChangeConf{
|
stateChange := StateChangeConf{
|
||||||
Pending: []string{"running", "stopping"},
|
Pending: []string{"running", "pending", "stopping"},
|
||||||
Target: "stopped",
|
Target: "stopped",
|
||||||
Refresh: InstanceStateRefreshFunc(ec2conn, *instance.InstanceId),
|
Refresh: InstanceStateRefreshFunc(ec2conn, *instance.InstanceId),
|
||||||
StepState: state,
|
StepState: state,
|
||||||
|
|
Loading…
Reference in New Issue