Skip to content

Instantly share code, notes, and snippets.

@chrislawlor
Created June 27, 2012 14:44
Show Gist options
  • Save chrislawlor/3004519 to your computer and use it in GitHub Desktop.
Save chrislawlor/3004519 to your computer and use it in GitHub Desktop.
Fabric scripts for installing commonly used web app infrastructure on Ubuntu-flavored OS's
# Import these into your fabfile.py as to not clutter it up.
import re
import textwrap
from fabric.api import *
from fabric.operations import prompt, require
from fabric.context_managers import settings, prefix
from fabric.contrib.console import confirm
from fabric.contrib.files import upload_template, exists, append
from fabric.utils import warn
# Helper functions
def printme(f):
def wrapper(*args, **kwargs):
print("***\n*** %s (%s)***\n***" % (f.__name__, f.__doc__))
return f(*args, **kwargs)
wrapper.__name__ = f.__name__
wrapper.__doc__ = f.__doc__
return wrapper
def lsb_release():
"""Return the /etc/lsb-release file as a dict"""
f = NamedTemporaryFile(delete=True)
f.close()
get('/etc/lsb-release', f.name)
return dict([ kv.strip().split('=') for kv in file(f.name).readlines() ])
#
# Redis
#
@printme
def install_redis(tarball="http://redis.googlecode.com/files/redis-2.4.12.tar.gz"):
"""Install Redis from tarball"""
require('settings', provided_by=environments)
m = re.compile(r".*(redis-[\d.]+).tar.gz").match(tarball)
src_dir = m.group(1) if m else None
with cd("/tmp"):
run("curl %s | tar zxf -" % tarball)
with cd(src_dir):
run("make")
sudo("make install")
sudo("mkdir /etc/redis")
sudo("mkdir -p /var/redis/6379")
put(pwd + '/init.d/redis', '/tmp/redis', mode=0755)
sudo('cp -a /tmp/redis /etc/init.d/redis')
put(pwd + '/conf/redis.conf', '/tmp/redis.conf', mode=0755)
sudo('cp -a /tmp/redis.conf /etc/redis/6379.conf')
sudo('update-rc.d redis defaults')
sudo('service redis start', shell=False)
#
# Memcached
#
@printme
def install_memcached():
"""Install the memcached server package"""
sudo('apt-get --yes -q install memcached')
@printme
def restart_memcached():
"""Restart the memcache daemon"""
require('settings', provided_by=environments)
sudo('service memcached restart && sleep 1')
@printme
def install_postgresql_server(listen_address='localhost'):
"""Install db server package for env.pg_version, set postgres password"""
require('settings', provided_by=environments)
version = env.get('pg_version')
env.pg_package = '-'.join(["postgresql", version]) if version else "postgresql"
if not version or version.startswith('8'):
sudo("apt-get --yes -q install %(pg_package)s" % env)
elif version.startswith('9'):
lsb = lsb_release()
# ubuntu 12.04 has PostgreSQL 9 in the repo
if lsb['DISTRIB_CODENAME'] == 'precise':
sudo('apt-get install -yq postgresql')
else:
sudo("add-apt-repository ppa:pitti/postgresql")
sudo("apt-get --yes -q update")
sudo("apt-get --yes -q install postgresql-9.0 libpq-dev")
else:
abort("Don't know about postgresql package '%(pg_package)s'")
if not env.has_key('db_root_pass'):
env.db_root_pass = prompt("Enter postgres dbuser password: ")
sudo('echo "ALTER USER postgres WITH UNENCRYPTED PASSWORD \'%(db_root_pass)s\';" | psql template1' % env, user='postgres')
if headless:
append("/etc/postgresql/%(pg_version)s/main/postgresql.conf" % env,
"listen_addresses = '*'",
use_sudo=True)
append("/etc/postgresql/%(pg_version)s/main/pg_hba.conf" % env,
["# IPv4 Intranet connections",
"host all all %(pg_allow)s md5" % env],
use_sudo=True)
sudo("service postgresql restart")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment