|
|
|
|
@ -11,17 +11,38 @@ import (
|
|
|
|
|
|
|
|
|
|
type StateChangeConf struct {
|
|
|
|
|
Conn *ec2.EC2
|
|
|
|
|
Instance *ec2.Instance
|
|
|
|
|
Pending []string
|
|
|
|
|
Refresh func() (interface{}, string, error)
|
|
|
|
|
StepState map[string]interface{}
|
|
|
|
|
Target string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func WaitForState(conf *StateChangeConf) (i *ec2.Instance, err error) {
|
|
|
|
|
func InstanceStateRefreshFunc(conn *ec2.EC2, i *ec2.Instance) func() (interface{}, string, error) {
|
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
|
resp, err := conn.Instances([]string{i.InstanceId}, ec2.NewFilter())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
i = &resp.Reservations[0].Instances[0]
|
|
|
|
|
return i, i.State.Name, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func WaitForState(conf *StateChangeConf) (i interface{}, err error) {
|
|
|
|
|
log.Printf("Waiting for instance state to become: %s", conf.Target)
|
|
|
|
|
|
|
|
|
|
i = conf.Instance
|
|
|
|
|
for i.State.Name != conf.Target {
|
|
|
|
|
for {
|
|
|
|
|
var currentState string
|
|
|
|
|
i, currentState, err = conf.Refresh()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if currentState == conf.Target {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if conf.StepState != nil {
|
|
|
|
|
if _, ok := conf.StepState[multistep.StateCancelled]; ok {
|
|
|
|
|
return nil, errors.New("interrupted")
|
|
|
|
|
@ -30,24 +51,17 @@ func WaitForState(conf *StateChangeConf) (i *ec2.Instance, err error) {
|
|
|
|
|
|
|
|
|
|
found := false
|
|
|
|
|
for _, allowed := range conf.Pending {
|
|
|
|
|
if i.State.Name == allowed {
|
|
|
|
|
if currentState == allowed {
|
|
|
|
|
found = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
|
fmt.Errorf("unexpected state '%s', wanted target '%s'", i.State.Name, conf.Target)
|
|
|
|
|
fmt.Errorf("unexpected state '%s', wanted target '%s'", currentState, conf.Target)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var resp *ec2.InstancesResp
|
|
|
|
|
resp, err = conf.Conn.Instances([]string{i.InstanceId}, ec2.NewFilter())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
i = &resp.Reservations[0].Instances[0]
|
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|