Revision 0d069390 snf-cyclades-app/synnefo/logic/backend.py

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)

Also available in: Unified diff