mirror of
https://github.com/kevingruesser/bootstrap-vz.git
synced 2025-08-24 07:26:29 +00:00
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:
parent
c632785e6c
commit
b934808cce
1 changed files with 27 additions and 26 deletions
|
@ -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,27 +50,18 @@ 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
|
||||
|
||||
f.write('')
|
||||
else:
|
||||
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)
|
||||
f.write(instance.ip_address)
|
||||
else:
|
||||
|
|
Loading…
Add table
Reference in a new issue