2013-07-20 22:58:27 -04:00
|
|
|
package common
|
2013-05-21 03:55:32 -04:00
|
|
|
|
|
|
|
import (
|
2013-07-25 21:49:15 -04:00
|
|
|
"errors"
|
2013-05-21 03:55:32 -04:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2014-10-13 12:22:33 -04:00
|
|
|
"net"
|
2014-09-29 21:14:52 -04:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2015-11-22 21:55:09 -05:00
|
|
|
"strings"
|
2016-02-12 02:53:40 -05:00
|
|
|
"time"
|
2015-04-05 17:58:48 -04:00
|
|
|
|
2015-06-03 17:13:52 -04:00
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2015-04-05 17:58:48 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2013-05-21 03:55:32 -04:00
|
|
|
)
|
|
|
|
|
2013-08-03 19:21:01 -04:00
|
|
|
// StateRefreshFunc is a function type used for StateChangeConf that is
|
|
|
|
// responsible for refreshing the item being watched for a state change.
|
|
|
|
//
|
|
|
|
// It returns three results. `result` is any object that will be returned
|
|
|
|
// as the final object after waiting for state change. This allows you to
|
|
|
|
// return the final updated object, for example an EC2 instance after refreshing
|
|
|
|
// it.
|
|
|
|
//
|
|
|
|
// `state` is the latest state of that object. And `err` is any error that
|
|
|
|
// may have happened while refreshing the state.
|
|
|
|
type StateRefreshFunc func() (result interface{}, state string, err error)
|
|
|
|
|
|
|
|
// StateChangeConf is the configuration struct used for `WaitForState`.
|
2013-07-25 21:49:15 -04:00
|
|
|
type StateChangeConf struct {
|
|
|
|
Pending []string
|
2013-08-03 19:21:01 -04:00
|
|
|
Refresh StateRefreshFunc
|
2013-08-31 15:58:55 -04:00
|
|
|
StepState multistep.StateBag
|
2013-07-25 21:49:15 -04:00
|
|
|
Target string
|
|
|
|
}
|
|
|
|
|
2013-09-12 23:33:32 -04:00
|
|
|
// 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) {
|
2015-04-05 17:58:48 -04:00
|
|
|
resp, err := conn.DescribeImages(&ec2.DescribeImagesInput{
|
2015-08-17 20:44:01 -04:00
|
|
|
ImageIds: []*string{&imageId},
|
2015-04-05 17:58:48 -04:00
|
|
|
})
|
2013-09-12 23:33:32 -04:00
|
|
|
if err != nil {
|
2015-05-28 13:20:34 -04:00
|
|
|
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidAMIID.NotFound" {
|
2013-09-12 23:33:32 -04:00
|
|
|
// Set this to nil as if we didn't find anything.
|
|
|
|
resp = nil
|
2014-10-13 12:22:33 -04:00
|
|
|
} else if isTransientNetworkError(err) {
|
|
|
|
// Transient network error, treat it as if we didn't find anything
|
|
|
|
resp = nil
|
2013-09-12 23:33:32 -04:00
|
|
|
} 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
|
|
|
|
}
|
|
|
|
|
|
|
|
i := resp.Images[0]
|
2015-04-05 17:58:48 -04:00
|
|
|
return i, *i.State, nil
|
2013-09-12 23:33:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-03 19:21:01 -04:00
|
|
|
// InstanceStateRefreshFunc returns a StateRefreshFunc that is used to watch
|
|
|
|
// an EC2 instance.
|
2014-12-15 20:18:47 -05:00
|
|
|
func InstanceStateRefreshFunc(conn *ec2.EC2, instanceId string) StateRefreshFunc {
|
2013-07-29 21:47:43 -04:00
|
|
|
return func() (interface{}, string, error) {
|
2015-04-05 17:58:48 -04:00
|
|
|
resp, err := conn.DescribeInstances(&ec2.DescribeInstancesInput{
|
2015-08-17 20:44:01 -04:00
|
|
|
InstanceIds: []*string{&instanceId},
|
2015-04-05 17:58:48 -04:00
|
|
|
})
|
2013-07-29 21:47:43 -04:00
|
|
|
if err != nil {
|
2015-05-28 13:20:34 -04:00
|
|
|
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidInstanceID.NotFound" {
|
2013-10-11 17:05:24 -04:00
|
|
|
// Set this to nil as if we didn't find anything.
|
|
|
|
resp = nil
|
2014-10-13 12:22:33 -04:00
|
|
|
} else if isTransientNetworkError(err) {
|
|
|
|
// Transient network error, treat it as if we didn't find anything
|
|
|
|
resp = nil
|
2013-10-11 17:05:24 -04:00
|
|
|
} else {
|
|
|
|
log.Printf("Error on InstanceStateRefresh: %s", err)
|
|
|
|
return nil, "", err
|
|
|
|
}
|
2013-07-29 21:47:43 -04:00
|
|
|
}
|
|
|
|
|
2013-10-11 17:05:24 -04:00
|
|
|
if resp == nil || len(resp.Reservations) == 0 || len(resp.Reservations[0].Instances) == 0 {
|
2013-09-05 20:19:23 -04:00
|
|
|
// Sometimes AWS just has consistency issues and doesn't see
|
|
|
|
// our instance yet. Return an empty state.
|
|
|
|
return nil, "", nil
|
|
|
|
}
|
|
|
|
|
2015-06-10 13:33:01 -04:00
|
|
|
i := resp.Reservations[0].Instances[0]
|
2015-04-05 17:58:48 -04:00
|
|
|
return i, *i.State.Name, nil
|
2013-07-29 21:47:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-07 13:13:27 -04:00
|
|
|
// 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) {
|
2015-04-05 17:58:48 -04:00
|
|
|
resp, err := conn.DescribeSpotInstanceRequests(&ec2.DescribeSpotInstanceRequestsInput{
|
2015-08-17 20:44:01 -04:00
|
|
|
SpotInstanceRequestIds: []*string{&spotRequestId},
|
2015-04-05 17:58:48 -04:00
|
|
|
})
|
|
|
|
|
2014-05-07 13:13:27 -04:00
|
|
|
if err != nil {
|
2015-05-28 13:20:34 -04:00
|
|
|
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidSpotInstanceRequestID.NotFound" {
|
2014-05-07 13:13:27 -04:00
|
|
|
// Set this to nil as if we didn't find anything.
|
|
|
|
resp = nil
|
2014-10-13 12:22:33 -04:00
|
|
|
} else if isTransientNetworkError(err) {
|
|
|
|
// Transient network error, treat it as if we didn't find anything
|
|
|
|
resp = nil
|
2014-05-07 13:13:27 -04:00
|
|
|
} else {
|
|
|
|
log.Printf("Error on SpotRequestStateRefresh: %s", err)
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-05 17:58:48 -04:00
|
|
|
if resp == nil || len(resp.SpotInstanceRequests) == 0 {
|
2014-05-07 13:13:27 -04:00
|
|
|
// Sometimes AWS has consistency issues and doesn't see the
|
|
|
|
// SpotRequest. Return an empty state.
|
|
|
|
return nil, "", nil
|
|
|
|
}
|
|
|
|
|
2015-04-05 17:58:48 -04:00
|
|
|
i := resp.SpotInstanceRequests[0]
|
|
|
|
return i, *i.State, nil
|
2014-05-07 13:13:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-22 21:55:09 -05:00
|
|
|
func ImportImageRefreshFunc(conn *ec2.EC2, importTaskId string) StateRefreshFunc {
|
2016-02-12 02:53:40 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp == nil || len(resp.ImportImageTasks) == 0 {
|
|
|
|
return nil, "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
i := resp.ImportImageTasks[0]
|
|
|
|
return i, *i.Status, nil
|
|
|
|
}
|
2015-11-22 21:55:09 -05:00
|
|
|
}
|
|
|
|
|
2013-08-03 19:21:01 -04:00
|
|
|
// WaitForState watches an object and waits for it to achieve a certain
|
|
|
|
// state.
|
2013-07-29 21:47:43 -04:00
|
|
|
func WaitForState(conf *StateChangeConf) (i interface{}, err error) {
|
2013-07-30 20:32:41 -04:00
|
|
|
log.Printf("Waiting for state to become: %s", conf.Target)
|
2013-07-25 21:49:15 -04:00
|
|
|
|
2016-10-28 03:28:12 -04:00
|
|
|
sleepSeconds := SleepSeconds()
|
2014-10-28 11:35:21 -04:00
|
|
|
maxTicks := int(TimeoutSeconds()/sleepSeconds) + 1
|
2013-09-12 23:37:14 -04:00
|
|
|
notfoundTick := 0
|
|
|
|
|
2013-07-29 21:47:43 -04:00
|
|
|
for {
|
|
|
|
var currentState string
|
|
|
|
i, currentState, err = conf.Refresh()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-09-12 23:37:14 -04:00
|
|
|
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
|
2014-09-29 21:14:52 -04:00
|
|
|
if notfoundTick > maxTicks {
|
2013-09-12 23:37:14 -04:00
|
|
|
return nil, errors.New("couldn't find resource")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Reset the counter for when a resource isn't found
|
|
|
|
notfoundTick = 0
|
|
|
|
|
2013-09-05 20:19:23 -04:00
|
|
|
if currentState == conf.Target {
|
|
|
|
return
|
|
|
|
}
|
2013-07-29 21:47:43 -04:00
|
|
|
|
2013-09-05 20:19:23 -04:00
|
|
|
if conf.StepState != nil {
|
|
|
|
if _, ok := conf.StepState.GetOk(multistep.StateCancelled); ok {
|
|
|
|
return nil, errors.New("interrupted")
|
|
|
|
}
|
2013-07-25 21:49:15 -04:00
|
|
|
}
|
2013-05-21 03:55:32 -04:00
|
|
|
|
2013-09-05 20:19:23 -04:00
|
|
|
found := false
|
|
|
|
for _, allowed := range conf.Pending {
|
|
|
|
if currentState == allowed {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
2013-06-04 14:29:59 -04:00
|
|
|
}
|
|
|
|
|
2013-09-05 20:19:23 -04:00
|
|
|
if !found {
|
2014-05-10 12:40:50 -04:00
|
|
|
err := fmt.Errorf("unexpected state '%s', wanted target '%s'", currentState, conf.Target)
|
|
|
|
return nil, err
|
2013-09-05 20:19:23 -04:00
|
|
|
}
|
2013-06-04 14:29:59 -04:00
|
|
|
}
|
|
|
|
|
2014-09-29 21:14:52 -04:00
|
|
|
time.Sleep(time.Duration(sleepSeconds) * time.Second)
|
2013-05-21 03:55:32 -04:00
|
|
|
}
|
|
|
|
}
|
2014-10-14 18:39:13 -04:00
|
|
|
|
|
|
|
func isTransientNetworkError(err error) bool {
|
|
|
|
if nerr, ok := err.(net.Error); ok && nerr.Temporary() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2014-09-29 21:14:52 -04:00
|
|
|
|
|
|
|
// Returns 300 seconds (5 minutes) by default
|
|
|
|
// Some AWS operations, like copying an AMI to a distant region, take a very long time
|
|
|
|
// Allow user to override with AWS_TIMEOUT_SECONDS environment variable
|
|
|
|
func TimeoutSeconds() (seconds int) {
|
|
|
|
seconds = 300
|
|
|
|
|
|
|
|
override := os.Getenv("AWS_TIMEOUT_SECONDS")
|
|
|
|
if override != "" {
|
|
|
|
n, err := strconv.Atoi(override)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Invalid timeout seconds '%s', using default", override)
|
|
|
|
} else {
|
|
|
|
seconds = n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Allowing %ds to complete (change with AWS_TIMEOUT_SECONDS)", seconds)
|
|
|
|
return seconds
|
|
|
|
}
|
2016-10-28 03:28:12 -04:00
|
|
|
|
|
|
|
// Returns 2 seconds by default
|
|
|
|
// AWS async operations sometimes takes long times, if there are multiple parallel builds,
|
|
|
|
// polling at 2 second frequency will exceed the request limit. Allow 2 seconds to be
|
|
|
|
// overwritten with AWS_POLL_DELAY_SECONDS
|
|
|
|
func SleepSeconds() (seconds int) {
|
|
|
|
seconds = 2
|
|
|
|
|
|
|
|
override := os.Getenv("AWS_POLL_DELAY_SECONDS")
|
|
|
|
if override != "" {
|
|
|
|
n, err := strconv.Atoi(override)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Invalid timeout seconds '%s', using default", override)
|
|
|
|
} else {
|
|
|
|
seconds = n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Using %ds as polling delay (change with AWS_POLL_DELAY_SECONDS)", seconds)
|
|
|
|
return seconds
|
|
|
|
}
|