From e14d857037affb99fe80d51271fd825a259ab9c0 Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Wed, 25 Sep 2013 16:12:38 -0500 Subject: [PATCH] Make the ec2 wait code a little smarter The code to determine the number of running instances could blow up if the response from AWS did not actually contain any data. This code makes it a bit smarter in handling, so that it will wait for a valid response regardless of the wait condition. Fixes #3980 --- cloud/ec2 | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/cloud/ec2 b/cloud/ec2 index 0c86238d503..c797ca64378 100644 --- a/cloud/ec2 +++ b/cloud/ec2 @@ -477,15 +477,24 @@ def create_instances(module, ec2): module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message)) # wait here until the instances are up - res_list = res.connection.get_all_instances(instids) - this_res = res_list[0] + this_res = [] num_running = 0 wait_timeout = time.time() + wait_timeout - while wait and wait_timeout > time.time() and num_running < len(instids): + while wait_timeout > time.time() and num_running < len(instids): res_list = res.connection.get_all_instances(instids) - this_res = res_list[0] - num_running = len([ i for i in this_res.instances if i.state=='running' ]) - time.sleep(5) + if len(res_list) > 0: + this_res = res_list[0] + num_running = len([ i for i in this_res.instances if i.state=='running' ]) + else: + # got a bad response of some sort, possibly due to + # stale/cached data. Wait a second and then try again + time.sleep(1) + continue + if wait and num_running < len(instids): + time.sleep(5) + else: + break + if wait and wait_timeout <= time.time(): # waiting took too long module.fail_json(msg = "wait for instances running timeout on %s" % time.asctime())