Skip to content

Instantly share code, notes, and snippets.

@albertojacini
Last active December 17, 2015 16:59
Show Gist options
  • Save albertojacini/5642806 to your computer and use it in GitHub Desktop.
Save albertojacini/5642806 to your computer and use it in GitHub Desktop.
Fabfile for deployment on Webfaction
# Fabfile for deployment on Webfaction
from fabric.api import *
# This is used by fabric to login in the server and must be set to the Webfaction account username.
env.user = 'iperborea'
### Define roles ###
def staging():
"""
* SELECTION FUNCTION * Example command: fab staging deploy
"""
env.hosts = ['web331.webfaction.com']
env.code_dir = '/home/iperborea/iperborea/dev/iperborea_project/iperborea'
env.settings_module = 'iperborea.settings.staging'
env.apache_dir = '/home/iperborea/webapps/dev_iperborea/apache2/bin'
env.virtualenv_name = 'dev_iperborea'
env.git_branch = 'develop'
def production():
"""
* SELECTION FUNCTION * Example command: fab production deploy
"""
env.hosts = ['web331.webfaction.com']
env.code_dir = '/home/iperborea/iperborea/www/iperborea_project/iperborea'
env.settings_module = 'iperborea.settings.production'
env.apache_dir = '/home/iperborea/webapps/iperborea/apache2/bin'
env.virtualenv_name = 'production_iperborea'
env.git_branch = 'master'
### Commands ###
def full_deploy():
"""
Full deploy: new code, update dependencies, migrate, and restart services.
"""
deploy_code()
# update_dependencies()
migrate()
collectstatic()
restart_apache()
# memcached("restart")
def deploy():
"""
Quick deploy: new code and an in-place reload.
"""
deploy_code()
collectstatic()
restart_apache()
def deploy_code():
"""
Update code on the servers from Git.
"""
puts('Executing git pull...')
with cd(env.code_dir):
run('git pull origin {0}'.format(env.git_branch))
def restart_apache():
"""
Restart Apache.
"""
puts('Restarting Apache...')
with cd(env.apache_dir):
run('./restart')
def collectstatic():
"""
Run collectstatic.
"""
managepy('collectstatic --noinput')
def migrate():
"""
Run migrate/syncdb.
"""
managepy('syncdb')
managepy('migrate')
def managepy(cmd):
"""
Helper: run a management command remotely.
"""
with cd(env.code_dir):
with prefix('workon {0}'.format(env.virtualenv_name)):
run('python manage.py {0} --settings={1}'.format(cmd, env.settings_module))
def print_working_dir():
"""
Use it for testing fabfile
"""
with cd(env.code_dir):
with prefix('workon {0}'.format(env.virtualenv_name)):
run('pwd')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment