Created
August 7, 2011 11:57
-
-
Save tdavis/1130316 to your computer and use it in GitHub Desktop.
Simply fabfile for gunicorn deployment
This file contains hidden or 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
| app_directory: /workspace/project | |
| static_directory: static | |
| default: | |
| postgres_user: postgres | |
| venv_root: /var/env | |
| gunicorn: my-gunicorn | |
| app-servers: | |
| venv: my_venv | |
| hosts: | |
| - host.com: | |
| user: tom | |
| static-servers: | |
| hosts: | |
| - host.com: | |
| user: tom |
This file contains hidden or 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
| from __future__ import with_statement | |
| from fabric.api import * | |
| from django.conf import settings | |
| env.stages = ['staging', 'production'] | |
| env.config_file = 'config/fab/%s.yaml' | |
| def virtualenv(cmd, **kwargs): | |
| """ | |
| Run a command in a virtualenv | |
| """ | |
| return 'source {venv_root}/{venv}/bin/activate && ' + cmd | |
| def install_nginx(version='0.8.32'): | |
| """ | |
| Compile and install nginx. | |
| This configuration results in the following:: | |
| nginx path prefix: "/usr/local/nginx" | |
| nginx binary file: "/usr/local/sbin/nginx" | |
| nginx configuration prefix: "/etc" | |
| nginx configuration file: "/etc/nginx" | |
| nginx pid file: "/var/run/nginx.pid" | |
| nginx error log file: "/var/log/nginx/error.log" | |
| nginx http access log file: "/var/log/nginx/access.log" | |
| nginx http client request body temporary files: "client_body_temp" | |
| nginx http proxy temporary files: "proxy_temp" | |
| nginx http fastcgi temporary files: "fastcgi_temp" | |
| .. warning:: | |
| Only tested on Debian Lenny. Consult the | |
| `nginx wiki <http://wiki.nginx.org/>`_ for package requirements. | |
| :param version: The version of nginx to use. | |
| """ | |
| configure = """ | |
| ./configure --conf-path=/etc/nginx/nginx.conf \ | |
| --pid-path=/var/run/nginx.pid \ | |
| --lock-path=/var/lock/nginx.lock \ | |
| --error-log-path=/var/log/nginx/error.log \ | |
| --http-log-path=/var/log/nginx/access.log --user=www-data \ | |
| --group=www-data --with-http_ssl_module \ | |
| --with-http_realip_module \ | |
| --sbin-path=/usr/local/sbin/nginx | |
| """ | |
| with cd('/tmp'): | |
| run('wget -qq http://nginx.org/download/nginx-%s.tar.gz' % version) | |
| run('tar -xzf nginx-%s.tar.gz' % version) | |
| with cd('nginx-%s' % version): | |
| run(configure) | |
| run('make') | |
| sudo('make install') | |
| # nginx launcher | |
| put('bin/nginx', '/tmp') | |
| sudo('mv /tmp/nginx /etc/init.d/') | |
| sudo('chown root /etc/init.d/nginx') | |
| sudo('chmod +x /etc/init.d/nginx') | |
| # config | |
| sudo('mkdir /etc/nginx/sites-enabled') | |
| put('config/nginx.conf', '/tmp') | |
| sudo('mv /tmp/nginx.conf /etc/nginx/') | |
| put('config/proxy.conf', '/tmp') | |
| sudo('mv /tmp/proxy.conf /etc/nginx/conf.d/') | |
| # cleanup | |
| with cd('/tmp'): | |
| run('rm nginx-%s.tar.gz' % version) | |
| run('rm -rf nginx-%s/' % version) | |
| run('rm -rf agentzh-*') | |
| def setup_db(): | |
| """ | |
| Setup a PostgreSQL database using Django settings. | |
| .. note:: | |
| This command assumes the user has existing ``psql`` priviledges; the | |
| command will fail without them. The easiest way to get them is:: | |
| sudo su - postgres | |
| createuser <name> | |
| where ``<name>`` is your unix username. | |
| """ | |
| DB = settings.DATABASES['default'] | |
| run('psql postgres -c "CREATE ROLE %s WITH CREATEDB LOGIN PASSWORD \'%s\';"' | |
| % (DB['USER'], DB['PASSWORD'])) | |
| run('createdb %s -O %s' % (DB['NAME'], DB['USER'])) | |
| @task | |
| def setup_app_server(): | |
| """ | |
| Setup a full app server. This is incomplete. | |
| """ | |
| install_nginx() | |
| setup_db() | |
| @task('app-servers') | |
| def deploy(): | |
| """ | |
| Deploy the application. | |
| """ | |
| # App servers | |
| with cd(env.config.app_directory): | |
| with prefix('source ~/.keychain/`hostname`-sh'): | |
| env('app-servers').run('git pull') | |
| env().run(virtualenv('pip install mercurial'), format=True) | |
| env().run(virtualenv('pip install -qr requirements.txt'), format=True) | |
| env().run(virtualenv('python src/manage.py syncdb'), format=True) | |
| env().run(virtualenv('python src/manage.py migrate'), format=True) | |
| env().sudo('supervisorctl restart {gunicorn}', format=True) | |
| # Static servers | |
| with cd(env.config.app_directory): | |
| with prefix('source ~/.keychain/`hostname`-sh'): | |
| env('static-servers').run('git pull') | |
| env('static-servers').run('which compass && compass compile ' | |
| '{config.static_directory}', | |
| format=True, | |
| warn_only=True) | |
| env('static-servers').sudo('/etc/init.d/nginx restart') | |
| @task | |
| def merge(): | |
| local('git pull origin develop') | |
| local('git push origin develop') | |
| local('git checkout master') | |
| local('git merge origin/develop') | |
| local('git push origin master') | |
| local('git checkout develop') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment