Commit 04a4d914 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

builder/amazonebs: Better checking for states

parent a099e32d
......@@ -7,12 +7,24 @@ import (
"time"
)
func waitForState(ec2conn *ec2.EC2, originalInstance *ec2.Instance, target string) (i *ec2.Instance, err error) {
func waitForState(ec2conn *ec2.EC2, originalInstance *ec2.Instance, pending []string, target string) (i *ec2.Instance, err error) {
log.Printf("Waiting for instance state to become: %s", target)
i = originalInstance
original := i.State.Name
for i.State.Name == original {
for i.State.Name != target {
found := false
for _, allowed := range pending {
if i.State.Name == allowed {
found = true
break
}
}
if !found {
fmt.Errorf("unexpected state '%s', wanted target '%s'", i.State.Name, target)
return
}
var resp *ec2.InstancesResp
resp, err = ec2conn.Instances([]string{i.InstanceId}, ec2.NewFilter())
if err != nil {
......@@ -20,14 +32,8 @@ func waitForState(ec2conn *ec2.EC2, originalInstance *ec2.Instance, target strin
}
i = &resp.Reservations[0].Instances[0]
time.Sleep(2 * time.Second)
}
if i.State.Name != target {
err = fmt.Errorf("unexpected target state '%s', wanted '%s'", i.State.Name, target)
return
}
return
}
......@@ -36,7 +36,7 @@ func (s *stepRunSourceInstance) Run(state map[string]interface{}) multistep.Step
log.Printf("instance id: %s", s.instance.InstanceId)
ui.Say("Waiting for instance to become ready...")
s.instance, err = waitForState(ec2conn, s.instance, "running")
s.instance, err = waitForState(ec2conn, s.instance, []string{"pending"}, "running")
if err != nil {
ui.Error(err.Error())
return multistep.ActionHalt
......
......@@ -25,7 +25,7 @@ func (s *stepStopInstance) Run(state map[string]interface{}) multistep.StepActio
// TODO(mitchellh): Handle diff source states, i.e. this force state sucks
ui.Say("Waiting for the instance to stop...")
instance.State.Name = "stopping"
instance, err = waitForState(ec2conn, instance, "stopped")
instance, err = waitForState(ec2conn, instance, []string{"running", "stopping"}, "stopped")
if err != nil {
ui.Error(err.Error())
return multistep.ActionHalt
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment