<commit_before>"""Brings up a set of oneprovider nodes. They can create separate clusters."""

from __future__ import print_function

import copy
import json
import os
import sys
import time

import common
import docker


try:
    import xml.etree.cElementTree as eTree
except ImportError:
    import xml.etree.ElementTree as eTree

try:  # Python 2
    from urllib2 import urlopen
    from urllib2 import URLError
    from httplib import BadStatusLine
except ImportError:  # Python 3
    from urllib.request import urlopen
    from urllib.error import URLError
    from http.client import BadStatusLine


def _tweak_config(config, name, uid):
    cfg = copy.deepcopy(config)
    cfg['nodes'] = {'node': cfg['nodes'][name]}

    sys_config = cfg['nodes']['node']['sys.config']
    sys_config['ccm_nodes'] = [common.format_hostname(n, uid) for n in
                               sys_config['ccm_nodes']]

    if 'global_registry_node' in sys_config:
        sys_config['global_registry_node'] = \
            common.format_hostname(sys_config['global_registry_node'], uid)

    vm_args = cfg['nodes']['node']['vm.args']
    vm_args['name'] = common.format_hostname(vm_args['name'], uid)

    return cfg


def _node_up(image, bindir, logdir, uid, config, dns_servers):
    node_type = config['nodes']['node']['sys.config']['node_type']
    node_name = config['nodes']['node']['vm.args']['name']

    (name, sep, hostname) = node_name.partition('@')

    command = \
        '''set -e
cat <<"EOF" > /tmp/gen_dev_args.json
{gen_dev_args}
EOF
escript bamboos/gen_dev/gen_dev.escript /tmp/gen_dev_args.json
/root/bin/node/bin/oneprovider_node console'''
    command = command.format(
        gen_dev_args=json.dumps({'oneprovider_node': config}),
        uid=os.geteuid(),
        gid=os.getegid())

    logdir = os.path.join(os.path.abspath(logdir), name)
    volumes = [(bindir, '/root/build', 'ro')]
    volumes.extend([(logdir, '/root/bin/node/log', 'rw')] if logdir else [])

    container = docker.run(
        image=image,
        hostname=hostname,
        detach=True,
        interactive=True,
        tty=True,
        workdir='/root/build',
        name=common.format_dockername(name, uid),
        volumes=volumes,
        dns_list=dns_servers,
        command=command)

    return (
        [container] if node_type == 'ccm' else [],
        [container] if node_type == 'worker' else [],
        {
            'docker_ids': [container],
            'op_{0}_nodes'.format(node_type): [node_name]
        }
    )


def _is_up(ip):
    url = 'https://{0}/nagios'.format(ip)
    try:
        fo = urlopen(url, timeout=1)
        tree = eTree.parse(fo)
        healthdata = tree.getroot()
        status = healthdata.attrib['status']
        return status == 'ok'
    except URLError:
        return False
    except BadStatusLine:
        return False
<commit_msg>VFS-1019 increase nagios timeout<commit_after>"""Brings up a set of oneprovider nodes. They can create separate clusters."""

from __future__ import print_function

import copy
import json
import os
import sys
import time

import common
import docker


try:
    import xml.etree.cElementTree as eTree
except ImportError:
    import xml.etree.ElementTree as eTree

try:  # Python 2
    from urllib2 import urlopen
    from urllib2 import URLError
    from httplib import BadStatusLine
except ImportError:  # Python 3
    from urllib.request import urlopen
    from urllib.error import URLError
    from http.client import BadStatusLine


def _tweak_config(config, name, uid):
    cfg = copy.deepcopy(config)
    cfg['nodes'] = {'node': cfg['nodes'][name]}

    sys_config = cfg['nodes']['node']['sys.config']
    sys_config['ccm_nodes'] = [common.format_hostname(n, uid) for n in
                               sys_config['ccm_nodes']]

    if 'global_registry_node' in sys_config:
        sys_config['global_registry_node'] = \
            common.format_hostname(sys_config['global_registry_node'], uid)

    vm_args = cfg['nodes']['node']['vm.args']
    vm_args['name'] = common.format_hostname(vm_args['name'], uid)

    return cfg


def _node_up(image, bindir, logdir, uid, config, dns_servers):
    node_type = config['nodes']['node']['sys.config']['node_type']
    node_name = config['nodes']['node']['vm.args']['name']

    (name, sep, hostname) = node_name.partition('@')

    command = \
        '''set -e
cat <<"EOF" > /tmp/gen_dev_args.json
{gen_dev_args}
EOF
escript bamboos/gen_dev/gen_dev.escript /tmp/gen_dev_args.json
/root/bin/node/bin/oneprovider_node console'''
    command = command.format(
        gen_dev_args=json.dumps({'oneprovider_node': config}),
        uid=os.geteuid(),
        gid=os.getegid())

    logdir = os.path.join(os.path.abspath(logdir), name)
    volumes = [(bindir, '/root/build', 'ro')]
    volumes.extend([(logdir, '/root/bin/node/log', 'rw')] if logdir else [])

    container = docker.run(
        image=image,
        hostname=hostname,
        detach=True,
        interactive=True,
        tty=True,
        workdir='/root/build',
        name=common.format_dockername(name, uid),
        volumes=volumes,
        dns_list=dns_servers,
        command=command)

    return (
        [container] if node_type == 'ccm' else [],
        [container] if node_type == 'worker' else [],
        {
            'docker_ids': [container],
            'op_{0}_nodes'.format(node_type): [node_name]
        }
    )


def _is_up(ip):
    url = 'https://{0}/nagios'.format(ip)
    try:
        fo = urlopen(url, timeout=5)
        tree = eTree.parse(fo)
        healthdata = tree.getroot()
        status = healthdata.attrib['status']
        return status == 'ok'
    except URLError:
        return False
    except BadStatusLine:
        return False

{'fraction_non_alphanumeric': 0.13288170922355394, 'fraction_numerical': 0.003126628452318916, 'mean_word_length': 2.9682977257064094, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '4097339', 'n_tokens_mistral': 1909, 'n_tokens_neox': 1873, 'n_words': 411}