1 ##############################################################################
2 # Copyright (c) 2016 Max Breitenfeldt and others.
3 # Copyright (c) 2018 Parker Berberian, Sawyer Bergeron, and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
12 from celery import shared_task
13 from django.utils import timezone
14 from django.db.models import Q
15 from booking.models import Booking
16 from notifier.manager import *
17 from notifier.models import *
18 from api.models import *
19 from resource_inventory.resource_manager import ResourceManager
23 def conjure_aggregate_notifiers():
29 def cleanup_hardware(qs):
30 for hostrelation in qs:
31 config = hostrelation.config
33 config.set_power("off")
35 hostrelation.status=JobStatus.NEW
38 def cleanup_network(qs):
39 for hostrelation in qs:
40 network = hostrelation.config
41 network.interfaces.clear()
42 host = hostrelation.host
45 for interface in host.interfaces.all():
46 for vlan in interface.config:
49 host.lab.vlan_manager.release_public_vlan(vlan.vlan_id)
50 except: # will fail if we already released in this loop
53 vlans.append(vlan.vlan_id)
57 host.lab.vlan_manager.release_vlans(vlans)
59 interface.config.clear()
60 network.add_interface(interface)
62 hostrelation.status=JobStatus.NEW
65 def cleanup_software(qs):
68 software = relation.config.opnfv
69 software.clear_delta()
71 relation.status=JobStatus.NEW
74 def cleanup_access(qs):
78 cleanup_set = Booking.objects.filter(end__lte=timezone.now()).filter(job__complete=False)
80 for booking in cleanup_set:
81 if not booking.job.complete:
83 cleanup_software(SoftwareRelation.objects.filter(job=job))
84 cleanup_hardware(HostHardwareRelation.objects.filter(job=job))
85 cleanup_network(HostNetworkRelation.objects.filter(job=job))
86 cleanup_access(AccessRelation.objects.filter(job=job))
94 gets all hosts from the database that need to be freed and frees them
96 networks = ~Q(~Q(job__hostnetworkrelation__status=200))
97 hardware = ~Q(~Q(job__hosthardwarerelation__status=200))
99 bookings = Booking.objects.filter(
102 end__lt=timezone.now(),
104 resource__isnull=False
106 for booking in bookings:
107 ResourceManager.getInstance().deleteResourceBundle(booking.resource)