Rewrite Notification subsystem
[pharos-tools.git] / dashboard / 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 NotificationHandler
17 from api.models import *
18 from resource_inventory.resource_manager import ResourceManager
19
20
21 @shared_task
22 def booking_poll():
23     def cleanup_hardware(qs):
24         for hostrelation in qs:
25             config = hostrelation.config
26             config.clear_delta()
27             config.set_power("off")
28             config.save()
29             hostrelation.status=JobStatus.NEW
30             hostrelation.save()
31
32     def cleanup_network(qs):
33         for hostrelation in qs:
34             network = hostrelation.config
35             network.interfaces.clear()
36             host = hostrelation.host
37             network.clear_delta()
38             vlans = []
39             for interface in host.interfaces.all():
40                 for vlan in interface.config.all():
41                     if vlan.public:
42                         try:
43                             host.lab.vlan_manager.release_public_vlan(vlan.vlan_id)
44                         except:  # will fail if we already released in this loop
45                             pass
46                     else:
47                         vlans.append(vlan.vlan_id)
48
49                 # release all vlans
50                 if len(vlans) > 0:
51                     host.lab.vlan_manager.release_vlans(vlans)
52
53                 interface.config.clear()
54                 network.add_interface(interface)
55                 network.save()
56             hostrelation.status=JobStatus.NEW
57             hostrelation.save()
58
59     def cleanup_software(qs):
60         if qs.exists():
61             relation = qs.first()
62             software = relation.config.opnfv
63             software.clear_delta()
64             software.save()
65             relation.status=JobStatus.NEW
66             relation.save()
67
68     def cleanup_access(qs):
69         for relation in qs:
70             pass # TODO
71
72     cleanup_set = Booking.objects.filter(end__lte=timezone.now()).filter(job__complete=False)
73
74     for booking in cleanup_set:
75         if not booking.job.complete:
76             job = booking.job
77             cleanup_software(SoftwareRelation.objects.filter(job=job))
78             cleanup_hardware(HostHardwareRelation.objects.filter(job=job))
79             cleanup_network(HostNetworkRelation.objects.filter(job=job))
80             cleanup_access(AccessRelation.objects.filter(job=job))
81             job.complete = True
82             job.save()
83             NotificationHandler.notify_booking_end(booking)
84
85
86 @shared_task
87 def free_hosts():
88     """
89     gets all hosts from the database that need to be freed and frees them
90     """
91     networks = ~Q(~Q(job__hostnetworkrelation__status=200))
92     hardware = ~Q(~Q(job__hosthardwarerelation__status=200))
93
94     bookings = Booking.objects.filter(
95             networks,
96             hardware,
97             end__lt=timezone.now(),
98             job__complete=True,
99             resource__isnull=False
100             )
101     for booking in bookings:
102         ResourceManager.getInstance().deleteResourceBundle(booking.resource)