1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Merge pull request #6020 from bpennypacker/ec2_elb_timeout

cloud/ec2_elb module: Added wait_timeout parameter
This commit is contained in:
Michael DeHaan 2014-03-11 10:21:38 -05:00
commit f6878d4ada

View file

@ -82,6 +82,12 @@ options:
choices: ["yes", "no"] choices: ["yes", "no"]
aliases: [] aliases: []
version_added: "1.5" version_added: "1.5"
wait_timeout:
description:
- Number of seconds to wait for an instance to change state. If 0 then this module may return an error if a transient error occurs. If non-zero then any transient errors are ignored until the timeout is reached. Ignored when wait=no.
required: false
default: 0
version_added: "1.5"
""" """
@ -133,7 +139,7 @@ class ElbManager:
self.lbs = self._get_instance_lbs(ec2_elbs) self.lbs = self._get_instance_lbs(ec2_elbs)
self.changed = False self.changed = False
def deregister(self, wait): def deregister(self, wait, timeout):
"""De-register the instance from all ELBs and wait for the ELB """De-register the instance from all ELBs and wait for the ELB
to report it out-of-service""" to report it out-of-service"""
@ -146,13 +152,13 @@ class ElbManager:
return return
if wait: if wait:
self._await_elb_instance_state(lb, 'OutOfService', initial_state) self._await_elb_instance_state(lb, 'OutOfService', initial_state, timeout)
else: else:
# We cannot assume no change was made if we don't wait # We cannot assume no change was made if we don't wait
# to find out # to find out
self.changed = True self.changed = True
def register(self, wait, enable_availability_zone): def register(self, wait, enable_availability_zone, timeout):
"""Register the instance for all ELBs and wait for the ELB """Register the instance for all ELBs and wait for the ELB
to report the instance in-service""" to report the instance in-service"""
for lb in self.lbs: for lb in self.lbs:
@ -175,7 +181,7 @@ class ElbManager:
lb.register_instances([self.instance_id]) lb.register_instances([self.instance_id])
if wait: if wait:
self._await_elb_instance_state(lb, 'InService', initial_state) self._await_elb_instance_state(lb, 'InService', initial_state, timeout)
else: else:
# We cannot assume no change was made if we don't wait # We cannot assume no change was made if we don't wait
# to find out # to find out
@ -205,10 +211,12 @@ class ElbManager:
# lb.availability_zones # lb.availability_zones
return instance.placement in lb.availability_zones return instance.placement in lb.availability_zones
def _await_elb_instance_state(self, lb, awaited_state, initial_state): def _await_elb_instance_state(self, lb, awaited_state, initial_state, timeout):
"""Wait for an ELB to change state """Wait for an ELB to change state
lb: load balancer lb: load balancer
awaited_state : state to poll for (string)""" awaited_state : state to poll for (string)"""
wait_timeout = time.time() + timeout
while True: while True:
instance_state = self._get_instance_health(lb) instance_state = self._get_instance_health(lb)
@ -227,7 +235,8 @@ class ElbManager:
# If it's pending, we'll skip further checks andd continue waiting # If it's pending, we'll skip further checks andd continue waiting
pass pass
elif (awaited_state == 'InService' elif (awaited_state == 'InService'
and instance_state.reason_code == "Instance"): and instance_state.reason_code == "Instance"
and time.time() >= wait_timeout):
# If the reason_code for the instance being out of service is # If the reason_code for the instance being out of service is
# "Instance" this indicates a failure state, e.g. the instance # "Instance" this indicates a failure state, e.g. the instance
# has failed a health check or the ELB does not have the # has failed a health check or the ELB does not have the
@ -309,7 +318,8 @@ def main():
instance_id={'required': True}, instance_id={'required': True},
ec2_elbs={'default': None, 'required': False, 'type':'list'}, ec2_elbs={'default': None, 'required': False, 'type':'list'},
enable_availability_zone={'default': True, 'required': False, 'choices': BOOLEANS, 'type': 'bool'}, enable_availability_zone={'default': True, 'required': False, 'choices': BOOLEANS, 'type': 'bool'},
wait={'required': False, 'choices': BOOLEANS, 'default': True, 'type': 'bool'} wait={'required': False, 'choices': BOOLEANS, 'default': True, 'type': 'bool'},
wait_timeout={'requred': False, 'default': 0, 'type': 'int'}
) )
) )
@ -325,6 +335,7 @@ def main():
region = module.params['region'] region = module.params['region']
wait = module.params['wait'] wait = module.params['wait']
enable_availability_zone = module.params['enable_availability_zone'] enable_availability_zone = module.params['enable_availability_zone']
timeout = module.params['wait_timeout']
if module.params['state'] == 'present' and 'ec2_elbs' not in module.params: if module.params['state'] == 'present' and 'ec2_elbs' not in module.params:
module.fail_json(msg="ELBs are required for registration") module.fail_json(msg="ELBs are required for registration")
@ -340,9 +351,9 @@ def main():
module.fail_json(msg=msg) module.fail_json(msg=msg)
if module.params['state'] == 'present': if module.params['state'] == 'present':
elb_man.register(wait, enable_availability_zone) elb_man.register(wait, enable_availability_zone, timeout)
elif module.params['state'] == 'absent': elif module.params['state'] == 'absent':
elb_man.deregister(wait) elb_man.deregister(wait, timeout)
ansible_facts = {'ec2_elbs': [lb.name for lb in elb_man.lbs]} ansible_facts = {'ec2_elbs': [lb.name for lb in elb_man.lbs]}
ec2_facts_result = dict(changed=elb_man.changed, ansible_facts=ansible_facts) ec2_facts_result = dict(changed=elb_man.changed, ansible_facts=ansible_facts)