Merge pull request #4776 from jlaska/ec2_wait_on_terminate
Honor wait on ec2 instance termination
This commit is contained in:
commit
83b00e60e3
1 changed files with 27 additions and 1 deletions
28
cloud/ec2
28
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():
|
||||
|
|
Loading…
Reference in a new issue