<commit_before># -*- coding: utf-8 -*-
'''
Module for managing container and VM images

.. versionadded:: Helium
'''

# Import python libs
import os.path
import pprint
import logging

# Import salt libs
import salt.utils
import salt.syspaths


log = logging.getLogger(__name__)

CMD_MAP = {
    'yum': ('yum', 'rpm'),
    'deb': ('debootstrap',),
    'pacman': ('pacman',),
}


def __virtual__():
    '''
    By default, this will be available on all platforms; but not all distros
    will necessarily be supported
    '''
    return True


def bootstrap(platform,
              root,
              img_format='dir',
              fs_format='ext2',
              arch=None,
              flavor=None,
              repo_url=None,
              static_qemu=None):
    '''
    Create an image for a specific platform.

    Please note that this function *MUST* be run as root, as images that are
    created make files belonging to root.

    platform
        Which platform to use to create the image. Currently supported platforms
        are rpm, deb and pacman.

    root
        Local path to create the root of the image filesystem.

    img_format
        Which format to create the image in. By default, just copies files into
        a directory on the local fileysstem (``dir``). Future support will exist
        for ``sparse``.

    fs_format
        When using a non-``dir`` img_format, which filesystem to format the
        image to. By default, ``ext2``.

    arch
        Architecture to install packages for, if supported by the underlying
        bootstrap tool. Currently only used for deb.

    flavor
        Which flavor of operating system to install. This correlates to a
        specific directory on the distribution repositories. For instance,
        ``wheezy`` on Debian.

    repo_url
        Mainly important for Debian-based repos. Base URL for the mirror to
        install from. (e.x.: http://ftp.debian.org/debian/)

    static_qemu
        Local path to the static qemu binary required for this arch.
        (e.x.: /usr/bin/qemu-amd64-static)

    pkg_confs
        The location of the conf files to copy into the image, to point the
        installer to the right repos and configuration.

    CLI Examples:

        salt myminion genesis.bootstrap pacman /root/arch
        salt myminion genesis.bootstrap rpm /root/redhat
        salt myminion genesis.bootstrap deb /root/wheezy arch=amd64 \
            flavor=wheezy static_qemu=/usr/bin/qemu-x86_64-static

    '''
    if img_format == 'dir':
        # We can just use the root as the root
        if not __salt__['file.directory_exists'](root):
            try:
                __salt__['file.mkdir'](root)
            except Exception as exc:
                return {'Error': pprint.pformat(exc)}
    elif img_format == 'sparse':
        # TODO: Create a sparse file at the specified root
        pass

    if platform in ('rpm', 'yum'):
        return _bootstrap_yum(root)
    elif platform == 'deb':
        return _bootstrap_deb(
            root, arch=arch, flavor=flavor, static_qemu=static_qemu
        )
    elif platform == 'pacman':
        return _bootstrap_pacman(root)


def _bootstrap_yum(root, pkg_confs='/etc/yum*'):
    '''
    Bootstrap an image using the yum tools

    root
        The root of the image to install to. Will be created as a directory if
        if does not exist. (e.x.: /root/arch)

    pkg_confs
        The location of the conf files to copy into the image, to point yum
        to the right repos and configuration.

    TODO: Set up a pre-install overlay, to copy files into /etc/ and so on,
        which are required for the install to work.
    '''
    _make_nodes(root)
    __salt__['cmd.run']('cp /etc/resolv/conf /etc/*release {root}/etc'.format(root=root, confs=pkg_confs))
    __salt__['cmd.run']('cp -r /etc/*release {root}/etc'.format(root=root, confs=pkg_confs))
    __salt__['cmd.run']('cp -r {confs} {root}/etc'.format(root=root, confs=pkg_confs))
    __salt__['cmd.run']('yum install --installroot={0} -y yum centos-release iputils'.format(root))
    __salt__['cmd.run']('rpm --root={0} -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm'.format(root))


def _bootstrap_deb(
        root,
        arch,
        flavor,
        repo_url='http://ftp.debian.org/debian/',
        static_qemu=None
    ):
    '''
    Bootstrap an image using the Debian tools

    root
        The root of the image to install to. Will be created as a directory if
        if does not exist. (e.x.: /root/wheezy)

    arch
        Architecture of the target image. (e.x.: amd64)

    flavor
        Flavor of Debian to install. (e.x.: wheezy)

    repo_url
        Base URL for the mirror to install from.
        (e.x.: http://ftp.debian.org/debian/)

    static_qemu
        Local path to the static qemu binary required for this arch.
        (e.x.: /usr/bin/qemu-amd64-static)
    '''

    __salt__['cmd.run'](
        'debootstrap --foreign --arch {arch} {flavor} {root} {url}'.format(
            arch=arch, flavor=flavor, root=root, url=repo_url
        )
    )
    __salt__['cmd.run'](
        'cp {qemu} {root}/usr/bin/'.format(qemu=static_qemu, root=root)
    )
    env = ('DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true ',
           'LC_ALL=C LANGUAGE=C LANG=C PATH=/sbin:/bin:/usr/bin')
    __salt__['cmd.run'](
        '{env} chroot {root} /debootstrap/debootstrap --second-stage'.format(
            env=env, root=root
        )
    )
    __salt__['cmd.run'](
        '{env} chroot {root} dpkg --configure -a'.format(env=env, root=root)
    )

<commit_msg>modules.genesis.bootstrap(): pass repo_url arg thru to _bootstrap_deb()<commit_after># -*- coding: utf-8 -*-
'''
Module for managing container and VM images

.. versionadded:: Helium
'''

# Import python libs
import os.path
import pprint
import logging

# Import salt libs
import salt.utils
import salt.syspaths


log = logging.getLogger(__name__)

CMD_MAP = {
    'yum': ('yum', 'rpm'),
    'deb': ('debootstrap',),
    'pacman': ('pacman',),
}


def __virtual__():
    '''
    By default, this will be available on all platforms; but not all distros
    will necessarily be supported
    '''
    return True


def bootstrap(platform,
              root,
              img_format='dir',
              fs_format='ext2',
              arch=None,
              flavor=None,
              repo_url=None,
              static_qemu=None):
    '''
    Create an image for a specific platform.

    Please note that this function *MUST* be run as root, as images that are
    created make files belonging to root.

    platform
        Which platform to use to create the image. Currently supported platforms
        are rpm, deb and pacman.

    root
        Local path to create the root of the image filesystem.

    img_format
        Which format to create the image in. By default, just copies files into
        a directory on the local fileysstem (``dir``). Future support will exist
        for ``sparse``.

    fs_format
        When using a non-``dir`` img_format, which filesystem to format the
        image to. By default, ``ext2``.

    arch
        Architecture to install packages for, if supported by the underlying
        bootstrap tool. Currently only used for deb.

    flavor
        Which flavor of operating system to install. This correlates to a
        specific directory on the distribution repositories. For instance,
        ``wheezy`` on Debian.

    repo_url
        Mainly important for Debian-based repos. Base URL for the mirror to
        install from. (e.x.: http://ftp.debian.org/debian/)

    static_qemu
        Local path to the static qemu binary required for this arch.
        (e.x.: /usr/bin/qemu-amd64-static)

    pkg_confs
        The location of the conf files to copy into the image, to point the
        installer to the right repos and configuration.

    CLI Examples:

        salt myminion genesis.bootstrap pacman /root/arch
        salt myminion genesis.bootstrap rpm /root/redhat
        salt myminion genesis.bootstrap deb /root/wheezy arch=amd64 \
            flavor=wheezy static_qemu=/usr/bin/qemu-x86_64-static

    '''
    if img_format == 'dir':
        # We can just use the root as the root
        if not __salt__['file.directory_exists'](root):
            try:
                __salt__['file.mkdir'](root)
            except Exception as exc:
                return {'Error': pprint.pformat(exc)}
    elif img_format == 'sparse':
        # TODO: Create a sparse file at the specified root
        pass

    if platform in ('rpm', 'yum'):
        return _bootstrap_yum(root)
    elif platform == 'deb':
        return _bootstrap_deb(
            root, arch=arch, flavor=flavor, repo_url=repo_url, static_qemu=static_qemu
        )
    elif platform == 'pacman':
        return _bootstrap_pacman(root)


def _bootstrap_yum(root, pkg_confs='/etc/yum*'):
    '''
    Bootstrap an image using the yum tools

    root
        The root of the image to install to. Will be created as a directory if
        if does not exist. (e.x.: /root/arch)

    pkg_confs
        The location of the conf files to copy into the image, to point yum
        to the right repos and configuration.

    TODO: Set up a pre-install overlay, to copy files into /etc/ and so on,
        which are required for the install to work.
    '''
    _make_nodes(root)
    __salt__['cmd.run']('cp /etc/resolv/conf /etc/*release {root}/etc'.format(root=root, confs=pkg_confs))
    __salt__['cmd.run']('cp -r /etc/*release {root}/etc'.format(root=root, confs=pkg_confs))
    __salt__['cmd.run']('cp -r {confs} {root}/etc'.format(root=root, confs=pkg_confs))
    __salt__['cmd.run']('yum install --installroot={0} -y yum centos-release iputils'.format(root))
    __salt__['cmd.run']('rpm --root={0} -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm'.format(root))


def _bootstrap_deb(
        root,
        arch,
        flavor,
        repo_url=None,
        static_qemu=None
    ):
    '''
    Bootstrap an image using the Debian tools

    root
        The root of the image to install to. Will be created as a directory if
        if does not exist. (e.x.: /root/wheezy)

    arch
        Architecture of the target image. (e.x.: amd64)

    flavor
        Flavor of Debian to install. (e.x.: wheezy)

    repo_url
        Base URL for the mirror to install from.
        (e.x.: http://ftp.debian.org/debian/)

    static_qemu
        Local path to the static qemu binary required for this arch.
        (e.x.: /usr/bin/qemu-amd64-static)
    '''

    if repo_url is None:
        repo_url = 'http://ftp.debian.org/debian/'

    __salt__['cmd.run'](
        'debootstrap --foreign --arch {arch} {flavor} {root} {url}'.format(
            arch=arch, flavor=flavor, root=root, url=repo_url
        )
    )
    __salt__['cmd.run'](
        'cp {qemu} {root}/usr/bin/'.format(qemu=static_qemu, root=root)
    )
    env = ('DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true ',
           'LC_ALL=C LANGUAGE=C LANG=C PATH=/sbin:/bin:/usr/bin')
    __salt__['cmd.run'](
        '{env} chroot {root} /debootstrap/debootstrap --second-stage'.format(
            env=env, root=root
        )
    )
    __salt__['cmd.run'](
        '{env} chroot {root} dpkg --configure -a'.format(env=env, root=root)
    )


{'fraction_non_alphanumeric': 0.10690832676648279, 'fraction_numerical': 0.0040276683302688035, 'mean_word_length': 2.349560117302053, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1842638', 'n_tokens_mistral': 3641, 'n_tokens_neox': 3469, 'n_words': 1125}