Skip to content

Instantly share code, notes, and snippets.

@jeanphix
Created February 23, 2013 16:50
Show Gist options
  • Save jeanphix/5020421 to your computer and use it in GitHub Desktop.
Save jeanphix/5020421 to your computer and use it in GitHub Desktop.
django deployment from git remotes.
# -*- coding: utf-8 -*-
from fabric.state import env
from fabric.api import cd, local, run, abort, task
from fabric.context_managers import prefix
def worktree(cmd):
with prefix('source /usr/bin/virtualenvwrapper.sh'):
with cd(env.repo_path):
with prefix('cd `git config --get core.worktree`'):
with prefix('source .env'):
run('honcho run %s' % cmd)
def get_url(remote):
try:
return local('git config --get remote.%s.url' % remote, capture=True)
except:
abort('%s is not a valid git remote' % remote)
@task
def checkout(commit=None):
with cd(env.repo_path):
run('git checkout %s -f' % (commit if commit is not None else ''))
@task
def collectstatic():
worktree('python manage.py collectstatic --noinput')
@task
def deploy(commit=None):
push()
checkout(commit)
requirements()
collectstatic()
stop()
migrate()
start()
@task
def migrate():
worktree('python manage.py syncdb --noinput')
worktree('python manage.py migrate --noinput')
@task
def push():
for remote in env.remotes:
local('git push %s master' % remote)
@task
def requirements():
worktree('pip install -r requirements.txt')
@task
def remotes(remotes):
remotes = {remote: get_url(remote) for remote in remotes.split(' ')}
env.repo_path = next(remotes.itervalues()).split(':')[1]
env.hosts = [remotes[r].split(':')[0] for r in remotes]
env.remotes = remotes
@task
def restart():
worktree('sudo /usr/bin/systemctl restart ${APP}')
@task
def start():
worktree('sudo /usr/bin/systemctl start ${APP}')
@task
def stop():
worktree('sudo /usr/bin/systemctl stop ${APP}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment