Source

pushingkarma / fabfile.py

Full commit
"""
Deploy code from the BitBucket repo on the production server.
"""
import os
from fabric.api import *
from fabric.contrib.files import *

env.hosts = ["pushingkarma.com"]
env.directory = '/home/mjs7231/Projects/pushingkarma'
env.virtualenv = '/home/mjs7231/.virtualenvs/pushingkarma'
env.privatemount = '/home/mjs7231/Projects/'


def virtualenv(command):
    """ Run a virtualenv command. """
    activate = 'source %s/bin/activate' % env.virtualenv
    with cd(env.virtualenv):
        run('%s && %s' % (activate, command))


##############################
#  Useful Commands
##############################

def check_privatemount():
    """ Check the Private dir is mounted. """
    if not len(os.listdir(env.privatemount)):
        raise SystemExit("Private directory not mounted!")

def collectstatic():
    """ Collect static files. """
    run('rm -rf %s/collectstatic' % env.directory)
    virtualenv('%s/manage.py collectstatic -l --noinput --verbosity=0' % env.directory)

def copy_secrets():
    """ Copy the settings.secrets.py to production. """
    secrets_local = "%s/settings/secrets.py" % os.path.dirname(__file__)
    secrets_remote = "%s/settings/secrets.py" % env.directory
    put(secrets_local, secrets_remote)

def reload_apache():
    """ Reload the apache server. """
    sudo('service apache2 reload', shell=False)

def restart_apache():
    """ Reload the apache server. """
    sudo('service apache2 restart', shell=False)

def update_jquery_wysiwym():
    """ Update jquery-wysiwym source. """
    with cd('/home/mjs7231/Projects/jquery-wysiwym'):
        run('hg fetch')

def update_mypip():
    """ Update only my projects in pip. """
    virtualenv('pip install -U hg+https://bitbucket.org/mjs7231/django-dbbackup')
    virtualenv('pip install -U hg+https://bitbucket.org/mjs7231/django-betteremail')

def update_pip():
    """ Update to the latest requirements.pip. """
    requirments = '%s/environment/requirements.pip' % env.directory
    virtualenv('pip install -r %s' % requirments)

def update_source():
    """ Update the project source. """
    with cd(env.directory):
        run('hg fetch')


##############################
#  Deploy to Production
##############################

def pip_full():
    """ Update to the latest requirements.pip. """
    requirments = '%s/environment/requirements.pip' % env.directory
    virtualenv('pip install -U -r %s' % requirments)

def deploy():
    """ Deploy to the production server. """
    check_privatemount()
    update_source()
    update_jquery_wysiwym()
    update_pip()
    copy_secrets()
    collectstatic()
    reload_apache()