Lab as a Service 2.0
[laas.git] / src / dashboard / tasks.py
1 ##############################################################################
2 # Copyright (c) 2016 Max Breitenfeldt and others.
3 # Copyright (c) 2018 Parker Berberian, Sawyer Bergeron, and others.
4 #
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 ##############################################################################
10
11
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
20
21
22 @shared_task
23 def conjure_aggregate_notifiers():
24     NotifyPeriodic.task()
25
26
27 @shared_task
28 def booking_poll():
29     def cleanup_hardware(qs):
30         for hostrelation in qs:
31             config = hostrelation.config
32             config.clear_delta()
33             config.set_power("off")
34             config.save()
35             hostrelation.status=JobStatus.NEW
36             hostrelation.save()
37
38     def cleanup_network(qs):
39         for hostrelation in qs:
40             network = hostrelation.config
41             network.interfaces.clear()
42             host = hostrelation.host
43             network.clear_delta()
44             vlans = []
45             for interface in host.interfaces.all():
46                 for vlan in interface.config:
47                     if vlan.public:
48                         try:
49                             host.lab.vlan_manager.release_public_vlan(vlan.vlan_id)
50                         except:  # will fail if we already released in this loop
51                             pass
52                     else:
53                         vlans.append(vlan.vlan_id)
54
55                 # release all vlans
56                 if len(vlans) > 0:
57                     host.lab.vlan_manager.release_vlans(vlans)
58
59                 interface.config.clear()
60                 network.add_interface(interface)
61                 network.save()
62             hostrelation.status=JobStatus.NEW
63             hostrelation.save()
64
65     def cleanup_software(qs):
66         if qs.exists():
67             relation = qs.first()
68             software = relation.config.opnfv
69             software.clear_delta()
70             software.save()
71             relation.status=JobStatus.NEW
72             relation.save()
73
74     def cleanup_access(qs):
75         for relation in qs:
76             pass # TODO
77
78     cleanup_set = Booking.objects.filter(end__lte=timezone.now()).filter(job__complete=False)
79
80     for booking in cleanup_set:
81         if not booking.job.complete:
82             job = booking.job
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))
87             job.complete = True
88             job.save()
89
90
91 @shared_task
92 def free_hosts():
93     """
94     gets all hosts from the database that need to be freed and frees them
95     """
96     networks = ~Q(~Q(job__hostnetworkrelation__status=200))
97     hardware = ~Q(~Q(job__hosthardwarerelation__status=200))
98
99     bookings = Booking.objects.filter(
100             networks,
101             hardware,
102             end__lt=timezone.now(),
103             job__complete=True,
104             resource__isnull=False
105             )
106     for booking in bookings:
107         ResourceManager.getInstance().deleteResourceBundle(booking.resource)