builder/amazonebs: Better checking for states

pull/15/head
Mitchell Hashimoto 13 years ago
parent a099e32df2
commit 04a4d91431

@ -7,12 +7,24 @@ import (
"time" "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) log.Printf("Waiting for instance state to become: %s", target)
i = originalInstance i = originalInstance
original := i.State.Name for i.State.Name != target {
for i.State.Name == original { 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 var resp *ec2.InstancesResp
resp, err = ec2conn.Instances([]string{i.InstanceId}, ec2.NewFilter()) resp, err = ec2conn.Instances([]string{i.InstanceId}, ec2.NewFilter())
if err != nil { if err != nil {
@ -20,14 +32,8 @@ func waitForState(ec2conn *ec2.EC2, originalInstance *ec2.Instance, target strin
} }
i = &resp.Reservations[0].Instances[0] i = &resp.Reservations[0].Instances[0]
time.Sleep(2 * time.Second) 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 return
} }

@ -36,7 +36,7 @@ func (s *stepRunSourceInstance) Run(state map[string]interface{}) multistep.Step
log.Printf("instance id: %s", s.instance.InstanceId) log.Printf("instance id: %s", s.instance.InstanceId)
ui.Say("Waiting for instance to become ready...") 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 { if err != nil {
ui.Error(err.Error()) ui.Error(err.Error())
return multistep.ActionHalt 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 // TODO(mitchellh): Handle diff source states, i.e. this force state sucks
ui.Say("Waiting for the instance to stop...") ui.Say("Waiting for the instance to stop...")
instance.State.Name = "stopping" instance.State.Name = "stopping"
instance, err = waitForState(ec2conn, instance, "stopped") instance, err = waitForState(ec2conn, instance, []string{"running", "stopping"}, "stopped")
if err != nil { if err != nil {
ui.Error(err.Error()) ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt

Loading…
Cancel
Save