builder/amazonebs: Better checking for states

This commit is contained in:
Mitchell Hashimoto 2013-06-04 11:29:59 -07:00
parent a099e32df2
commit 04a4d91431
3 changed files with 17 additions and 11 deletions

View File

@ -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
}

View File

@ -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

View File

@ -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