X-Git-Url: https://code.grnet.gr/git/snf-image-creator/blobdiff_plain/37d581b868919aaee6cc8c14aa7e99dccb465a79..81a63274ea28916c52de17038c8c059163892a2e:/image_creator/dialog_wizard.py diff --git a/image_creator/dialog_wizard.py b/image_creator/dialog_wizard.py index 61e7ecd..5605cc7 100644 --- a/image_creator/dialog_wizard.py +++ b/image_creator/dialog_wizard.py @@ -33,7 +33,6 @@ # interpreted as representing official policies, either expressed # or implied, of GRNET S.A. -import dialog import time import StringIO @@ -46,23 +45,34 @@ PAGE_WIDTH = 70 class WizardExit(Exception): + """Exception used to exit the wizard""" pass class WizardInvalidData(Exception): + """Exception triggered when the user provided data are invalid""" 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: @@ -73,29 +83,57 @@ class Wizard: 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): + """Represents a page in a wizard""" 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): + """Display this wizard page + + This function is used by the wizard program when accessing a page. + """ raise NotImplementedError class WizardRadioListPage(WizardPage): - - def __init__(self, name, message, choices, **kargs): + """Represent a Radio List in a wizard""" + 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'] @@ -106,116 +144,94 @@ class WizardRadioListPage(WizardPage): 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, 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.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 - w[self.name] = answer - self.default = answer + 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): + """Represents an input field in a wizard""" + def __init__(self, name, printable, message, **kargs): + super(WizardInputPage, self).__init__(**kargs) self.name = name + self.printable = printable self.message = message + self.info = "%s: " % self.printable 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 - - setattr(self, "validate", validate) def run(self, session, index, total): 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)) + (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() - self.init = value - w[self.name] = self.validate(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 __init__(self, message, **kargs): - self.message = message - self.title = kargs['title'] if 'title' in kargs else '' - - def run(self, session, index, total): - d = session['dialog'] - - 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)) - - if ret == d.DIALOG_CANCEL: - return self.PREV - elif ret == d.DIALOG_EXTRA: - raise WizardExit - elif ret == d.DIALOG_OK: - return self.NEXT - - -def wizard(session): - +def start_wizard(session): + """Run the image creation wizard""" init_token = Kamaki.get_token() if init_token is None: init_token = "" - name = WizardInputPage("ImageName", "Please provide a name for the image:", - title="Image Name", init=session['device'].distro) + name = WizardInputPage( + "ImageName", "Image Name", "Please provide a name for the image:", + title="Image Name", init=session['image'].distro) + descr = WizardInputPage( - "ImageDescription", "Please provide a description for the image:", + "ImageDescription", "Image Description", + "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:", + "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") def validate_account(token): + """Check if a token is valid""" + d = session['dialog'] + if len(token) == 0: d.msgbox("The token cannot be empty", width=PAGE_WIDTH) raise WizardInvalidData account = Kamaki.get_account(token) if account is None: - session['dialog'].msgbox("The token you provided in not valid!", - width=PAGE_WIDTH) + d.msgbox("The token you provided in not valid!", width=PAGE_WIDTH) raise WizardInvalidData return account account = WizardInputPage( - "account", "Please provide your ~okeanos authentication token:", - title="~okeanos account", init=init_token, validate=validate_account) - - msg = "All necessary information has been gathered. Confirm and Proceed." - proceed = WizardYesNoPage(msg, title="Confirmation") + "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) @@ -223,7 +239,6 @@ def wizard(session): w.add_page(descr) w.add_page(registration) w.add_page(account) - w.add_page(proceed) if w.run(): create_image(session) @@ -234,39 +249,37 @@ 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']) + 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() + image.mount(False) + image.os.do_sysprep() + metadata = image.os.meta + image.umount() #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) + session['checksum'] = md5.compute(image.device, size) #Metadata metastring = '\n'.join( @@ -276,12 +289,12 @@ def create_image(session): out.output() try: out.output("Uploading image to pithos:") - kamaki = Kamaki(wizard['account'], 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: + with open(image.device, 'rb') as f: pithos_file = kamaki.upload(f, size, name, "(1/4) Calculating block hashes", "(2/4) Uploading missing blocks") @@ -297,9 +310,10 @@ def create_image(session): out.success('done') out.output() - is_public = True if w['ImageRegistration'] == "Public" else False + is_public = True if wizard['ImageRegistration'] == "Public" else \ + False out.output('Registering %s image with ~okeanos ...' % - w['ImageRegistration'].lower(), False) + wizard['ImageRegistration'].lower(), False) kamaki.register(wizard['ImageName'], pithos_file, metadata, is_public) out.success('done') @@ -312,7 +326,7 @@ def create_image(session): 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() + % wizard['ImageRegistration'].lower() if not d.yesno(msg, width=PAGE_WIDTH): extract_image(session)