<commit_before>import contextlib
import logging
import time

from cStringIO import StringIO

from teuthology import packaging
from teuthology.orchestra import run
from teuthology.parallel import parallel

log = logging.getLogger(__name__)


@contextlib.contextmanager
def install(ctx, config):
    """
    Installs rh ceph on all hosts in ctx.

    :param ctx: the argparse.Namespace object
    :param config: the config dict
    """
    version = config['rhbuild']
    rh_versions = ['1.3.0', '1.3.1', '1.3.2', '2.0']
    if version in rh_versions:
        log.info("%s is a supported version", version)
    else:
        raise RuntimeError("Unsupported RH Ceph version %s", version)

    with parallel() as p:
        for remote in ctx.cluster.remotes.iterkeys():
            if remote.os.name == 'rhel':
                log.info("Installing on RHEL node: %s", remote.shortname)
                p.spawn(install_pkgs, ctx, remote, version)
            else:
                log.info("Node %s is not RHEL", remote.shortname)
                raise RuntimeError("Test requires RHEL nodes")
    try:
        yield
    finally:
        if config.get('skip_uninstall'):
            log.info("Skipping uninstall of Ceph")
        else:
            uninstall(ctx=ctx, config=config)


def uninstall(ctx, config):
    """
     Uninstalls rh ceph on all hosts.
     It actually spawns uninstall_pkgs() on the remotes for uninstall.

    :param ctx: the argparse.Namespace object
    :param config: the config dict
    """
    with parallel() as p:
        for remote in ctx.cluster.remotes.iterkeys():
            p.spawn(uninstall_pkgs, ctx, remote)


def install_pkgs(ctx, remote, installed_version):
    """
    Installs RH build using ceph-deploy.

    :param ctx: the argparse.Namespace object<commit_msg>use rh/downstream.yaml to define packages<commit_after>import contextlib
import logging
import time
import yaml
import os

from cStringIO import StringIO

from teuthology import packaging
from teuthology.orchestra import run
from teuthology.parallel import parallel

log = logging.getLogger(__name__)


@contextlib.contextmanager
def install(ctx, config):
    """
    Installs rh ceph on all hosts in ctx.

    :param ctx: the argparse.Namespace object
    :param config: the config dict

    uses yaml defined in qa suite or in users
    home dir to check for supported versions and
    packages to install.

    the format of yaml is:
    versions:
        supported:
           - '1.3.0'
        rpm:
            mapped:
               '1.3.0' : '0.94.1'
        deb:
            mapped:
               '1.3.0' : '0.94.1'
        pkgs:
            rpm:
             - ceph-mon
             - ceph-osd
            deb:
             - ceph-osd
             - ceph-mds
    """
    yaml_path = None
    # Look for rh specific packages in <suite_path>/rh/downstream.yaml
    if 'suite_path' in ctx.config:
        ds_yaml = os.path.join(
            ctx.config['suite_path'],
            'rh',
            'downstream.yaml',
        )
        if os.path.exists(ds_yaml):
            yaml_path = ds_yaml
    # default to user home dir if one exists
    default_yaml = os.path.expanduser('~/downstream.yaml')
    if os.path.exists(default_yaml):
        yaml_path = default_yaml
    log.info("using yaml path %s", yaml_path)
    downstream_config = yaml.safe_load(open(yaml_path))
    rh_versions = downstream_config.get('versions', dict()).get('supported', [])
    version = config['rhbuild']
    if version in rh_versions:
        log.info("%s is a supported version", version)
    else:
        raise RuntimeError("Unsupported RH Ceph version %s", version)
    with parallel() as p:
        for remote in ctx.cluster.remotes.iterkeys():
            if remote.os.name == 'rhel':
                log.info("Installing on RHEL node: %s", remote.shortname)
                p.spawn(rh_install_pkgs, ctx, remote, version, downstream_config)
            else:
                log.info("Node %s is not RHEL", remote.shortname)
                raise RuntimeError("Test requires RHEL nodes")
    try:
        yield
    finally:
        if config.get('skip_uninstall'):
            log.info("Skipping uninstall of Ceph")
        else:
            uninstall(ctx=ctx, config=config)


def uninstall(ctx, config):
    """
     Uninstalls rh ceph on all hosts.
     It actually spawns uninstall_pkgs() on the remotes for uninstall.

    :param ctx: the argparse.Namespace object
    :param config: the config dict
    """
    with parallel() as p:
        for remote in ctx.cluster.remotes.iterkeys():
            p.spawn(uninstall_pkgs, ctx, remote)


def install_pkgs(ctx, remote, installed_version):
    """
    Installs RH build using ceph-deploy.

    :param ctx: the argparse.Namespace object
{'fraction_non_alphanumeric': 0.09619406106231702, 'fraction_numerical': 0.005855290673358427, 'mean_word_length': 2.3123268698060944, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5543985', 'n_tokens_mistral': 1441, 'n_tokens_neox': 1398, 'n_words': 430}