Created
October 27, 2011 20:47
-
-
Save epicserve/1320821 to your computer and use it in GitHub Desktop.
Django project fabfile template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fabric.api import cd, run, env, local | |
from fabric.colors import green | |
from django.conf import settings | |
env.hosts = ['foo.example.com'] | |
env.code_dir = '/var/www/project' | |
env.virtualenv = '/usr/local/virtualenvs/project' | |
env.django_project_root = settings.DJANGO_PROJECT_ROOT | |
env.django_settings_module = 'config.settings' | |
def sync_media(): | |
"Pulls and syncs photos from the production server your local dev instance" | |
local("rsync -avz -e ssh %s:%s/media/ %s/media/" % (env.host, env.code_dir, env.django_project_root)) | |
def sync_database(): | |
"Refreshes the local copy of the database passed in by pulling it from the server specified" | |
params = {'db_name': 'db_name'} | |
params.update(env) | |
# dump the database and compress it on the remote server | |
run('/usr/local/pgsql/bin/pg_dump %(db_name)s | gzip > /tmp/%(db_name)s.sql.gz' % params) | |
# securely copy the compressed file to local temporary storage | |
local('scp %(host)s:/tmp/%(db_name)s.sql.gz /tmp/' % params) | |
print('Creating database...[%s]' % green(params['db_name'], True)) | |
# drop it if it exists and re-create | |
local('psql -d template1 -c "drop database if exists %(db_name)s;" && createdb %(db_name)s -O %(db_name)s' % params) | |
# unzip the temp file and restore | |
local('gunzip -f /tmp/%(db_name)s.sql.gz' % params) | |
local('cat /tmp/%(db_name)s.sql | psql %(db_name)s' % params) | |
local('rm /tmp/%(db_name)s.sql' % params) | |
run('rm /tmp/%(db_name)s.sql.gz' % params) | |
local('''psql -d %(db_name)s -c "UPDATE django_site SET domain = '127.0.0.1:8000', name = '127.0.0.1:8000' WHERE id = 1;"''' % params) | |
def push(): | |
"Push new code and pull on all hosts" | |
local('git push origin master') | |
with cd(env.code_dir): | |
run('git pull') | |
def update_requirements(): | |
"Update requirements in the virtualenv." | |
run("%s/bin/pip install -r %s/config/requirements.txt" % (env.virtualenv, env.code_dir)) | |
def migrate(app=None): | |
"""Run the migrate task | |
Usage: fab migrate:app_name""" | |
if app: | |
run("source %s/bin/activate; django-admin.py migrate %s --settings=%s" % (env.virtualenv, app, env.django_settings_module)) | |
else: | |
run("source %s/bin/activate; django-admin.py migrate --settings=%s" % (env.virtualenv, env.django_settings_module)) | |
def reload_site(): | |
"Makes the site reload" | |
run("touch %s/config/apache/project.wsgi" % env.code_dir) | |
def deploy(): | |
"Deploy from the current local dev site to production" | |
push() | |
update_requirements() | |
migrate() | |
reload_site() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment