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.all():
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):
76 if "vpn" in relation.config.access_type.lower():
77 relation.config.set_revoke(True)
78 relation.config.save()
79 relation.status = JobStatus.NEW
82 cleanup_set = Booking.objects.filter(end__lte=timezone.now()).filter(job__complete=False)
84 for booking in cleanup_set:
85 if not booking.job.complete:
87 cleanup_software(SoftwareRelation.objects.filter(job=job))
88 cleanup_hardware(HostHardwareRelation.objects.filter(job=job))
89 cleanup_network(HostNetworkRelation.objects.filter(job=job))
90 cleanup_access(AccessRelation.objects.filter(job=job))
98 gets all hosts from the database that need to be freed and frees them
100 networks = ~Q(~Q(job__hostnetworkrelation__status=200))
101 hardware = ~Q(~Q(job__hosthardwarerelation__status=200))
103 bookings = Booking.objects.filter(
106 end__lt=timezone.now(),
108 resource__isnull=False
110 for booking in bookings:
111 ResourceManager.getInstance().deleteResourceBundle(booking.resource)