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 ##############################################################################
11 from django.utils import timezone
14 from booking.models import Booking
15 from resource_inventory.models import Host, Image
16 from workflow.models import WorkflowStep
17 from workflow.forms import SnapshotMetaForm, SnapshotHostSelectForm
20 class Select_Host_Step(WorkflowStep):
21 template = "snapshot_workflow/steps/select_host.html"
23 description = "Choose which machine you want to snapshot"
26 def get_context(self):
27 context = super(Select_Host_Step, self).get_context()
28 context['form'] = SnapshotHostSelectForm()
31 user = self.repo_get(self.repo.SESSION_USER)
32 bookings = Booking.objects.filter(start__lt=now, end__gt=now, owner=user)
33 for booking in bookings:
34 booking_hosts[booking.id] = {}
35 booking_hosts[booking.id]['purpose'] = booking.purpose
36 booking_hosts[booking.id]['start'] = booking.start.strftime("%Y-%m-%d")
37 booking_hosts[booking.id]['end'] = booking.end.strftime("%Y-%m-%d")
38 booking_hosts[booking.id]['hosts'] = []
39 for genericHost in booking.resource.template.getHosts():
40 booking_hosts[booking.id]['hosts'].append({"name": genericHost.resource.name})
42 context['booking_hosts'] = booking_hosts
44 chosen_host = self.repo_get(self.repo.SNAPSHOT_MODELS, {}).get("host")
47 chosen['booking_id'] = self.repo_get(self.repo.SNAPSHOT_BOOKING_ID)
48 chosen['hostname'] = chosen_host.template.resource.name
49 context['chosen'] = chosen
52 def post_render(self, request):
53 host_data = request.POST.get("host")
55 self.metastep.set_invalid("Please select a host")
56 return self.render(request)
57 host = json.loads(host_data)
58 if 'name' not in host or 'booking' not in host:
59 self.metastep.set_invalid("Invalid host selected")
60 return self.render(request)
62 booking_id = host['booking']
63 booking = Booking.objects.get(pk=booking_id)
64 host = Host.objects.get(bundle=booking.resource, template__resource__name=name)
65 models = self.repo_get(self.repo.SNAPSHOT_MODELS, {})
66 if "host" not in models:
68 if 'snapshot' not in models:
69 models['snapshot'] = Image()
70 self.repo_put(self.repo.SNAPSHOT_MODELS, models)
71 self.repo_put(self.repo.SNAPSHOT_BOOKING_ID, booking_id)
73 confirm = self.repo_get(self.repo.CONFIRMATION, {})
74 snap_confirm = confirm.get("snapshot", {})
75 snap_confirm['host'] = name
76 confirm['snapshot'] = snap_confirm
77 self.repo_put(self.repo.CONFIRMATION, confirm)
78 self.metastep.set_valid("Success")
79 return self.render(request)
82 class Image_Meta_Step(WorkflowStep):
83 template = "snapshot_workflow/steps/meta.html"
84 title = "Additional Information"
85 description = "We need some more info"
88 def get_context(self):
89 context = super(Image_Meta_Step, self).get_context()
90 name = self.repo_get(self.repo.SNAPSHOT_NAME, False)
91 desc = self.repo_get(self.repo.SNAPSHOT_DESC, False)
94 form = SnapshotMetaForm(initial={"name": name, "description": desc})
96 form = SnapshotMetaForm()
97 context['form'] = form
100 def post_render(self, request):
101 form = SnapshotMetaForm(request.POST)
103 name = form.cleaned_data['name']
104 self.repo_put(self.repo.SNAPSHOT_NAME, name)
105 description = form.cleaned_data['description']
106 self.repo_put(self.repo.SNAPSHOT_DESC, description)
108 confirm = self.repo_get(self.repo.CONFIRMATION, {})
109 snap_confirm = confirm.get("snapshot", {})
110 snap_confirm['name'] = name
111 snap_confirm['description'] = description
112 confirm['snapshot'] = snap_confirm
113 self.repo_put(self.repo.CONFIRMATION, confirm)
115 self.metastep.set_valid("Success")
117 self.metastep.set_invalid("Please Fill out the Form")
119 return self.render(request)