Reduce log spam: deletion on resource bundles should be deprecated
[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 booking.models import Booking
15 from notifier.manager import NotificationHandler
16 from api.models import (
17     Job,
18     JobStatus,
19     SoftwareRelation,
20     HostHardwareRelation,
21     HostNetworkRelation,
22     AccessRelation,
23     JobFactory
24 )
25
26 from resource_inventory.resource_manager import ResourceManager
27 from resource_inventory.models import ConfigState
28
29
30 @shared_task
31 def booking_poll():
32     def cleanup_resource_task(qs):
33         for hostrelation in qs:
34             hostrelation.config.state = ConfigState.CLEAN
35             hostrelation.config.save()
36             hostrelation.status = JobStatus.NEW
37             hostrelation.save()
38
39     def cleanup_software(qs):
40         if qs.exists():
41             relation = qs.first()
42             software = relation.config.opnfv
43             software.clear_delta()
44             software.save()
45             relation.status = JobStatus.NEW
46             relation.save()
47
48     def cleanup_access(qs):
49         for relation in qs:
50             if "vpn" in relation.config.access_type.lower():
51                 relation.config.set_revoke(True)
52                 relation.config.save()
53                 relation.status = JobStatus.NEW
54                 relation.save()
55
56     cleanup_set = Booking.objects.filter(end__lte=timezone.now()).filter(job__complete=False)
57
58     for booking in cleanup_set:
59         if not booking.job.complete:
60             job = booking.job
61             cleanup_software(SoftwareRelation.objects.filter(job=job))
62             cleanup_resource_task(HostHardwareRelation.objects.filter(job=job))
63             cleanup_resource_task(HostNetworkRelation.objects.filter(job=job))
64             cleanup_access(AccessRelation.objects.filter(job=job))
65             job.complete = True
66             job.save()
67             NotificationHandler.notify_booking_end(booking)
68
69
70 @shared_task
71 def free_hosts():
72     """Free all hosts that should be freed."""
73     undone_statuses = [JobStatus.NEW, JobStatus.CURRENT, JobStatus.ERROR]
74     undone_jobs = Job.objects.filter(
75         hostnetworkrelation__status__in=undone_statuses,
76         hosthardwarerelation__status__in=undone_statuses
77     )
78
79     bookings = Booking.objects.exclude(
80         job__in=undone_jobs
81     ).filter(
82         end__lt=timezone.now(),
83         job__complete=True,
84         resource__isnull=False
85     )
86     for booking in bookings:
87         ResourceManager.getInstance().releaseResourceBundle(booking.resource)
88
89
90 @shared_task
91 def query_vpn_users():
92     """ get active vpn users """
93     JobFactory.makeActiveUsersTask()