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.
This commit is contained in:
Jonh Wendell 2015-04-06 11:47:15 -03:00
parent c632785e6c
commit b934808cce

View file

@ -1,10 +1,20 @@
from bootstrapvz.base import Task from bootstrapvz.base import Task
from bootstrapvz.common import phases from bootstrapvz.common import phases
from bootstrapvz.providers.ec2.tasks import ami from bootstrapvz.providers.ec2.tasks import ami
import time
import logging 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): class LaunchEC2Instance(Task):
description = 'Launching EC2 instance' description = 'Launching EC2 instance'
phase = phases.image_registration phase = phases.image_registration
@ -40,27 +50,18 @@ class PrintPublicIPAddress(Task):
filename = '/dev/null' filename = '/dev/null'
f = open(filename, 'w') 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']]) r = ec2['connection'].get_only_instances([ec2['instance_id']])
if not r and not r[0]: if not r and not r[0]:
logger.error('Could not get instance metadata') logger.error('Could not get instance metadata')
break f.write('')
else:
instance = r[0] instance = r[0]
if instance.ip_address:
break
if instance and instance.ip_address: 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) logger.info('******* EC2 IP ADDRESS: %s *******' % instance.ip_address)
f.write(instance.ip_address) f.write(instance.ip_address)
else: else: