corporal/machines/server/fabfile.py

157 lines
5 KiB
Python
Raw Normal View History

# -*- coding: utf-8; -*-
"""
Fabric script for 'server' machine
Please see the accompanying README for full instructions.
"""
from fabric2 import task
from rattail.core import Object
from rattail_fabric2 import apt, postfix, postgresql, python, exists, make_system_user, mkdir
from corporal.fablib import make_deploy
from corporal.fablib.python import bootstrap_python
env = Object()
deploy = make_deploy(__file__)
##############################
# bootstrap
##############################
@task
def bootstrap_all(c):
"""
Bootstrap all aspects of the machine
"""
bootstrap_base(c)
bootstrap_corporal(c)
@task
def bootstrap_base(c):
"""
Bootstrap the base system
"""
apt.dist_upgrade(c)
# postfix
postfix.install(c)
# rattail user
make_system_user(c, 'rattail', home='/var/lib/rattail', shell='/bin/bash')
postfix.alias(c, 'rattail', 'root')
# python
bootstrap_python(c, user='rattail', env=env,
virtualenvwrapper_from_apt=True,
python3=True)
# postgres
apt.install(c, 'postgresql')
postgresql.create_user(c, 'rattail', password=env.password_postgresql_rattail)
# misc.
apt.install(
c,
'git',
'libpq-dev',
'supervisor',
)
# uncomment this if you prefer to use emacs
#apt.install_emacs(c)
@task
def bootstrap_corporal(c):
"""
Bootstrap the Corporal app
"""
user = 'rattail'
c.sudo('supervisorctl stop corporal:', warn=True)
# virtualenv
if not exists(c, '/srv/envs/corporal'):
python.mkvirtualenv(c, 'corporal', python='/usr/bin/python3', runas_user=user)
c.sudo('chmod 0600 /srv/envs/corporal/pip.conf')
mkdir(c, '/srv/envs/corporal/src', owner=user, use_sudo=True)
# Corporal
if env.machine_is_live:
c.sudo("bash -lc 'workon corporal && pip install Corporal'",
user=user)
else:
# TODO: this really only works for vagrant
c.sudo("bash -lc 'workon corporal && pip install /vagrant/Corporal-*.tar.gz'",
user=user)
# app dir
if not exists(c, '/srv/envs/corporal/app'):
c.sudo("bash -lc 'workon corporal && cdvirtualenv && rattail make-appdir'", user=user)
c.sudo('chmod 0750 /srv/envs/corporal/app/log')
mkdir(c, '/srv/envs/corporal/app/data', use_sudo=True, owner=user)
# config / scripts
deploy(c, 'corporal/rattail.conf.mako', '/srv/envs/corporal/app/rattail.conf',
owner=user, mode='0600', use_sudo=True, context={'env': env})
if not exists(c, '/srv/envs/corporal/app/quiet.conf'):
c.sudo("bash -lc 'workon corporal && cdvirtualenv app && rattail make-config -T quiet'",
user=user)
deploy(c, 'corporal/cron.conf', '/srv/envs/corporal/app/cron.conf',
owner=user, use_sudo=True)
deploy(c, 'corporal/web.conf.mako', '/srv/envs/corporal/app/web.conf',
owner=user, mode='0600', use_sudo=True, context={'env': env})
deploy(c, 'corporal/upgrade.sh', '/srv/envs/corporal/app/upgrade.sh',
owner=user, mode='0755', use_sudo=True)
deploy(c, 'corporal/tasks.py', '/srv/envs/corporal/app/tasks.py',
owner=user, use_sudo=True)
deploy(c, 'corporal/upgrade-wrapper.sh', '/srv/envs/corporal/app/upgrade-wrapper.sh',
owner=user, mode='0755', use_sudo=True)
deploy(c, 'corporal/overnight.sh', '/srv/envs/corporal/app/overnight.sh',
owner=user, mode='0755', use_sudo=True)
deploy(c, 'corporal/overnight-wrapper.sh', '/srv/envs/corporal/app/overnight-wrapper.sh',
owner=user, mode='0755', use_sudo=True)
# database
if not postgresql.db_exists(c, 'corporal'):
postgresql.create_db(c, 'corporal', owner='rattail', checkfirst=False)
c.sudo("bash -lc 'workon corporal && cdvirtualenv && bin/alembic --config app/rattail.conf upgrade heads'",
user=user)
postgresql.sql(c, "insert into setting values ('rattail.app_title', 'Corporal')",
database='corporal')
postgresql.sql(c, "insert into setting values ('tailbone.theme', 'falafel')",
database='corporal')
postgresql.sql(c, "insert into setting values ('tailbone.themes.expose_picker', 'false')",
database='corporal')
# supervisor
deploy(c, 'corporal/supervisor.conf', '/etc/supervisor/conf.d/corporal.conf',
use_sudo=True)
c.sudo('supervisorctl update')
c.sudo('supervisorctl start corporal:')
# cron etc.
deploy.sudoers(c, 'corporal/sudoers', '/etc/sudoers.d/corporal')
deploy(c, 'corporal/crontab.mako', '/etc/cron.d/corporal',
use_sudo=True, context={'env': env})
deploy(c, 'corporal/logrotate.conf', '/etc/logrotate.d/corporal',
use_sudo=True)
##############################
# fabenv
##############################
try:
import fabenv
except ImportError as error:
print("\ncouldn't import fabenv: {}".format(error))
env.setdefault('machine_is_live', False)