From 582a32040b850edb512f1d3f2fd615e9f034a2a4 Mon Sep 17 00:00:00 2001 From: James Laska Date: Fri, 1 Nov 2013 13:13:07 -0400 Subject: [PATCH] Honor wait on ec2 instance termination --- cloud/ec2 | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/cloud/ec2 b/cloud/ec2 index e97bcea8e2e..09a9a6d1582 100644 --- a/cloud/ec2 +++ b/cloud/ec2 @@ -533,6 +533,10 @@ def terminate_instances(module, ec2, instance_ids): """ + # Whether to wait for termination to complete before returning + wait = module.params.get('wait') + wait_timeout = int(module.params.get('wait_timeout')) + changed = False instance_dict_array = [] @@ -551,8 +555,30 @@ def terminate_instances(module, ec2, instance_ids): module.fail_json(msg='Unable to terminate instance {0}, error: {1}'.format(inst.id, e)) changed = True - return (changed, instance_dict_array, terminated_instance_ids) + # wait here until the instances are 'terminated' + if wait: + num_terminated = 0 + wait_timeout = time.time() + wait_timeout + while wait_timeout > time.time() and num_terminated < len(terminated_instance_ids): + response = ec2.get_all_instances( \ + instance_ids=terminated_instance_ids, \ + filters={'instance-state-name':'terminated'}) + try: + num_terminated = len(response.pop().instances) + except Exception, e: + # 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 num_terminated < len(terminated_instance_ids): + time.sleep(5) + + # waiting took too long + if wait_timeout < time.time() and num_terminated < len(terminated_instance_ids): + module.fail_json(msg = "wait for instance termination timeout on %s" % time.asctime()) + return (changed, instance_dict_array, terminated_instance_ids) def main():