vinstall / vinstall / controller /

#-*- coding: utf-8 -*-

"""Bootloader options for the Vector installer


from vinstall.core import Application, Render, Controller, model
from import Disk, Partition
from vinstall.backend import bootloader, utils
from itertools import chain
import os, subprocess

class Bootloader(Controller):
    """Select a bootloader

    def init(self):
        """Find the disks available in the system.

        self.disks = [ i for i in Disk.all() if not i.is_read_only() ]
        self.partitions = [ i for i in Partition.all() if i.query_filesystem()
                != "xfs" ]
        self.targets = self.disks + self.partitions
        self.bootloaders = [ u"Lilo", u"Grub2", u"None" ]

    def render(self):
        """Show bootloader options

        title = u"Bootloader"
        intro = u"Select a bootloader to boot your system"

        bootloaders = model.DropdownOptionList()
        bootloaders.label = u"Select a bootloader"
        for i in self.bootloaders:

        location = model.DropdownOptionList()
        location.label = u"Install the bootloader in"
        for i in self.targets:

        return Render(title, intro, bootloaders, location)

    def next(self):
        """Return next step

        return None

    def previous(self):
        """Return previous step

        from vinstall.controller import usersetup
        return usersetup.UserSetup

    def command(self, bootloader, target):
        """Schedule command for later execution

        yield self.set_bind_mounts, tuple(), "Mounting pseudo filesystems"
        yield self.install_bootloader, (bootloader, target), "Installing bootloader"
        yield self.clear_bind_mounts, tuple(), "Clearing bind mounts"

    def install_bootloader(self, bloader, target):
        """Install the bootloader

        bloader = self.bootloaders[bloader]
        disk = self.targets[target]
        target = disk.path()

        if bloader.lower() == "lilo":
            bl = bootloader.Lilo(target)
            for system in bootloader.OperatingSystem.all():
	    sequence = (bl.backup_config, bl.write_config, bl.install)
        elif bloader == "Grub2":
            bl = bootloader.Grub2(target)
	    sequence = (bl.backup_config, bl.install, bl.write_config)

        with utils.Chroot("/mnt/TARGET"):
	    for meth in sequence:
#            bl.backup_config()
#            bl.write_config()
#            bl.install()

    def set_bind_mounts(self):
        """Bind mount some paths into the target.
        Later steps will need this when we chroot there

        for point in ("sys","proc","dev"):
            cmd = "mount -o bind /%s /mnt/TARGET/%s" % (point, point)

    def clear_bind_mounts(self):
        """Clear the mountpoints that were mounted for the configuration

        for mpoint in ("sys","proc","dev"):
            cmd = "umount -f /mnt/TARGET/%s" % mpoint