|
|
|
|
@ -1,16 +1,13 @@
|
|
|
|
|
package common
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
"net"
|
|
|
|
|
"os"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
|
"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/hashicorp/packer/helper/multistep"
|
|
|
|
|
)
|
|
|
|
|
@ -35,189 +32,220 @@ type StateChangeConf struct {
|
|
|
|
|
Target string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AMIStateRefreshFunc returns a StateRefreshFunc that is used to watch
|
|
|
|
|
// an AMI for state changes.
|
|
|
|
|
func AMIStateRefreshFunc(conn *ec2.EC2, imageId string) StateRefreshFunc {
|
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
|
resp, err := conn.DescribeImages(&ec2.DescribeImagesInput{
|
|
|
|
|
ImageIds: []*string{&imageId},
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidAMIID.NotFound" {
|
|
|
|
|
// Set this to nil as if we didn't find anything.
|
|
|
|
|
resp = nil
|
|
|
|
|
} else if isTransientNetworkError(err) {
|
|
|
|
|
// Transient network error, treat it as if we didn't find anything
|
|
|
|
|
resp = nil
|
|
|
|
|
} else {
|
|
|
|
|
log.Printf("Error on AMIStateRefresh: %s", err)
|
|
|
|
|
return nil, "", err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if resp == nil || len(resp.Images) == 0 {
|
|
|
|
|
// Sometimes AWS has consistency issues and doesn't see the
|
|
|
|
|
// AMI. Return an empty state.
|
|
|
|
|
return nil, "", nil
|
|
|
|
|
}
|
|
|
|
|
// Following are wrapper functions that use Packer's environment-variables to
|
|
|
|
|
// determing retry logic, then call the AWS SDK's built-in waiters.
|
|
|
|
|
|
|
|
|
|
i := resp.Images[0]
|
|
|
|
|
return i, *i.State, nil
|
|
|
|
|
func WaitUntilAMIAvailable(conn *ec2.EC2, imageId string) error {
|
|
|
|
|
imageInput := ec2.DescribeImagesInput{
|
|
|
|
|
ImageIds: []*string{&imageId},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// InstanceStateRefreshFunc returns a StateRefreshFunc that is used to watch
|
|
|
|
|
// an EC2 instance.
|
|
|
|
|
func InstanceStateRefreshFunc(conn *ec2.EC2, instanceId string) StateRefreshFunc {
|
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
|
resp, err := conn.DescribeInstances(&ec2.DescribeInstancesInput{
|
|
|
|
|
InstanceIds: []*string{&instanceId},
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidInstanceID.NotFound" {
|
|
|
|
|
// Set this to nil as if we didn't find anything.
|
|
|
|
|
resp = nil
|
|
|
|
|
} else if isTransientNetworkError(err) {
|
|
|
|
|
// Transient network error, treat it as if we didn't find anything
|
|
|
|
|
resp = nil
|
|
|
|
|
} else {
|
|
|
|
|
log.Printf("Error on InstanceStateRefresh: %s", err)
|
|
|
|
|
return nil, "", err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if resp == nil || len(resp.Reservations) == 0 || len(resp.Reservations[0].Instances) == 0 {
|
|
|
|
|
// Sometimes AWS just has consistency issues and doesn't see
|
|
|
|
|
// our instance yet. Return an empty state.
|
|
|
|
|
return nil, "", nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
i := resp.Reservations[0].Instances[0]
|
|
|
|
|
return i, *i.State.Name, nil
|
|
|
|
|
}
|
|
|
|
|
err := conn.WaitUntilImageAvailableWithContext(aws.BackgroundContext(),
|
|
|
|
|
&imageInput,
|
|
|
|
|
getWaiterOptions()...)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SpotRequestStateRefreshFunc returns a StateRefreshFunc that is used to watch
|
|
|
|
|
// a spot request for state changes.
|
|
|
|
|
func SpotRequestStateRefreshFunc(conn *ec2.EC2, spotRequestId string) StateRefreshFunc {
|
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
|
resp, err := conn.DescribeSpotInstanceRequests(&ec2.DescribeSpotInstanceRequestsInput{
|
|
|
|
|
SpotInstanceRequestIds: []*string{&spotRequestId},
|
|
|
|
|
})
|
|
|
|
|
func WaitUntilInstanceTerminated(conn *ec2.EC2, instanceId string) error {
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidSpotInstanceRequestID.NotFound" {
|
|
|
|
|
// Set this to nil as if we didn't find anything.
|
|
|
|
|
resp = nil
|
|
|
|
|
} else if isTransientNetworkError(err) {
|
|
|
|
|
// Transient network error, treat it as if we didn't find anything
|
|
|
|
|
resp = nil
|
|
|
|
|
} else {
|
|
|
|
|
log.Printf("Error on SpotRequestStateRefresh: %s", err)
|
|
|
|
|
return nil, "", err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
instanceInput := ec2.DescribeInstancesInput{
|
|
|
|
|
InstanceIds: []*string{&instanceId},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if resp == nil || len(resp.SpotInstanceRequests) == 0 {
|
|
|
|
|
// Sometimes AWS has consistency issues and doesn't see the
|
|
|
|
|
// SpotRequest. Return an empty state.
|
|
|
|
|
return nil, "", nil
|
|
|
|
|
}
|
|
|
|
|
err := conn.WaitUntilInstanceTerminatedWithContext(aws.BackgroundContext(),
|
|
|
|
|
&instanceInput,
|
|
|
|
|
getWaiterOptions()...)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
i := resp.SpotInstanceRequests[0]
|
|
|
|
|
return i, *i.State, nil
|
|
|
|
|
// This function works for both requesting and cancelling spot instances.
|
|
|
|
|
func WaitUntilSpotRequestFulfilled(conn *ec2.EC2, spotRequestId string) error {
|
|
|
|
|
spotRequestInput := ec2.DescribeSpotInstanceRequestsInput{
|
|
|
|
|
SpotInstanceRequestIds: []*string{&spotRequestId},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := conn.WaitUntilSpotInstanceRequestFulfilledWithContext(aws.BackgroundContext(),
|
|
|
|
|
&spotRequestInput,
|
|
|
|
|
getWaiterOptions()...)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ImportImageRefreshFunc(conn *ec2.EC2, importTaskId string) StateRefreshFunc {
|
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
|
resp, err := conn.DescribeImportImageTasks(&ec2.DescribeImportImageTasksInput{
|
|
|
|
|
ImportTaskIds: []*string{
|
|
|
|
|
&importTaskId,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if ec2err, ok := err.(awserr.Error); ok && strings.HasPrefix(ec2err.Code(), "InvalidConversionTaskId") {
|
|
|
|
|
resp = nil
|
|
|
|
|
} else if isTransientNetworkError(err) {
|
|
|
|
|
resp = nil
|
|
|
|
|
} else {
|
|
|
|
|
log.Printf("Error on ImportImageRefresh: %s", err)
|
|
|
|
|
return nil, "", err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
func WaitUntilVolumeAvailable(conn *ec2.EC2, volumeId string) error {
|
|
|
|
|
volumeInput := ec2.DescribeVolumesInput{
|
|
|
|
|
VolumeIds: []*string{&volumeId},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if resp == nil || len(resp.ImportImageTasks) == 0 {
|
|
|
|
|
return nil, "", nil
|
|
|
|
|
}
|
|
|
|
|
err := conn.WaitUntilVolumeAvailableWithContext(aws.BackgroundContext(),
|
|
|
|
|
&volumeInput,
|
|
|
|
|
getWaiterOptions()...)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
i := resp.ImportImageTasks[0]
|
|
|
|
|
return i, *i.Status, nil
|
|
|
|
|
func WaitUntilSnapshotDone(conn *ec2.EC2, snapshotID string) error {
|
|
|
|
|
snapInput := ec2.DescribeSnapshotsInput{
|
|
|
|
|
SnapshotIds: []*string{&snapshotID},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := conn.WaitUntilSnapshotCompletedWithContext(aws.BackgroundContext(),
|
|
|
|
|
&snapInput,
|
|
|
|
|
getWaiterOptions()...)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WaitForState watches an object and waits for it to achieve a certain
|
|
|
|
|
// state.
|
|
|
|
|
func WaitForState(conf *StateChangeConf) (i interface{}, err error) {
|
|
|
|
|
log.Printf("Waiting for state to become: %s", conf.Target)
|
|
|
|
|
// Wrappers for our custom AWS waiters
|
|
|
|
|
|
|
|
|
|
sleepSeconds := SleepSeconds()
|
|
|
|
|
maxTicks := TimeoutSeconds()/sleepSeconds + 1
|
|
|
|
|
notfoundTick := 0
|
|
|
|
|
func WaitUntilVolumeAttached(conn *ec2.EC2, volumeId string) error {
|
|
|
|
|
volumeInput := ec2.DescribeVolumesInput{
|
|
|
|
|
VolumeIds: []*string{&volumeId},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
var currentState string
|
|
|
|
|
i, currentState, err = conf.Refresh()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
err := WaitForVolumeToBeAttached(conn,
|
|
|
|
|
aws.BackgroundContext(),
|
|
|
|
|
&volumeInput,
|
|
|
|
|
getWaiterOptions()...)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if i == nil {
|
|
|
|
|
// If we didn't find the resource, check if we have been
|
|
|
|
|
// not finding it for awhile, and if so, report an error.
|
|
|
|
|
notfoundTick += 1
|
|
|
|
|
if notfoundTick > maxTicks {
|
|
|
|
|
return nil, errors.New("couldn't find resource")
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Reset the counter for when a resource isn't found
|
|
|
|
|
notfoundTick = 0
|
|
|
|
|
func WaitUntilVolumeDetached(conn *ec2.EC2, volumeId string) error {
|
|
|
|
|
volumeInput := ec2.DescribeVolumesInput{
|
|
|
|
|
VolumeIds: []*string{&volumeId},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if currentState == conf.Target {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
err := WaitForVolumeToBeAttached(conn,
|
|
|
|
|
aws.BackgroundContext(),
|
|
|
|
|
&volumeInput,
|
|
|
|
|
getWaiterOptions()...)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if conf.StepState != nil {
|
|
|
|
|
if _, ok := conf.StepState.GetOk(multistep.StateCancelled); ok {
|
|
|
|
|
return nil, errors.New("interrupted")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
func WaitUntilImageImported(conn *ec2.EC2, taskID string) error {
|
|
|
|
|
importInput := ec2.DescribeImportImageTasksInput{
|
|
|
|
|
ImportTaskIds: []*string{&taskID},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
found := false
|
|
|
|
|
for _, allowed := range conf.Pending {
|
|
|
|
|
if currentState == allowed {
|
|
|
|
|
found = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
err := WaitForImageToBeImported(conn,
|
|
|
|
|
aws.BackgroundContext(),
|
|
|
|
|
&importInput,
|
|
|
|
|
getWaiterOptions()...)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
|
err := fmt.Errorf("unexpected state '%s', wanted target '%s'", currentState, conf.Target)
|
|
|
|
|
return nil, err
|
|
|
|
|
// Custom waiters using AWS's request.Waiter
|
|
|
|
|
|
|
|
|
|
func WaitForVolumeToBeAttached(c *ec2.EC2, ctx aws.Context, input *ec2.DescribeVolumesInput, opts ...request.WaiterOption) error {
|
|
|
|
|
w := request.Waiter{
|
|
|
|
|
Name: "DescribeVolumes",
|
|
|
|
|
MaxAttempts: 40,
|
|
|
|
|
Delay: request.ConstantWaiterDelay(5 * time.Second),
|
|
|
|
|
Acceptors: []request.WaiterAcceptor{
|
|
|
|
|
{
|
|
|
|
|
State: request.SuccessWaiterState,
|
|
|
|
|
Matcher: request.PathAllWaiterMatch,
|
|
|
|
|
Argument: "Volumes[].State",
|
|
|
|
|
Expected: "attached",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
State: request.FailureWaiterState,
|
|
|
|
|
Matcher: request.PathAnyWaiterMatch,
|
|
|
|
|
Argument: "Volumes[].State",
|
|
|
|
|
Expected: "deleted",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Logger: c.Config.Logger,
|
|
|
|
|
NewRequest: func(opts []request.Option) (*request.Request, error) {
|
|
|
|
|
var inCpy *ec2.DescribeVolumesInput
|
|
|
|
|
if input != nil {
|
|
|
|
|
tmp := *input
|
|
|
|
|
inCpy = &tmp
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
req, _ := c.DescribeVolumesRequest(inCpy)
|
|
|
|
|
req.SetContext(ctx)
|
|
|
|
|
req.ApplyOptions(opts...)
|
|
|
|
|
return req, nil
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
return w.WaitWithContext(ctx)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
time.Sleep(time.Duration(sleepSeconds) * time.Second)
|
|
|
|
|
func WaitForVolumeToBeDetached(c *ec2.EC2, ctx aws.Context, input *ec2.DescribeVolumesInput, opts ...request.WaiterOption) error {
|
|
|
|
|
w := request.Waiter{
|
|
|
|
|
Name: "DescribeVolumes",
|
|
|
|
|
MaxAttempts: 40,
|
|
|
|
|
Delay: request.ConstantWaiterDelay(5 * time.Second),
|
|
|
|
|
Acceptors: []request.WaiterAcceptor{
|
|
|
|
|
{
|
|
|
|
|
State: request.SuccessWaiterState,
|
|
|
|
|
Matcher: request.PathAllWaiterMatch,
|
|
|
|
|
Argument: "Volumes[].State",
|
|
|
|
|
Expected: "detached",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Logger: c.Config.Logger,
|
|
|
|
|
NewRequest: func(opts []request.Option) (*request.Request, error) {
|
|
|
|
|
var inCpy *ec2.DescribeVolumesInput
|
|
|
|
|
if input != nil {
|
|
|
|
|
tmp := *input
|
|
|
|
|
inCpy = &tmp
|
|
|
|
|
}
|
|
|
|
|
req, _ := c.DescribeVolumesRequest(inCpy)
|
|
|
|
|
req.SetContext(ctx)
|
|
|
|
|
req.ApplyOptions(opts...)
|
|
|
|
|
return req, nil
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
return w.WaitWithContext(ctx)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func isTransientNetworkError(err error) bool {
|
|
|
|
|
if nerr, ok := err.(net.Error); ok && nerr.Temporary() {
|
|
|
|
|
return true
|
|
|
|
|
func WaitForImageToBeImported(c *ec2.EC2, ctx aws.Context, input *ec2.DescribeImportImageTasksInput, opts ...request.WaiterOption) error {
|
|
|
|
|
w := request.Waiter{
|
|
|
|
|
Name: "DescribeImages",
|
|
|
|
|
MaxAttempts: 40,
|
|
|
|
|
Delay: request.ConstantWaiterDelay(5 * time.Second),
|
|
|
|
|
Acceptors: []request.WaiterAcceptor{
|
|
|
|
|
{
|
|
|
|
|
State: request.SuccessWaiterState,
|
|
|
|
|
Matcher: request.PathAllWaiterMatch,
|
|
|
|
|
Argument: "ImportImageTasks[].State",
|
|
|
|
|
Expected: "completed",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
State: request.RetryWaiterState,
|
|
|
|
|
Matcher: request.ErrorWaiterMatch,
|
|
|
|
|
Expected: "InvalidConversionTaskId",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Logger: c.Config.Logger,
|
|
|
|
|
NewRequest: func(opts []request.Option) (*request.Request, error) {
|
|
|
|
|
var inCpy *ec2.DescribeImportImageTasksInput
|
|
|
|
|
if input != nil {
|
|
|
|
|
tmp := *input
|
|
|
|
|
inCpy = &tmp
|
|
|
|
|
}
|
|
|
|
|
req, _ := c.DescribeImportImageTasksRequest(inCpy)
|
|
|
|
|
req.SetContext(ctx)
|
|
|
|
|
req.ApplyOptions(opts...)
|
|
|
|
|
return req, nil
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
return w.WaitWithContext(ctx)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
// This helper function uses the environment variables AWS_TIMEOUT_SECONDS and
|
|
|
|
|
// AWS_POLL_DELAY_SECONDS to generate waiter options that can be passed into any
|
|
|
|
|
// request.Waiter function. These options will control how many times the waiter
|
|
|
|
|
// will retry the request, as well as how long to wait between the retries.
|
|
|
|
|
func getWaiterOptions() []request.WaiterOption {
|
|
|
|
|
// use env vars to read in the wait delay and the max amount of time to wait
|
|
|
|
|
delay := SleepSeconds()
|
|
|
|
|
timeoutSeconds := TimeoutSeconds()
|
|
|
|
|
// AWS sdk uses max attempts instead of a timeout; convert timeout into
|
|
|
|
|
// max attempts
|
|
|
|
|
maxAttempts := timeoutSeconds / delay
|
|
|
|
|
delaySeconds := request.ConstantWaiterDelay(time.Duration(delay) * time.Second)
|
|
|
|
|
|
|
|
|
|
return []request.WaiterOption{
|
|
|
|
|
request.WithWaiterDelay(delaySeconds),
|
|
|
|
|
request.WithWaiterMaxAttempts(maxAttempts)}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns 300 seconds (5 minutes) by default
|
|
|
|
|
|