Revision 0d069390

b/snf-cyclades-app/synnefo/logic/backend.py
57 57

  
58 58
# Timeout in seconds for building NICs. After this period the NICs considered
59 59
# stale and removed from DB.
60
BUILDING_NIC_TIMEOUT = 180
60
BUILDING_NIC_TIMEOUT = timedelta(seconds=180)
61 61

  
62 62
NIC_FIELDS = ["state", "mac", "ipv4", "ipv6", "network", "firewall_profile",
63 63
              "index"]
64
UNKNOWN_NIC_PREFIX = "unknown-"
64 65

  
65 66

  
66 67
def handle_vm_quotas(vm, job_id, job_opcode, job_status, job_fields):
......
244 245
            # state for more than 5 minutes, then we remove the NIC.
245 246
            # TODO: This is dangerous as the job may be stack in the queue, and
246 247
            # releasing the IP may lead to duplicate IP use.
247
            if nic.state != "BUILDING" or (nic.state == "BUILDING" and
248
               etime > nic.created + timedelta(seconds=BUILDING_NIC_TIMEOUT)):
249
                release_nic_address(nic)
250
                nic.delete()
248
            if db_nic.state != "BUILDING" or\
249
                (db_nic.state == "BUILDING" and
250
                 etime > db_nic.created + BUILDING_NIC_TIMEOUT):
251
                release_nic_address(db_nic)
252
                db_nic.delete()
251 253
            else:
252
                log.warning("Ignoring recent building NIC: %s", nic)
254
                log.warning("Ignoring recent building NIC: %s", db_nic)
253 255
        elif db_nic is None:
254 256
            # NIC exists in Ganeti but not in DB
257
            if str(nic_name).startswith(UNKNOWN_NIC_PREFIX):
258
                msg = "Can not process NIC! NIC '%s' does not have a"\
259
                      " valid name." % ganeti_nic
260
                log.error(msg)
261
                continue
255 262
            if ganeti_nic["ipv4"]:
256 263
                network = ganeti_nic["network"]
257 264
                network.reserve_address(ganeti_nic["ipv4"])
258
            vm.nics.create(**ganeti_nic)
265
            vm.nics.create(id=nic_name, **ganeti_nic)
259 266
        elif not nics_are_equal(db_nic, ganeti_nic):
260 267
            # Special case where the IPv4 address has changed, because you
261 268
            # need to release the old IPv4 address and reserve the new one
......
292 299
        else:
293 300
            # Put as default value the index. If it is an unknown NIC to
294 301
            # synnefo it will be created automaticaly.
295
            nic_id = "unknown-" + str(index)
302
            nic_id = UNKNOWN_NIC_PREFIX + str(index)
296 303
        network_name = gnic.get('network', '')
297 304
        network_id = utils.id_from_network_name(network_name)
298 305
        network = Network.objects.get(id=network_id)
b/snf-cyclades-app/synnefo/logic/reconciliation.py
61 61
import logging
62 62
import itertools
63 63
import bitarray
64
from datetime import datetime, timedelta
64
from datetime import datetime
65 65

  
66 66
from django.db import transaction
67 67
from synnefo.db.models import (Backend, VirtualMachine, Flavor,
......
286 286

  
287 287
    def reconcile_unsynced_nics(self, server_id, db_server, gnt_server):
288 288
        building_time = (self.event_time -
289
                         timedelta(seconds=backend_mod.BUILDING_NIC_TIMEOUT))
289
                         backend_mod.BUILDING_NIC_TIMEOUT)
290 290
        db_nics = db_server.nics.exclude(state="BUILDING",
291 291
                                         created__lte=building_time) \
292
                                .order_by("index")
292
                                .order_by("id")
293 293
        gnt_nics = gnt_server["nics"]
294 294
        gnt_nics_parsed = backend_mod.process_ganeti_nics(gnt_nics)
295 295
        nics_changed = len(db_nics) != len(gnt_nics)
......
306 306
                  "\tDB:\n\t\t%s\n\tGaneti:\n\t\t%s"
307 307
            db_nics_str = "\n\t\t".join(map(format_db_nic, db_nics))
308 308
            gnt_nics_str = "\n\t\t".join(map(format_gnt_nic,
309
                                         gnt_nics_parsed.items()))
309
                                         sorted(gnt_nics_parsed.items())))
310 310
            self.log.info(msg, server_id, db_nics_str, gnt_nics_str)
311 311
            if self.options["fix_unsynced_nics"]:
312 312
                backend_mod.process_net_status(vm=db_server,
......
347 347

  
348 348
def format_gnt_nic(nic):
349 349
    nic_name, nic = nic
350
    return NIC_MSG % (nic_name, nic["state"], nic["ipv4"], nic["network"],
350
    return NIC_MSG % (nic_name, nic["state"], nic["ipv4"], nic["network"].id,
351 351
                      nic["mac"], nic["index"], nic["firewall_profile"])
352 352

  
353 353

  

Also available in: Unified diff