Exemple #1
0
def modify_clouds(session):
    """Modify existing cloud accounts"""
    d = session['dialog']

    while 1:
        clouds = Kamaki.get_clouds()
        if not len(clouds):
            if not add_cloud(session):
                break
            continue

        choices = []
        for (name, cloud) in clouds.items():
            descr = cloud['description'] if 'description' in cloud else ''
            choices.append((name, descr))

        (code, choice) = d.menu(
            "In this menu you can edit existing cloud accounts or add new "
            " ones. Press <Edit> to edit an existing account or <Add> to add "
            " a new one. Press <Back> or hit <ESC> when done.", height=18,
            width=WIDTH, choices=choices, menu_height=10, ok_label="Edit",
            extra_button=1, extra_label="Add", cancel="Back", title="Clouds")

        if code in (d.CANCEL, d.ESC):
            return True
        elif code == d.OK:  # Edit button
            edit_cloud(session, choice)
        elif code == d.EXTRA:  # Add button
            add_cloud(session)
 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 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
Exemple #4
0
def modify_clouds(session):
    """Modify existing cloud accounts"""
    d = session['dialog']

    while 1:
        clouds = Kamaki.get_clouds()
        if not len(clouds):
            if not add_cloud(session):
                break
            else:
                # Select the newly added cloud
                session['current_cloud'] = Kamaki.get_clouds().keys()[0]
            continue

        choices = []
        for (name, cloud) in clouds.items():
            descr = cloud['description'] if 'description' in cloud else ''
            choices.append((name, descr))

        (code, choice) = d.menu(
            "In this menu you can edit existing cloud accounts or add new "
            " ones. Press <Edit> to edit an existing account or <Add> to add "
            " a new one. Press <Back> or hit <ESC> when done.",
            height=18,
            width=WIDTH,
            choices=choices,
            menu_height=10,
            ok_label="Edit",
            extra_button=1,
            extra_label="Add",
            cancel="Back",
            title="Clouds")

        if code in (d.CANCEL, d.ESC):
            return True
        elif code == d.OK:  # Edit button
            edit_cloud(session, choice)
        elif code == d.EXTRA:  # Add button
            add_cloud(session)
def start_wizard(session):
    """Run the image creation wizard"""

    metadata = session['image'].meta
    distro = session['image'].distro
    ostype = session['image'].ostype

    # Create Cloud Wizard Page
    def cloud_choices():
        """Returns the available clouds"""
        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):
        """Checks if a cloud is valid"""
        if not Kamaki.get_account(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
            raise WizardReloadPage
        return cloud

    cloud = WizardMenuPage(
        "Cloud", lambda:
        "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)

    # Create Image Name Wizard Page
    name = WizardInputPage("ImageName", lambda:
                           "Please provide a name for the image:",
                           default=ostype if distro == "unknown" else distro)

    # Create Image Description Wizard Page
    descr = WizardInputPage(
        "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():
        """Returns the installed VirtIO drivers"""
        image = session['image']
        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

            dialog = session['dialog']
            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 = WizardInfoPage(
        "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
    def registration_choices():
        """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 = Wizard(session['dialog'])

    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)

    if wizard.run():
        create_image(session, wizard.answers)
    else:
        return False

    return True
def start_wizard(session):
    """Run the image creation wizard"""

    metadata = session['image'].meta
    distro = session['image'].distro
    ostype = session['image'].ostype

    # Create Cloud Wizard Page
    def cloud_choices():
        """Returns the available clouds"""
        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):
        """Checks if a cloud is valid"""
        if not Kamaki.get_account(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
            raise WizardReloadPage
        return cloud

    cloud = WizardMenuPage(
        "Cloud",
        lambda:
        "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)

    # Create Image Name Wizard Page
    name = WizardInputPage("ImageName",
                           lambda: "Please provide a name for the image:",
                           default=ostype if distro == "unknown" else distro)

    # Create Image Description Wizard Page
    descr = WizardInputPage(
        "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():
        """Returns the installed VirtIO drivers"""
        image = session['image']
        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

            dialog = session['dialog']
            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 = WizardInfoPage("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
    def registration_choices():
        """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 = Wizard(session['dialog'])

    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)

    if wizard.run():
        create_image(session, wizard.answers)
    else:
        return False

    return True