rattail-fabdemo/servers/host/fabfile.py
Lance Edgar cf06a1987d Initial commit, with 'host' server example.
Even that isn't quite complete, but I'm anxious to test other things..
2015-11-15 20:22:01 -06:00

145 lines
4 KiB
Python

# -*- coding: utf-8 -*-
"""
Fabric script for the 'host' server
Please see the accompanying README for full instructions.
"""
from __future__ import unicode_literals
import os
from fabric.api import *
from fabric.utils import warn, puts
from fabric.contrib.files import append, exists
from rattail.fablib import make_deploy, mkdir, make_system_user, cdvirtualenv, workon
from rattail.fablib import apt, pod, postgresql, python
__all__ = ['bootstrap_all', 'bootstrap_system', 'bootstrap_rattail', 'bootstrap_pod']
# Set the 'live' role to the canonical hostname for this server.
#env.roledefs = {'live': ['host.example.com']}
deploy = make_deploy(__file__)
try:
import fabenv
except ImportError as error:
warn("Couldn't import fabenv: {0}".format(error))
@task
def bootstrap_all():
"""
Bootstrap all aspects of the server.
"""
bootstrap_system()
bootstrap_rattail()
bootstrap_pod()
@task
def bootstrap_system():
"""
Bootstrap the base system.
"""
apt.dist_upgrade()
# postgresql
apt.install('postgresql')
# python
apt.install('libpython-dev', 'libpq-dev')
python.install_pip()
python.install_virtualenvwrapper()
deploy('python/premkvirtualenv', '/srv/envs/premkvirtualenv')
sudo('mkvirtualenv BASELINE')
# apache
apt.install('apache2', 'libapache2-mod-wsgi')
deploy('apache/wsgi.conf', '/etc/apache2/conf-available/wsgi.conf')
sudo('a2enconf wsgi')
sudo('a2enmod ssl')
sudo('service apache2 restart')
# misc
apt.install('git', 'emacs-nox')
@task
def bootstrap_rattail():
"""
Bootstrap the Rattail software.
"""
from rattail.fablib.rattail import bootstrap_rattail
# rattail user and core files
bootstrap_rattail()
# virtual environment
python.mkvirtualenv('rattail')
with cdvirtualenv('rattail'):
mkdir('src')
with workon('rattail'):
python.pip('psycopg2')
# rattail source
with cdvirtualenv('rattail', 'src'):
if not exists('rattail'):
sudo('git clone https://rattailproject.org/git/rattail.git')
with cdvirtualenv('rattail', 'src/rattail'):
sudo('git pull')
sudo('pip install --editable .')
with workon('rattail'):
sudo('pip install --upgrade rattail[db,auth]')
# tailbone source
with cdvirtualenv('rattail', 'src'):
if not exists('tailbone'):
sudo('git clone https://rattailproject.org/git/tailbone.git')
with cdvirtualenv('rattail', 'src/tailbone'):
sudo('git pull')
sudo('pip install --upgrade --editable .')
# config
with cdvirtualenv('rattail', 'app'):
deploy('rattail/rattail.conf.template', 'rattail.conf')
# database
postgresql.create_user('rattail', password=env.password_postgresql_rattail)
postgresql.create_db('rattail', owner='rattail')
with cd('/srv/envs/rattail'):
sudo('bin/alembic --config=app/rattail.conf upgrade heads', user='rattail')
sudo('bin/rattail --config app/rattail.conf initdb --with-admin', user='rattail')
# website
with cdvirtualenv('rattail', 'app'):
mkdir('sessions', owner='rattail:rattail')
deploy('rattail/web.conf', 'web.conf')
deploy('rattail/rattail.wsgi', 'rattail.wsgi')
append('/etc/hosts', '127.0.0.1 rattail.localhost', use_sudo=True)
deploy('rattail/rattail.localhost.conf', '/etc/apache2/sites-available/rattail.localhost.conf')
sudo('a2ensite rattail.localhost')
sudo('service apache2 restart')
@task
def bootstrap_pod():
"""
Bootstrap the POD images website.
"""
# images
mkdir('/srv/pod')
if getattr(env, 'rattail_download_pod', False):
pod.install_pod()
else:
puts("Skipping POD download, per 'fabenv' settings.")
# website
append('/etc/hosts', '127.0.0.1 pod.localhost', use_sudo=True)
deploy('pod/pod.localhost.conf', '/etc/apache2/sites-available/pod.localhost.conf')
sudo('a2ensite pod.localhost')
sudo('service apache2 restart')