blob: d4b418944186bd4b59e992046b0390cb98cc52c6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
##############################################################################
# Copyright (c) 2016 Max Breitenfeldt and others.
# Copyright (c) 2018 Parker Berberian, Sawyer Bergeron, and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
from celery import shared_task
from django.utils import timezone
from django.db.models import Q
from booking.models import Booking
from notifier.manager import NotificationHandler
from api.models import *
from resource_inventory.resource_manager import ResourceManager
@shared_task
def booking_poll():
def cleanup_hardware(qs):
for hostrelation in qs:
config = hostrelation.config
config.clear_delta()
config.set_power("off")
config.save()
hostrelation.status=JobStatus.NEW
hostrelation.save()
def cleanup_network(qs):
for hostrelation in qs:
network = hostrelation.config
network.interfaces.clear()
host = hostrelation.host
network.clear_delta()
vlans = []
for interface in host.interfaces.all():
for vlan in interface.config.all():
if vlan.public:
try:
host.lab.vlan_manager.release_public_vlan(vlan.vlan_id)
except: # will fail if we already released in this loop
pass
else:
vlans.append(vlan.vlan_id)
# release all vlans
if len(vlans) > 0:
host.lab.vlan_manager.release_vlans(vlans)
interface.config.clear()
network.add_interface(interface)
network.save()
hostrelation.status=JobStatus.NEW
hostrelation.save()
def cleanup_software(qs):
if qs.exists():
relation = qs.first()
software = relation.config.opnfv
software.clear_delta()
software.save()
relation.status=JobStatus.NEW
relation.save()
def cleanup_access(qs):
for relation in qs:
if "vpn" in relation.config.access_type.lower():
relation.config.set_revoke(True)
relation.config.save()
relation.status = JobStatus.NEW
relation.save()
cleanup_set = Booking.objects.filter(end__lte=timezone.now()).filter(job__complete=False)
for booking in cleanup_set:
if not booking.job.complete:
job = booking.job
cleanup_software(SoftwareRelation.objects.filter(job=job))
cleanup_hardware(HostHardwareRelation.objects.filter(job=job))
cleanup_network(HostNetworkRelation.objects.filter(job=job))
cleanup_access(AccessRelation.objects.filter(job=job))
job.complete = True
job.save()
NotificationHandler.notify_booking_end(booking)
@shared_task
def free_hosts():
"""
gets all hosts from the database that need to be freed and frees them
"""
networks = ~Q(~Q(job__hostnetworkrelation__status=200))
hardware = ~Q(~Q(job__hosthardwarerelation__status=200))
bookings = Booking.objects.filter(
networks,
hardware,
end__lt=timezone.now(),
job__complete=True,
resource__isnull=False
)
for booking in bookings:
ResourceManager.getInstance().deleteResourceBundle(booking.resource)
|