From 22bcbe3dda67bb8c4dbd632a8ac8925f6b0d9b5e Mon Sep 17 00:00:00 2001 From: Parker Berberian Date: Tue, 15 Jan 2019 12:49:20 -0500 Subject: Allow for Hosts to be Re-Imaged This change adds a button the user can press on thier booking detail page to reset thier host. They can choose to deploy any available image to thier servers (not just the one already used) Change-Id: I97a9869d2b38389c54f13173bb28a68cc52bb8d5 Signed-off-by: Parker Berberian --- dashboard/src/account/views.py | 4 +- dashboard/src/booking/forms.py | 10 +- dashboard/src/booking/urls.py | 7 +- dashboard/src/booking/views.py | 58 ++++++-- .../src/templates/booking/booking_detail.html | 146 ++++++++++++++++----- 5 files changed, 175 insertions(+), 50 deletions(-) diff --git a/dashboard/src/account/views.py b/dashboard/src/account/views.py index 09c5266..e880208 100644 --- a/dashboard/src/account/views.py +++ b/dashboard/src/account/views.py @@ -181,8 +181,8 @@ def account_booking_view(request): if not request.user.is_authenticated: return render(request, "dashboard/login.html", {'title': 'Authentication Required'}) template = "account/booking_list.html" - bookings = list(Booking.objects.filter(owner=request.user)) - collab_bookings = list(request.user.collaborators.all()) + bookings = list(Booking.objects.filter(owner=request.user).order_by("-start")) + collab_bookings = list(request.user.collaborators.all().order_by("-start")) context = {"title": "My Bookings", "bookings": bookings, "collab_bookings": collab_bookings} return render(request, template, context=context) diff --git a/dashboard/src/booking/forms.py b/dashboard/src/booking/forms.py index cb76383..7ba5af0 100644 --- a/dashboard/src/booking/forms.py +++ b/dashboard/src/booking/forms.py @@ -1,5 +1,5 @@ ############################################################################## -# Copyright (c) 2018 Sawyer Bergeron, Parker Berberian, 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 @@ -28,7 +28,7 @@ class QuickBookingForm(forms.Form): installer = forms.ModelChoiceField(queryset=Installer.objects.all(), required=False) scenario = forms.ModelChoiceField(queryset=Scenario.objects.all(), required=False) - def __init__(self, data=None, *args, user=None, **kwargs): + def __init__(self, data=None, user=None, *args, **kwargs): chosen_users = [] if "default_user" in kwargs: default_user = kwargs.pop("default_user") @@ -104,3 +104,9 @@ class QuickBookingForm(forms.Form): 'edit': False } return attrs + + +class HostReImageForm(forms.Form): + + image_id = forms.IntegerField() + host_id = forms.IntegerField() diff --git a/dashboard/src/booking/urls.py b/dashboard/src/booking/urls.py index c6504e0..310aaa7 100644 --- a/dashboard/src/booking/urls.py +++ b/dashboard/src/booking/urls.py @@ -33,7 +33,8 @@ from booking.views import ( BookingListView, booking_stats_view, booking_stats_json, - quick_create + quick_create, + booking_modify_image ) app_name = "booking" @@ -42,12 +43,10 @@ urlpatterns = [ url(r'^detail/(?P[0-9]+)/$', booking_detail_view, name='detail'), url(r'^(?P[0-9]+)/$', booking_detail_view, name='booking_detail'), - url(r'^delete/$', BookingDeleteView.as_view(), name='delete_prefix'), url(r'^delete/(?P[0-9]+)/$', BookingDeleteView.as_view(), name='delete'), - url(r'^delete/(?P[0-9]+)/confirm/$', bookingDelete, name='delete_booking'), - + url(r'^modify/(?P[0-9]+)/image/$', booking_modify_image, name='modify_booking_image'), url(r'^list/$', BookingListView.as_view(), name='list'), url(r'^stats/$', booking_stats_view, name='stats'), url(r'^stats/json$', booking_stats_json, name='stats_json'), diff --git a/dashboard/src/booking/views.py b/dashboard/src/booking/views.py index bc1d2c9..3be9c7b 100644 --- a/dashboard/src/booking/views.py +++ b/dashboard/src/booking/views.py @@ -10,17 +10,20 @@ from django.contrib import messages from django.shortcuts import get_object_or_404 -from django.http import JsonResponse +from django.http import JsonResponse, HttpResponse from django.utils import timezone from django.views import View from django.views.generic import TemplateView from django.shortcuts import redirect, render +from django.db.models import Q -from account.models import Lab +from resource_inventory.models import ResourceBundle, HostProfile, Image, Host from resource_inventory.resource_manager import ResourceManager -from resource_inventory.models import ResourceBundle +from account.models import Lab from booking.models import Booking from booking.stats import StatisticsManager +from booking.forms import HostReImageForm +from api.models import HostHardwareRelation, JobStatus from workflow.views import login from booking.forms import QuickBookingForm from booking.quick_deployer import create_from_form, drop_filter @@ -125,6 +128,19 @@ class ResourceBookingsJSON(View): return JsonResponse({'bookings': list(bookings)}) +def build_image_mapping(lab, user): + mapping = {} + for profile in HostProfile.objects.filter(labs=lab): + images = Image.objects.filter( + from_lab=lab, + host_type=profile + ).filter( + Q(public=True) | Q(owner=user) + ) + mapping[profile.name] = [{"name": image.name, "value": image.id} for image in images] + return mapping + + def booking_detail_view(request, booking_id): user = None if request.user.is_authenticated: @@ -138,15 +154,39 @@ def booking_detail_view(request, booking_id): if user not in allowed_users: return render(request, "dashboard/login.html", {'title': 'This page is private'}) + context = { + 'title': 'Booking Details', + 'booking': booking, + 'pdf': booking.pdf, + 'user_id': user.id, + 'image_mapping': build_image_mapping(booking.lab, user) + } + return render( request, "booking/booking_detail.html", - { - 'title': 'Booking Details', - 'booking': booking, - 'pdf': booking.pdf, - 'user_id': user.id - }) + context + ) + + +def booking_modify_image(request, booking_id): + form = HostReImageForm(request.POST) + if form.is_valid(): + booking = Booking.objects.get(id=booking_id) + if request.user != booking.owner: + return HttpResponse("unauthorized") + if timezone.now() > booking.end: + return HttpResponse("unauthorized") + new_image = Image.objects.get(id=form.cleaned_data['image_id']) + host = Host.objects.get(id=form.cleaned_data['host_id']) + relation = HostHardwareRelation.objects.get(host=host, job__booking=booking) + config = relation.config + config.set_image(new_image.lab_id) + config.save() + relation.status = JobStatus.NEW + relation.save() + return HttpResponse(new_image.name) + return HttpResponse("error") def booking_stats_view(request): diff --git a/dashboard/src/templates/booking/booking_detail.html b/dashboard/src/templates/booking/booking_detail.html index cae0e25..51dd328 100644 --- a/dashboard/src/templates/booking/booking_detail.html +++ b/dashboard/src/templates/booking/booking_detail.html @@ -1,20 +1,29 @@ {% extends "base.html" %} {% load staticfiles %} +{% load bootstrap3 %} {% block extrahead %} {{block.super}} - {% endblock %} {% block content %} + + +
-
+

Overview

- Expand + Expand
@@ -50,9 +59,7 @@
- -
- +

Pod

@@ -79,7 +86,15 @@
- + @@ -152,10 +167,7 @@
Image:{{host.config.image}} + {{host.config.image}} +
RAM:
- - - {% endfor %} @@ -163,31 +175,15 @@
-
- -
-
-

PDF

- Expand -
- -
-
-{{pdf}}
-                            
-
-
-
-
+

Deployment Progress

These are the different tasks that have to be completed before your deployment is ready

Expand
-