"""
Staging the OSP allura
* For the most part this is ubuntu specific
* This requires fabric 1.5
"""
from fabric.api import *
from fabric.contrib.console import confirm
from fabric.contrib import files
from fabric.context_managers import *
from contextlib import contextmanager as _contextmanager
from fabric.contrib.console import confirm
import os
env.use_ssh_config = True
env.hosts = ['osp-staging']
#
# Some more or less hardcoded options
#
#ALLURA_GIT_REPO = 'http://git-wip-us.apache.org/repos/asf/incubator-allura'
#ALLURA_GIT_BRANCH = 'asf_release_1.0.0'
ALLURA_GIT_REPO = 'https://opensourceprojects.eu/git/p/osp/allura'
ALLURA_GIT_BRANCH = 'opensourceprojects'
OSP_GIT_REPO = 'http://opensourceprojects.eu/git/p/osp/opensourceprojects'
PKGDEPS = 'mongodb-server git-core default-jre-headless python-dev libssl-dev libldap2-dev libsasl2-dev libjpeg8-dev zlib1g-dev'
def _fix_lib64(name, lib='/usr/lib', lib64='/usr/lib/x86_64-linux-gnu'):
"""
Create symlinks in /usr/lib to libs in /usr/lib/x86_64...
"""
if files.exists(lib64) and not files.exists(os.path.join(lib, name )):
sudo('ln -s %s %s' % (os.path.join(lib64, name), lib))
def install(what):
"""
Install packages using apt-get
"""
sudo('DEBIAN_FRONTEND=noninteractive apt-get -q -y install %s' % what)
def enable_service(what):
"""
Enable service using update-rc.d
"""
sudo('update-rc.d %s enable' % what)
def setup_system():
"""
Install packages and setup stuff - run this on a new system
"""
sudo('apt-get update') # In case no one ran this before
install(PKGDEPS)
_fix_lib64('libz.so')
_fix_lib64('libjpeg.so')
install('subversion python-svn')
install('apache2 slapd ldap-utils')
install('build-essential g++') # * We might need this
install('python-matplotlib libfreetype6-dev libpng12-dev libjpeg-dev') # * Honestly I prefer to use the system packages
# for this - pip install is prone to fail
# here
# python-PIL is required by allura - install here to avoid issues later on
# See the PIL rewrite in the the requirements-common.txt
install('python-imaging')
# pip, virtualenv
install('python-pip')
sudo('pip install virtualenv')
if not user_exists('allura'):
sudo('useradd -m -r -s /bin/bash allura')
if not user_exists('solr'):
sudo('useradd -r -s /bin/bash solr')
sudo('update-rc.d apache2 enable')
sudo('update-rc.d mongodb enable')
sudo('a2enmod proxy_http')
ALLURA_BASE = '/opt/allura/'
ALLURA_ANVIL = os.path.join(ALLURA_BASE, 'anvil')
ALLURA_SRC = os.path.join(ALLURA_BASE, 'forge-src')
LOGDIR = os.path.join(ALLURA_BASE, 'log')
SOLR = os.path.join(ALLURA_BASE, 'solr')
@_contextmanager
def _virtualenv():
with prefix('source %s' % os.path.join(ALLURA_ANVIL, 'bin/activate')):
yield
def setup_allura():
"""
Setup allura installation
"""
if not files.exists(ALLURA_BASE):
sudo('mkdir %s' % ALLURA_BASE)
sudo('chown allura %s' % ALLURA_BASE)
if not files.exists(LOGDIR):
sudo('mkdir %s' % LOGDIR)
LOGDIR_SOLR = os.path.join(LOGDIR, 'solr')
sudo('mkdir %s' % LOGDIR_SOLR )
sudo('chown %s %s' % ('solr', LOGDIR_SOLR) )
LOGDIR_ALLURA = os.path.join(LOGDIR, 'allura')
sudo('mkdir %s' % LOGDIR_ALLURA )
sudo('chown %s %s' % ('allura', LOGDIR_ALLURA) )
# FIXME: this has not been tested yet
LOGDIR_APACHE2 = os.path.join(LOGDIR, 'apache2')
sudo('mkdir %s' % LOGDIR_APACHE2 )
sudo('chown %s %s' % ('www-data', LOGDIR_APACHE2) )
with settings(sudo_user='allura'):
#
# Run(sudo) this as user allura
#
with cd(ALLURA_BASE):
if not files.exists('forge-src'):
sudo( 'GIT_SSL_NO_VERIFY=1 git clone %s %s' % (ALLURA_GIT_REPO, ALLURA_SRC))
with cd(ALLURA_SRC):
sudo( 'git checkout %s' % ALLURA_GIT_BRANCH )
if not files.exists(ALLURA_ANVIL):
sudo('virtualenv --system-site-packages %s' % ALLURA_ANVIL)
# FIXME: we need to fix this in our Allura
# This is needed because the dependencies allura asks for are no longer
# in the repos and recent mercurial versions break allura
sudo('sed -i "s/mercurial==1.4.3//" %s/%s' % (ALLURA_BASE, 'forge-src/requirements-common.txt') )
sudo('sed -i "s/Ming==0.3.2dev-20121101/Ming/" %s/%s' % (ALLURA_BASE, 'forge-src/requirements-common.txt') )
sudo('sed -i "s/PIL==1.1.7/PIL/" %s/%s' % (ALLURA_BASE, 'forge-src/requirements-common.txt') )
sudo('sed -i "s/pytidylib==0.2.1/pytidylib/" %s/%s' % (ALLURA_BASE, 'forge-src/requirements-common.txt') )
with _virtualenv():
with cd(os.path.join(ALLURA_BASE, 'forge-src')):
sudo('pip install -r requirements.txt')
SITE_PKGS = os.path.join(ALLURA_ANVIL, 'lib/python2.7/site-packages/pysvn')
if not files.exists(SITE_PKGS):
sudo('ln -s /usr/lib/python2.7/dist-packages/pysvn %s' % SITE_PKGS )
with _virtualenv():
with cd( os.path.join(ALLURA_BASE, 'forge-src')):
with cd('Allura'):
sudo('python setup.py develop')
if files.exists('rebuild-all.bash'):
sudo('./rebuild-all.bash')
else:
# In previous versions the name was different
sudo('./rebuild.bash')
if not files.exists(SOLR):
sudo('mkdir %s' % SOLR)
sudo('chown solr %s' % SOLR)
with settings(sudo_user='solr'):
with cd(SOLR):
sudo('wget -c http://archive.apache.org/dist/lucene/solr/1.4.1/apache-solr-1.4.1.tgz')
sudo('tar xf apache-solr-1.4.1.tgz')
sudo('cp -r %s .' % os.path.join(ALLURA_SRC, 'solr_config'))
sudo('mkdir solr_config/conf')
sudo('cp apache-solr-1.4.1/example/solr/conf/solrconfig.xml solr_config/conf')
def setup_osp():
"""
Setup the OSP plugin
"""
OSP_DIR = 'OpenSourceProjects'
with cd(ALLURA_BASE):
if not files.exists(OSP_DIR):
sudo( 'GIT_SSL_NO_VERIFY=1 git clone %s %s' % (OSP_GIT_REPO, OSP_DIR))
with _virtualenv():
with cd(os.path.join(ALLURA_BASE, OSP_DIR)):
sudo('python setup.py develop')
def setup_app():
"""
Run paster setup app for allura - be *carefull* with this
"""
with settings(sudo_user='allura',
sudo_prefix=env['sudo_prefix'] + '-i '
):
with _virtualenv(), cd(os.path.join(ALLURA_SRC, 'Allura')):
sudo('paster setup-app development.ini' )
# Start services
def start_solr():
with settings(sudo_user='solr'):
with cd(os.path.join(SOLR, 'apache-solr-1.4.1/example')):
sudo('nohup java -Dsolr.solr.home=%s/solr_config -jar start.jar &> %s/solr/solr.log & sleep 1; exit 0' % (SOLR, LOGDIR))
def stop_solr():
sudo('pkill -u solr java')
def start_taskd():
# We NEED sudo -i
with settings(sudo_user='allura',
sudo_prefix=env['sudo_prefix'] + '-i '
):
with _virtualenv():
with cd(os.path.join(ALLURA_SRC, 'Allura')):
sudo('nohup paster taskd development.ini &> %s/allura/taskd.log & sleep 1; exit 0' % ( LOGDIR) )
def start_serve(config):
# We NEED sudo -i
with settings(sudo_user='allura',
sudo_prefix=env['sudo_prefix'] + '-i '
):
with _virtualenv():
sudo('nohup paster serve %s &> %s/allura/tg.log & sleep 1; exit 0' % (config, LOGDIR) )
def stop_allura():
sudo('pkill -u allura')
def start(config=ALLURA_SRC+'/Allura/development.ini'):
"""
Start all Allura related services
"""
start_solr()
start_taskd()
start_serve(config)
def start_osp():
"""
Same as @start but uses the opensourceprojects.ini settings file
"""
start(ALLURA_SRC+'/../OpenSourceProjects/opensourceprojects.ini')
def stop():
"""
Stop all services
"""
stop_solr()
stop_allura()
def restart():
"""
Stops all services and restarts a clean Allura
"""
stop()
start()
def restart_osp():
"""
Same as @restart but uses the opensourceprojects.ini settings
"""
stop()
start_osp()
def allura_shell():
"""
Opens up a paster shell
"""
with settings(sudo_user='allura',
sudo_prefix=env['sudo_prefix'] + '-i '
):
with _virtualenv():
with cd(os.path.join(ALLURA_SRC, 'Allura')):
sudo('paster shell' )
def user_exists(user):
"""
Return True if a user exists or False otherwise
"""
result = run('grep %s /etc/passwd' % user, quiet=True)
if result.failed:
return False
return True