From 2a5874b94f213ed85e9328ba223899af0f2ef213 Mon Sep 17 00:00:00 2001
From: James Laska <jlaska@ansibleworks.com>
Date: Fri, 1 Nov 2013 13:13:07 -0400
Subject: [PATCH] Honor wait on ec2 instance termination

---
 library/cloud/ec2 | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)

diff --git a/library/cloud/ec2 b/library/cloud/ec2
index e97bcea8e2e..09a9a6d1582 100644
--- a/library/cloud/ec2
+++ b/library/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():