Lab as a Service 2.0
[laas.git] / src / workflow / snapshot_workflow.py
1 ##############################################################################
2 # Copyright (c) 2018 Parker Berberian, Sawyer Bergeron, and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10
11 import datetime
12 import json
13
14 from resource_inventory.models import *
15 from workflow.models import *
16 from workflow.forms import *
17
18 class Select_Host_Step(WorkflowStep):
19     template = "snapshot_workflow/steps/select_host.html"
20     title = "Select Host"
21     description = "Choose which machine you want to snapshot"
22     short_title = "host"
23
24     def get_context(self):
25         context = super(Select_Host_Step, self).get_context()
26         context['form'] = SnapshotHostSelectForm()
27         booking_hosts = {}
28         now = datetime.datetime.now()
29         user = self.repo_get(self.repo.SESSION_USER)
30         bookings = Booking.objects.filter(start__lt=now, end__gt=now, owner=user)
31         for booking in bookings:
32             booking_hosts[booking.id] = {}
33             booking_hosts[booking.id]['purpose'] = booking.purpose
34             booking_hosts[booking.id]['start'] = booking.start.strftime("%Y-%m-%d")
35             booking_hosts[booking.id]['end'] = booking.end.strftime("%Y-%m-%d")
36             booking_hosts[booking.id]['hosts'] = []
37             for genericHost in booking.resource.template.getHosts():
38                 booking_hosts[booking.id]['hosts'].append({"name": genericHost.resource.name})
39
40
41         context['booking_hosts'] = booking_hosts
42
43         chosen_host = self.repo_get(self.repo.SNAPSHOT_MODELS, {}).get("host")
44         if chosen_host:
45             chosen = {}
46             chosen['booking_id'] = self.repo_get(self.repo.SNAPSHOT_BOOKING_ID)
47             chosen['hostname'] = chosen_host.template.resource.name
48             context['chosen'] = chosen
49         return context
50
51     def post_render(self, request):
52         host_data = request.POST.get("host")
53         if not host_data:
54             self.metastep.set_invalid("Please select a host")
55             return self.render(request)
56         host = json.loads(host_data)
57         if 'name' not in host or 'booking' not in host:
58             self.metastep.set_invalid("Invalid host selected")
59             return self.render(request)
60         name = host['name']
61         booking_id = host['booking']
62         booking = Booking.objects.get(pk=booking_id)
63         host = Host.objects.get(bundle=booking.resource, template__resource__name=name)
64         models = self.repo_get(self.repo.SNAPSHOT_MODELS, {})
65         if "host" not in models:
66             models['host'] = host
67         if 'snapshot' not in models:
68             models['snapshot'] = Image()
69         self.repo_put(self.repo.SNAPSHOT_MODELS, models)
70         self.repo_put(self.repo.SNAPSHOT_BOOKING_ID, booking_id)
71
72         confirm = self.repo_get(self.repo.CONFIRMATION, {})
73         snap_confirm = confirm.get("snapshot", {})
74         snap_confirm['host'] = name
75         confirm['snapshot'] = snap_confirm
76         self.repo_put(self.repo.CONFIRMATION, confirm)
77         self.metastep.set_valid("Success")
78         return self.render(request)
79
80 class Image_Meta_Step(WorkflowStep):
81     template = "snapshot_workflow/steps/meta.html"
82     title = "Additional Information"
83     description = "We need some more info"
84     short_title = "info"
85
86     def get_context(self):
87         context = super(Image_Meta_Step, self).get_context()
88         context['form'] = SnapshotMetaForm()
89         return context
90
91
92     def post_render(self, request):
93         form = SnapshotMetaForm(request.POST)
94         if form.is_valid():
95             name = form.cleaned_data['name']
96             self.repo_put(self.repo.SNAPSHOT_NAME, name)
97             description = form.cleaned_data['description']
98             self.repo_put(self.repo.SNAPSHOT_DESC, description)
99
100             confirm = self.repo_get(self.repo.CONFIRMATION, {})
101             snap_confirm = confirm.get("snapshot", {})
102             snap_confirm['name'] = name
103             snap_confirm['description'] = description
104             confirm['snapshot'] = snap_confirm
105             self.repo_put(self.repo.CONFIRMATION, confirm)
106
107             self.metastep.set_valid("Success")
108         else:
109             self.metastep.set_invalid("Please Fill out the Form")
110
111         return self.render(request)