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"
|
|
|
|
"github.com/mitchellh/goamz/ec2"
|
2013-07-25 21:49:15 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2013-05-21 03:55:32 -04:00
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
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) {
|
|
|
|
resp, err := conn.Images([]string{imageId}, ec2.NewFilter())
|
|
|
|
if err != nil {
|
|
|
|
if ec2err, ok := err.(*ec2.Error); ok && ec2err.Code == "InvalidAMIID.NotFound" {
|
|
|
|
// Set this to nil 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
|
|
|
|
}
|
|
|
|
|
|
|
|
i := resp.Images[0]
|
|
|
|
return i, i.State, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-03 19:21:01 -04:00
|
|
|
// InstanceStateRefreshFunc returns a StateRefreshFunc that is used to watch
|
|
|
|
// an EC2 instance.
|
|
|
|
func InstanceStateRefreshFunc(conn *ec2.EC2, i *ec2.Instance) StateRefreshFunc {
|
2013-07-29 21:47:43 -04:00
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
resp, err := conn.Instances([]string{i.InstanceId}, ec2.NewFilter())
|
|
|
|
if err != nil {
|
2013-10-11 17:05:24 -04:00
|
|
|
if ec2err, ok := err.(*ec2.Error); ok && ec2err.Code == "InvalidInstanceID.NotFound" {
|
|
|
|
// Set this to nil as if we didn't find anything.
|
|
|
|
resp = nil
|
|
|
|
} 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
|
|
|
|
}
|
|
|
|
|
2013-07-29 21:47:43 -04:00
|
|
|
i = &resp.Reservations[0].Instances[0]
|
|
|
|
return i, i.State.Name, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
resp, err := conn.DescribeSpotRequests([]string{spotRequestId}, ec2.NewFilter())
|
|
|
|
if err != nil {
|
|
|
|
if ec2err, ok := err.(*ec2.Error); ok && ec2err.Code == "InvalidSpotInstanceRequestID.NotFound" {
|
|
|
|
// Set this to nil as if we didn't find anything.
|
|
|
|
resp = nil
|
|
|
|
} else {
|
|
|
|
log.Printf("Error on SpotRequestStateRefresh: %s", err)
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp == nil || len(resp.SpotRequestResults) == 0 {
|
|
|
|
// Sometimes AWS has consistency issues and doesn't see the
|
|
|
|
// SpotRequest. Return an empty state.
|
|
|
|
return nil, "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
i := resp.SpotRequestResults[0]
|
|
|
|
return i, i.State, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
if notfoundTick > 20 {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-05-21 03:55:32 -04:00
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|