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.
pull/8412/head
Monty Taylor 10 years ago
parent 6045923cab
commit 2611246b89

@ -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 there is a list of IP addresses, make that the list
if module.params['floating_ip'].has_key('ips'): 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: else:
# get the list of all floating IPs. Mileage may # get the list of all floating IPs. Mileage may
# vary according to Nova Compute configuration # 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(): for f_ip in floating_ip_obj.list():
# if not reserved and the correct pool, add # if not reserved and the correct pool, add
if f_ip.instance_id is None: 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: if not usable_floating_ips:
try: try:
new_ip = nova.floating_ips.create() new_ip = nova.floating_ips.create()
except Exception, e: except Exception, e:
module.fail_json(msg = "Unable to create floating ip") 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 # finally, add ip(s) to instance
for ip in usable_floating_ips: for ip in usable_floating_ips:
try: try:
server.add_floating_ip(ip) server.add_floating_ip(ip['ip'])
except Exception, e: 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)) module.fail_json(msg = "Error attaching IP %s to instance %s: %s " % (ip, server.id, e.message))

Loading…
Cancel
Save