<commit_before>import logging
from time import sleep
from devops.helpers import ssh, os
import keystoneclient.v2_0
import re
from fuel_test.settings import OS_FAMILY, PUPPET_CLIENT_PACKAGE, PUPPET_VERSION, PUPPET_MASTER_SERVICE
from root import root
#from glanceclient import Client

def get_file_as_string(path):
    with open(path) as f:
        return f.read()


def execute(remote, command):
    chan, stdin, stderr, stdout = execute_async(remote, command)
    result = {
        'stdout': [],
        'stderr': [],
        'exit_code': 0
    }
    for line in stdout:
        result['stdout'].append(line)
        print line
    for line in stderr:
        result['stderr'].append(line)
        print line

    result['exit_code'] = chan.recv_exit_status()
    chan.close()

    return result


def udp_ping(remote, host, port):
    result = execute(remote, 'nmap -PU -sU -p%s %s' % (port, host))
    for line in result['stdout']:
        if line.find('udp open') != -1:
            return True
    return False


def tcp_ping(remote, host, port):
    result = execute(remote, 'nmap -PU -p%s %s' % (port, host))
    for line in result['stdout']:
        if line.find('tcp open') != -1:
            return True
    return False


def load(path):
    with open(path) as f:
        return f.read()


def execute_async(remote, command):
    logging.debug("Executing command: '%s'" % command.rstrip())
    chan = remote._ssh.get_transport().open_session()
    stdin = chan.makefile('wb')
    stdout = chan.makefile('rb')
    stderr = chan.makefile_stderr('rb')
    cmd = "%s\n" % command
    if remote.sudo_mode:
        cmd = 'sudo -S bash -c "%s"' % cmd.replace('"', '\\"')
    chan.exec_command(cmd)
    if stdout.channel.closed is False:
        stdin.write('%s\n' % remote.password)
        stdin.flush()
    return chan, stdin, stderr, stdout


def extract_virtual_ips(ipaout):
    pattern = '(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*(eth\d{1,}):keepalived'
    return dict((v, k) for k, v in re.findall(pattern, ipaout))

def tempest_build_config_folsom(host, image_ref, image_ref_alt,
                                path_to_private_key,
                                compute_db_uri='mysql://user:pass@localhost/nova'):
    sample = load(
        root('fuel', 'fuel_test', 'config', 'tempest.conf.folsom.sample'))

    config = sample % {
        'IDENTITY_USE_SSL': 'false',
        'IDENTITY_HOST': host,
        'IDENTITY_PORT': '5000',
        'IDENTITY_API_VERSION': 'v2.0',
        'IDENTITY_PATH': 'tokens',
        'IDENTITY_STRATEGY': 'keystone',
        'COMPUTE_ALLOW_TENANT_ISOLATION': 'true',
        'COMPUTE_ALLOW_TENANT_REUSE': 'true',
        'USERNAME': 'tempest1',
        'PASSWORD': '<PASSWORD>',
        'TENANT_NAME': 'tenant1',
        'ALT_USERNAME': 'tempest2',
        'ALT_PASSWORD': '<PASSWORD>',
        'ALT_TENANT_NAME': 'tenant2',
        'IMAGE_ID': image_ref,
        'IMAGE_ID_ALT': image_ref_alt,
        'FLAVOR_REF': '1',
        'FLAVOR_REF_ALT': '2',
        'COMPUTE_BUILD_INTERVAL': '10',
        'COMPUTE_BUILD_TIMEOUT': '600',
        'COMPUTE_CATALOG_TYPE': 'compute',
        'COMPUTE_CREATE_IMAGE_ENABLED': 'true',
        'COMPUTE_RESIZE_AVAILABLE': 'true',
        'COMPUTE_CHANGE_PASSWORD_AVAILABLE': 'true',
        'COMPUTE_LOG_LEVEL': 'DEBUG',
        'COMPUTE_WHITEBOX_ENABLED': 'true',
        'COMPUTE_SOURCE_DIR': '/opt/stack/nova',
        'COMPUTE_CONFIG_PATH': '/etc/nova/nova.conf',
        'COMPUTE_BIN_DIR': '/usr/local/bin',
        'COMPUTE_PATH_TO_PRIVATE_KEY': path_to_private_key,
        'COMPUTE_DB_URI': compute_db_uri,
        'IMAGE_CATALOG_TYPE': 'image',
        'IMAGE_API_VERSION': '1',
        'IMAGE_HOST': host,
        'IMAGE_PORT': '9292',
        'IMAGE_USERNAME': 'tempest1',
        'IMAGE_PASSWORD': '<PASSWORD>',
        'IMAGE_TENANT_NAME': 'tenant1',
        'COMPUTE_ADMIN_USERNAME': 'nova',
        'COMPUTE_ADMIN_PASSWORD': '<PASSWORD>',
        'COMPUTE_ADMIN_TENANT_NAME': 'admin',
        'IDENTITY_ADMIN_USERNAME': 'nova',
        'IDENTITY_ADMIN_PASSWORD': '<PASSWORD>',
        'IDENTITY_ADMIN_TENANT_NAME': 'admin',
        'VOLUME_CATALOG_TYPE': 'volume',
        'VOLUME_BUILD_INTERVAL': '10',
        'VOLUME_BUILD_TIMEOUT': '300',
    }
    
    return config


def tempest_build_config_essex(host, image_ref, image_ref_alt):
    sample = load(
        root('fuel', 'fuel_test', 'config', 'tempest.conf.essex.sample'))
    config = sample % {
        'HOST': host,
        'USERNAME': 'tempest1',
        'PASSWORD': '<PASSWORD>',
        'TENANT_NAME': 'tenant1',
        'ALT_USERNAME': 'tempest2',
        'ALT_PASSWORD': '<PASSWORD>',
        'ALT_TENANT_NAME': 'tenant2',
        'IMAGE_ID': image_ref,
        'IMAGE_ID_ALT': image_ref_alt,
        'ADMIN_USERNAME': 'admin',
        'ADMIN_PASSWORD': '<PASSWORD>',
        'ADMIN_TENANT_NAME': 'openstack',
    }
    return config


def tempest_write_config(config):
    with open(root('tempest.conf'), 'w') as f:
        f.write(config)


def get_auth_url(auth_host):
    auth_url = 'http://%s:5000/v2.0/' % auth_host
    print auth_url
    return auth_url


def credentials(auth_host, username, password, tenant_name):
    credentials = (
        ' --os_username %(username)s '<commit_msg>Fix credentials in tempest config<commit_after>import logging
from time import sleep
from devops.helpers import ssh, os
import keystoneclient.v2_0
import re
from fuel_test.settings import OS_FAMILY, PUPPET_CLIENT_PACKAGE, PUPPET_VERSION, PUPPET_MASTER_SERVICE, ADMIN_USERNAME, ADMIN_PASSWORD, ADMIN_TENANT_FOLSOM, ADMIN_TENANT_ESSEX
from root import root
#from glanceclient import Client

def get_file_as_string(path):
    with open(path) as f:
        return f.read()


def execute(remote, command):
    chan, stdin, stderr, stdout = execute_async(remote, command)
    result = {
        'stdout': [],
        'stderr': [],
        'exit_code': 0
    }
    for line in stdout:
        result['stdout'].append(line)
        print line
    for line in stderr:
        result['stderr'].append(line)
        print line

    result['exit_code'] = chan.recv_exit_status()
    chan.close()

    return result


def udp_ping(remote, host, port):
    result = execute(remote, 'nmap -PU -sU -p%s %s' % (port, host))
    for line in result['stdout']:
        if line.find('udp open') != -1:
            return True
    return False


def tcp_ping(remote, host, port):
    result = execute(remote, 'nmap -PU -p%s %s' % (port, host))
    for line in result['stdout']:
        if line.find('tcp open') != -1:
            return True
    return False


def load(path):
    with open(path) as f:
        return f.read()


def execute_async(remote, command):
    logging.debug("Executing command: '%s'" % command.rstrip())
    chan = remote._ssh.get_transport().open_session()
    stdin = chan.makefile('wb')
    stdout = chan.makefile('rb')
    stderr = chan.makefile_stderr('rb')
    cmd = "%s\n" % command
    if remote.sudo_mode:
        cmd = 'sudo -S bash -c "%s"' % cmd.replace('"', '\\"')
    chan.exec_command(cmd)
    if stdout.channel.closed is False:
        stdin.write('%s\n' % remote.password)
        stdin.flush()
    return chan, stdin, stderr, stdout


def extract_virtual_ips(ipaout):
    pattern = '(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*(eth\d{1,}):keepalived'
    return dict((v, k) for k, v in re.findall(pattern, ipaout))

def tempest_build_config_folsom(host, image_ref, image_ref_alt,
                                path_to_private_key,
                                compute_db_uri='mysql://user:pass@localhost/nova'):
    sample = load(
        root('fuel', 'fuel_test', 'config', 'tempest.conf.folsom.sample'))

    config = sample % {
        'IDENTITY_USE_SSL': 'false',
        'IDENTITY_HOST': host,
        'IDENTITY_PORT': '5000',
        'IDENTITY_API_VERSION': 'v2.0',
        'IDENTITY_PATH': 'tokens',
        'IDENTITY_STRATEGY': 'keystone',
        'COMPUTE_ALLOW_TENANT_ISOLATION': 'true',
        'COMPUTE_ALLOW_TENANT_REUSE': 'true',
        'USERNAME': 'tempest1',
        'PASSWORD': '<PASSWORD>',
        'TENANT_NAME': 'tenant1',
        'ALT_USERNAME': 'tempest2',
        'ALT_PASSWORD': '<PASSWORD>',
        'ALT_TENANT_NAME': 'tenant2',
        'IMAGE_ID': image_ref,
        'IMAGE_ID_ALT': image_ref_alt,
        'FLAVOR_REF': '1',
        'FLAVOR_REF_ALT': '2',
        'COMPUTE_BUILD_INTERVAL': '10',
        'COMPUTE_BUILD_TIMEOUT': '600',
        'COMPUTE_CATALOG_TYPE': 'compute',
        'COMPUTE_CREATE_IMAGE_ENABLED': 'true',
        'COMPUTE_RESIZE_AVAILABLE': 'true',
        'COMPUTE_CHANGE_PASSWORD_AVAILABLE': 'true',
        'COMPUTE_LOG_LEVEL': 'DEBUG',
        'COMPUTE_WHITEBOX_ENABLED': 'true',
        'COMPUTE_SOURCE_DIR': '/opt/stack/nova',
        'COMPUTE_CONFIG_PATH': '/etc/nova/nova.conf',
        'COMPUTE_BIN_DIR': '/usr/local/bin',
        'COMPUTE_PATH_TO_PRIVATE_KEY': path_to_private_key,
        'COMPUTE_DB_URI': compute_db_uri,
        'IMAGE_CATALOG_TYPE': 'image',
        'IMAGE_API_VERSION': '1',
        'IMAGE_HOST': host,
        'IMAGE_PORT': '9292',
        'IMAGE_USERNAME': 'tempest1',
        'IMAGE_PASSWORD': '<PASSWORD>',
        'IMAGE_TENANT_NAME': 'tenant1',
        'COMPUTE_ADMIN_USERNAME': ADMIN_USERNAME,
        'COMPUTE_ADMIN_PASSWORD': <PASSWORD>,
        'COMPUTE_ADMIN_TENANT_NAME': ADMIN_TENANT_FOLSOM,
        'IDENTITY_ADMIN_USERNAME': ADMIN_USERNAME,
        'IDENTITY_ADMIN_PASSWORD': <PASSWORD>,
        'IDENTITY_ADMIN_TENANT_NAME': ADMIN_TENANT_FOLSOM,
        'VOLUME_CATALOG_TYPE': 'volume',
        'VOLUME_BUILD_INTERVAL': '10',
        'VOLUME_BUILD_TIMEOUT': '300',
    }
    
    return config


def tempest_build_config_essex(host, image_ref, image_ref_alt):
    sample = load(
        root('fuel', 'fuel_test', 'config', 'tempest.conf.essex.sample'))
    config = sample % {
        'HOST': host,
        'USERNAME': 'tempest1',
        'PASSWORD': '<PASSWORD>',
        'TENANT_NAME': 'tenant1',
        'ALT_USERNAME': 'tempest2',
        'ALT_PASSWORD': '<PASSWORD>',
        'ALT_TENANT_NAME': 'tenant2',
        'IMAGE_ID': image_ref,
        'IMAGE_ID_ALT': image_ref_alt,
        'ADMIN_USERNAME':  ADMIN_USERNAME,
        'ADMIN_PASSWORD': <PASSWORD>,
        'ADMIN_TENANT_NAME': ADMIN_TENANT_ESSEX,
    }
    return config


def tempest_write_config(config):
    with open(root('tempest.conf'), 'w') as f:
        f.write(config)


def get_auth_url(auth_host):
    auth_url = 'http://%s:5000/v2.0/' % auth_host
    print auth_url
    return auth_url


def credentials(auth_host, username, password, tenant_name):
    credentials = (
        ' --os_username %(username)s '
{'fraction_non_alphanumeric': 0.14333958724202628, 'fraction_numerical': 0.009943714821763603, 'mean_word_length': 2.8654822335025383, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '4512513', 'n_tokens_mistral': 3849, 'n_tokens_neox': 3787, 'n_words': 716}