1 ##############################################################################
2 # Copyright (c) 2018 Parker Berberian, Sawyer Bergeron, and others.
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 ##############################################################################
14 from resource_inventory.models import *
15 from workflow.models import *
16 from workflow.forms import *
18 class Select_Host_Step(WorkflowStep):
19 template = "snapshot_workflow/steps/select_host.html"
21 description = "Choose which machine you want to snapshot"
24 def get_context(self):
25 context = super(Select_Host_Step, self).get_context()
26 context['form'] = SnapshotHostSelectForm()
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})
41 context['booking_hosts'] = booking_hosts
43 chosen_host = self.repo_get(self.repo.SNAPSHOT_MODELS, {}).get("host")
46 chosen['booking_id'] = self.repo_get(self.repo.SNAPSHOT_BOOKING_ID)
47 chosen['hostname'] = chosen_host.template.resource.name
48 context['chosen'] = chosen
51 def post_render(self, request):
52 host_data = request.POST.get("host")
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)
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:
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)
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)
80 class Image_Meta_Step(WorkflowStep):
81 template = "snapshot_workflow/steps/meta.html"
82 title = "Additional Information"
83 description = "We need some more info"
86 def get_context(self):
87 context = super(Image_Meta_Step, self).get_context()
88 context['form'] = SnapshotMetaForm()
92 def post_render(self, request):
93 form = SnapshotMetaForm(request.POST)
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)
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)
107 self.metastep.set_valid("Success")
109 self.metastep.set_invalid("Please Fill out the Form")
111 return self.render(request)