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