-
-
Save nrrb/6b178e5471b6478755456f38e54e66bd to your computer and use it in GitHub Desktop.
Simple django fabric script for deployment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
""" | |
Credit: https://gist.github.com/roselleebarle04/9807df87c7f32f9507bd | |
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 | |
Enjoy! | |
""" | |
""" 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 = 'https://github.com/tothebeat/parature-search.git' | |
""" 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' | |
@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_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 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, | |
Redis/Memcache. | |
Make sure that key-based authentication between server and local is enabled (See id_rsa) | |
""" | |
"""Prepare Local""" | |
prepare_git() | |
"""Prepare Remote Server""" | |
prepare_packages() | |
""" 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 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("python manage.py collectstatic") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment