Source

copelco / fabfile.py

Full commit
import os
import random
import string
from fabric.api import cd, env, local, require, run, sudo
from fabric.contrib.files import exists, upload_template


env.project = 'copelco'
env.user = 'website'
env.home = '/home/%(user)s/' % env
env.repo = u'https://copelco@bitbucket.org/copelco/copelco'
# remove -l from env.shell, "mesg n" in ~/.profile was causing issues
# see Why do I sometimes see ``err: stdin: is not a tty``?
# http://github.com/bitprophet/fabric/blob/master/FAQ
env.shell = '/bin/bash -c'


def _setup_path():
    env.root = os.path.join(env.home, 'www', env.environment)
    env.log_dir = os.path.join(env.root, 'log')
    env.code_root = os.path.join(env.root, 'code_root')
    env.project_root = os.path.join(env.code_root, 'lib', env.project)
    env.virtualenv_root = os.path.join(env.root, 'env')
    env.services = os.path.join(env.home, 'services')
    env.db = '%s_%s' % (env.project, env.environment)
    env.vhost = '%s_%s' % (env.project, env.environment)


def staging():
    """ run commands on the remote staging environment """
    env.environment = 'staging'
    env.hosts = ['ec2-50-19-40-110.compute-1.amazonaws.com']
    env.branch = 'default'
    env.server_port = '7000'
    _setup_path()


def production():
    """ run commands on the remote staging environment """
    env.environment = 'production'
    env.hosts = ['ec2-50-19-40-110.compute-1.amazonaws.com']
    env.branch = 'default'
    env.server_port = '8000'
    _setup_path()


def create_virtualenv():
    """ create virtual environment on remote host """
    require('virtualenv_root', provided_by=('production', 'staging'))
    args = '--clear --distribute'
    run('virtualenv -q %s %s' % (args, env.virtualenv_root))


def update_requirements():
    """ update remote Python environment """
    require('code_root', provided_by=('production', 'staging'))
    requirements = os.path.join(env.code_root, 'requirements')
    with cd(requirements):
        cmd = ['pip install']
        cmd += ['-q -E %(virtualenv_root)s' % env]
        cmd += ['--requirement %s' % os.path.join(requirements, 'apps.txt')]
        run(' '.join(cmd))


def clone():
    """ clone github repository on remote machine """
    require('code_root', provided_by=('production', 'staging'))
    run('mkdir -p %(root)s' % env)
    with cd(env.root):
        run('hg clone %(repo)s %(code_root)s' % env)


def update_source():
    """ pull latest code to remote environment """
    require('code_root', provided_by=('production', 'staging'))
    with cd(env.code_root):
        run('hg pull')
        run('hg update %(branch)s' % env)


def upload_supervisor_conf():
    """ upload Supervisor configuration from the template """
    require('environment', provided_by=('staging', 'production'))
    template = os.path.join(os.path.dirname(__file__), 'services', 'templates', 'supervisor.conf')
    destination = os.path.join(env.services, 'supervisor', '%(environment)s.conf' % env)
    upload_template(template, destination, context=env)
    _supervisor_command('update')


def upload_nginx_conf():
    """ upload Nginx configuration from the template """
    require('environment', provided_by=('staging', 'production'))
    template = os.path.join(os.path.dirname(__file__), 'services', 'templates', 'nginx.conf')
    destination = os.path.join(env.services, 'nginx', '%(environment)s.conf' % env)
    upload_template(template, destination, context=env)
    restart_nginx()


def upload_gunicorn_conf():
    """ upload Gunicorn configuration from the template """
    require('environment', provided_by=('staging', 'production'))
    template = os.path.join(os.path.dirname(__file__), 'services', 'templates', 'gunicorn.conf')
    destination = os.path.join(env.services, 'gunicorn', '%(environment)s.py' % env)
    upload_template(template, destination, context=env)


def update_services():
    """ upload changes to services configurations as nginx """
    upload_supervisor_conf()
    upload_nginx_conf()
    upload_gunicorn_conf()
    netstat_plnt()


def netstat_plnt():
    """ run netstat -plnt on a remote host """
    require('hosts', provided_by=('production', 'staging'))
    run('sudo netstat -plnt')


def restart_nginx():
    """ restart nginx on remote machine """
    require('hosts', provided_by=('production', 'staging'))
    run('sudo /etc/init.d/nginx restart')


def _supervisor_command(command):
    sudo('supervisorctl %s' % command)


def restart_server():
    """ restart gunicorn server """
    require('environment', provided_by=('staging', 'production'))
    _supervisor_command('restart %(environment)s:%(environment)s-server' % env)


def restart_supervisor():
    """ restart all Supervisor controlled processes """
    require('environment', provided_by=('staging', 'production'))
    _supervisor_command('restart %(environment)s:*' % env)


def restart_all():
    """ restart Nginx and Supervisor controlled processes """
    restart_nginx()
    restart_supervisor()


def bootstrap():
    """ bootstrap remote environment """
    require('hosts', provided_by=('production', 'staging'))
    clone()
    create_virtualenv()
    update_requirements()
    setup_dir_permissions()


def setup_dir_permissions():
    """ make upload media dirs writable """
    require('environment', provided_by=('production', 'staging'))
    run('mkdir -p %(log_dir)s' % env)
    #sudo('chown :www-data %(log_dir)s' % env, user='copelco')
    #sudo('chmod g+w %(log_dir)s' % env, user='copelco')
    run('mkdir -p %(services)s/nginx' % env)
    run('mkdir -p %(services)s/supervisor' % env)
    run('mkdir -p %(services)s/gunicorn' % env)


def syncdb():
    """ run syncdb and south migrations """
    require('environment', provided_by=('staging', 'production'))
    run('%(virtualenv_root)s/bin/python %(project_root)s/manage.py syncdb --noinput' % env)
    run('%(virtualenv_root)s/bin/python %(project_root)s/manage.py migrate --noinput' % env)


def deploy():
    """ deploy to a given environment """
    require('environment', provided_by=('staging', 'production'))
    update_source()
    update_requirements()
    syncdb()
    collectstatic()
    restart_server()