From 2611246b89d8f67dd26f22c634bd1731d0e577db Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Sat, 2 Aug 2014 15:51:01 -0700 Subject: [PATCH] Cleanup in nova after a failed floating ip There is a potential leak of resources if there is somehow a failure adding a floating ip to a server. Clean up after ourselves. --- library/cloud/nova_compute | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/library/cloud/nova_compute b/library/cloud/nova_compute index fb74ce15e29..34abc47801d 100644 --- a/library/cloud/nova_compute +++ b/library/cloud/nova_compute @@ -255,7 +255,7 @@ def _add_floating_ip_no_pool(module, nova, server, floating_ip_obj): # if there is a list of IP addresses, make that the list if module.params['floating_ip'].has_key('ips'): - usable_floating_ips = module.params['floating_ip']['ips'] + usable_floating_ips = [dict(ip=f, created=False) for f in module.params['floating_ip']['ips']] else: # get the list of all floating IPs. Mileage may # vary according to Nova Compute configuration @@ -263,20 +263,23 @@ def _add_floating_ip_no_pool(module, nova, server, floating_ip_obj): for f_ip in floating_ip_obj.list(): # if not reserved and the correct pool, add if f_ip.instance_id is None: - usable_floating_ips.append(f_ip.ip) + usable_floating_ips.append(dict(ip=f_ip.ip, created=False)) if not usable_floating_ips: try: new_ip = nova.floating_ips.create() except Exception, e: module.fail_json(msg = "Unable to create floating ip") - usable_floating_ips.append(new_ip.ip) + usable_floating_ips.append(dict(ip=new_ip.ip, created=True)) # finally, add ip(s) to instance for ip in usable_floating_ips: try: - server.add_floating_ip(ip) + server.add_floating_ip(ip['ip']) except Exception, e: + # Clean up - we auto-created this ip, and it's not attached + # to the server, so the cloud will not know what to do with it + server.floating_ips.delete(ip['ip']) module.fail_json(msg = "Error attaching IP %s to instance %s: %s " % (ip, server.id, e.message))