2016-10-31 16:44:41 -05:00
|
|
|
package common
|
2013-05-21 00:55:32 -07:00
|
|
|
|
|
|
|
|
import (
|
2018-01-22 15:32:33 -08:00
|
|
|
"context"
|
2013-06-19 20:54:02 -07:00
|
|
|
"fmt"
|
2019-04-08 17:57:27 +02:00
|
|
|
"time"
|
2015-04-05 17:58:48 -04:00
|
|
|
|
2015-06-03 17:13:52 -04:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2019-04-08 17:57:27 +02:00
|
|
|
"github.com/hashicorp/packer/common/retry"
|
2018-01-19 16:18:44 -08:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 13:39:01 -07:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-05-21 00:55:32 -07:00
|
|
|
)
|
|
|
|
|
|
2016-10-31 16:44:41 -05:00
|
|
|
type StepStopEBSBackedInstance struct {
|
2017-10-23 12:16:12 -07:00
|
|
|
Skip bool
|
2016-03-14 13:49:42 -04:00
|
|
|
DisableStopInstance bool
|
2014-05-08 01:13:27 +08:00
|
|
|
}
|
2013-05-21 00:55:32 -07:00
|
|
|
|
2018-04-23 12:57:04 -07:00
|
|
|
func (s *StepStopEBSBackedInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2013-08-31 13:00:43 -07:00
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
|
|
|
|
instance := state.Get("instance").(*ec2.Instance)
|
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-05-21 00:55:32 -07:00
|
|
|
|
2014-05-08 13:58:05 +08:00
|
|
|
// Skip when it is a spot instance
|
2017-10-23 12:16:12 -07:00
|
|
|
if s.Skip {
|
2014-05-08 13:58:05 +08:00
|
|
|
return multistep.ActionContinue
|
|
|
|
|
}
|
2014-05-08 01:13:27 +08:00
|
|
|
|
2016-03-14 13:49:42 -04:00
|
|
|
var err error
|
2016-03-14 12:54:03 -04:00
|
|
|
|
2016-03-15 08:01:20 -04:00
|
|
|
if !s.DisableStopInstance {
|
2016-03-14 13:49:42 -04:00
|
|
|
// Stop the instance so we can create an AMI from it
|
|
|
|
|
ui.Say("Stopping the source instance...")
|
2017-06-13 14:28:44 -07:00
|
|
|
|
|
|
|
|
// 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.
|
2019-11-12 15:27:47 -05:00
|
|
|
err := retry.Config{Tries: 6, ShouldRetry: func(error) bool {
|
|
|
|
|
if isAWSErr(err, "InvalidInstanceID.NotFound", "") {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
},
|
2019-04-08 17:57:27 +02:00
|
|
|
RetryDelay: (&retry.Backoff{InitialBackoff: 10 * time.Second, MaxBackoff: 60 * time.Second, Multiplier: 2}).Linear,
|
|
|
|
|
}.Run(ctx, func(ctx context.Context) error {
|
|
|
|
|
ui.Message(fmt.Sprintf("Stopping instance"))
|
2017-06-13 14:28:44 -07:00
|
|
|
|
|
|
|
|
_, err = ec2conn.StopInstances(&ec2.StopInstancesInput{
|
|
|
|
|
InstanceIds: []*string{instance.InstanceId},
|
|
|
|
|
})
|
|
|
|
|
|
2019-04-08 17:57:27 +02:00
|
|
|
return err
|
2016-03-14 13:49:42 -04:00
|
|
|
})
|
2017-06-13 14:28:44 -07:00
|
|
|
|
2016-03-14 13:49:42 -04:00
|
|
|
if err != nil {
|
|
|
|
|
err := fmt.Errorf("Error stopping instance: %s", err)
|
|
|
|
|
state.Put("error", err)
|
|
|
|
|
ui.Error(err.Error())
|
|
|
|
|
return multistep.ActionHalt
|
|
|
|
|
}
|
2017-06-13 14:28:44 -07:00
|
|
|
|
2016-03-15 08:01:20 -04:00
|
|
|
} else {
|
|
|
|
|
ui.Say("Automatic instance stop disabled. Please stop instance manually.")
|
2016-03-14 13:49:42 -04:00
|
|
|
}
|
2013-05-21 00:55:32 -07:00
|
|
|
|
2017-12-07 14:05:59 -08:00
|
|
|
// Wait for the instance to actually stop
|
2013-05-21 00:55:32 -07:00
|
|
|
ui.Say("Waiting for the instance to stop...")
|
2018-07-12 14:30:44 -07:00
|
|
|
err = ec2conn.WaitUntilInstanceStoppedWithContext(ctx,
|
|
|
|
|
&ec2.DescribeInstancesInput{
|
|
|
|
|
InstanceIds: []*string{instance.InstanceId},
|
|
|
|
|
},
|
|
|
|
|
getWaiterOptions()...)
|
2017-12-07 14:05:59 -08:00
|
|
|
|
2013-05-21 00:55:32 -07:00
|
|
|
if err != nil {
|
2013-06-19 20:54:02 -07:00
|
|
|
err := fmt.Errorf("Error waiting for instance to stop: %s", err)
|
2013-08-31 13:00:43 -07:00
|
|
|
state.Put("error", err)
|
2013-05-21 00:55:32 -07:00
|
|
|
ui.Error(err.Error())
|
2013-06-04 10:00:06 -07:00
|
|
|
return multistep.ActionHalt
|
2013-05-21 00:55:32 -07:00
|
|
|
}
|
|
|
|
|
|
2013-06-04 10:00:06 -07:00
|
|
|
return multistep.ActionContinue
|
2013-05-21 00:55:32 -07:00
|
|
|
}
|
|
|
|
|
|
2016-10-31 16:44:41 -05:00
|
|
|
func (s *StepStopEBSBackedInstance) Cleanup(multistep.StateBag) {
|
2013-05-21 00:55:32 -07:00
|
|
|
// No cleanup...
|
|
|
|
|
}
|