1 # -*- coding: utf-8 -*-
3 # Copyright 2012 GRNET S.A. All rights reserved.
5 # Redistribution and use in source and binary forms, with or
6 # without modification, are permitted provided that the following
9 # 1. Redistributions of source code must retain the above
10 # copyright notice, this list of conditions and the following
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.
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.
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.
36 """This module implements the "wizard" mode of the dialog-based version of
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, \
52 SYSPREP_PARAM_MAXLEN = 20
55 class WizardExit(Exception):
56 """Exception used to exit the wizard"""
60 class WizardReloadPage(Exception):
61 """Exception that reloads the last WizardPage"""
66 """Represents a dialog-based wizard
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.
72 def __init__(self, session):
73 self.session = session
75 self.session['wizard'] = {}
76 self.d = session['dialog']
78 def add_page(self, page):
79 """Add a new page to the wizard"""
80 self.pages.append(page)
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)
92 except WizardReloadPage:
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?"
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")
106 if ret == self.d.DIALOG_CANCEL:
108 elif ret == self.d.DIALOG_EXTRA:
110 elif ret == self.d.DIALOG_OK:
117 class WizardPage(object):
118 """Represents a page in a wizard"""
122 def __init__(self, name, display_name, text, **kargs):
124 self.display_name = display_name
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
131 kargs['extra_label'] if 'extra_label' in kargs else 'Extra'
133 self.info = "%s: <none>" % self.display_name
135 validate = kargs['validate'] if 'validate' in kargs else lambda x: x
136 setattr(self, "validate", validate)
138 display = kargs['display'] if 'display' in kargs else lambda x: x
139 setattr(self, "display", display)
141 def run(self, session, title):
142 """Display this wizard page
144 This function is used by the wizard program when accessing a page.
146 raise NotImplementedError
149 class WizardPageWthChoices(WizardPage):
150 """Represents a Wizard Page that allows the user to select something from
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.
157 def __init__(self, name, display_name, text, choices, **kargs):
158 super(WizardPageWthChoices, self).__init__(name, display_name, text,
160 self.choices = choices
161 self.fallback = kargs['fallback'] if 'fallback' in kargs else None
164 class WizardRadioListPage(WizardPageWthChoices):
165 """Represent a Radio List in a wizard"""
167 def run(self, session, title):
168 d = session['dialog']
169 w = session['wizard']
172 for choice in self.choices():
173 default = 1 if choice[0] == self.default else 0
174 choices.append((choice[0], choice[1], default))
176 (code, answer) = d.radiolist(
177 self.text, width=PAGE_WIDTH, ok_label="Next", cancel="Back",
178 choices=choices, height=PAGE_HEIGHT, title=title)
180 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
183 w[self.name] = self.validate(answer)
184 self.default = answer
185 self.info = "%s: %s" % (self.display_name, self.display(w[self.name]))
190 class WizardInputPage(WizardPage):
191 """Represents an input field in a wizard"""
193 def run(self, session, title):
194 d = session['dialog']
195 w = session['wizard']
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)
201 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
204 value = answer.strip()
206 w[self.name] = self.validate(value)
207 self.info = "%s: %s" % (self.display_name, self.display(w[self.name]))
212 class WizardFormPage(WizardPage):
213 """Represents a Form in a wizard"""
215 def __init__(self, name, display_name, text, fields, **kargs):
216 super(WizardFormPage, self).__init__(name, display_name, text, **kargs)
219 def run(self, session, title):
220 d = session['dialog']
221 w = session['wizard']
223 field_lenght = len(self.fields())
224 form_height = field_lenght if field_lenght < PAGE_HEIGHT - 4 \
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)
232 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
235 w[self.name] = self.validate(output)
236 self.default = output
237 self.info = "%s: %s" % (self.display_name, self.display(w[self.name]))
242 class WizardMenuPage(WizardPageWthChoices):
243 """Represents a menu dialog with available choices in a wizard"""
245 def run(self, session, title):
246 d = session['dialog']
247 w = session['wizard']
249 extra_button = 1 if self.extra else 0
251 choices = self.choices()
253 if len(choices) == 0:
254 assert self.fallback, "Zero choices and no fallback"
256 raise WizardReloadPage
260 default_item = self.default if self.default else choices[0][0]
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)
268 if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
270 elif code == d.DIALOG_EXTRA:
272 raise WizardReloadPage
274 self.default = choice
275 w[self.name] = self.validate(choice)
276 self.info = "%s: %s" % (self.display_name, self.display(w[self.name]))
281 def start_wizard(session):
282 """Run the image creation wizard"""
284 image = session['image']
285 distro = image.distro
286 ostype = image.ostype
288 # Create Cloud Wizard Page
291 for (name, cloud) in Kamaki.get_clouds().items():
292 descr = cloud['description'] if 'description' in cloud else ''
293 choices.append((name, descr))
298 return add_cloud(session)
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)
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):
315 raise WizardReloadPage
319 cloud = WizardMenuPage(
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)
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)
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 '')
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]
342 def sysprep_params_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))
351 def sysprep_params_validate(answer):
353 for i in range(len(answer)):
355 value = needed[param_names[i]].type(answer[i])
356 if needed[param_names[i]].validate(value):
357 params[param_names[i]] = value
362 session['dialog'].msgbox("Invalid value for parameter `%s'" %
364 raise WizardReloadPage
367 def sysprep_params_display(params):
368 return ",".join(["%s=%s" % (key, val) for key, val in params.items()])
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
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")]
382 registration = WizardRadioListPage(
383 "ImageRegistration", "Registration Type",
384 "Please provide a registration type:", registration_choices,
385 title="Registration Type", default="Private")
392 if sysprep_params is not None:
393 w.add_page(sysprep_params)
394 w.add_page(registration)
397 create_image(session)
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']
410 with_progress = OutputWthProgress(True)
412 out.add(with_progress)
417 image.os.sysprep_params.update(wizard['SysprepParams'])
418 image.os.do_sysprep()
419 metadata = image.os.meta
422 size = image.shrink()
423 session['shrinked'] = True
424 update_background_title(session)
426 metadata.update(image.meta)
427 metadata['DESCRIPTION'] = wizard['ImageDescription']
431 session['checksum'] = md5.compute(image.device, size)
435 out.output("Uploading image to the cloud:")
436 account = Kamaki.get_account(wizard['Cloud'])
437 assert account, "Cloud: %s is not valid" % wizard['Cloud']
438 kamaki = Kamaki(account, out)
440 name = "%s-%s.diskdump" % (wizard['ImageName'],
441 time.strftime("%Y%m%d%H%M"))
443 with open(image.device, 'rb') as f:
444 pithos_file = kamaki.upload(f, size, name,
445 "(1/3) Calculating block hashes",
446 "(2/3) Uploading missing blocks")
448 out.output("(3/3) Uploading md5sum file ...", False)
449 md5sumstr = '%s %s\n' % (session['checksum'], name)
450 kamaki.upload(StringIO.StringIO(md5sumstr), size=len(md5sumstr),
451 remote_path="%s.%s" % (name, 'md5sum'))
455 is_public = True if wizard['ImageRegistration'] == "Public" else \
457 out.output('Registering %s image with the cloud ...' %
458 wizard['ImageRegistration'].lower(), False)
459 result = kamaki.register(wizard['ImageName'], pithos_file,
462 out.output("Uploading metadata file ...", False)
463 metastring = unicode(json.dumps(result, ensure_ascii=False))
464 kamaki.upload(StringIO.StringIO(metastring), size=len(metastring),
465 remote_path="%s.%s" % (name, 'meta'))
469 out.output("Sharing md5sum file ...", False)
470 kamaki.share("%s.md5sum" % name)
472 out.output("Sharing metadata file ...", False)
473 kamaki.share("%s.meta" % name)
478 except ClientError as e:
479 raise FatalError("Storage service client: %d %s" %
480 (e.status, e.message))
482 out.remove(with_progress)
484 text = "The %s image was successfully uploaded to the storage service " \
485 "and registered with the compute service of %s. Would you like " \
486 "to keep a local copy?" % \
487 (wizard['Cloud'], wizard['ImageRegistration'].lower())
489 if not d.yesno(text, width=PAGE_WIDTH):
490 extract_image(session)
492 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :