Fix a KeyError bug in dialog_wizard.py
[snf-image-creator] / image_creator / dialog_wizard.py
index 61e7ecd..ffabecf 100644 (file)
@@ -1,5 +1,5 @@
-#!/usr/bin/env python
-
+# -*- coding: utf-8 -*-
+#
 # Copyright 2012 GRNET S.A. All rights reserved.
 #
 # Redistribution and use in source and binary forms, with or
 # interpreted as representing official policies, either expressed
 # or implied, of GRNET S.A.
 
-import dialog
+"""This module implements the "wizard" mode of the dialog-based version of
+snf-image-creator.
+"""
+
 import time
 import StringIO
+import json
 
 from image_creator.kamaki_wrapper import Kamaki, ClientError
 from image_creator.util import MD5, FatalError
 from image_creator.output.cli import OutputWthProgress
-from image_creator.dialog_util import extract_image, update_background_title
+from image_creator.dialog_util import extract_image, update_background_title, \
+    add_cloud, edit_cloud
 
 PAGE_WIDTH = 70
+PAGE_HEIGHT = 10
+SYSPREP_PARAM_MAXLEN = 20
 
 
 class WizardExit(Exception):
+    """Exception used to exit the wizard"""
     pass
 
 
-class WizardInvalidData(Exception):
+class WizardReloadPage(Exception):
+    """Exception that reloads the last WizardPage"""
     pass
 
 
 class Wizard:
+    """Represents a dialog-based wizard
+
+    The wizard is a collection of pages that have a "Next" and a "Back" button
+    on them. The pages are used to collect user data.
+    """
+
     def __init__(self, session):
         self.session = session
         self.pages = []
         self.session['wizard'] = {}
+        self.d = session['dialog']
 
     def add_page(self, page):
+        """Add a new page to the wizard"""
         self.pages.append(page)
 
     def run(self):
+        """Run the wizard"""
         idx = 0
         while True:
             try:
-                idx += self.pages[idx].run(self.session, idx, len(self.pages))
+                total = len(self.pages)
+                title = "(%d/%d) %s" % (idx + 1, total, self.pages[idx].title)
+                idx += self.pages[idx].run(self.session, title)
             except WizardExit:
                 return False
-            except WizardInvalidData:
+            except WizardReloadPage:
                 continue
 
             if idx >= len(self.pages):
-                break
+                text = "All necessary information has been gathered:\n\n"
+                for page in self.pages:
+                    text += " * %s\n" % page.info
+                text += "\nContinue with the image creation process?"
+
+                ret = self.d.yesno(
+                    text, width=PAGE_WIDTH, height=8 + len(self.pages),
+                    ok_label="Yes", cancel="Back", extra_button=1,
+                    extra_label="Quit", title="Confirmation")
+
+                if ret == self.d.DIALOG_CANCEL:
+                    idx -= 1
+                elif ret == self.d.DIALOG_EXTRA:
+                    return False
+                elif ret == self.d.DIALOG_OK:
+                    return True
 
             if idx < 0:
                 return False
-        return True
 
 
-class WizardPage:
+class WizardPage(object):
+    """Represents a page in a wizard"""
     NEXT = 1
     PREV = -1
 
-    def run(self, session, index, total):
+    def __init__(self, name, display_name, text, **kargs):
+        self.name = name
+        self.display_name = display_name
+        self.text = text
+
+        self.title = kargs['title'] if 'title' in kargs else ""
+        self.default = kargs['default'] if 'default' in kargs else ""
+        self.extra = kargs['extra'] if 'extra' in kargs else None
+        self.extra_label = \
+            kargs['extra_label'] if 'extra_label' in kargs else 'Extra'
+
+        self.info = "%s: <none>" % self.display_name
+
+        validate = kargs['validate'] if 'validate' in kargs else lambda x: x
+        setattr(self, "validate", validate)
+
+        display = kargs['display'] if 'display' in kargs else lambda x: x
+        setattr(self, "display", display)
+
+    def run(self, session, title):
+        """Display this wizard page
+
+        This function is used by the wizard program when accessing a page.
+        """
         raise NotImplementedError
 
 
