<commit_before>import datetime
import os
import re
import tempfile
import time

import yaml

from fabric.api import (
    env, execute, get, hide, local, put, require, run, settings, sudo, task)
from fabric.contrib import files, project
from fabric.utils import abort


DEFAULT_SALT_LOGLEVEL = 'info'

SALT_VERSION = '2014.7.6'

PROJECT_ROOT = os.path.dirname(__file__)

CONF_ROOT = os.path.join(PROJECT_ROOT, 'conf')

VALID_ROLES = (
    'salt-master',
    'web',
    'worker',
    'balancer',
    'db-master',
    'queue',
    'cache',
)


@task
def staging():
    env.environment = 'staging'
    env.master = '172.16.17.32'
    initialize_env()


@task
def production():
    env.environment = 'production'
    env.master = '192.168.3.11'
    initialize_env()


@task
def vagrant():
    env.environment = 'local'
    env.user = 'vagrant'
    # convert vagrant's ssh-config output to a dictionary
    ssh_config_output = local('vagrant ssh-config', capture=True)
    ssh_config = dict(line.split() for line in ssh_config_output.splitlines())
    env.master = '{HostName}:{Port}'.format(**ssh_config)
    env.key_filename = ssh_config['IdentityFile']
    initialize_env()


def initialize_env():
    """Build some common variables into the env dictionary."""
    env.gpg_key = os.path.join(CONF_ROOT, '{}.pub.gpg'.format(env.environment))


def db_get_remote_dump(path=None):
    """Dump remote database content to a local file."""
    if not path:
        now = datetime.datetime.now().isoformat()
        filename = "tracpro-{env}-db-{now}.sql.gz".format(env=env.environment, now=now)
        path = os.path.join('/tmp/', filename)

    with settings(host_string=env.master):
        # Dump the database to a file on the remote host.
        cmd = 'pg_dump -Z 5 -U tracpro_{env} -d tracpro_{env} -f {output}'
        run(cmd.format(env=env.environment, output=path))

        # Copy the file from the remote host to the local machine.
        # File will be put in the same place both locally and on the remote.
        get(remote_path=path, local_path=path)

        # Remove the remote copy of the file.
        run("rm {}".format(path))

    return path


def db_load_from_file_local(path, runner, db_name):
    """Load content from a database dump file to the remote database."""
    unzipped = os.path.splitext(path)[0]
    local("cat {} | gunzip > {}".format(path, unzipped))
    local("dropdb --if-exists {}".format(db_name))
    local("createdb -E UTF-8 {}".format(db_name))
    local("psql -d {} -f {}".format(db_name, unzipped))


def db_load_from_file_remote(path):
    """Load content from a database dump file to the remote database."""
    unzipped = os.path.splitext(path)[0]
    run("cat {path} | gunzip > {filename}".format(
        path=path, filename=unzipped))
    run("dropdb -U tracpro_{env} --if-exists tracpro_{env}".format(
        env=env.environment))
    with settings(sudo_user="postgres"):
        sudo("createdb -O tracpro_{env} -E UTF-8 tracpro_{env}".format(
             env=env.environment))
    run("psql -U tracpro_{env} -d tracpro_{env} -f {filename}".format(
        env=env.environment, filename=unzipped))


@task
def reset_local_db():
    """Copy the remote database to the local environment."""
    dump = db_get_remote_dump()
    db_load_from_file_local(dump)
    local("rm {}".format(dump))


@task
def prod_db_to_staging():
    """Copy the production database to staging."""
    production()
    dump = db_get_remote_dump()
    staging()
    with settings(host_string=env.master):
        put(local_path=dump, remote_path=dump)
        stop_server()
        sudo("service postgresql restart")  # kill hanging queries
        db_load_from_file_remote(dump)
        start_server()
        run("rm {}".format(dump))
    local("rm {}".format(dump))


@task
def start_server():
    with settings(host_string=env.master):
        sudo("supervisorctl start tracpro-server")


@task
def stop_server():
    with settings(host_string=env.master):
        sudo("supervisorctl stop tracpro-server")


def get_salt_version(command):<commit_msg>Stop and start postgres rather than restart<commit_after>import datetime
import os
import re
import tempfile
import time

import yaml

from fabric.api import (
    env, execute, get, hide, local, put, require, run, settings, sudo, task)
from fabric.contrib import files, project
from fabric.utils import abort


DEFAULT_SALT_LOGLEVEL = 'info'

SALT_VERSION = '2014.7.6'

PROJECT_ROOT = os.path.dirname(__file__)

CONF_ROOT = os.path.join(PROJECT_ROOT, 'conf')

VALID_ROLES = (
    'salt-master',
    'web',
    'worker',
    'balancer',
    'db-master',
    'queue',
    'cache',
)


@task
def staging():
    env.environment = 'staging'
    env.master = '172.16.17.32'
    initialize_env()


@task
def production():
    env.environment = 'production'
    env.master = '192.168.3.11'
    initialize_env()


@task
def vagrant():
    env.environment = 'local'
    env.user = 'vagrant'
    # convert vagrant's ssh-config output to a dictionary
    ssh_config_output = local('vagrant ssh-config', capture=True)
    ssh_config = dict(line.split() for line in ssh_config_output.splitlines())
    env.master = '{HostName}:{Port}'.format(**ssh_config)
    env.key_filename = ssh_config['IdentityFile']
    initialize_env()


def initialize_env():
    """Build some common variables into the env dictionary."""
    env.gpg_key = os.path.join(CONF_ROOT, '{}.pub.gpg'.format(env.environment))


def db_get_remote_dump(path=None):
    """Dump remote database content to a local file."""
    if not path:
        now = datetime.datetime.now().isoformat()
        filename = "tracpro-{env}-db-{now}.sql.gz".format(env=env.environment, now=now)
        path = os.path.join('/tmp/', filename)

    with settings(host_string=env.master):
        # Dump the database to a file on the remote host.
        cmd = 'pg_dump -Z 5 -U tracpro_{env} -d tracpro_{env} -f {output}'
        run(cmd.format(env=env.environment, output=path))

        # Copy the file from the remote host to the local machine.
        # File will be put in the same place both locally and on the remote.
        get(remote_path=path, local_path=path)

        # Remove the remote copy of the file.
        run("rm {}".format(path))

    return path


def db_load_from_file_local(path, runner, db_name):
    """Load content from a database dump file to the remote database."""
    unzipped = os.path.splitext(path)[0]
    local("cat {} | gunzip > {}".format(path, unzipped))
    local("dropdb --if-exists {}".format(db_name))
    local("createdb -E UTF-8 {}".format(db_name))
    local("psql -d {} -f {}".format(db_name, unzipped))


def db_load_from_file_remote(path):
    """Load content from a database dump file to the remote database."""
    unzipped = os.path.splitext(path)[0]
    run("cat {path} | gunzip > {filename}".format(
        path=path, filename=unzipped))
    run("dropdb -U tracpro_{env} --if-exists tracpro_{env}".format(
        env=env.environment))
    with settings(sudo_user="postgres"):
        sudo("createdb -O tracpro_{env} -E UTF-8 tracpro_{env}".format(
             env=env.environment))
    run("psql -U tracpro_{env} -d tracpro_{env} -f {filename}".format(
        env=env.environment, filename=unzipped))


@task
def reset_local_db():
    """Copy the remote database to the local environment."""
    dump = db_get_remote_dump()
    db_load_from_file_local(dump)
    local("rm {}".format(dump))


@task
def prod_db_to_staging():
    """Copy the production database to staging."""
    production()
    dump = db_get_remote_dump()
    staging()
    with settings(host_string=env.master):
        put(local_path=dump, remote_path=dump)
        stop_server()
        sudo("service postgresql stop")  # kill hanging queries
        sudo("service postgresql start")
        db_load_from_file_remote(dump)
        start_server()
        run("rm {}".format(dump))
    local("rm {}".format(dump))


@task
def start_server():
    with settings(host_string=env.master):
        sudo("supervisorctl start tracpro-server")


@task
def stop_server():
    with settings(host_string=env.master):
        sudo("supervisorctl stop tracpro-server")


def get_salt_version(command):
{'fraction_non_alphanumeric': 0.13518113299294918, 'fraction_numerical': 0.007050814490639436, 'mean_word_length': 3.4088960342979635, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '7334643', 'n_tokens_mistral': 2759, 'n_tokens_neox': 2627, 'n_words': 678}