<commit_before>import os
from tempfile import NamedTemporaryFile

from behave import given
import chronos
import json

from itest_utils import get_service_connection_string
from paasta_tools import marathon_tools
from paasta_tools import utils
from paasta_tools import chronos_tools


def _get_marathon_connection_string():
    return 'http://%s' % get_service_connection_string('marathon')


def _get_chronos_connection_string():
    return 'http://%s' % get_service_connection_string('chronos')


def _get_zookeeper_connection_string(chroot):
    return 'zk://%s/%s' % (get_service_connection_string('zookeeper'), chroot)


def setup_marathon_client():
    marathon_connection_string = _get_marathon_connection_string()
    zk_connection_string = _get_zookeeper_connection_string('mesos-testcluster')
    marathon_config = marathon_tools.MarathonConfig({
        'url': marathon_connection_string,
        'user': None,
        'password': <PASSWORD>,
    }, '/some_fake_path_to_marathon.json')
    client = marathon_tools.get_marathon_client(marathon_config.get_url(), marathon_config.get_username(),
                                                marathon_config.get_password())
    system_paasta_config = utils.SystemPaastaConfig({
        'cluster': 'testcluster',
        'docker_volumes': [],
        'docker_registry': u'docker-dev.yelpcorp.com',
        'zookeeper': zk_connection_string
    }, '/some_fake_path_to_config_dir/')
    return (client, marathon_config, system_paasta_config)


def setup_chronos_client():
    connection_string = get_service_connection_string('chronos')
    return chronos.connect(connection_string)


def setup_chronos_config():
    chronos_connection_string = _get_chronos_connection_string()
    chronos_config = chronos_tools.ChronosConfig({
        'user': None,
        'password': <PASSWORD>,
        'url': [chronos_connection_string],
    }, '/some_fake_path_to_chronos.json')
    return chronos_config


def _generate_mesos_cli_config(zk_host_and_port):
    config = {
        'profile': 'default',
        'default': {
            'master': zk_host_and_port,
            'log_level': 'warning',
            'log_file': 'None',
        }
    }
    return config


def write_mesos_cli_config(config):
    mesos_cli_config_file = NamedTemporaryFile(delete=False)
    mesos_cli_config_file.write(json.dumps(config))
    mesos_cli_config_file.close()
    return mesos_cli_config_file.name


def write_etc_paasta(config, filename):
    paasta_dir = '/etc/paasta'
    if not os.path.exists(paasta_dir):
        os.makedirs(paasta_dir)
    with open(os.path.join(paasta_dir, filename), 'w') as f:
        f.write(json.dumps(config))


@given('a working paasta cluster')
def working_paasta_cluster(context):
    """Adds a working marathon client and chronos client for the purposes of
    interacting with them in the test."""
    if not hasattr(context, 'marathon_client'):
        context.marathon_client, context.marathon_config, context.system_paasta_config = setup_marathon_client()
    else:
        print 'Marathon connection already established'

    if not hasattr(context, 'chronos_client'):
        context.chronos_config = setup_chronos_config()
        context.chronos_client = setup_chronos_client()
    else:
        print 'Chronos connection already established'

    mesos_cli_config = _generate_mesos_cli_config(_get_zookeeper_connection_string('mesos-testcluster'))
    context.mesos_cli_config_filename = write_mesos_cli_config(mesos_cli_config)
    write_etc_paasta(context.marathon_config, 'marathon.json')
    write_etc_paasta(context.chronos_config, 'chronos.json')<commit_msg>add necessary work for paasta services<commit_after>import os
from tempfile import NamedTemporaryFile

from behave import given
import chronos
import json
import yaml

from itest_utils import get_service_connection_string
from paasta_tools import marathon_tools
from paasta_tools import utils
from paasta_tools import chronos_tools


def _get_marathon_connection_string():
    return 'http://%s' % get_service_connection_string('marathon')


def _get_chronos_connection_string():
    return 'http://%s' % get_service_connection_string('chronos')


def _get_zookeeper_connection_string(chroot):
    return 'zk://%s/%s' % (get_service_connection_string('zookeeper'), chroot)


