diff options
author | Parker Berberian <pberberian@iol.unh.edu> | 2019-03-22 17:59:57 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@opnfv.org> | 2019-03-22 17:59:57 +0000 |
commit | 4816475aecde4d949e1a4d37b4f494573d7257bd (patch) | |
tree | 0e41bdca89b2b620ab6bb44ad4b18c45b88e1c68 | |
parent | 03f6255afca6bf441f255794c2ca031af92ace09 (diff) | |
parent | 2718d7f5de9c767c80bd9c6b2853fe69a2f3b3a8 (diff) |
Merge "Add ability to clean up hosts and vlans"
-rw-r--r-- | src/dashboard/actions.py | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/dashboard/actions.py b/src/dashboard/actions.py new file mode 100644 index 0000000..44b1fdd --- /dev/null +++ b/src/dashboard/actions.py @@ -0,0 +1,47 @@ +############################################################################## +# 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) |