aboutsummaryrefslogtreecommitdiffstats
path: root/src/dashboard/tasks.py
diff options
context:
space:
mode:
authorParker Berberian <pberberian@iol.unh.edu>2018-10-10 16:06:47 -0400
committerParker Berberian <pberberian@iol.unh.edu>2018-10-15 13:16:11 -0400
commit1f3a770d2547848590f39e9d9b9bdffeb94eec14 (patch)
tree97222e5facd1a242d951c38482315057b5790d51 /src/dashboard/tasks.py
parent6d4019e59eda897384e9c00d1daf8b2ce87d128f (diff)
Lab as a Service 2.0
See changes here: https://wiki.opnfv.org/display/INF/Pharos+Laas Change-Id: I59ada5f98e70a28d7f8c14eab3239597e236ca26 Signed-off-by: Sawyer Bergeron <sbergeron@iol.unh.edu> Signed-off-by: Parker Berberian <pberberian@iol.unh.edu>
Diffstat (limited to 'src/dashboard/tasks.py')
-rw-r--r--src/dashboard/tasks.py113
1 files changed, 91 insertions, 22 deletions
diff --git a/src/dashboard/tasks.py b/src/dashboard/tasks.py
index fa2ee9d..827c7c5 100644
--- a/src/dashboard/tasks.py
+++ b/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
@@ -7,32 +8,100 @@
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
-from datetime import timedelta
from celery import shared_task
from django.utils import timezone
-from django.conf import settings
+from django.db.models import Q
from booking.models import Booking
+from notifier.manager import *
+from notifier.models import *
+from api.models import *
+from resource_inventory.resource_manager import ResourceManager
-from jenkins.models import JenkinsStatistic
@shared_task
-def database_cleanup():
- now = timezone.now()
- JenkinsStatistic.objects.filter(timestamp__lt=now - timedelta(weeks=4)).delete()
-
-def booking_cleanup():
- expire_time = timedelta(days=int(settings.BOOKING_EXP_TIME))
- expire_number = int(settings.BOOKING_MAX_NUM)
- expired_set = Booking.objects.filter(end__lte=timezone.now())
- expired_count = len(expired_set)
-
- for booking in expired_set:
- if timezone.now() - booking.end > expire_time:
- booking.delete()
- expired_count = expired_count - 1
-
- if expired_count > expire_number:
- oldest = expired_set.order_by("end")[:expired_count-expire_number]
- for booking in oldest:
- booking.delete()
+def conjure_aggregate_notifiers():
+ NotifyPeriodic.task()
+
+
+@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:
+ if vlan.public:
+ try:
+ host.lab.vlan_manager.release_public_vlan(vlan.vlan_id)
+ except: # 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)
+
+ 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:
+ pass # TODO
+
+ 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()
+
+
+@shared_task
+def free_hosts():
+ """
+ gets all hosts from the database that need to be freed and frees them
+ """
+ networks = ~Q(~Q(job__hostnetworkrelation__status=200))
+ hardware = ~Q(~Q(job__hosthardwarerelation__status=200))
+
+ bookings = Booking.objects.filter(
+ networks,
+ hardware,
+ end__lt=timezone.now(),
+ job__complete=True,
+ resource__isnull=False
+ )
+ for booking in bookings:
+ ResourceManager.getInstance().deleteResourceBundle(booking.resource)