From b934808cce62361c1ec9f829507373301de73287 Mon Sep 17 00:00:00 2001 From: Jonh Wendell Date: Mon, 6 Apr 2015 11:47:15 -0300 Subject: [PATCH] ec2_launch: Simplify the logic for getting the IP address Use the function waituntil() available in the integration-test branch to simplify the code that retrieves the IP of the instance. A 'TODO' note was also added to remember us to merge this function once it gets merged. --- bootstrapvz/plugins/ec2_launch/tasks.py | 53 +++++++++++++------------ 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/bootstrapvz/plugins/ec2_launch/tasks.py b/bootstrapvz/plugins/ec2_launch/tasks.py index 53190ef..df159db 100644 --- a/bootstrapvz/plugins/ec2_launch/tasks.py +++ b/bootstrapvz/plugins/ec2_launch/tasks.py @@ -1,10 +1,20 @@ from bootstrapvz.base import Task from bootstrapvz.common import phases from bootstrapvz.providers.ec2.tasks import ami -import time import logging +# TODO: Merge with the method available in wip-integration-tests branch +def waituntil(predicate, timeout=5, interval=0.05): + import time + threshhold = time.time() + timeout + while time.time() < threshhold: + if predicate(): + return True + time.sleep(interval) + return False + + class LaunchEC2Instance(Task): description = 'Launching EC2 instance' phase = phases.image_registration @@ -40,31 +50,22 @@ class PrintPublicIPAddress(Task): filename = '/dev/null' f = open(filename, 'w') - i = 0 - instance = None - while True: - logger.debug('Waiting a bit to get instance metadata...') - time.sleep(5) - - i += 1 - if i > 10: - logger.error('Waited too much, giving up') - break - - r = ec2['connection'].get_only_instances([ec2['instance_id']]) - if not r and not r[0]: - logger.error('Could not get instance metadata') - break - - instance = r[0] - if instance.ip_address: - break - - if instance and instance.ip_address: - logger.info('******* EC2 IP ADDRESS: %s *******' % instance.ip_address) - f.write(instance.ip_address) - else: - logger.error('Could not get IP address for the instance') + r = ec2['connection'].get_only_instances([ec2['instance_id']]) + if not r and not r[0]: + logger.error('Could not get instance metadata') f.write('') + else: + instance = r[0] + + def instance_has_ip(): + instance.update() + return instance.ip_address + + if waituntil(instance_has_ip, timeout=120, interval=5): + logger.info('******* EC2 IP ADDRESS: %s *******' % instance.ip_address) + f.write(instance.ip_address) + else: + logger.error('Could not get IP address for the instance') + f.write('') f.close()