mirror of
https://github.com/kevingruesser/bootstrap-vz.git
synced 2025-08-24 15:36:27 +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.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,31 +50,22 @@ class PrintPublicIPAddress(Task):
|
||||||
filename = '/dev/null'
|
filename = '/dev/null'
|
||||||
f = open(filename, 'w')
|
f = open(filename, 'w')
|
||||||
|
|
||||||
i = 0
|
r = ec2['connection'].get_only_instances([ec2['instance_id']])
|
||||||
instance = None
|
if not r and not r[0]:
|
||||||
while True:
|
logger.error('Could not get instance metadata')
|
||||||
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')
|
|
||||||
f.write('')
|
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()
|
f.close()
|
||||||
|
|
Loading…
Add table
Reference in a new issue