1 ##############################################################################
2 # Copyright (c) 2016 Max Breitenfeldt and others.
3 # Copyright (c) 2018 Parker Berberian, Sawyer Bergeron, and others.
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 ##############################################################################
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 (
26 from resource_inventory.resource_manager import ResourceManager
27 from resource_inventory.models import ConfigState
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
39 def cleanup_software(qs):
42 software = relation.config.opnfv
43 software.clear_delta()
45 relation.status = JobStatus.NEW
48 def cleanup_access(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
56 cleanup_set = Booking.objects.filter(end__lte=timezone.now()).filter(job__complete=False)
58 for booking in cleanup_set:
59 if not booking.job.complete:
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))
67 NotificationHandler.notify_booking_end(booking)
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
79 bookings = Booking.objects.exclude(
82 end__lt=timezone.now(),
84 resource__isnull=False
86 for booking in bookings:
87 ResourceManager.getInstance().releaseResourceBundle(booking.resource)
91 def query_vpn_users():
92 """ get active vpn users """
93 JobFactory.makeActiveUsersTask()