Update actions to free hosts and vlans 26/72126/6
authorSean Smith <ssmith@iol.unh.edu>
Tue, 23 Feb 2021 20:41:59 +0000 (15:41 -0500)
committerSean Smith <ssmith@iol.unh.edu>
Tue, 23 Mar 2021 22:03:13 +0000 (18:03 -0400)
Signed-off-by: Sean Smith <ssmith@iol.unh.edu>
Change-Id: I94425ca5a48cccdf0b5382ef2bb16a989ee6b32d
Signed-off-by: Sawyer Bergeron <sbergeron@iol.unh.edu>
src/dashboard/actions.py [deleted file]
src/dashboard/admin_utils.py

diff --git a/src/dashboard/actions.py b/src/dashboard/actions.py
deleted file mode 100644 (file)
index 44b1fdd..0000000
+++ /dev/null
@@ -1,47 +0,0 @@
-##############################################################################
-# Copyright (c) 2019 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
-# which accompanies this distribution, and is available at
-# http://www.apache.org/licenses/LICENSE-2.0
-##############################################################################
-
-from resource_inventory.models import Host, Vlan
-from account.models import Lab
-from booking.models import Booking
-from datetime import timedelta
-from django.utils import timezone
-
-
-def free_leaked_hosts(free_old_bookings=False, old_booking_age=timedelta(days=1)):
-    bundles = [booking.resource for booking in Booking.objects.filter(end__gt=timezone.now())]
-    active_hosts = set()
-    for bundle in bundles:
-        active_hosts.update([host for host in bundle.hosts.all()])
-
-    marked_hosts = set(Host.objects.filter(booked=True))
-
-    for host in (marked_hosts - active_hosts):
-        host.booked = False
-        host.save()
-
-
-def free_leaked_public_vlans():
-    booked_host_interfaces = []
-
-    for lab in Lab.objects.all():
-
-        for host in Host.objects.filter(booked=True).filter(lab=lab):
-            for interface in host.interfaces.all():
-                booked_host_interfaces.append(interface)
-
-        in_use_vlans = Vlan.objects.filter(public=True).distinct('vlan_id').filter(interface__in=booked_host_interfaces)
-
-        manager = lab.vlan_manager
-
-        for vlan in Vlan.objects.all():
-            if vlan not in in_use_vlans:
-                if vlan.public:
-                    manager.release_public_vlan(vlan.vlan_id)
-                manager.release_vlans(vlan)
index 222ccd3..b69981c 100644 (file)
@@ -20,7 +20,10 @@ import json
 
 from django.contrib.auth.models import User
 
-from account.models import Lab
+from account.models import (
+    Lab,
+    PublicNetwork
+)
 
 from resource_inventory.resource_manager import ResourceManager
 from resource_inventory.pdf_templater import PDFTemplater
@@ -220,6 +223,21 @@ def force_release_booking(booking_id):
         task.save()
 
 
+def free_leaked_public_vlans(safety_buffer_days=2):
+    for lab in Lab.objects.all():
+        current_booking_set = Booking.objects.filter(end__gte=timezone.now() + timedelta(days=safety_buffer_days))
+
+        marked_nets = set()
+
+        for booking in current_booking_set:
+            for network in get_network_metadata(booking.id):
+                marked_nets.add(network["vlan_id"])
+
+        for net in PublicNetwork.objects.filter(lab=lab).filter(in_use=True):
+            if net.vlan not in marked_nets:
+                lab.vlan_manager.release_public_vlan(net.vlan)
+
+
 def get_network_metadata(booking_id: int):
     booking = Booking.objects.get(id=booking_id)
     bundle = booking.resource