Revision bd392934

b/snf-cyclades-app/synnefo/logic/backend.py
84 84
        utils.update_state(vm, state_for_success)
85 85
        # Set the deleted flag explicitly, cater for admin-initiated removals
86 86
        if opcode == 'OP_INSTANCE_REMOVE':
87
            release_instance_nics(vm)
87 88
            vm.deleted = True
88 89
            vm.nics.all().delete()
89 90

  
......
115 116
    Update the state of the VM in the DB accordingly.
116 117
    """
117 118

  
118
    old_nics = vm.nics.order_by('index')
119
    new_nics = enumerate(nics)
120

  
121
    networks = {}
122

  
123
    for old_nic in old_nics:
124
        pk = old_nic.network.pk
125
        # Get the cached Network or get it from DB
126
        if pk in networks:
127
            net = networks[pk]
128
        else:
129
            # Get the network object in exclusive mode in order
130
            # to guarantee consistency of the address pool
131
            net = Network.objects.select_for_update().get(pk=pk)
132
        net.release_address(old_nic.ipv4)
133
        old_nic.delete()
119
    # Release the ips of the old nics. Get back the networks as multiple
120
    # changes in the same network, must happen in the same Network object,
121
    # because transaction will be commited only on exit of the function.
122
    networks = release_instance_nics(vm)
134 123

  
124
    new_nics = enumerate(nics)
135 125
    for i, new_nic in new_nics:
136 126
        network = new_nic.get('network', '')
137 127
        n = str(network)
......
169 159
    vm.save()
170 160

  
171 161

  
162
def release_instance_nics(vm):
163
    networks = {}
164

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

  
177
    return networks
178

  
179

  
172 180
@transaction.commit_on_success
173 181
def process_network_status(back_network, etime, jobid, opcode, status, logmsg):
174 182
    if status not in [x[0] for x in BACKEND_STATUSES]:

Also available in: Unified diff