def setup_marathon_client():
    marathon_connection_string = _get_marathon_connection_string()
    zk_connection_string = _get_zookeeper_connection_string('mesos-testcluster')
    marathon_config = marathon_tools.MarathonConfig({
        'url': marathon_connection_string,
        'user': None,
        'password': <PASSWORD>,
    }, '/some_fake_path_to_marathon.json')
    client = marathon_tools.get_marathon_client(marathon_config.get_url(), marathon_config.get_username(),
                                                marathon_config.get_password())
    system_paasta_config = utils.SystemPaastaConfig({
        'cluster': 'testcluster',
        'docker_volumes': [],
        'docker_registry': u'docker-dev.yelpcorp.com',
        'zookeeper': zk_connection_string
    }, '/some_fake_path_to_config_dir/')
    return (client, marathon_config, system_paasta_config)


def setup_chronos_client():
    connection_string = get_service_connection_string('chronos')
    return chronos.connect(connection_string)


def setup_chronos_config():
    chronos_connection_string = _get_chronos_connection_string()
    chronos_config = chronos_tools.ChronosConfig({
        'user': None,
        'password': <PASSWORD>,
        'url': [chronos_connection_string],
    }, '/some_fake_path_to_chronos.json')
    return chronos_config


def _generate_mesos_cli_config(zk_host_and_port):
    config = {
        'profile': 'default',
        'default': {
            'master': zk_host_and_port,
            'log_level': 'warning',
            'log_file': 'None',
        }
    }
    return config


def write_mesos_cli_config(config):
    mesos_cli_config_file = NamedTemporaryFile(delete=False)
    mesos_cli_config_file.write(json.dumps(config))
    mesos_cli_config_file.close()
    return mesos_cli_config_file.name


def write_etc_paasta(config, filename):
    paasta_dir = '/etc/paasta'
    if not os.path.exists(paasta_dir):
        os.makedirs(paasta_dir)
    with open(os.path.join(paasta_dir, filename), 'w') as f:
        f.write(json.dumps(config))

def write_soa_dir():
    soa_dir = '/nail/etc/services'
    if not os.path.exists(soa_dir):
        os.makedirs(soa_dir)

def write_service_dir(service_name):
    soa_dir = '/nail/etc/services'
    if not os.path.exists(os.path.join(soa_dir, service_name)):
        os.makedirs(os.path.join(soa_dir, service_name))

def write_chronos_job_for_service_cluster(service, cluster):
    soa_dir = '/nail/etc/services/%s' % service
    with open(os.path.join(soa_dir, 'chronos-%s.yaml' % cluster), 'w+') as f:
        f.write(yaml.dump({
            "test_job": {
                "command": "echo foo",
            }
        }))



@given('a working paasta cluster')
def working_paasta_cluster(context):
    """Adds a working marathon client and chronos client for the purposes of
    interacting with them in the test."""
    if not hasattr(context, 'marathon_client'):
        context.marathon_client, context.marathon_config, context.system_paasta_config = setup_marathon_client()
    else:
        print 'Marathon connection already established'

    if not hasattr(context, 'chronos_client'):
        context.chronos_config = setup_chronos_config()
        context.chronos_client = setup_chronos_client()
    else:
        print 'Chronos connection already established'

    mesos_cli_config = _generate_mesos_cli_config(_get_zookeeper_connection_string('mesos-testcluster'))
    context.mesos_cli_config_filename = write_mesos_cli_config(mesos_cli_config)
    write_etc_paasta(context.marathon_config, 'marathon.json')
    write_etc_paasta(context.chronos_config, 'chronos.json')
    write_etc_paasta({
        "cluster": "testcluster",
        "zookeeper": "zk://fake",
        "docker_registry": "fake.com"
    }, 'cluster.json')
    write_service_dir('example_service')
    write_chronos_job_for_service_cluster('example_service', 'testcluster')
{'fraction_non_alphanumeric': 0.10004868549172347, 'fraction_numerical': 0.0, 'mean_word_length': 4.091078066914498, 'pattern_counts': {'":': 5, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5430702', 'n_tokens_mistral': 2624, 'n_tokens_neox': 2568, 'n_words': 449}