Skip to content

Instantly share code, notes, and snippets.

@hzbd
Created April 23, 2013 18:35
Show Gist options
  • Save hzbd/5446169 to your computer and use it in GitHub Desktop.
Save hzbd/5446169 to your computer and use it in GitHub Desktop.
fabric deployment QpV-0424_01
# Template fabric deployment file for deploying Git projects
import datetime
from fabric.api import local, run, sudo, env
from fabric.colors import green, red
from fabric.context_managers import cd, lcd
from fabric.operations import put, prompt
from fabconfig import *
# Utilities
def _get_commit_id():
"Returns the commit ID for the branch about to be deployed"
return local('git rev-parse HEAD', capture=True)[:20]
# Deployment tasks
def deploy(branch='master', repo='origin'):
"""
Deploys the codebase to the VM
Usage:
$ fab dev deploy # Deploy to dev
$ fab stage deploy # Deploy to stage
$ fab production deploy # Deploy to production
"""
update_codebase(branch, repo)
commit_id = _get_commit_id()
print green("Building from revision %s" % commit_id)
archive_file = '/tmp/build-%s.tar.gz' % str(commit_id)
prepare_build(archive_file)
env.user = prompt('Username for remote host? [default is current user] ')
upload(archive_file)
unpack(archive_file, commit_id)
_deploy_apache_config()
_deploy_cronjobs()
restart()
def update_codebase(branch='master', repo='origin'):
"""Updates the codebase from the Git repo"""
print(green('Updating codebase from remote "%s", branch "%s"' % (repo, branch)))
local('git pull %s %s' % (repo, branch))
def test():
"""Runs the tests"""
print green('Running tests')
with lcd(env.web_dir):
local('./manage.py test %(test_apps) --settings=settings_test')
def prepare_build(archive_file, branch='master'):
"""Creates a gzipped tarball with the code to be deployed"""
local('git archive --format tar %s %s | gzip > %s' % (branch, env.web_dir, archive_file))
def upload(local_path, remote_path=None):
"""Uploads a file"""
if not remote_path:
remote_path = local_path
print green("Uploading %s to %s" % (local_path, remote_path))
put(local_path, remote_path)
def unpack(archive_path, commit_id):
"""Unpacks the tarball into the correct place"""
print green("Creating build folder")
now = datetime.datetime.now()
env.build_dir = '%s-%s' % (env.build, now.strftime('%Y-%m-%d-%H-%M'))
with cd(env.builds_dir):
sudo('tar xzf %s' % archive_path)
# Create new build folder
sudo('if [ -d "%(build_dir)s" ]; then rm -rf "%(build_dir)s"; fi' % env)
sudo('mv %(web_dir)s %(build_dir)s' % env)
# Create new symlink
sudo('if [ -h %(build)s ]; then unlink %(build)s; fi' % env)
sudo('ln -s %(build_dir)s %(build)s' % env)
# Add file indicating Git commit
sudo('touch %(build)s/built-from-commit-%s' % commit_id)
# Create a file indicating Git commit
sudo('rm %s' % archive_path)
def _deploy_apache_config():
"""Deploys the apache config"""
print green('Moving apache config into place')
with cd(env.builds_dir):
sudo('mv %(build)s/%(apache_conf)s /etc/apache2/sites-available/' % env)
def _deploy_cronjobs():
"""Deploys the cron jobs"""
print green('Deploying cronjobs')
with cd(env.builds_dir):
sudo('if [ $(ls %(build)s/cron.d) ]; then mv %(build)s/cron.d/* /etc/cron.d/; fi' % env)
def restart():
"""Reloads python code"""
print green('Touching WSGI file to reload python code')
with cd(env.builds_dir):
sudo('touch %(build)s/%(wsgi)s' % env)
def apache_reload():
"""Reloads apache config"""
sudo('/etc/init.d/apache2 force-reload')
def apache_restart():
"""Restarts apache"""
sudo('/etc/init.d/apache2 restart')
def nginx_reload():
"""Reloads nginx config"""
sudo('/etc/init.d/nginx force-reload')
def nginx_restart():
"""Restarts nginx"""
sudo('/etc/init.d/nginx restart')
def apache_configtest():
"""Checks apache config syntax"""
sudo('/usr/sbin/apache2ctl configtest')
def nginx_configtest():
"""Checks nginx config syntax"""
sudo('/usr/sbin/nginx -t')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment