Skip to content

Instantly share code, notes, and snippets.

@starenka
Created September 18, 2011 20:02
Show Gist options
  • Save starenka/1225493 to your computer and use it in GitHub Desktop.
Save starenka/1225493 to your computer and use it in GitHub Desktop.
fabfile to deploy flask app to nginx/supervisor/uwsgi stack
.idea/*
*.pyc
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import StringIO
from fabric.api import *
from fabric.operations import get, put
from jinja2 import Template
env.hosts = ['vps']
env.user = "starenka"
VHOST = 'my.domain.net'
APPS_DIR = '/www'
APP_ROOT = '%s%s' % (APPS_DIR, VHOST.replace('.', '_'))
MODULE = 'main_app_module_filename'
REPOS = '/repos/hg'
STATIC = 'static'
SUPERVISOR_DIR = '/etc/supervisor/conf.d/'
NGINX_DIR = '/etc/nginx/sites-'
def _render_template(string, context):
return Template(string).render(context)
def _make_supervisor_conf():
template = StringIO.StringIO()
get('%sflaskapp.tpl' % SUPERVISOR_DIR, template)
interpolated = StringIO.StringIO()
interpolated.write(_render_template(template.getvalue(), {
'domain': VHOST,
'root': APP_ROOT,
'module': MODULE
}))
put(interpolated, '%(supervisor_dir)s%(vhost)s.conf' % {'supervisor_dir': SUPERVISOR_DIR, 'vhost': VHOST},
use_sudo=True)
def _make_vhost():
template = StringIO.StringIO()
get('%savailable/flask.tpl' % NGINX_DIR, template)
interpolated = StringIO.StringIO()
interpolated.write(_render_template(template.getvalue(), {
'domain': VHOST,
'root': APP_ROOT,
'static': STATIC
}))
put(interpolated, '%(nginx)savailable/%(vhost)s' % {'nginx': NGINX_DIR, 'vhost': VHOST}, use_sudo=True)
sudo('ln -s %(src)s %(tar)s' % {'src': '%(nginx)savailable/%(vhost)s' % {'nginx': NGINX_DIR, 'vhost': VHOST},
'tar': '%(nginx)senabled/%(vhost)s' % {'nginx': NGINX_DIR, 'vhost': VHOST}}
)
run('touch %s/access.log' % APP_ROOT)
run('touch %s/error.log' % APP_ROOT)
def _clone_repo():
with cd(APPS_DIR):
run('hg clone %(repos)s%(vhost)s %(to)s' % {'repos': REPOS, 'vhost': VHOST, 'to': APP_ROOT})
def _update_repo():
with cd(APP_ROOT):
run('hg pull')
run('hg up -C default')
def _reload_webserver():
sudo("/etc/init.d/nginx reload")
def _reload_supervisor():
sudo('supervisorctl update')
def _start_app():
sudo('supervisorctl start %s' % VHOST)
def _reload_app(touch=True):
if touch:
with cd(APP_ROOT):
run('touch app.wsgi')
else:
sudo('supervisorctl restart %s' % VHOST)
def init_deploy():
_clone_repo()
_make_vhost()
_make_supervisor_conf()
_reload_webserver()
_reload_supervisor()
_start_app()
def deploy():
_update_repo()
_reload_app()
server {
listen 80;
server_name {{ domain }};
root {{ root }};
access_log {{ root }}/access.log;
error_log {{ root }}/error.log;
location / {
uwsgi_pass unix:///{{ root }}/uwsgi.sock;
include uwsgi_params;
}
location /static {
alias {{ root }}/{{ static }};
}
}
[program:{{ domain }}]
command=/usr/local/bin/uwsgi
--socket uwsgi.sock
--pythonpath /
--touch-reload /app.wsgi
--chmod-socket 666
--uid www-data
--gid www-data
--processes 1
--master
--no-orphans
--max-requests 5000
--module {{ module }}
--callable app
directory={{ root }}
stdout_logfile={{ root }}/uwsgi.log
user=www-data
autostart=true
autorestart=true
redirect_stderr=true
stopsignal=QUIT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment