diff --git a/builder/amazonebs/instance.go b/builder/amazonebs/instance.go index f3150466c..0362ebe8f 100644 --- a/builder/amazonebs/instance.go +++ b/builder/amazonebs/instance.go @@ -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 } diff --git a/builder/amazonebs/step_run_source_instance.go b/builder/amazonebs/step_run_source_instance.go index 104ae7ce4..0b2db97fb 100644 --- a/builder/amazonebs/step_run_source_instance.go +++ b/builder/amazonebs/step_run_source_instance.go @@ -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 diff --git a/builder/amazonebs/step_stop_instance.go b/builder/amazonebs/step_stop_instance.go index 46d9f0daf..72c7f4224 100644 --- a/builder/amazonebs/step_stop_instance.go +++ b/builder/amazonebs/step_stop_instance.go @@ -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