Skip to content

Instantly share code, notes, and snippets.

@joepreludian
Last active July 23, 2016 16:38
Show Gist options
  • Save joepreludian/8837192 to your computer and use it in GitHub Desktop.
Save joepreludian/8837192 to your computer and use it in GitHub Desktop.
Simple Fabfile for django based apps deployment
# -*- encoding: utf8 -*-
from fabric.api import env, settings, cd
from fabric.contrib.files import exists
from fabric.operations import local, sudo, run, put
import os
# Default configurations
env.hosts = ['[email protected]']
env.project_name = "trids"
env.install_to = "/webapps/trids"
env.static_dir = "%s/static" % env.install_to
env.git_repo = "[email protected]:trilhus/trids.git"
env.site_url = "trids.universo42.com.br"
env.site_port = "80"
# Environment params
env.virtualenv_folder = '%s/env/bin' % env.install_to
env.nginx_upstream_resource = "%s_upstream" % env.project_name
env.gunicorn_socket_file = "%(virtualenv_folder)s/sock" % env
env.log_error_file = "%s/env/logs/nginx-error.log" % env.install_to
env.log_acess_file = "%s/env/logs/nginx-access.log" % env.install_to
env.static_dir = "%s/static/" % env.install_to # MUST have ending slash
env.media_dir = "%s/media/" % env.install_to
env.circusd_file = "/opt/circus/watchers/%(project_name)s.ini" % env
env.nginx_file = "/etc/nginx/sites-enabled/%(project_name)s.conf" % env
## Enviroment specific settings
env.commands = {
'virtualenv': '/usr/bin/virtualenv',
'bower': '/opt/nodejs/bin/bower' # Because of Debian custom Install
}
#
# Tools
#
def run_in_virtualenv(command):
run('%s/%s' % (env.virtualenv_folder, command))
#
# Custom actions
#
def clone_repo():
run('git clone %(git_repo)s %(install_to)s' % env)
with cd(env.install_to):
run('mkdir static')
run('mkdir media')
def setup_virtualenv():
with cd(env.install_to):
run('%s env' % env.commands['virtualenv'])
run('mkdir env/logs') # Criação dos logs
def update_requirements():
run_in_virtualenv('pip install -r %s/requirements.pip' % env.install_to)
run_in_virtualenv('pip install gunicorn')
def init_django():
with cd(env.install_to):
run_in_virtualenv('python manage.py syncdb')
run_in_virtualenv('python manage.py migrate --all')
run_in_virtualenv('python manage.py collectstatic --noinput')
def sync_bower():
with cd(env.static_dir):
run('%s install' % env.commands['bower'])
def create_circusd():
template_file = """[watcher:%(project_name)s]
cmd = env/bin/gunicorn
args = %(project_name)s.wsgi:application --bind unix:%(gunicorn_socket_file)s
working_dir = %(install_to)s
numprocesses = 3""" % env
f = open('circusd.ini' % env, 'w')
f.write(template_file)
f.close()
put(local_path="circusd.ini", remote_path=env.circusd_file, use_sudo=True)
os.remove('circusd.ini')
def create_nginx():
template_file = """upstream %(nginx_upstream_resource)s {
server unix:%(gunicorn_socket_file)s fail_timeout=0;
}
server {
listen %(site_port)s;
server_name %(site_url)s;
client_max_body_size 4G;
access_log %(log_acess_file)s;
error_log %(log_error_file)s;
location /static/ {
alias %(static_dir)s;
}
location /media/ {
alias %(media_dir)s;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://%(nginx_upstream_resource)s;
break;
}
}
error_page 500 502 503 504 /500.html;
location = /500.html {
root %(static_dir)s;
}
}""" % env
f = open('nginx.conf' % env, 'w')
f.write(template_file)
f.close()
put(local_path="nginx.conf", remote_path=env.nginx_file, use_sudo=True)
os.remove('nginx.conf')
def restart_services():
with settings(warn_only=True):
sudo('/etc/init.d/nginx reload')
sudo('/opt/circus/bin/circusctl reload')
#
# Main functions
#
def setup():
clone_repo()
setup_virtualenv()
update_requirements()
init_django()
sync_bower()
create_circusd()
create_nginx()
restart_services()
def deploy():
create_circusd()
create_nginx()
with cd(env.install_to):
run('git pull')
update_requirements()
run('rm -Rf static/*')
run_in_virtualenv('python manage.py collectstatic --noinput')
run_in_virtualenv('python manage.py migrate --all') # south migrate
sync_bower()
restart_services()
def uninstall():
sudo('rm -Rf %(install_to)s' % env)
sudo('rm -Rf %(circusd_file)s' % env)
sudo('rm -Rf %(nginx_file)s' % env)
restart_services()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment