Skip to content

Instantly share code, notes, and snippets.

@varikin
Created February 20, 2012 23:41
Show Gist options
  • Select an option

  • Save varikin/1872331 to your computer and use it in GitHub Desktop.

Select an option

Save varikin/1872331 to your computer and use it in GitHub Desktop.
A sort of hand crafted fabfile that servers my needs
from os.path import join
from fabric.api import env, require, run, cd
# Fabfile to deploy a Django app. To use run:
#
# fab staging deploy
#
# or
#
# fab prod deploy
# Settings for each environment
# Defines the base location of each project plus values to override the defaults.
# Each host needs at least a host and project_root defined.
SETTINGS = {
'staging': {
'host': 'staging.example.com',
'project_root': '/path/to/project',
},
'production': {
'host': 'production.example.com',
'project_root': '/path/to/project',
'reload_file': '/different/path/to/django.wsgi',
},
}
def set_host(host):
"""Sets all the environment variables for a host.
Host should be one of the hosts within SETTINGS.
Note that dict.get() is used to get values from the host configuration with
default values if not set for a host.
"""
# env.hosts is required by Fabric.
env.hosts = [host['host']]
# Project root is used to define all other location.
project_root = host['project_root']
# Path to the virtualenv activate script
env.activate_script = host.get('activate_script', join(project_root, 'bin/activate'))
# Root of the project repository
env.repo_root = host.get('repo_root', join(project_root, 'app/project'))
# Path to the Django manage.py file
env.manage_dir = host.get('manage_dir', env.repo_root)
# Path to the pip requirments file
env.requirements_file = host.get('requirements_file', join(env.repo_root, 'requirements.txt'))
# Path to the wsgi file to touch for reloading the application
env.reload_file = host.get('reload_file', join(env.repo_root, 'django.wsgi'))
# Command to update the repository
env.update_command = host.get('update_command', 'svn update')
def staging():
""Fab command to set all the staging paths."""
set_host(SETTINGS['staging'])
def production():
"""Fab task to set all the production paths."""
set_host(SETTINGS['production'])
prod = production # Short name for production
def virtualenv(command):
""" Wrapper to run all tasks in the virtualenv."""
run('source "%s" && %s' % (env.activate_script, command))
def update():
"""Task to update the repository"""
require('repo_root', provided_by=['staging', 'production'])
with cd(env.repo_root):
run(env.update_command)
def install_requirements():
"""Task to install any new dependencies for the project."""
require('repo_root', provided_by=['staging', 'production'])
virtualenv('pip install -q -r %(requirements_file)s' % env)
def manage_py(command):
"""Task to run any management tasks."""
require('repo_root', provided_by=['staging', 'production'])
with cd(env.manage_dir):
virtualenv('python manage.py %s' % command)
def syncdb():
"""Task to sync the database."""
require('repo_root', provided_by=['staging', 'production'])
manage_py('syncdb --noinput')
def migrate():
"""Task to migrate the database."""
require('repo_root', provided_by=['staging', 'production'])
manage_py('migrate')
def collectstatic():
"""Task to collect all the static files."""
require('repo_root', provided_by=['staging', 'production'])
manage_py('collectstatic --noinput')
def reload():
"""Task to reload the application."""
require('repo_root', provided_by=['staging', 'production'])
run('touch %s' % env.reload_file)
def deploy():
"""Task to deploy the latest version of the app."""
update()
install_requirements()
syncdb()
migrate()
collectstatic()
reload()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment