root / image_creator / dialog_wizard.py @ b25b422b
History | View | Annotate | Download (16.6 kB)
1 |
# -*- coding: utf-8 -*-
|
---|---|
2 |
#
|
3 |
# Copyright 2012 GRNET S.A. All rights reserved.
|
4 |
#
|
5 |
# Redistribution and use in source and binary forms, with or
|
6 |
# without modification, are permitted provided that the following
|
7 |
# conditions are met:
|
8 |
#
|
9 |
# 1. Redistributions of source code must retain the above
|
10 |
# copyright notice, this list of conditions and the following
|
11 |
# disclaimer.
|
12 |
#
|
13 |
# 2. Redistributions in binary form must reproduce the above
|
14 |
# copyright notice, this list of conditions and the following
|
15 |
# disclaimer in the documentation and/or other materials
|
16 |
# provided with the distribution.
|
17 |
#
|
18 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
19 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
20 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
21 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
22 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
23 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
24 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
25 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
26 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
27 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
28 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
29 |
# POSSIBILITY OF SUCH DAMAGE.
|
30 |
#
|
31 |
# The views and conclusions contained in the software and
|
32 |
# documentation are those of the authors and should not be
|
33 |
# interpreted as representing official policies, either expressed
|
34 |
# or implied, of GRNET S.A.
|
35 |
|
36 |
"""This module implements the "wizard" mode of the dialog-based version of
|
37 |
snf-image-creator.
|
38 |
"""
|
39 |
|
40 |
import time |
41 |
import StringIO |
42 |
import json |
43 |
|
44 |
from image_creator.kamaki_wrapper import Kamaki, ClientError |
45 |
from image_creator.util import MD5, FatalError |
46 |
from image_creator.output.cli import OutputWthProgress |
47 |
from image_creator.dialog_util import extract_image, update_background_title, \ |
48 |
add_cloud, edit_cloud |
49 |
|
50 |
PAGE_WIDTH = 70
|
51 |
PAGE_HEIGHT = 10
|
52 |
SYSPREP_PARAM_MAXLEN = 20
|
53 |
|
54 |
|
55 |
class WizardExit(Exception): |
56 |
"""Exception used to exit the wizard"""
|
57 |
pass
|
58 |
|
59 |
|
60 |
class WizardReloadPage(Exception): |
61 |
"""Exception that reloads the last WizardPage"""
|
62 |
pass
|
63 |
|
64 |
|
65 |
class Wizard: |
66 |
"""Represents a dialog-based wizard
|
67 |
|
68 |
The wizard is a collection of pages that have a "Next" and a "Back" button
|
69 |
on them. The pages are used to collect user data.
|
70 |
"""
|
71 |
|
72 |
def __init__(self, session): |
73 |
self.session = session
|
74 |
self.pages = []
|
75 |
self.session['wizard'] = {} |
76 |
self.d = session['dialog'] |
77 |
|
78 |
def add_page(self, page): |
79 |
"""Add a new page to the wizard"""
|
80 |
self.pages.append(page)
|
81 |
|
82 |
def run(self): |
83 |
"""Run the wizard"""
|
84 |
idx = 0
|
85 |
while True: |
86 |
try:
|
87 |
total = len(self.pages) |
88 |
title = "(%d/%d) %s" % (idx + 1, total, self.pages[idx].title) |
89 |
idx += self.pages[idx].run(self.session, title) |
90 |
except WizardExit:
|
91 |
return False |
92 |
except WizardReloadPage:
|
93 |
continue
|
94 |
|
95 |
if idx >= len(self.pages): |
96 |
text = "All necessary information has been gathered:\n\n"
|
97 |
for page in self.pages: |
98 |
text += " * %s\n" % page.info
|
99 |
text += "\nContinue with the image creation process?"
|
100 |
|
101 |
ret = self.d.yesno(
|
102 |
text, width=PAGE_WIDTH, height=8 + len(self.pages), |
103 |
ok_label="Yes", cancel="Back", extra_button=1, |
104 |
extra_label="Quit", title="Confirmation") |
105 |
|
106 |
if ret == self.d.DIALOG_CANCEL: |
107 |
idx -= 1
|
108 |
elif ret == self.d.DIALOG_EXTRA: |
109 |
return False |
110 |
elif ret == self.d.DIALOG_OK: |
111 |
return True |
112 |
|
113 |
if idx < 0: |
114 |
return False |
115 |
|
116 |
|
117 |
class WizardPage(object): |
118 |
"""Represents a page in a wizard"""
|
119 |
NEXT = 1
|
120 |
PREV = -1
|
121 |
|
122 |
def __init__(self, name, display_name, text, **kargs): |
123 |
self.name = name
|
124 |
self.display_name = display_name
|
125 |
self.text = text
|
126 |
|
127 |
self.title = kargs['title'] if 'title' in kargs else "" |
128 |
self.default = kargs['default'] if 'default' in kargs else "" |
129 |
self.extra = kargs['extra'] if 'extra' in kargs else None |
130 |
self.extra_label = \
|
131 |
kargs['extra_label'] if 'extra_label' in kargs else 'Extra' |
132 |
|
133 |
self.info = "%s: <none>" % self.display_name |
134 |
|
135 |
validate = kargs['validate'] if 'validate' in kargs else lambda x: x |
136 |
setattr(self, "validate", validate) |
137 |
|
138 |
display = kargs['display'] if 'display' in kargs else lambda x: x |
139 |
setattr(self, "display", display) |
140 |
|
141 |
def run(self, session, title): |
142 |
"""Display this wizard page
|
143 |
|
144 |
This function is used by the wizard program when accessing a page.
|
145 |
"""
|
146 |
raise NotImplementedError |
147 |
|
148 |
|
149 |
class WizardPageWthChoices(WizardPage): |
150 |
"""Represents a Wizard Page that allows the user to select something from
|
151 |
a list of choices.
|
152 |
|
153 |
The available choices are created by a function passed to the class through
|
154 |
the choices variable. If the choices function returns an empty list, a
|
155 |
fallback funtion is executed if available.
|
156 |
"""
|
157 |
def __init__(self, name, display_name, text, choices, **kargs): |
158 |
super(WizardPageWthChoices, self).__init__(name, display_name, text, |
159 |
**kargs) |
160 |
self.choices = choices
|
161 |
self.fallback = kargs['fallback'] if 'fallback' in kargs else None |
162 |
|
163 |
|
164 |
class WizardRadioListPage(WizardPageWthChoices): |
165 |
"""Represent a Radio List in a wizard"""
|
166 |
|
167 |
def run(self, session, title): |
168 |
d = session['dialog']
|
169 |
w = session['wizard']
|
170 |
|
171 |
choices = [] |
172 |
for choice in self.choices(): |
173 |
default = 1 if choice[0] == self.default else 0 |
174 |
choices.append((choice[0], choice[1], default)) |
175 |
|
176 |
(code, answer) = d.radiolist( |
177 |
self.text, width=PAGE_WIDTH, ok_label="Next", cancel="Back", |
178 |
choices=choices, height=PAGE_HEIGHT, title=title) |
179 |
|
180 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
181 |
return self.PREV |
182 |
|
183 |
w[self.name] = self.validate(answer) |
184 |
self.default = answer
|
185 |
self.info = "%s: %s" % (self.display_name, self.display(w[self.name])) |
186 |
|
187 |
return self.NEXT |
188 |
|
189 |
|
190 |
class WizardInputPage(WizardPage): |
191 |
"""Represents an input field in a wizard"""
|
192 |
|
193 |
def run(self, session, title): |
194 |
d = session['dialog']
|
195 |
w = session['wizard']
|
196 |
|
197 |
(code, answer) = d.inputbox( |
198 |
self.text, init=self.default, width=PAGE_WIDTH, ok_label="Next", |
199 |
cancel="Back", height=PAGE_HEIGHT, title=title)
|
200 |
|
201 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
202 |
return self.PREV |
203 |
|
204 |
value = answer.strip() |
205 |
self.default = value
|
206 |
w[self.name] = self.validate(value) |
207 |
self.info = "%s: %s" % (self.display_name, self.display(w[self.name])) |
208 |
|
209 |
return self.NEXT |
210 |
|
211 |
|
212 |
class WizardFormPage(WizardPage): |
213 |
"""Represents a Form in a wizard"""
|
214 |
|
215 |
def __init__(self, name, display_name, text, fields, **kargs): |
216 |
super(WizardFormPage, self).__init__(name, display_name, text, **kargs) |
217 |
self.fields = fields
|
218 |
|
219 |
def run(self, session, title): |
220 |
d = session['dialog']
|
221 |
w = session['wizard']
|
222 |
|
223 |
field_lenght = len(self.fields()) |
224 |
form_height = field_lenght if field_lenght < PAGE_HEIGHT - 4 \ |
225 |
else PAGE_HEIGHT - 4 |
226 |
|
227 |
(code, output) = d.form( |
228 |
self.text, width=PAGE_WIDTH, height=PAGE_HEIGHT,
|
229 |
form_height=form_height, ok_label="Next", cancel="Back", |
230 |
fields=self.fields(), title=title)
|
231 |
|
232 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
233 |
return self.PREV |
234 |
|
235 |
w[self.name] = self.validate(output) |
236 |
self.default = output
|
237 |
self.info = "%s: %s" % (self.display_name, self.display(w[self.name])) |
238 |
|
239 |
return self.NEXT |
240 |
|
241 |
|
242 |
class WizardMenuPage(WizardPageWthChoices): |
243 |
"""Represents a menu dialog with available choices in a wizard"""
|
244 |
|
245 |
def run(self, session, title): |
246 |
d = session['dialog']
|
247 |
w = session['wizard']
|
248 |
|
249 |
extra_button = 1 if self.extra else 0 |
250 |
|
251 |
choices = self.choices()
|
252 |
|
253 |
if len(choices) == 0: |
254 |
assert self.fallback, "Zero choices and no fallback" |
255 |
if self.fallback(): |
256 |
raise WizardReloadPage
|
257 |
else:
|
258 |
return self.PREV |
259 |
|
260 |
default_item = self.default if self.default else choices[0][0] |
261 |
|
262 |
(code, choice) = d.menu( |
263 |
self.text, width=PAGE_WIDTH, ok_label="Next", cancel="Back", |
264 |
title=title, choices=choices, height=PAGE_HEIGHT, |
265 |
default_item=default_item, extra_label=self.extra_label,
|
266 |
extra_button=extra_button) |
267 |
|
268 |
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC): |
269 |
return self.PREV |
270 |
elif code == d.DIALOG_EXTRA:
|
271 |
self.extra()
|
272 |
raise WizardReloadPage
|
273 |
|
274 |
self.default = choice
|
275 |
w[self.name] = self.validate(choice) |
276 |
self.info = "%s: %s" % (self.display_name, self.display(w[self.name])) |
277 |
|
278 |
return self.NEXT |
279 |
|
280 |
|
281 |
def start_wizard(session): |
282 |
"""Run the image creation wizard"""
|
283 |
|
284 |
image = session['image']
|
285 |
distro = image.distro |
286 |
ostype = image.ostype |
287 |
|
288 |
# Create Cloud Wizard Page
|
289 |
def cloud_choices(): |
290 |
choices = [] |
291 |
for (name, cloud) in Kamaki.get_clouds().items(): |
292 |
descr = cloud['description'] if 'description' in cloud else '' |
293 |
choices.append((name, descr)) |
294 |
|
295 |
return choices
|
296 |
|
297 |
def cloud_add(): |
298 |
return add_cloud(session)
|
299 |
|
300 |
def cloud_none_available(): |
301 |
if not session['dialog'].yesno( |
302 |
"No available clouds found. Would you like to add one now?",
|
303 |
width=PAGE_WIDTH, defaultno=0):
|
304 |
return add_cloud(session)
|
305 |
return False |
306 |
|
307 |
def cloud_validate(cloud): |
308 |
if not Kamaki.get_account(cloud): |
309 |
if not session['dialog'].yesno( |
310 |
"The cloud you have selected is not valid! Would you "
|
311 |
"like to edit it now?", width=PAGE_WIDTH, defaultno=0): |
312 |
if edit_cloud(session, cloud):
|
313 |
return cloud
|
314 |
|
315 |
raise WizardReloadPage
|
316 |
|
317 |
return cloud
|
318 |
|
319 |
cloud = WizardMenuPage( |
320 |
"Cloud", "Cloud", |
321 |
"Please select a cloud account or press <Add> to add a new one:",
|
322 |
choices=cloud_choices, extra_label="Add", extra=cloud_add,
|
323 |
title="Clouds", validate=cloud_validate, fallback=cloud_none_available)
|
324 |
|
325 |
# Create Image Name Wizard Page
|
326 |
name = WizardInputPage( |
327 |
"ImageName", "Image Name", "Please provide a name for the image:", |
328 |
title="Image Name", default=ostype if distro == "unknown" else distro) |
329 |
|
330 |
# Create Image Description Wizard Page
|
331 |
descr = WizardInputPage( |
332 |
"ImageDescription", "Image Description", |
333 |
"Please provide a description for the image:",
|
334 |
title="Image Description", default=session['metadata']['DESCRIPTION'] |
335 |
if 'DESCRIPTION' in session['metadata'] else '') |
336 |
|
337 |
# Create Sysprep Params Wizard Page
|
338 |
needed = image.os.needed_sysprep_params |
339 |
# Only show the parameters that don't have default values
|
340 |
param_names = [param for param in needed if needed[param].default is None] |
341 |
|
342 |
def sysprep_params_fields(): |
343 |
fields = [] |
344 |
available = image.os.sysprep_params |
345 |
for name in param_names: |
346 |
text = needed[name].description |
347 |
default = str(available[name]) if name in available else "" |
348 |
fields.append(("%s: " % text, default, SYSPREP_PARAM_MAXLEN))
|
349 |
return fields
|
350 |
|
351 |
def sysprep_params_validate(answer): |
352 |
params = {} |
353 |
for i in range(len(answer)): |
354 |
try:
|
355 |
value = needed[param_names[i]].type(answer[i]) |
356 |
if needed[param_names[i]].validate(value):
|
357 |
params[param_names[i]] = value |
358 |
continue
|
359 |
except ValueError: |
360 |
pass
|
361 |
|
362 |
session['dialog'].msgbox("Invalid value for parameter `%s'" % |
363 |
param_names[i]) |
364 |
raise WizardReloadPage
|
365 |
return params
|
366 |
|
367 |
def sysprep_params_display(params): |
368 |
return ",".join(["%s=%s" % (key, val) for key, val in params.items()]) |
369 |
|
370 |
sysprep_params = WizardFormPage( |
371 |
"SysprepParams", "Sysprep Parameters", |
372 |
"Prease fill in the following system preparation parameters:",
|
373 |
title="System Preparation Parameters", fields=sysprep_params_fields,
|
374 |
display=sysprep_params_display, validate=sysprep_params_validate |
375 |
) if len(needed) != 0 else None |
376 |
|
377 |
# Create Image Registration Wizard Page
|
378 |
def registration_choices(): |
379 |
return [("Private", "Image is accessible only by this user"), |
380 |
("Public", "Everyone can create VMs from this image")] |
381 |
|
382 |
registration = WizardRadioListPage( |
383 |
"ImageRegistration", "Registration Type", |
384 |
"Please provide a registration type:", registration_choices,
|
385 |
title="Registration Type", default="Private") |
386 |
|
387 |
w = Wizard(session) |
388 |
|
389 |
w.add_page(cloud) |
390 |
w.add_page(name) |
391 |
w.add_page(descr) |
392 |
if sysprep_params is not None: |
393 |
w.add_page(sysprep_params) |
394 |
w.add_page(registration) |
395 |
|
396 |
if w.run():
|
397 |
create_image(session) |
398 |
else:
|
399 |
return False |
400 |
|
401 |
return True |
402 |
|
403 |
|
404 |
def create_image(session): |
405 |
"""Create an image using the information collected by the wizard"""
|
406 |
d = session['dialog']
|
407 |
image = session['image']
|
408 |
wizard = session['wizard']
|
409 |
|
410 |
with_progress = OutputWthProgress(True)
|
411 |
out = image.out |
412 |
out.add(with_progress) |
413 |
try:
|
414 |
out.clear() |
415 |
|
416 |
#Sysprep
|
417 |
if 'SysprepParams' in wizard: |
418 |
image.os.sysprep_params.update(wizard['SysprepParams'])
|
419 |
image.os.do_sysprep() |
420 |
metadata = image.os.meta |
421 |
|
422 |
#Shrink
|
423 |
size = image.shrink() |
424 |
session['shrinked'] = True |
425 |
update_background_title(session) |
426 |
|
427 |
metadata.update(image.meta) |
428 |
metadata['DESCRIPTION'] = wizard['ImageDescription'] |
429 |
|
430 |
#MD5
|
431 |
md5 = MD5(out) |
432 |
session['checksum'] = md5.compute(image.device, size)
|
433 |
|
434 |
out.output() |
435 |
try:
|
436 |
out.output("Uploading image to the cloud:")
|
437 |
account = Kamaki.get_account(wizard['Cloud'])
|
438 |
assert account, "Cloud: %s is not valid" % wizard['Cloud'] |
439 |
kamaki = Kamaki(account, out) |
440 |
|
441 |
name = "%s-%s.diskdump" % (wizard['ImageName'], |
442 |
time.strftime("%Y%m%d%H%M"))
|
443 |
pithos_file = ""
|
444 |
with open(image.device, 'rb') as f: |
445 |
pithos_file = kamaki.upload(f, size, name, |
446 |
"(1/3) Calculating block hashes",
|
447 |
"(2/3) Uploading missing blocks")
|
448 |
|
449 |
out.output("(3/3) Uploading md5sum file ...", False) |
450 |
md5sumstr = '%s %s\n' % (session['checksum'], name) |
451 |
kamaki.upload(StringIO.StringIO(md5sumstr), size=len(md5sumstr),
|
452 |
remote_path="%s.%s" % (name, 'md5sum')) |
453 |
out.success('done')
|
454 |
out.output() |
455 |
|
456 |
is_public = True if wizard['ImageRegistration'] == "Public" else \ |
457 |
False
|
458 |
out.output('Registering %s image with the cloud ...' %
|
459 |
wizard['ImageRegistration'].lower(), False) |
460 |
result = kamaki.register(wizard['ImageName'], pithos_file,
|
461 |
metadata, is_public) |
462 |
out.success('done')
|
463 |
out.output("Uploading metadata file ...", False) |
464 |
metastring = unicode(json.dumps(result, ensure_ascii=False)) |
465 |
kamaki.upload(StringIO.StringIO(metastring), size=len(metastring),
|
466 |
remote_path="%s.%s" % (name, 'meta')) |
467 |
out.success('done')
|
468 |
|
469 |
if is_public:
|
470 |
out.output("Sharing md5sum file ...", False) |
471 |
kamaki.share("%s.md5sum" % name)
|
472 |
out.success('done')
|
473 |
out.output("Sharing metadata file ...", False) |
474 |
kamaki.share("%s.meta" % name)
|
475 |
out.success('done')
|
476 |
|
477 |
out.output() |
478 |
|
479 |
except ClientError as e: |
480 |
raise FatalError("Storage service client: %d %s" % |
481 |
(e.status, e.message)) |
482 |
finally:
|
483 |
out.remove(with_progress) |
484 |
|
485 |
text = "The %s image was successfully uploaded to the storage service " \
|
486 |
"and registered with the compute service of %s. Would you like " \
|
487 |
"to keep a local copy?" % \
|
488 |
(wizard['Cloud'], wizard['ImageRegistration'].lower()) |
489 |
|
490 |
if not d.yesno(text, width=PAGE_WIDTH): |
491 |
extract_image(session) |
492 |
|
493 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
|