-class WizardRadioListPage(WizardPage):
+class WizardPageWthChoices(WizardPage):
+    """Represents a Wizard Page that allows the user to select something from
+    a list of choices.
 
-    def __init__(self, name, message, choices, **kargs):
-        self.name = name
-        self.message = message
+    The available choices are created by a function passed to the class through
+    the choices variable. If the choices function returns an empty list, a
+    fallback funtion is executed if available.
+    """
+    def __init__(self, name, display_name, text, choices, **kargs):
+        super(WizardPageWthChoices, self).__init__(name, display_name, text,
+                                                   **kargs)
         self.choices = choices
-        self.title = kargs['title'] if 'title' in kargs else ''
-        self.default = kargs['default'] if 'default' in kargs else 0
+        self.fallback = kargs['fallback'] if 'fallback' in kargs else None
+
 
-    def run(self, session, index, total):
+class WizardRadioListPage(WizardPageWthChoices):
+    """Represent a Radio List in a wizard"""
+
+    def run(self, session, title):
         d = session['dialog']
         w = session['wizard']
 
         choices = []
-        for i in range(len(self.choices)):
-            default = 1 if self.choices[i][0] == self.default else 0
-            choices.append((self.choices[i][0], self.choices[i][1], default))
+        for choice in self.choices():
+            default = 1 if choice[0] == self.default else 0
+            choices.append((choice[0], choice[1], default))
 
-        while True:
-            (code, answer) = \
-                d.radiolist(self.message, height=10, width=PAGE_WIDTH,
-                            ok_label="Next", cancel="Back", choices=choices,
-                            title="(%d/%d) %s" % (index + 1, total, self.title)
-                            )
+        (code, answer) = d.radiolist(
+            self.text, width=PAGE_WIDTH, ok_label="Next", cancel="Back",
+            choices=choices, height=PAGE_HEIGHT, title=title)
 
-            if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
-                return self.PREV
+        if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
+            return self.PREV
 
-            w[self.name] = answer
-            self.default = answer
+        w[self.name] = self.validate(answer)
+        self.default = answer
+        self.info = "%s: %s" % (self.display_name, self.display(w[self.name]))
 
-            return self.NEXT
+        return self.NEXT
 
 
 class WizardInputPage(WizardPage):
+    """Represents an input field in a wizard"""
 
-    def __init__(self, name, message, **kargs):
-        self.name = name
-        self.message = message
-        self.title = kargs['title'] if 'title' in kargs else ''
-        self.init = kargs['init'] if 'init' in kargs else ''
-        if 'validate' in kargs:
-            validate = kargs['validate']
-        else:
-            validate = lambda x: x
+    def run(self, session, title):
+        d = session['dialog']
+        w = session['wizard']
 
-        setattr(self, "validate", validate)
+        (code, answer) = d.inputbox(
+            self.text, init=self.default, width=PAGE_WIDTH, ok_label="Next",
+            cancel="Back", height=PAGE_HEIGHT, title=title)
+
+        if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
+            return self.PREV
 
-    def run(self, session, index, total):
+        value = answer.strip()
+        self.default = value
+        w[self.name] = self.validate(value)
+        self.info = "%s: %s" % (self.display_name, self.display(w[self.name]))
+
+        return self.NEXT
+
+
+class WizardFormPage(WizardPage):
+    """Represents a Form in a wizard"""
+
+    def __init__(self, name, display_name, text, fields, **kargs):
+        super(WizardFormPage, self).__init__(name, display_name, text, **kargs)
+        self.fields = fields
+
+    def run(self, session, title):
         d = session['dialog']
         w = session['wizard']
 
-        while True:
-            (code, answer) = \
-                d.inputbox(self.message, init=self.init,
-                           width=PAGE_WIDTH, ok_label="Next", cancel="Back",
-                           title="(%d/%d) %s" % (index + 1, total, self.title))
+        field_lenght = len(self.fields())
+        form_height = field_lenght if field_lenght < PAGE_HEIGHT - 4 \
+            else PAGE_HEIGHT - 4
 
-            if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
-                return self.PREV
+        (code, output) = d.form(
+            self.text, width=PAGE_WIDTH, height=PAGE_HEIGHT,
+            form_height=form_height, ok_label="Next", cancel="Back",
+            fields=self.fields(), title=title)
 
-            value = answer.strip()
-            self.init = value
-            w[self.name] = self.validate(value)
-            break
+        if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
+            return self.PREV
 
-        return self.NEXT
+        w[self.name] = self.validate(output)
+        self.default = output
+        self.info = "%s: %s" % (self.display_name, self.display(w[self.name]))
 
+        return self.NEXT
 
-class WizardYesNoPage(WizardPage):
 
-    def __init__(self, message, **kargs):
-        self.message = message
-        self.title = kargs['title'] if 'title' in kargs else ''
+class WizardMenuPage(WizardPageWthChoices):
+    """Represents a menu dialog with available choices in a wizard"""
 
-    def run(self, session, index, total):
+    def run(self, session, title):
         d = session['dialog']
+        w = session['wizard']
 
-        while True:
-            ret = d.yesno(self.message, width=PAGE_WIDTH, ok_label="Yes",
-                          cancel="Back", extra_button=1, extra_label="Quit",
-                          title="(%d/%d) %s" % (index + 1, total, self.title))
+        extra_button = 1 if self.extra else 0
+
+        choices = self.choices()
 
-            if ret == d.DIALOG_CANCEL:
+        if len(choices) == 0:
+            assert self.fallback, "Zero choices and no fallback"
+            if self.fallback():
+                raise WizardReloadPage
+            else:
                 return self.PREV
-            elif ret == d.DIALOG_EXTRA:
-                raise WizardExit
-            elif ret == d.DIALOG_OK:
-                return self.NEXT
 
+        default_item = self.default if self.default else choices[0][0]
 
-def wizard(session):
+        (code, choice) = d.menu(
+            self.text, width=PAGE_WIDTH, ok_label="Next", cancel="Back",
+            title=title, choices=choices, height=PAGE_HEIGHT,
+            default_item=default_item, extra_label=self.extra_label,
+            extra_button=extra_button)
 
-    init_token = Kamaki.get_token()
-    if init_token is None:
-        init_token = ""
+        if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
+            return self.PREV
+        elif code == d.DIALOG_EXTRA:
+            self.extra()
+            raise WizardReloadPage
 
-    name = WizardInputPage("ImageName", "Please provide a name for the image:",
-                           title="Image Name", init=session['device'].distro)
-    descr = WizardInputPage(
-        "ImageDescription", "Please provide a description for the image:",
-        title="Image Description", init=session['metadata']['DESCRIPTION'] if
-        'DESCRIPTION' in session['metadata'] else '')
-    registration = WizardRadioListPage(
-        "ImageRegistration", "Please provide a registration type:",
-        [("Private", "Image is accessible only by this user"),
-         ("Public", "Everyone can create VMs from this image")],
-        title="Registration Type", default="Private")
+        self.default = choice
+        w[self.name] = self.validate(choice)
+        self.info = "%s: %s" % (self.display_name, self.display(w[self.name]))
+
+        return self.NEXT
+
+
+def start_wizard(session):
+    """Run the image creation wizard"""
+
+    image = session['image']
+    distro = image.distro
+    ostype = image.ostype
+
+    # Create Cloud Wizard Page
+    def cloud_choices():
+        choices = []
+        for (name, cloud) in Kamaki.get_clouds().items():
+            descr = cloud['description'] if 'description' in cloud else ''
+            choices.append((name, descr))
+
+        return choices
+
+    def cloud_add():
+        return add_cloud(session)
+
+    def cloud_none_available():
+        if not session['dialog'].yesno(
+                "No available clouds found. Would you like to add one now?",
+                width=PAGE_WIDTH, defaultno=0):
+            return add_cloud(session)
+        return False
 
-    def validate_account(token):
-        if len(token) == 0:
-            d.msgbox("The token cannot be empty", width=PAGE_WIDTH)
-            raise WizardInvalidData
+    def cloud_validate(cloud):
+        if not Kamaki.get_account(cloud):
+            if not session['dialog'].yesno(
+                    "The cloud you have selected is not valid! Would you "
+                    "like to edit it now?", width=PAGE_WIDTH, defaultno=0):
+                if edit_cloud(session, cloud):
+                    return cloud
 
-        account = Kamaki.get_account(token)
-        if account is None:
-            session['dialog'].msgbox("The token you provided in not valid!",
-                                     width=PAGE_WIDTH)
-            raise WizardInvalidData
+            raise WizardReloadPage
 
