-
-
Save kogakure/375132 to your computer and use it in GitHub Desktop.
Python: Deployment with Fabric
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
#!/usr/bin/env python | |
from __future__ import with_statement # needed for python 2.5 | |
from fabric.api import * | |
from fabric.contrib.console import confirm | |
# ================================================================ | |
# NOTE: | |
# using this fabfile expects that you have the python utility | |
# fabric installed locally, ssh access to reamea.com, and your | |
# ssh public key associated with the account '[email protected]' | |
# ================================================================ | |
# USAGE: | |
# | |
# * fab linode background_setup | |
# install the ubuntu/debian based packages needed for Apache, | |
# mod_wsgi, etc to support our pinax-based project | |
# | |
# * fab linode setup | |
# establish a python virtual environment in the worker directory | |
# | |
# * fab linode deploy | |
# deploys the contents of your current mercurial repository up | |
# to soulcraft.com and pushes out the prototype dir into | |
# /var/www for immediate viewing | |
# | |
# * fab linode initialize | |
# ONLY RUN ONCE FOR THE SITE - initializes a superuser account | |
# and sets the admin password | |
# ================================================================ | |
# GLOBALS | |
env.path = '/tmp' | |
env.project_name = 'reameabase' | |
# ENVIRONMENTS | |
def linode(): | |
""" | |
Configure for the host at linode. | |
""" | |
env.hosts = ['somewhere.com'] | |
env.user = 'mboza' | |
env.user_home = '/home/mboza' | |
env.use_photologue = False | |
# COMMANDS | |
def background_setup(): | |
require('hosts', provided_by=[linode]) | |
apt_install('apache2') | |
apt_install('libapache2-mod-wsgi') | |
apt_install('python-mysqldb') | |
apt_install('python-imaging') | |
apt_install('build-essential') | |
apt_install('python-dev') | |
apt_install('python-setuptools') | |
apt_install('python-virtualenv') | |
easy_install('pip') | |
pip_install('simplejson') | |
pip_install('httplib2') | |
pip_install('elementtree') | |
pip_install('python-dateutil') | |
sudo('cd /etc/apache2/sites-available/; a2dissite default;', pty=True) | |
def setup(): | |
require('hosts', provided_by=[linode]) | |
require('hosts', provided_by=[linode]) | |
require('path') | |
require('user_home', provided_by=[linode]) | |
cd(env.user_home) | |
run('wget http://downloads.pinaxproject.com/Pinax-0.7.1-bundle.tar.gz') | |
run('tar xvf Pinax-0.7.1-bundle.tar.gz') | |
with cd('%(user_home)s/Pinax-0.7.1-bundle' % env): | |
run('python scripts/pinax-boot.py %(user_home)s/' % env) | |
#run('virtualenv .', pty=True) | |
with cd(env.user_home): | |
if env.use_photologue: run('mkdir photologue'); | |
#run('mkdir logs; chmod a+w logs; mkdir releases; mkdir shared; mkdir packages;' % env, pty=True) | |
install_requirements() | |
def deploy(): | |
require('hosts', provided_by=[linode]) | |
test() | |
upload_tar_from_hg() | |
deploy_home_mboza() | |
def initialize(): | |
with cd(env.user_home): | |
with_virtualenv('python scorigins/manage.py createsuperuser --username=admin [email protected] --noinput') | |
with_virtualenv('python scorigins/setsuperuserpassword.py') | |
def test(): | |
with settings(warn_only=True): | |
with cd('src/scorigins'): | |
result1 = local('./manage.py runscript tests.smoke_test', capture=False) | |
result2 = local('./manage.py runscript tests.model_coverage', capture=False) | |
if result1.failed or result2.failed: | |
if not confirm("Tests failed. Continue anyway?"): | |
abort("Aborting at user request.") | |
def deploy_home_mboza(): | |
""" | |
Deploy the django/pinux project onto the target environment. | |
Assumes setup_home_mboza() has already been run. | |
""" | |
require('hosts', provided_by=[linode]) | |
require('path') | |
with cd('%(path)s/archive/src' % env): | |
run('tar cvf /tmp/dj.tar .', pty=True) | |
with cd('%(user_home)s' % env): | |
run('tar xvf /tmp/dj.tar', pty=True) | |
run ('rm -f /tmp/dj.tar') | |
cd('%(user_home)s' % env) | |
install_site() | |
migrate() | |
restart_webserver() | |
def migrate(): | |
"Update the database" | |
require('project_name') | |
require('user_home') | |
run('cd %(user_home)s/%(project_name)s; python manage.py syncdb --noinput' % env, pty=True) | |
run('cd %(user_home)s/%(project_name)s; python manage.py migrate' % env, pty=True) | |
def restart_webserver(): | |
"Restart the web server" | |
sudo('/etc/init.d/apache2 reload', pty=True) | |
def install_site(): | |
"Add the virtualhost file to apache" | |
sudo('cd %(user_home)s/scorigins/deploy; cp scorigins /etc/apache2/sites-available/%(project_name)s' % env) | |
sudo('cd /etc/apache2/sites-available/; a2ensite %(project_name)s' % env, pty=True) | |
# UTILITIES | |
def upload_tar_from_hg(): | |
""" | |
Create a tar archive from the current hg master branch and upload it. | |
""" | |
local('hg archive -t tgz /tmp/archive.tgz', capture=False) | |
put('/tmp/archive.tgz', '%(path)s' % env) | |
with cd(env.path): | |
run('tar zxf archive.tgz', pty=True) | |
local('rm /tmp/archive.tgz') | |
def apt_install(package): | |
""" | |
Install a single package on the remote server with Apt. | |
""" | |
sudo('aptitude install -y %s' % package) | |
def easy_install(package): | |
""" | |
Install a single package on the remote server with easy_install. | |
""" | |
sudo('easy_install %s' % package) | |
def pip_install(package): | |
""" | |
Install a single package on the remote server with pip. | |
""" | |
sudo('pip install %s' % package) | |
def with_virtualenv(command): | |
""" | |
Executes a command in this project's virtual environment. | |
""" | |
run('source bin/activate && %s' % command) | |
def install_requirements(): | |
""" | |
Install the required packages from the requirements file using pip. | |
""" | |
with cd(env.user_home): | |
with_virtualenv('easy_install -U setuptools') | |
with_virtualenv('pip install -U django') | |
with_virtualenv('pip install South') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment