diff options
Diffstat (limited to 'dashboard/src/dashboard/tasks.py')
-rw-r--r-- | dashboard/src/dashboard/tasks.py | 100 |
1 files changed, 92 insertions, 8 deletions
diff --git a/dashboard/src/dashboard/tasks.py b/dashboard/src/dashboard/tasks.py index c5ef505..597629f 100644 --- a/dashboard/src/dashboard/tasks.py +++ b/dashboard/src/dashboard/tasks.py @@ -1,5 +1,6 @@ ############################################################################## # 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 @@ -8,17 +9,100 @@ ############################################################################## -from datetime import timedelta - from celery import shared_task from django.utils import timezone +from booking.models import Booking +from notifier.manager import NotificationHandler +from api.models import Job, JobStatus, SoftwareRelation, HostHardwareRelation, HostNetworkRelation, AccessRelation +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 Exception: # 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) -from jenkins.models import JenkinsStatistic -from notification.models import BookingNotification + 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 database_cleanup(): - now = timezone.now() - JenkinsStatistic.objects.filter(timestamp__lt=now - timedelta(weeks=4)).delete() - BookingNotification.objects.filter(submit_time__lt=now - timedelta(weeks=4)).delete()
\ No newline at end of file +def free_hosts(): + """ + gets all hosts from the database that need to be freed and frees them + """ + undone_statuses = [JobStatus.NEW, JobStatus.CURRENT, JobStatus.ERROR] + undone_jobs = Job.objects.filter( + hostnetworkrelation__status__in=undone_statuses, + hosthardwarerelation__status__in=undone_statuses + ) + + bookings = Booking.objects.exclude( + job__in=undone_jobs + ).filter( + end__lt=timezone.now(), + job__complete=True, + resource__isnull=False + ) + for booking in bookings: + ResourceManager.getInstance().deleteResourceBundle(booking.resource) |