From e0829a4106185624a8c57281d8a365d1828c1a68 Mon Sep 17 00:00:00 2001 From: Michael Drake Date: Tue, 8 Sep 2015 16:26:18 +0000 Subject: Assign a floating IP address to the test instance. --- scripts/release-test-os | 95 ++++++++++++++++--------------------------------- 1 file changed, 31 insertions(+), 64 deletions(-) diff --git a/scripts/release-test-os b/scripts/release-test-os index de2b656e..b0ea3c4a 100755 --- a/scripts/release-test-os +++ b/scripts/release-test-os @@ -32,67 +32,7 @@ import tempfile import time import uuid -class NovaList: - def __init__(self): - self.output = [] - self.lines = [] - self.instance = [] - - def update(self): - self.output = cliapp.runcmd(['nova', 'list']) - self.lines = self.output.split('\n') - self.lines = self.lines[3:-2] - - def get_nova_details_for_instance(self, name): - self.update() - - for line in self.lines: - entries = line.split('|') - stripped_line = [entry.strip() for entry in entries] - if stripped_line.count(name) == 1: - self.instance = stripped_line - - def get_nova_state_for_instance(self, name): - self.get_nova_details_for_instance(name) - if not self.instance: - return - return self.instance[3] - - def get_nova_ip_for_instance(self, name): - self.get_nova_details_for_instance(name) - if not self.instance: - return - - if self.get_nova_state_for_instance(name) != 'ACTIVE': - return - - return self.instance[6] - - def get_nova_ip_for_instance_timeout(self, name, timeout=120): - start_time = time.time() - - while self.get_nova_state_for_instance(name) != 'ACTIVE': - - if time.time() > start_time + timeout: - print "%s not ACTIVE after %i seconds" % (name, timeout) - return - - time.sleep(1) - - ip_addr = self.get_nova_ip_for_instance(name) - if not ip_addr: - return - - if ip_addr.count('=') == 0: - return - - ip_addr = ip_addr[ip_addr.find('=') + 1:] - - if ip_addr.count(',') == 0: - return ip_addr - - return ip_addr[:ip_addr.find(',')] - +from novaclient import client class TimeoutError(cliapp.AppException): @@ -255,7 +195,15 @@ class Deployment(object): '--file', self.image_file] cliapp.runcmd(args, stdin=None, stdout=None, stderr=None) + # Get a novaclient object + nc = client.Client(2, + os.environ['OS_USERNAME'], + os.environ['OS_PASSWORD'], + os.environ['OS_TENANT_NAME'], + os.environ['OS_AUTH_URL']) + # Boot an instance from the image + # TODO: use python-novaclient args = ['nova', 'boot', '--flavor', 'm1.medium', '--image', hostname, @@ -273,9 +221,28 @@ class Deployment(object): output = '\n'.join(output_lines) print output - # Get ip address from nova list - nl = NovaList() - ip_addr = nl.get_nova_ip_for_instance_timeout(hostname) + # Sleep for a bit, or nova explodes when trying to assign IP address + time.sleep(20) + + # Assign a floating IP address + for retries in range(5, 0, -1): + ip_list = nc.floating_ips.list() + free_ip = None + for ip in ip_list: + if ip.instance_id == None: + free_ip = ip + break + if free_ip != None: + instance = nc.servers.find(name=hostname) + # TODO: switch back to cli tool, as python + # approach gave error. + instance.add_floating_ip(free_ip) + ip_addr = free_ip.ip + break + else: + raise cliapp.AppException('Could not get a floating IP') + + # Print the IP address print "IP address for instance %s: %s" % (hostname, ip_addr) return DeployedSystemInstance(self, self.host_machine, -- cgit v1.2.1