Source

Deployment / DjangoDefaults / webfaction-setup-fabfile.py

Full commit
#!/usr/bin/python
from __future__ import with_statement
from fabric.api import env, local, run, cd, settings, require
import sys
import random

try:
	from webf.webflib import WebFactionXmlRpc
except ImportError:
	print 'Please install the webf package'
	sys.exit(1)

PIP_FILENAME = 'pip-1.0.1'
PIP_URL = 'http://pypi.python.org/packages/source/p/pip/pip-1.0.1.tar.gz#md5=28dcc70225e5bf925532abc5b087a94b'
SVN_PATH = '%SVN_PATH%'

env.hosts = ['project.webfactional.com:22'] # One or multiple server addresses in format ip:port
env.user = 'project'
env.password = 'password'
env.python_version = '2.7'

env.home_path = '/home/%s' % env.user
env.django_path = '/home/%s/webapps/django' % env.user
env.apache_path = '/home/%s/webapps/django/apache2' % env.user
env.prj_name = env.user
env.code_path = '/home/%s/webapps/django/%s' % (env.user, env.prj_name)

def setup():
	create_directories()
	checkout_code()
	install_pip()
	prep_apache()
	default_applications()
	prep_webfaction()
	prep_code()


def create_directories():
	with cd(env.home_path):
		run('mkdir databackups')
		run('mkdir sources')
		run('mkdir lib/python%s' % env.python_version)
		run("echo 'export EDITOR=nano' >> .bash_profile")
		run("echo 'export PATH=/home/%s/bin:$PATH' >> .bash_profile" % env.user)
		run("echo 'alias python=/usr/local/bin/python%s' >> .bash_profile" %
															env.python_version)
		run('source .bash_profile')

	with cd(env.django_path):
		# remove default mytester app and script
		run('rm myp.wsgi')
		run('rm -rf myp')

def checkout_code():
	if SVN_PATH and SVN_PATH != '':
		with cd(env.django_path):
			run('svn co %s %s' % (SVN_PATH, env.prj_name))

def install_pip():
	# TODO: make sure /home/CLIENT/lib/PYTHONVERSION exists
	with cd('%s/sources' % env.home_path):
		run('wget %s' % PIP_URL)
		run('tar -xzvf %s.tar.gz' % PIP_FILENAME)
		run('rm %s.tar.gz' % PIP_FILENAME)
	with cd('%s/sources/%s' % (env.home_path, PIP_FILENAME)):
		run('python%s setup.py install' % env.python_version)

def prep_apache():
	with cd(env.apache_path + '/conf'):
		run('cp httpd.conf httpd.conf.original')

# step 2
def default_applications():
	run('pip install -r %s/deploy/requirements.txt' % env.code_path)
	grappelli()

def grappelli():
	run('pip install django-grappelli')
	grap_path = '%s/lib/python%s/grappelli/static/grappelli' % (env.home_path,
											   env.python_version)
	django_package_path = '%s/lib/python%s/django/contrib/admin' % (
											env.django_path, env.python_version)
	with cd(django_package_path):
		run('mv media django-media')
		run('ln -s %s grap-media' % grap_path)
		run('ln -s grap-media media')

# step 3
def prep_webfaction():
	db_pass = ''.join([random.choice("abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)") for i in range(10)])
	wf = WebFactionXmlRpc(env.user, env.password)
	if wf.create_db(env.user, 'postgresql', db_pass):
		print 'Database created with password of "%s"' % db_pass
	else:
		print 'Database creation failed'
		return
	
	with cd(env.home_path):
		run('echo "localhost:*:*:%s:%s" > .pgpass' % (env.user, db_pass))
		run('chmod 0600 .pgpass')
	if wf.create_app('includes', 'symlink_static_only', False,
				  '/home/%s/webapps/django/%s/includes' % (env.user, env.prj_name)):
		print '"includes" app built'
	else:
		print '"includes" app failed'
		return
	if wf.create_app('admin', 'symlink_static_only', False,
				  '/home/%s/webapps/django/lib/python%s/django/contrib/admin/media'
				  % (env.user, env.python_version)):
		print '"admin" app built'
	else:
		print '"admin" app failed'
		return

# step 4
def prep_code():
	'''
	Link Apache conf file and live django settings
	'''
	with cd(env.apache_path + '/conf'):
		run('rm httpd.conf')
	with cd(env.code_path):
		run('ln -s live_settings.py local_settings.py')
		run('ln -s %s/deploy/httpd.conf %s/conf/httpd.conf' % (env.code_path,
															   env.apache_path))

def satchmo():
	for app in ['satchmo', 'django-keyedcache', 'django-livesettings',
			'django-app-plugins', 'django-signals-ahoy',
			'django-threaded-multihost']:
		run('pip install %s' % app)