summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Drake <michael.drake@codethink.co.uk>2015-09-11 10:08:03 +0100
committerMichael Drake <michael.drake@codethink.co.uk>2015-09-11 15:01:30 +0100
commit7511d8fca7ab50ffccdd33580a55da737d06a182 (patch)
treeede3bee79d67b4a2ebc7d7ebb921f08fc2774c31
parente1f15030d50ea7953bf842c2ddf8a58573aff4e2 (diff)
downloadciat-tester-7511d8fca7ab50ffccdd33580a55da737d06a182.tar.gz
Assign a floating IP address to the test instance.
-rwxr-xr-xopenstack/tester95
1 files changed, 31 insertions, 64 deletions
diff --git a/openstack/tester b/openstack/tester
index a3a5a62..89a32a1 100755
--- a/openstack/tester
+++ b/openstack/tester
@@ -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,