Skip to content

Instantly share code, notes, and snippets.

@roselleebarle04
Created February 21, 2016 01:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save roselleebarle04/9807df87c7f32f9507bd to your computer and use it in GitHub Desktop.
Save roselleebarle04/9807df87c7f32f9507bd to your computer and use it in GitHub Desktop.
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:
The script is meant to deploy a bookshop application to a digital ocean vsp (works with any vps). 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, php, python, python-pip)
2. Create directory
3. Clone repository
4. Create virtualenvironment
5. Install Requirements
6. Run using gunicorn, supervisor, and nginx
7. Setup Solr
8. 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
Enjoy!
"""
""" Global Configurations """
ssh_user = 'roselle'
project_dir = '/home/%s/sites' % (ssh_user)
project_name = 'bookshop'
project_root = '%s/%s' % (project_dir, project_name)
venv_path = ''
repo_url = '<project repository>'
""" DB Configurations """
db_name = 'bookshopdb'
db_user = 'bookshop'
db_pass = 'bookshop'
""" Templates """
templates = {
"nginx": {
"local_path": "/home/roselle/sites/bookshop/deploy/nginx/bookshop.conf",
"remote_path": "/etc/nginx/sites-available/bookshop.conf",
"remote_path2": "/etc/nginx/sites-enabled/bookshop.conf",
"reload_command": "service nginx restart",
},
"supervisor": {
"local_path": "/home/roselle/sites/bookshop/deploy/supervisor/bookshop.conf",
"remote_path": "/etc/supervisor/conf.d/bookshop.conf",
"reload_command": "supervisorctl reload",
},
"gunicorn": {
"local_path": "/home/roselle/sites/bookshop/deploy/gunicorn/gunicorn_start",
},
"solr": {
"local_conf": "/home/roselle/sites/bookshop/deploy/solr/solr_conf",
"remote_path": "/home/roselle/sites/bookshop/deploy/solr/solr-4.7.2/example/solr/collection1/conf",
},
}
""" Global Commands """
def prod():
env.hosts = ['roselle@111.111.111.111']
env.user = 'roselle'
env.password = 'password'
@contextmanager
def virtualenv():
"""
Runs commands within the project's virtualenv.
"""
with cd(project_root):
with prefix("source venv/bin/activate"):
yield
""" Pre-deployment Commands """
def prepare_db():
""" Setup Postgresql Database """
try:
run('sudo su - postgres psql -c "dropdb %s"' % (db_name))
run('sudo su - postgres psql -c "dropuser %s"' % (db_user))
except: pass
run('sudo su - postgres psql -c "createdb %s;"' % (db_name))
run('sudo su - postgres psql -c "createuser -P -s -d -r %s"' % (db_user))
def prepare_git():
try:
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 postgresql memcached supervisor redis-server mercurial python-dev libpq-dev memcached unixodbc-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")
sudo("apt-get install uwsgi")
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")
""" prepare uwsgi """
# sudo("pip install uwsgi")
# if exists(templates['uwsgi']['remote_conf']):
# sudo("rm -r %s" % templates['uwsgi']['remote_conf'])
# if exists(templates['uwsgi']['remote_upstart']):
# sudo("rm %s" % templates['uwsgi']['remote_upstart'])
# sudo("mkdir -p %s" % templates['uwsgi']['remote_conf'])
# sudo("cp %s %s" % (templates['uwsgi']['local_conf'], templates['uwsgi']['remote_conf']))
# sudo("cp %s %s" % (templates['uwsgi']['local_upstart'], templates['uwsgi']['remote_upstart']))
""" prepare supervisor """
if exists(templates['supervisor']['remote_path']):
sudo("rm %s" % templates['supervisor']['remote_path'])
sudo("cp %s %s" % (templates['supervisor']['local_path'], templates['supervisor']['remote_path']))
with cd(project_root):
sudo("chmod +x deploy/gunicorn/gunicorn_start")
""" reload configurations """
sudo("service nginx restart")
sudo("service nginx reload")
# sudo("service uwsgi start")
sudo("supervisorctl reread")
sudo("supervisorctl update")
sudo("supervisorctl restart all")
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,
Redis/Memcache.
Make sure that key-based authentication between server and local is enabled (See id_rsa)
"""
""" Stop all current services/processes """
sudo("supervisorctl stop all")
"""Prepare Local"""
prepare_git()
"""Prepare Remote Server"""
prepare_packages()
""" Prepare Database """
prepare_db()
""" 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("pip install gunicorn")
run("venv/bin/python manage.py migrate")
run("venv/bin/python manage.py syncdb")
""" Prepare gunicorn or uwsgi & nginx """
prepare_server_config()
""" Post Deployment Configurations """
with virtualenv():
with cd(project_root):
run("pip install pycountry")
run("python manage.py oscar_populate_countries")
run("python manage.py collectstatic")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment