Restore html_theme = 'default' in docs/conf.py
[snf-image-creator] / image_creator / dialog_wizard.py
index 51424e3..31aa4a2 100644 (file)
 # interpreted as representing official policies, either expressed
 # or implied, of GRNET S.A.
 
-import dialog
 import time
 import StringIO
 
 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
 
 PAGE_WIDTH = 70
 
@@ -48,11 +48,16 @@ class WizardExit(Exception):
     pass
 
 
+class WizardInvalidData(Exception):
+    pass
+
+
 class Wizard:
     def __init__(self, session):
         self.session = session
         self.pages = []
         self.session['wizard'] = {}
+        self.d = session['dialog']
 
     def add_page(self, page):
         self.pages.append(page)
@@ -64,31 +69,56 @@ class Wizard:
                 idx += self.pages[idx].run(self.session, idx, len(self.pages))
             except WizardExit:
                 return False
+            except WizardInvalidData:
+                continue
 
             if idx >= len(self.pages):
-                break
+                msg = "All necessary information has been gathered:\n\n"
+                for page in self.pages:
+                    msg += " * %s\n" % page.info
+                msg += "\nContinue with the image creation process?"
+
+                ret = self.d.yesno(
+                    msg, 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):
     NEXT = 1
     PREV = -1
 
+    def __init__(self, **kargs):
+        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, index, total):
         raise NotImplementedError
 
 
 class WizardRadioListPage(WizardPage):
 
-    def __init__(self, name, message, choices, **kargs):
+    def __init__(self, name, printable, message, choices, **kargs):
+        super(WizardRadioListPage, self).__init__(**kargs)
         self.name = name
+        self.printable = printable
         self.message = message
         self.choices = choices
         self.title = kargs['title'] if 'title' in kargs else ''
-        self.default = kargs['default'] if 'default' in kargs else 0
+        self.default = kargs['default'] if 'default' in kargs else ""
 
     def run(self, session, index, total):
         d = session['dialog']
@@ -96,116 +126,111 @@ class WizardRadioListPage(WizardPage):
 
         choices = []
         for i in range(len(self.choices)):
-            default = 1 if i == self.default else 0
+            default = 1 if self.choices[i][0] == self.default else 0
             choices.append((self.choices[i][0], self.choices[i][1], default))
 
-        while True:
-            (code, answer) = d.radiolist(self.message, width=PAGE_WIDTH,
-                ok_label="Next", cancel="Back", choices=choices,
-                title="(%d/%d) %s" % (index + 1, total, self.title))
+        (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))
 
-            if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
-                return self.PREV
+        if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
+            return self.PREV
 
-            for i in range(len(choices)):
-                if self.choices[i] == answer:
-                    self.default = i
-                    w[name] = i
-                    break
+        w[self.name] = self.validate(answer)
+        self.default = answer
+        self.info = "%s: %s" % (self.printable, self.display(w[self.name]))
 
-            return self.NEXT
+        return self.NEXT
 
 
 class WizardInputPage(WizardPage):
 
-    def __init__(self, name, message, **kargs):
+    def __init__(self, name, printable, message, **kargs):
+        super(WizardInputPage, self).__init__(**kargs)
         self.name = name
+        self.printable = printable
         self.message = message
         self.title = kargs['title'] if 'title' in kargs else ''
-        self.init_value = kargs['init'] if 'init' in kargs else ''
-        self.allow_empty = kargs['empty'] if 'empty' in kargs else False
+        self.init = kargs['init'] if 'init' in kargs else ''
 
     def run(self, session, index, total):
         d = session['dialog']
         w = session['wizard']
 
-        init = w[self.name] if self.name in w else self.init_value
-        while True:
-            (code, answer) = d.inputbox(self.message, init=init,
-                width=PAGE_WIDTH, ok_label="Next", cancel="Back",
-                title="(%d/%d) %s" % (index + 1, total, self.title))
+        (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))
 
-            if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
-                return self.PREV
+        if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
+            return self.PREV
 
-            value = answer.strip()
-            if len(value) == 0 and self.allow_empty is False:
-                d.msgbox("The value cannot be empty!", width=PAGE_WIDTH)
-                continue
-            w[self.name] = value
-            break
+        value = answer.strip()
+        self.init = value
+        w[self.name] = self.validate(value)
+        self.info = "%s: %s" % (self.printable, self.display(w[self.name]))
 
         return self.NEXT
 
 
-class WizardYesNoPage(WizardPage):
+def wizard(session):
+    init_token = Kamaki.get_token()
+    if init_token is None:
+        init_token = ""
 
-    def __init__(self, message, **kargs):
-        self.message = message
-        self.title = kargs['title'] if 'title' in kargs else ''
+    name = WizardInputPage(
+        "ImageName", "Image Name", "Please provide a name for the image:",
+        title="Image Name", init=session['device'].distro)
 
-    def run(self, session, index, total):
-        d = session['dialog']
+    descr = WizardInputPage(
+        "ImageDescription", "Image Description",
+        "Please provide a description for the image:",
+        title="Image Description", init=session['metadata']['DESCRIPTION'] if
+        'DESCRIPTION' in session['metadata'] else '')
 
-        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))
+    registration = WizardRadioListPage(
+        "ImageRegistration", "Registration Type",
+        "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")
 
-            if ret == d.DIALOG_CANCEL:
-                return self.PREV
-            elif ret == d.DIALOG_EXTRA:
-                raise WizardExit
-            elif ret == d.DIALOG_OK:
-                return self.NEXT
+    def validate_account(token):
+        d = session['dialog']
 
+        if len(token) == 0:
+            d.msgbox("The token cannot be empty", width=PAGE_WIDTH)
+            raise WizardInvalidData
 
-def wizard(session):
+        account = Kamaki.get_account(token)
+        if account is None:
+            d.msgbox("The token you provided in not valid!", width=PAGE_WIDTH)
+            raise WizardInvalidData
 
-    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", empty=True,
-        init=session['metadata']['DESCRIPTION'] if 'DESCRIPTION' in
-        session['metadata'] else '')
-    account = WizardInputPage("account",
-        "Please provide your ~okeanos account e-mail:",
-        title="~okeanos account information", init=Kamaki.get_account())
-    token = WizardInputPage("token",
-        "Please provide your ~okeanos account token:",
-        title="~okeanos account token", init=Kamaki.get_token())
-
-    msg = "All necessary information has been gathered. Confirm and Proceed."
-    proceed = WizardYesNoPage(msg, title="Confirmation")
+        return account
+
+    account = WizardInputPage(
+        "Account", "Account",
+        "Please provide your ~okeanos authentication token:",
+        title="~okeanos account", init=init_token, validate=validate_account,
+        display=lambda account: account['username'])
 
     w = Wizard(session)
 
     w.add_page(name)
     w.add_page(descr)
+    w.add_page(registration)
     w.add_page(account)
-    w.add_page(token)
-    w.add_page(proceed)
 
     if w.run():
-        extract_image(session)
+        create_image(session)
     else:
         return False
 
     return True
 
 
-def extract_image(session):
+def create_image(session):
     d = session['dialog']
     disk = session['disk']
     device = session['device']
@@ -213,6 +238,9 @@ def extract_image(session):
     image_os = session['image_os']
     wizard = session['wizard']
 
+    # Save Kamaki credentials
+    Kamaki.save_token(wizard['Account']['auth_token'])
+
     with_progress = OutputWthProgress(True)
     out = disk.out
     out.add(with_progress)
@@ -227,6 +255,8 @@ def extract_image(session):
 
         #Shrink
         size = device.shrink()
+        session['shrinked'] = True
+        update_background_title(session)
 
         metadata.update(device.meta)
         metadata['DESCRIPTION'] = wizard['ImageDescription']
@@ -243,29 +273,33 @@ def extract_image(session):
         out.output()
         try:
             out.output("Uploading image to pithos:")
-            kamaki = Kamaki(wizard['account'], wizard['token'], out)
+            kamaki = Kamaki(wizard['Account'], out)
 
             name = "%s-%s.diskdump" % (wizard['ImageName'],
                                        time.strftime("%Y%m%d%H%M"))
             pithos_file = ""
             with open(snapshot, 'rb') as f:
                 pithos_file = kamaki.upload(f, size, name,
-                                             "(1/4)  Calculating block hashes",
-                                             "(2/4)  Uploading missing blocks")
+                                            "(1/4)  Calculating block hashes",
+                                            "(2/4)  Uploading missing blocks")
 
-            out.output("(3/4)  Uploading metadata file...", False)
+            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("(4/4)  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()
 
-            out.output('Registring image to ~okeanos...', False)
-            kamaki.register(wizard['ImageName'], pithos_file, metadata)
+            is_public = True if wizard['ImageRegistration'] == "Public" else \
+                False
+            out.output('Registering %s image with ~okeanos ...' %
+                       wizard['ImageRegistration'].lower(), False)
+            kamaki.register(wizard['ImageName'], pithos_file, metadata,
+                            is_public)
             out.success('done')
             out.output()
 
@@ -274,11 +308,10 @@ def extract_image(session):
     finally:
         out.remove(with_progress)
 
-    msg = "The image was successfully uploaded and registered to " \
-          "~okeanos. Would you like to keep a local copy of the image?"
+    msg = "The %s image was successfully uploaded and registered with " \
+          "~okeanos. Would you like to keep a local copy of the image?" \
+          % wizard['ImageRegistration'].lower()
     if not d.yesno(msg, width=PAGE_WIDTH):
-        getattr(__import__("image_creator.dialog_main",
-                fromlist=['image_creator']), "extract_image")(session)
-
+        extract_image(session)
 
 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :