<commit_before>import os

from django.conf import settings as django_settings
from django.template import Context, Engine

from fabric.api import (
    cd, env, local, prefix, prompt, put, run, sudo, task)
from fabric.contrib.console import confirm

import yaml


django_settings.configure()


BASE_PATH = os.path.dirname(os.path.abspath(__file__))
CELERY_BIN = '/home/chathan/chmvh-website/env/bin/celery'
CREDENTIAL_MAP = {
    'db_name': 'database name',
    'db_password': '<PASSWORD>',
    'db_user': 'database user',
    'django_admin_email': 'Django administrator email',
    'django_admin_name': 'Django administrator name',
    'secret_key': 'Django secret key',
    'sendgrid_password': '<PASSWORD>',
    'sendgrid_user': 'SendGrid username',
    'sudo_password': '<PASSWORD>',
}
DJANGO_LOG_FILE = '/var/log/django/logfile'
REMOTE_PROJECT_DIR = '/home/chathan/chmvh-website'
REPOSITORY_URL = 'https://github.com/cdriehuys/chmvh-website'


required_packages = (
    'git',
    'letsencrypt',
    'libjpeg-dev',
    'libpq-dev',
    'postgresql', 'postgresql-contrib',
    'nginx',
    'python3-dev', 'python3-pip',
    'rabbitmq-server',
    'ufw',
    'zlib1g-dev',
)


class Credentials:
    FILE_PATH = os.path.join(BASE_PATH, 'secure-config.yml')

    credentials = None

    @classmethod
    def get(cls, name):
        if cls.credentials is None:
            cls.load_credentials()

        host_credentials = cls.credentials.get(env.host, {})

        existing = host_credentials.get(name, None)

        # If that credential is already stored, return it
        if existing is not None:
            return existing

        # Credential doesn't exist, so prompt for it and then save it
        message = 'Enter {0} for {1}:'.format(CREDENTIAL_MAP[name], env.host)
        host_credentials[name] = prompt(message)

        cls.credentials[env.host] = host_credentials
        cls._save_credentials()

        return host_credentials[name]

    @classmethod
    def load_credentials(cls):
        if os.path.exists(cls.FILE_PATH):
            with open(cls.FILE_PATH, 'r') as f:
                cls.credentials = yaml.load(f)
        else:
            cls.credentials = {}

    @classmethod
    def _save_credentials(cls):
        with open(cls.FILE_PATH, 'w') as f:
            yaml.dump(cls.credentials, f)


@task
def prepare_local():
    """Prepare local machine for deployment"""
    # From http://stackoverflow.com/a/11958481/3762084
    env.current_branch = local(
        'git rev-parse --symbolic-full-name --abbrev-ref HEAD',
        capture=True)

    if env.current_branch != 'master':
        if not confirm("You are trying to deploy from the '{0}' branch. "
                       "Is this what you want?".format(env.current_branch),
                       default=False):
            env.orig_branch = env.current_branch
            env.current_branch = prompt(
                "Enter branch to deploy from:",
                default='master')

            local('git checkout {}'.format(env.current_branch))

    local('git push')


@task
def remote_setup():
    """Set up the remote machine"""
    sudo('apt-get update -y')

    # Install required packages
    sudo('apt-get install -y {0}'.format(' '.join(required_packages)))

    # Set up the database
    _configure_database()

    # Set up webserver
    _configure_gunicorn()
    _configure_nginx()
    _configure_ssl()

    # Set up celery
    _configure_celery()

    # Set up firewall
    sudo('ufw --force reset')
    sudo('ufw default deny incoming')
    sudo('ufw default allow outgoing')
    sudo('ufw allow ssh')
    sudo('ufw allow www')
    sudo('ufw allow 443/tcp')
    sudo('ufw disable')
    sudo('ufw --force enable')
    sudo('ufw status')


@task
def update_remote():
    """Update the code on the remote machine."""
    with cd('/home/chathan'):
        run('if ! test -d chmvh-website; then git clone {0}; fi'.format(
            REPOSITORY_URL))

    with cd(REMOTE_PROJECT_DIR):
        run('git pull && git checkout {0} && git pull'.format(
            env.current_branch))

    _configure_env()

    # Clear out static files
    with cd(REMOTE_PROJECT_DIR):
        run('rm -rf chmvh_website/staticfiles')<commit_msg>Remove old local settings during deployment.<commit_after>import os

from django.conf import settings as django_settings
from django.template import Context, Engine

from fabric.api import (
    cd, env, local, prefix, prompt, put, run, sudo, task)
from fabric.contrib.console import confirm

import yaml


django_settings.configure()


BASE_PATH = os.path.dirname(os.path.abspath(__file__))
CELERY_BIN = '/home/chathan/chmvh-website/env/bin/celery'
CREDENTIAL_MAP = {
    'db_name': 'database name',
    'db_password': '<PASSWORD>',
    'db_user': 'database user',
    'django_admin_email': 'Django administrator email',
    'django_admin_name': 'Django administrator name',
    'secret_key': 'Django secret key',
    'sendgrid_password': '<PASSWORD>',
    'sendgrid_user': 'SendGrid username',
    'sudo_password': '<PASSWORD>',
}
DJANGO_LOG_FILE = '/var/log/django/logfile'
REMOTE_PROJECT_DIR = '/home/chathan/chmvh-website'
REPOSITORY_URL = 'https://github.com/cdriehuys/chmvh-website'


required_packages = (
    'git',
    'letsencrypt',
    'libjpeg-dev',
    'libpq-dev',
    'postgresql', 'postgresql-contrib',
    'nginx',
    'python3-dev', 'python3-pip',
    'rabbitmq-server',
    'ufw',
    'zlib1g-dev',
)


class Credentials:
    FILE_PATH = os.path.join(BASE_PATH, 'secure-config.yml')

    credentials = None

    @classmethod
    def get(cls, name):
        if cls.credentials is None:
            cls.load_credentials()

        host_credentials = cls.credentials.get(env.host, {})

        existing = host_credentials.get(name, None)

        # If that credential is already stored, return it
        if existing is not None:
            return existing

        # Credential doesn't exist, so prompt for it and then save it
        message = 'Enter {0} for {1}:'.format(CREDENTIAL_MAP[name], env.host)
        host_credentials[name] = prompt(message)

        cls.credentials[env.host] = host_credentials
        cls._save_credentials()

        return host_credentials[name]

    @classmethod
    def load_credentials(cls):
        if os.path.exists(cls.FILE_PATH):
            with open(cls.FILE_PATH, 'r') as f:
                cls.credentials = yaml.load(f)
        else:
            cls.credentials = {}

    @classmethod
    def _save_credentials(cls):
        with open(cls.FILE_PATH, 'w') as f:
            yaml.dump(cls.credentials, f)


@task
def prepare_local():
    """Prepare local machine for deployment"""
    # From http://stackoverflow.com/a/11958481/3762084
    env.current_branch = local(
        'git rev-parse --symbolic-full-name --abbrev-ref HEAD',
        capture=True)

    if env.current_branch != 'master':
        if not confirm("You are trying to deploy from the '{0}' branch. "
                       "Is this what you want?".format(env.current_branch),
                       default=False):
            env.orig_branch = env.current_branch
            env.current_branch = prompt(
                "Enter branch to deploy from:",
                default='master')

            local('git checkout {}'.format(env.current_branch))

    local('git push')


@task
def remote_setup():
    """Set up the remote machine"""
    sudo('apt-get update -y')

    # Install required packages
    sudo('apt-get install -y {0}'.format(' '.join(required_packages)))

    # Set up the database
    _configure_database()

    # Set up webserver
    _configure_gunicorn()
    _configure_nginx()
    _configure_ssl()

    # Set up celery
    _configure_celery()

    # Set up firewall
    sudo('ufw --force reset')
    sudo('ufw default deny incoming')
    sudo('ufw default allow outgoing')
    sudo('ufw allow ssh')
    sudo('ufw allow www')
    sudo('ufw allow 443/tcp')
    sudo('ufw disable')
    sudo('ufw --force enable')
    sudo('ufw status')


@task
def update_remote():
    """Update the code on the remote machine."""
    with cd('/home/chathan'):
        run('if ! test -d chmvh-website; then git clone {0}; fi'.format(
            REPOSITORY_URL))

    with cd(REMOTE_PROJECT_DIR):
        run('git pull && git checkout {0} && git pull'.format(
            env.current_branch))

    _configure_env()

    # Clear out old files
    with cd(REMOTE_PROJECT_DIR):
        run('rm -rf chmvh_website/chmvh_website/local_settings.py')
        run('rm -rf chmvh_website/staticfiles')
{'fraction_non_alphanumeric': 0.1238886289190454, 'fraction_numerical': 0.006317267197005147, 'mean_word_length': 2.749561403508772, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '589414', 'n_tokens_mistral': 2813, 'n_tokens_neox': 2644, 'n_words': 659}