From 93cd6b6b319c1eec4a1f2f2af5e49055443762d4 Mon Sep 17 00:00:00 2001 From: Michael Drake Date: Wed, 16 Sep 2015 10:08:05 +0100 Subject: Improve cleanup when IP address assignment fails. --- openstack/tester | 56 ++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/openstack/tester b/openstack/tester index 8ba9764..8b6e361 100755 --- a/openstack/tester +++ b/openstack/tester @@ -39,6 +39,38 @@ class TimeoutError(cliapp.AppException): def __init__(self, msg): super(TimeoutError, self).__init__(msg) + +def delete_image(hostname): + # TODO: Do all this stuff properly with python novaclient + # Remove an image from the openstack tenancy + print "Deleting %s test disc image" % (hostname) + try: + cliapp.runcmd(['nova', 'image-delete', hostname]) + except cliapp.AppException as e: + # TODO: Stop assuming that image-delete failed because it was + # already removed + print "- Failed" + pass + +def delete_instance_and_image(hostname): + # TODO: Do all this stuff properly with python novaclient + # Stop and remove VM, and its image + print "Deleting %s test instance" % (hostname) + try: + cliapp.runcmd(['nova', 'delete', hostname]) + except cliapp.AppException as e: + # TODO: Stop assuming that delete failed because the instance + # wasn't running + print "- Failed" + pass + + # Sleep for a bit, or nova silently fails to delete the image + # TODO: Test whether the image has been deleted in a retry loop. + time.sleep(20) + + delete_image(hostname) + + class DeployedSystemInstance(object): def __init__(self, deployment, ip_addr, hostname): @@ -119,28 +151,7 @@ class DeployedSystemInstance(object): print "Test system %s ready to run tests." % (self.hostname) def delete(self): - # Stop and remove VM - print "Deleting %s test instance" % (self.hostname) - try: - cliapp.runcmd(['nova', 'delete', self.hostname]) - except cliapp.AppException as e: - # TODO: Stop assuming that delete failed because the instance - # wasn't running - print "- Failed" - pass - - # Sleep for a bit, or nova silently fails to delete the image - # TODO: Test whether the image has been deleted in a retry loop. - time.sleep(20) - - print "Deleting %s test disc image" % (self.hostname) - try: - cliapp.runcmd(['nova', 'image-delete', self.hostname]) - except cliapp.AppException as e: - # TODO: Stop assuming that image-delete failed because it was - # already removed - print "- Failed" - pass + delete_instance_and_image(self.hostname) class Deployment(object): @@ -211,6 +222,7 @@ class Deployment(object): ip_addr = free_ip.ip break else: + delete_instance_and_image(hostname) raise cliapp.AppException('Could not get a floating IP') # Print the IP address -- cgit v1.2.1