-        return account
+        return cloud
 
-    account = WizardInputPage(
-        "account", "Please provide your ~okeanos authentication token:",
-        title="~okeanos account", init=init_token, validate=validate_account)
+    cloud = WizardMenuPage(
+        "Cloud", "Cloud",
+        "Please select a cloud account or press <Add> to add a new one:",
+        choices=cloud_choices, extra_label="Add", extra=cloud_add,
+        title="Clouds", validate=cloud_validate, fallback=cloud_none_available)
 
-    msg = "All necessary information has been gathered. Confirm and Proceed."
-    proceed = WizardYesNoPage(msg, title="Confirmation")
+    # Create Image Name Wizard Page
+    name = WizardInputPage(
+        "ImageName", "Image Name", "Please provide a name for the image:",
+        title="Image Name", default=ostype if distro == "unknown" else distro)
+
+    # Create Image Description Wizard Page
+    descr = WizardInputPage(
+        "ImageDescription", "Image Description",
+        "Please provide a description for the image:",
+        title="Image Description", default=session['metadata']['DESCRIPTION']
+        if 'DESCRIPTION' in session['metadata'] else '')
+
+    # Create Sysprep Params Wizard Page
+    needed = image.os.needed_sysprep_params
+    # Only show the parameters that don't have default values
+    param_names = [param for param in needed if needed[param].default is None]
+
+    def sysprep_params_fields():
+        fields = []
+        available = image.os.sysprep_params
+        for name in param_names:
+            text = needed[name].description
+            default = str(available[name]) if name in available else ""
+            fields.append(("%s: " % text, default, SYSPREP_PARAM_MAXLEN))
+        return fields
+
+    def sysprep_params_validate(answer):
+        params = {}
+        for i in range(len(answer)):
+            try:
+                value = needed[param_names[i]].type(answer[i])
+                if needed[param_names[i]].validate(value):
+                    params[param_names[i]] = value
+                    continue
+            except ValueError:
+                pass
+
+            session['dialog'].msgbox("Invalid value for parameter `%s'" %
+                                     param_names[i])
+            raise WizardReloadPage
+        return params
+
+    def sysprep_params_display(params):
+        return ",".join(["%s=%s" % (key, val) for key, val in params.items()])
+
+    sysprep_params = WizardFormPage(
+        "SysprepParams", "Sysprep Parameters",
+        "Prease fill in the following system preparation parameters:",
+        title="System Preparation Parameters", fields=sysprep_params_fields,
+        display=sysprep_params_display, validate=sysprep_params_validate
+    ) if len(needed) != 0 else None
+
+    # Create Image Registration Wizard Page
+    def registration_choices():
+        return [("Private", "Image is accessible only by this user"),
+                ("Public", "Everyone can create VMs from this image")]
+
+    registration = WizardRadioListPage(
+        "ImageRegistration", "Registration Type",
+        "Please provide a registration type:", registration_choices,
+        title="Registration Type", default="Private")
 
     w = Wizard(session)
 
+    w.add_page(cloud)
     w.add_page(name)
     w.add_page(descr)
+    if sysprep_params is not None:
+        w.add_page(sysprep_params)
     w.add_page(registration)
-    w.add_page(account)
-    w.add_page(proceed)
 
     if w.run():
         create_image(session)
@@ -234,86 +402,92 @@ def wizard(session):
 
 
 def create_image(session):
+    """Create an image using the information collected by the wizard"""
     d = session['dialog']
-    disk = session['disk']
-    device = session['device']
-    snapshot = session['snapshot']
-    image_os = session['image_os']
+    image = session['image']
     wizard = session['wizard']
 
-    # Save Kamaki credentials
-    Kamaki.save_token(wizard['account']['auth_token'])
-
     with_progress = OutputWthProgress(True)
-    out = disk.out
+    out = image.out
     out.add(with_progress)
     try:
         out.clear()
 
         #Sysprep
-        device.mount(False)
-        image_os.do_sysprep()
-        metadata = image_os.meta
-        device.umount()
+        if 'SysprepParams' in wizard:
+            image.os.sysprep_params.update(wizard['SysprepParams'])
+        image.os.do_sysprep()
+        metadata = image.os.meta
 
         #Shrink
