Skip to content

Instantly share code, notes, and snippets.

Forked from roselleebarle04/
Last active January 9, 2018 19:31
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Simple django fabric script for deployment
import os
import sys
from fab_deploy import *
from fabric.contrib.console import confirm
from fabric.api import env, cd, prefix, local, sudo as _sudo, run as _run, hide, task, settings, abort
from fabric.contrib.files import exists, upload_template
from fabric.colors import yellow, green, blue, red
from fabric.operations import _prefix_commands, _prefix_env_vars
from contextlib import contextmanager
What the fab script does:
Server configurations are found in a deploy folder within the project root.
Virtualenv is inside the project root as well.
1. Prepare fresh environment - Install sudo apt-packages (nginx, python, python-pip)
1. Create directory
1. Clone repository
1. Create Python virtual environment
1. Install requirements
1. Run using gunicorn, and nginx
1. Script for auto-update and reload
Of course, prior to using the script, make sure to update the variables.
Upon deployment, hit fab prod deploy
""" Global Configurations """
ssh_user = 'ubuntu'
ssh_host = os.getenv('EC2_HOSTNAME')
project_dir = '/home/%s' % (ssh_user)
project_name = 'parature_search'
project_root = '%s/%s' % (project_dir, project_name)
repo_url = ''
""" Templates """
templates = {
"nginx": {
"local_path": "/home/nick/sites/parature_search/deploy/nginx/parature_search.conf",
"remote_path": "/etc/nginx/sites-available/parature_search.conf",
"remote_path2": "/etc/nginx/sites-enabled/parature_search.conf",
"reload_command": "service nginx restart",
"gunicorn": {
"local_path": "/home/nick/sites/parature_search/deploy/gunicorn/gunicorn_start",
""" Global Commands """
def prod():
env.hosts = ['{0}@{1}'.format(ssh_user, ssh_host)]
env.user = ssh_user
# env.password = 'password'
def virtualenv():
Runs commands within the project's virtualenv.
with cd(project_root):
with prefix("source venv/bin/activate"):
""" Pre-deployment Commands """
def prepare_git():
local("git add -p && git commit")
local("git push")
except: pass
def prepare_packages():
sudo("apt-get update")
sudo("apt-get upgrade")
sudo("apt-get install nginx python-dev libpq-dev git-core")
# sudo("sudo apt-get install python-dev python-setuptools libtiff5-dev libjpeg8-dev zlib1g-dev libfreetype6-dev liblcms2-dev libwebp-dev tcl8.6-dev tk8.6-dev python-tk")
sudo("easy_install pip")
sudo("pip install virtualenv")
def prepare_server_config():
""" prepare nginx """
if exists(templates['nginx']['remote_path']):
sudo("rm %s" % templates['nginx']['remote_path'])
sudo("rm %s" % templates['nginx']['remote_path2'])
sudo("cp %s %s" % (templates['nginx']['local_path'], templates['nginx']['remote_path']))
sudo("ln -s %s %s" % (templates['nginx']['remote_path'], templates['nginx']['remote_path2']))
with cd(project_root):
if exists("deploy/logs"):
sudo("rm -r deploy/logs")
sudo("mkdir deploy/logs")
sudo("touch deploy/logs/nginx-access.log")
sudo("touch deploy/logs/nginx-error.log")
sudo("touch deploy/logs/gunicorn.log")
with cd(project_root):
sudo("chmod +x deploy/gunicorn/gunicorn_start")
""" reload configurations """
sudo("service nginx restart")
sudo("service nginx reload")
def deploy():
Utility function for deployment: This fab script deploys your local django app
to your remote server using Gunicorn, Supervisor & Nginx. Addons: Solr/Haystack, Celery,
Make sure that key-based authentication between server and local is enabled (See id_rsa)
"""Prepare Local"""
"""Prepare Remote Server"""
""" Create directory for project """
if exists(project_dir):
sudo("rm -r %s" % project_dir)
run("mkdir %s" % (project_dir))
""" Clone repository """
with cd(project_dir):
run("git clone %s" % (repo_url))
"""Setup virtualenv and install requirements"""
with cd(project_root):
run("virtualenv venv")
with virtualenv():
run("pip install -r requirements.txt")
run("venv/bin/python migrate")
run("venv/bin/python syncdb")
""" Prepare gunicorn or uwsgi & nginx """
""" Post Deployment Configurations """
with virtualenv():
with cd(project_root):
run("python collectstatic")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment