Revision fdc94944 snf-cyclades-app/synnefo/logic/backend.py

b/snf-cyclades-app/synnefo/logic/backend.py
41 41
from synnefo.db.models import (Backend, VirtualMachine, Network,
42 42
                               BackendNetwork, BACKEND_STATUSES)
43 43
from synnefo.logic import utils
44
from synnefo.db.pools import IPPool
44
from synnefo.db.pools import EmptyPool
45 45
from synnefo.api.faults import OverLimit
46 46
from synnefo.api.util import backend_public_networks, get_network_free_address
47 47
from synnefo.util.rapi import GanetiRapiClient
......
118 118
    Update the state of the VM in the DB accordingly.
119 119
    """
120 120

  
121
    # Release the ips of the old nics. Get back the networks as multiple
122
    # changes in the same network, must happen in the same Network object,
123
    # because transaction will be commited only on exit of the function.
124
    networks = release_instance_nics(vm)
121
    release_instance_nics(vm)
125 122

  
126 123
    new_nics = enumerate(nics)
127 124
    for i, new_nic in new_nics:
......
129 126
        n = str(network)
130 127
        pk = utils.id_from_network_name(n)
131 128

  
132
        # Get the cached Network or get it from DB
133
        if pk in networks:
134
            net = networks[pk]
135
        else:
136
            net = Network.objects.select_for_update().get(pk=pk)
129
        net = Network.objects.get(pk=pk)
137 130

  
138 131
        # Get the new nic info
139 132
        mac = new_nic.get('mac', '')
......
162 155

  
163 156

  
164 157
def release_instance_nics(vm):
165
    networks = {}
166

  
167 158
    for nic in vm.nics.all():
168
        pk = nic.network.pk
169
        # Get the cached Network or get it from DB
170
        if pk in networks:
171
            net = networks[pk]
172
        else:
173
            # Get the network object in exclusive mode in order
174
            # to guarantee consistency of the address pool
175
            net = Network.objects.select_for_update().get(pk=pk)
176
        if nic.ipv4:
177
            net.release_address(nic.ipv4)
159
        nic.network.release_address(nic.ipv4)
178 160
        nic.delete()
179 161

  
180
    return networks
181

  
182 162

  
183 163
@transaction.commit_on_success
184 164
def process_network_status(back_network, etime, jobid, opcode, status, logmsg):
......
367 347
        try:
368 348
            address = get_network_free_address(network)
369 349
            return (network, address)
370
        except IPPool.IPPoolExhausted:
350
        except EmptyPool:
371 351
            pass
372 352
    return (None, None)
373 353

  

Also available in: Unified diff