-        size = device.shrink()
+        size = image.shrink()
         session['shrinked'] = True
         update_background_title(session)
 
-        metadata.update(device.meta)
+        metadata.update(image.meta)
         metadata['DESCRIPTION'] = wizard['ImageDescription']
 
         #MD5
         md5 = MD5(out)
-        session['checksum'] = md5.compute(snapshot, size)
-
-        #Metadata
-        metastring = '\n'.join(
-            ['%s=%s' % (key, value) for (key, value) in metadata.items()])
-        metastring += '\n'
+        session['checksum'] = md5.compute(image.device, size)
 
         out.output()
         try:
-            out.output("Uploading image to pithos:")
-            kamaki = Kamaki(wizard['account'], out)
+            out.output("Uploading image to the cloud:")
+            account = Kamaki.get_account(wizard['Cloud'])
+            assert account, "Cloud: %s is not valid" % wizard['Cloud']
+            kamaki = Kamaki(account, out)
 
             name = "%s-%s.diskdump" % (wizard['ImageName'],
                                        time.strftime("%Y%m%d%H%M"))
             pithos_file = ""
-            with open(snapshot, 'rb') as f:
+            with open(image.device, 'rb') as f:
                 pithos_file = kamaki.upload(f, size, name,
-                                            "(1/4)  Calculating block hashes",
-                                            "(2/4)  Uploading missing blocks")
+                                            "(1/3)  Calculating block hashes",
+                                            "(2/3)  Uploading missing blocks")
 
-            out.output("(3/4)  Uploading metadata file ...", False)
-            kamaki.upload(StringIO.StringIO(metastring), size=len(metastring),
-                          remote_path="%s.%s" % (name, 'meta'))
-            out.success('done')
-            out.output("(4/4)  Uploading md5sum file ...", False)
+            out.output("(3/3)  Uploading md5sum file ...", False)
             md5sumstr = '%s %s\n' % (session['checksum'], name)
             kamaki.upload(StringIO.StringIO(md5sumstr), size=len(md5sumstr),
                           remote_path="%s.%s" % (name, 'md5sum'))
             out.success('done')
             out.output()
 
-            is_public = True if w['ImageRegistration'] == "Public" else False
-            out.output('Registering %s image with ~okeanos ...' %
-                       w['ImageRegistration'].lower(), False)
-            kamaki.register(wizard['ImageName'], pithos_file, metadata,
-                            is_public)
+            is_public = True if wizard['ImageRegistration'] == "Public" else \
+                False
+            out.output('Registering %s image with the cloud ...' %
+                       wizard['ImageRegistration'].lower(), False)
+            result = kamaki.register(wizard['ImageName'], pithos_file,
+                                     metadata, is_public)
+            out.success('done')
+            out.output("Uploading metadata file ...", False)
+            metastring = unicode(json.dumps(result, ensure_ascii=False))
+            kamaki.upload(StringIO.StringIO(metastring), size=len(metastring),
+                          remote_path="%s.%s" % (name, 'meta'))
             out.success('done')
+
+            if is_public:
+                out.output("Sharing md5sum file ...", False)
+                kamaki.share("%s.md5sum" % name)
+                out.success('done')
+                out.output("Sharing metadata file ...", False)
+                kamaki.share("%s.meta" % name)
+                out.success('done')
+
             out.output()
 
         except ClientError as e:
-            raise FatalError("Pithos client: %d %s" % (e.status, e.message))
+            raise FatalError("Storage service client: %d %s" %
+                             (e.status, e.message))
     finally:
         out.remove(with_progress)
 
-    msg = "The %s image was successfully uploaded and registered with " \
-          "~okeanos. Would you like to keep a local copy of the image?" \
-          % w['ImageRegistration'].lower()
-    if not d.yesno(msg, width=PAGE_WIDTH):
+    text = "The %s image was successfully uploaded to the storage service " \
+           "and registered with the compute service of %s. Would you like " \
+           "to keep a local copy?" % \
+           (wizard['Cloud'], wizard['ImageRegistration'].lower())
+
+    if not d.yesno(text, width=PAGE_WIDTH):
         extract_image(session)
 
 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :