Newer
Older
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""This module implements the "wizard" mode of the dialog-based version of
snf-image-creator.
"""
from image_creator.kamaki_wrapper import Kamaki, ClientError, CONTAINER
from image_creator.util import FatalError, virtio_versions
from image_creator.output.cli import OutputWthProgress
from image_creator.dialog_util import extract_image, update_background_title, \
add_cloud, edit_cloud, update_sysprep_param, create_form_elements
class WizardExit(Exception):
class WizardReloadPage(Exception):
"""Exception that reloads the last WizardPage"""
"""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, dialog):
"""Initialize the Wizard"""
self._pages = []
self.dialog = dialog
total = len(self._pages)
title = "(%d/%d) %s" % (idx + 1, total, self._pages[idx].title)
idx += self._pages[idx].show(self.dialog, title)
except WizardExit:
return False
text = "All necessary information has been gathered:\n\n"
for page in self._pages:
text += " * %s\n" % page
text += "\nContinue with the image creation process?"
ret = self.dialog.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.dialog.CANCEL:
elif ret == self.dialog.EXTRA:
elif ret == self.dialog.OK:
@property
def answers(self):
"""Returns the answers the user provided"""
return dict((page.name, page.answer) for page in self._pages)
class WizardPage(object):
self.print_name = kwargs['print_name'] if 'print_name' in kwargs \
else " ".join(re.findall('[A-Z][^A-Z]*', name))
self.title = kwargs['title'] if 'title' in kwargs else self.print_name
self.default = kwargs['default'] if 'default' in kwargs else ""
self.extra = kwargs['extra'] if 'extra' in kwargs else None
kwargs['validate'] if 'validate' in kwargs else lambda x: x
self.display = \
kwargs['display'] if 'display' in kwargs else lambda x: x
self.dargs = {}
self.dargs['ok_label'] = 'Next'
self.dargs['cancel'] = 'Back'
self.dargs['width'] = PAGE_WIDTH
self.dargs['height'] = PAGE_HEIGHT
self.extra_label = kwargs['extra_label'] if 'extra_label' in kwargs \
def __str__(self):
"""Prints the answer"""
return "%s: %s" % (self.print_name, self.display(self.answer))
"""Display this wizard page
This function is used by the wizard program when accessing a page.
"""
class WizardInputPage(WizardPage):
"""Represents an input field in a wizard"""
def show(self, dialog, title):
"""Display this wizard page"""
(code, answer) = dialog.inputbox(self.text(), init=self.default,
title=title,
extra_label=self.extra_label(),
**self.dargs)
if code in (dialog.CANCEL, dialog.ESC):
return self.PREV
self.answer = self.validate(answer.strip())
self.default = self.answer
return self.NEXT
class WizardInfoPage(WizardPage):
"""Represents a Wizard Page that just displays some user-defined
information.
The user-defined information is created by the info function.
def __init__(self, name, text, info, **kwargs):
"""Initialize the WizardInfoPage instance"""
super(WizardInfoPage, self).__init__(name, text, **kwargs)
def show(self, dialog, title):
"""Display this wizard page"""
text = "%s\n\n%s" % (self.text(), self.info())
ret = dialog.yesno(text, title=title, extra_label=self.extra_label(),
**self.dargs)
if ret in (dialog.CANCEL, dialog.ESC):
elif ret == dialog.EXTRA:
self.extra()
raise WizardReloadPage
self.answer = self.validate(None)
return self.NEXT
class WizardFormPage(WizardPage):
"""Represents a Form in a wizard"""
def __init__(self, name, text, fields, **kwargs):
"""Initialize the WizardFormPage instance"""
super(WizardFormPage, self).__init__(name, text, **kwargs)
self.fields = fields
def show(self, dialog, title):
"""Display this wizard page"""
field_lenght = len(self.fields())
form_height = field_lenght if field_lenght < PAGE_HEIGHT - 4 \
else PAGE_HEIGHT - 4
(code, output) = dialog.form(self.text(),
create_form_elements(self.fields(),
self.dargs['width']),
form_height=form_height, title=title,
extra_label=self.extra_label(),
default_item=self.default, **self.dargs)
if code in (dialog.CANCEL, dialog.ESC):
return self.PREV
self.answer = self.validate(output)
self.default = output
class WizardPageWthChoices(WizardPage):
"""Represents a Wizard Page that allows the user to select something from
a list of choices.
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 function is executed if available.
def __init__(self, name, text, choices, **kwargs):
"""Initialize the WizardPageWthChoices instance"""
super(WizardPageWthChoices, self).__init__(name, text, **kwargs)
self.fallback = kwargs['fallback'] if 'fallback' in kwargs else None
class WizardRadioListPage(WizardPageWthChoices):
"""Represent a Radio List in a wizard"""
def show(self, dialog, title):
"""Display this wizard page"""
for choice in self.choices():
default = 1 if choice[0] == self.default else 0
choices.append((choice[0], choice[1], default))
(code, answer) = dialog.radiolist(self.text(), choices=choices,
extra_label=self.extra_label(),
if code in (dialog.CANCEL, dialog.ESC):
self.default = answer
class WizardMenuPage(WizardPageWthChoices):
"""Represents a menu dialog with available choices in a wizard"""
def show(self, dialog, title):
"""Display this wizard page"""
choices = self.choices()
if len(choices) == 0:
assert self.fallback, "Zero choices and no fallback"
if self.fallback():
raise WizardReloadPage
else:
return self.PREV
default_item = self.default if self.default else choices[0][0]
(code, choice) = dialog.menu(self.text(), title=title, choices=choices,
extra_label=self.extra_label(),
if code in (dialog.CANCEL, dialog.ESC):
elif code == dialog.EXTRA:
self.extra()
raise WizardReloadPage
self.default = choice
return self.NEXT
def start_wizard(session):
"""Run the image creation wizard"""
metadata = session['image'].meta
distro = session['image'].distro
ostype = session['image'].ostype
choices = []
for (name, cloud) in Kamaki.get_clouds().items():
descr = cloud['description'] if 'description' in cloud else ''
choices.append((name, descr))
return choices
def no_clouds():
"""Fallback function when no cloud account exists"""
if session['dialog'].yesno(
"No available clouds found. Would you like to add one now?",
width=PAGE_WIDTH, defaultno=0) == session['dialog'].OK:
return add_cloud(session)
return False
def cloud_validate(cloud):
if session['dialog'].yesno(
"The cloud you have selected is not valid! Would you "
"like to edit it now?", width=PAGE_WIDTH,
defaultno=0) == session['dialog'].OK:
if edit_cloud(session, cloud):
return cloud
return cloud
cloud = WizardMenuPage(
"Please select a cloud account or press <Add> to add a new one:",
cloud_choices, extra_label=lambda: "Add",
extra=lambda: add_cloud(session), title="Clouds",
validate=cloud_validate, fallback=no_clouds)
name = WizardInputPage("ImageName", lambda:
"Please provide a name for the image:",
default=ostype if distro == "unknown" else distro)
# Create Image Description Wizard Page
"ImageDescription", lambda:
"Please provide a description for the image:",
default=metadata['DESCRIPTION'] if 'DESCRIPTION' in metadata else '')
# Create VirtIO Installation Page
def display_installed_drivers():
versions = virtio_versions(image.os.virtio_state)
ret = "Installed Block Device Driver: %(netkvm)s\n" \
"Installed Network Device Driver: %(viostor)s\n" % versions
virtio = image.os.sysprep_params['virtio'].value
if virtio:
ret += "\nBlock Device Driver to be installed: %(netkvm)s\n" \
"Network Device Driver to be installed: %(viostor)s\n" % \
virtio_versions(image.os.compute_virtio_state(virtio))
return ret
def validate_virtio(_):
"""Checks the state of the VirtIO drivers"""
image = session['image']
netkvm = len(image.os.virtio_state['netkvm']) != 0
viostor = len(image.os.virtio_state['viostor']) != 0
drv_dir = image.os.sysprep_params['virtio'].value
if netkvm is False or viostor is False:
new = image.os.compute_virtio_state(drv_dir) if drv_dir else None
new_viostor = len(new['viostor']) != 0 if new else False
new_netkvm = len(new['netkvm']) != 0 if new else False
title = "VirtIO driver missing"
msg = "Image creation cannot proceed unless a VirtIO %s driver " \
"is installed on the media!"
if not (viostor or new_viostor):
dialog.msgbox(msg % "Block Device", width=PAGE_WIDTH,
height=PAGE_HEIGHT, title=title)
raise WizardReloadPage
if not(netkvm or new_netkvm):
dialog.msgbox(msg % "Network Device", width=PAGE_WIDTH,
height=PAGE_HEIGHT, title=title)
raise WizardReloadPage
return drv_dir
def virtio_text():
if not session['image'].os.sysprep_params['virtio'].value:
return "Press <New> to update the image's VirtIO drivers."
else:
return "Press <Revert> to revert to the old state."
def virtio_extra():
if not session['image'].os.sysprep_params['virtio'].value:
title = "Please select a directory that hosts VirtIO drivers."
update_sysprep_param(session, 'virtio', title=title)
else:
session['image'].os.sysprep_params['virtio'].value = ""
def virtio_extra_label():
if not session['image'].os.sysprep_params['virtio'].value:
return "New"
else:
return "Revert"
"virtio", virtio_text, display_installed_drivers,
title="VirtIO Drivers", extra_label=virtio_extra_label,
extra=virtio_extra, validate=validate_virtio,
print_name="VirtIO Drivers Path")
# Create Image Registration Wizard Page
"""Choices for the registration wizard page"""
return [("Private", "Image is accessible only by this user"),
("Public", "Everyone can create VMs from this image")]
registration = WizardRadioListPage("RegistrationType", lambda:
"Please provide a registration type:",
registration_choices, default="Private")
wizard.add_page(cloud)
wizard.add_page(name)
wizard.add_page(descr)
if hasattr(session['image'].os, 'install_virtio_drivers'):
wizard.add_page(virtio)
wizard.add_page(registration)
else:
return False
return True
"""Create an image using the information collected by the wizard"""
with_progress = OutputWthProgress()
image.out.append(with_progress)
if 'virtio' in answers and image.os.sysprep_params['virtio'].value:
image.os.install_virtio_drivers()
image.os.do_sysprep()
metadata = image.os.meta
metadata['DESCRIPTION'] = answers['ImageDescription']
session['checksum'] = image.md5()
image.out.info("Uploading image to the cloud:")
account = Kamaki.get_account(answers['Cloud'])
assert account, "Cloud: %s is not valid" % answers['Cloud']
kamaki = Kamaki(account, image.out)
name = "%s-%s.diskdump" % (answers['ImageName'],
time.strftime("%Y%m%d%H%M"))
with image.raw_device() as raw:
with open(raw, 'rb') as device:
remote = kamaki.upload(device, image.size, name, CONTAINER,
"(1/3) Calculating block hashes",
"(2/3) Uploading image blocks")
image.out.info("(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'),
container=CONTAINER, content_type="text/plain")
image.out.info('Registering %s image with the cloud ...' %
answers['RegistrationType'].lower(), False)
result = kamaki.register(answers['ImageName'], remote, metadata,
answers['RegistrationType'] == "Public")
image.out.success('done')
image.out.info("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'),
container=CONTAINER, content_type="application/json")
if answers['RegistrationType'] == "Public":
image.out.info("Sharing md5sum file ...", False)
kamaki.share("%s.md5sum" % name)
image.out.info("Sharing metadata file ...", False)
kamaki.share("%s.meta" % name)
raise FatalError("Storage service client: %d %s" %
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?" % \
(answers['RegistrationType'].lower(), answers['Cloud'])
if session['dialog'].yesno(text, width=PAGE_WIDTH) == session['dialog'].OK:
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :