Initial commit; enough to prove concepts at least

was able to bootstrap a complete machine with this, although there's lots
missing yet...
This commit is contained in:
Lance Edgar 2018-12-03 01:11:38 -06:00
commit e8769c8245
26 changed files with 2808 additions and 0 deletions

View file

@ -0,0 +1,40 @@
# -*- coding: utf-8; -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2018 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Rattail-Fabric2 Library
This package contains various tasks and associated functions for use with
Fabric deployment and maintenance.
"""
from __future__ import unicode_literals, absolute_import
from .core import (
get_debian_version,
get_ubuntu_version,
make_deploy,
make_system_user,
mkdir,
set_timezone,
)
from .util import exists

View file

@ -0,0 +1,3 @@
# -*- coding: utf-8; -*-
__version__ = '0.2.0'

89
rattail_fabric2/apache.py Normal file
View file

@ -0,0 +1,89 @@
# -*- coding: utf-8; -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2018 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Fabric library for Apache web server
"""
from __future__ import unicode_literals, absolute_import
import re
from rattail_fabric2 import apt
def install(c):
"""
Install the Apache web service
"""
apt.install(c, 'apache2')
def get_version(c):
"""
Fetch the version of Apache running on the target system
"""
result = c.sudo('apache2 -v')
if not result.failed:
match = re.match(r'^Server version: Apache/(\d+\.\d+)\.\d+ \(.*\)', result.stdout)
if match:
return float(match.group(1))
def enable_mod(c, *names):
"""
Enable the given Apache modules
"""
for name in names:
c.sudo('a2enmod {}'.format(name))
def enable_site(c, *names):
"""
Enable the given Apache site(s)
"""
for name in names:
c.sudo('a2ensite {}'.format(name))
def deploy_site(c, deployer, local_path, name, enable=False, **kwargs):
"""
Deploy a file to Apache sites-available
"""
apache_version = get_version(c)
if apache_version == 2.2:
path = '/etc/apache2/sites-available/{}'.format(name)
else:
path = '/etc/apache2/sites-available/{}.conf'.format(
'000-default' if name == 'default' else name)
context = kwargs.pop('context', {})
context['apache_version'] = apache_version
deployer(c, local_path, path, context=context, **kwargs)
if enable and name != 'default':
enable_site(c, name)
def restart(c):
"""
Restart the Apache web service
"""
c.sudo('systemctl restart apache2.service')

105
rattail_fabric2/apt.py Normal file
View file

@ -0,0 +1,105 @@
# -*- coding: utf-8; -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2018 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Fabric library for the APT package system
"""
from __future__ import unicode_literals, absolute_import
from rattail_fabric2 import make_deploy, get_debian_version, get_ubuntu_version
deploy = make_deploy(__file__)
def install(c, *packages, **kwargs):
"""
Install one or more packages via ``apt-get install``.
"""
frontend = kwargs.get('frontend', 'noninteractive')
target = kwargs.get('target_release')
target = '--target-release={}'.format(target) if target else ''
force_yes = ' --force-yes' if kwargs.get('force_yes') else ''
c.sudo('DEBIAN_FRONTEND={} apt-get --assume-yes {}{} install {}'.format(
frontend, target, force_yes, ' '.join(packages)))
def purge(c, *packages):
"""
Uninstall and purge config for given packages
"""
c.sudo('apt-get --assume-yes purge {}'.format(' '.join(packages)))
def update(c):
"""
Perform an ``apt-get update`` operation.
"""
c.sudo('apt-get update')
def add_source(c, entry):
"""
Add a new entry to the apt/sources.list file
"""
if c.run("grep '^{}' /etc/apt/sources.list".format(entry), warn=True).failed:
c.sudo("""bash -c 'echo "{}" >> /etc/apt/sources.list'""".format(entry))
update(c)
def dist_upgrade(c, frontend='noninteractive'):
"""
Perform a full ``apt-get dist-upgrade`` operation.
"""
update(c)
options = ''
if frontend == 'noninteractive':
options = '--option Dpkg::Options::="--force-confdef" --option Dpkg::Options::="--force-confold"'
c.sudo('DEBIAN_FRONTEND={} apt-get --assume-yes {} dist-upgrade'.format(frontend, options))
def configure_listchanges(c):
"""
Configure apt listchanges to never use a frontend.
"""
deploy(c, 'apt/listchanges.conf', '/etc/apt/listchanges.conf', use_sudo=True)
def install_emacs(c):
"""
Install the Emacs editor
"""
if not c.run('which emacs', warn=True).failed:
return
emacs = 'emacs-nox'
debian_version = get_debian_version(c)
if debian_version:
if debian_version < 8:
emacs = 'emacs23-nox'
else:
ubuntu_version = get_ubuntu_version(c)
if ubuntu_version and ubuntu_version < 16:
emacs = 'emacs23-nox'
install(c, emacs, 'emacs-goodies-el')

View file

@ -0,0 +1,66 @@
# -*- coding: utf-8; -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2018 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Fabric library for Let's Encrypt certbot
"""
from __future__ import unicode_literals, absolute_import
from rattail_fabric2 import apt, exists, get_debian_version
def install(c, source=False, service='apache'):
"""
Install the Let's Encrypt certbot utility
"""
if source:
if not exists(c, '/usr/local/src/certbot'):
c.sudo('git clone https://github.com/certbot/certbot /usr/local/src/certbot')
c.sudo('ln --symbolic --force /usr/local/src/certbot/certbot-auto /usr/local/bin/certbot')
else:
version = get_debian_version(c)
# debian 7 wheezy
if 7 <= version < 8:
if not exists(c, '/usr/local/src/certbot'):
c.sudo('git clone https://github.com/certbot/certbot /usr/local/src/certbot')
c.sudo('ln --symbolic --force /usr/local/src/certbot/certbot-auto /usr/local/bin/certbot')
# debian 8 jessie
elif 8 <= version < 9:
apt.add_source(c, 'deb http://ftp.debian.org/debian jessie-backports main')
apt.install(c, 'python-certbot-apache', target_release='jessie-backports')
# debian 9 stretch, or later
elif version >= 9:
if service == 'apache':
apt.install(c, 'python-certbot-apache')
elif service == 'nginx':
apt.install(c, 'python-certbot-nginx')
else:
raise NotImplementedError("unknown web service: {}".format(service))
# other..? will have to investigate when this comes up
else:
raise NotImplementedError("don't know how to install certbot on debian version {}".format(version))

255
rattail_fabric2/core.py Normal file
View file

@ -0,0 +1,255 @@
# -*- coding: utf-8; -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2018 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Core Utilities
"""
from __future__ import unicode_literals, absolute_import
import os
import re
import tempfile
import six
from mako.template import Template
def is_link(c, path, use_sudo=False):
"""
Return True if the given path is a symlink on the current remote host.
If ``use_sudo`` is True, will use `.sudo` instead of `.run`.
.. note::
This function is derived from one copied from fabric v1.
"""
func = c.sudo if use_sudo else c.run
cmd = 'test -L "$(echo %s)"' % path
result = func(cmd)
return False if result.failed else True
def get_debian_version(c):
"""
Fetch the version of Debian running on the target system.
"""
version = c.run('cat /etc/debian_version').stdout.strip()
match = re.match(r'^(\d+\.\d+)$', version)
if match:
return float(match.group(1))
def get_ubuntu_version(c):
"""
Fetch the version of Ubuntu running on the target system
"""
info = c.run('cat /etc/lsb-release').stdout.strip()
match = re.search(r'DISTRIB_RELEASE=(\d+\.\d+)', info)
if match:
return float(match.group(1))
def mkdir(c, paths, owner=None, mode=None,
use_sudo=False, runas_user=None):
"""
Recursively make one or more directories.
"""
if isinstance(paths, six.string_types):
paths = [paths]
cmd = 'mkdir --parents {}'.format(' '.join(paths))
if use_sudo:
c.sudo(cmd, user=runas_user)
else:
c.run(cmd)
if owner:
if ':' not in owner:
owner = '{0}:{0}'.format(owner)
func = c.sudo if use_sudo else c.run
func('chown {} {}'.format(owner, ' '.join(paths)))
if mode is not None:
func = c.sudo if use_sudo else c.run
func('chmod {} {}'.format(mode, ' '.join(paths)))
def make_system_user(c, name, home=None, uid=None, shell=None):
"""
Make a new system user account, with the given home folder and shell path.
"""
if not c.run('getent passwd {}'.format(name), warn=True).failed:
return
home = '--home {}'.format(home) if home else ''
uid = '--uid {}'.format(uid) if uid else ''
shell = '--shell {}'.format(shell) if shell else ''
c.sudo('adduser --system --group {} {} {} {}'.format(name, home, uid, shell))
def set_timezone(c, timezone):
"""
Set the system timezone to the given value, e.g. 'America/Chicago'.
"""
c.sudo("bash -c 'echo {} > /etc/timezone'".format(timezone))
if is_link(c, '/etc/localtime'):
c.sudo('ln --symbolic --force /usr/share/zoneinfo/{} /etc/localtime'.format(timezone))
else:
c.sudo('cp /usr/share/zoneinfo/{} /etc/localtime'.format(timezone))
def put(c, local_path, remote_path, owner=None, mode=None,
use_sudo=False, **kwargs):
"""
Put a file on the server, and set its ownership.
"""
# TODO: are we mirroring now?
# if 'mode' not in kwargs:
# kwargs.setdefault('mirror_local_mode', True)
# upload file
if use_sudo:
tempdir = c.run('mktemp -d').stdout.strip()
basename = os.path.basename(remote_path)
temp_path = os.path.join(tempdir, basename)
c.put(local_path, temp_path, **kwargs)
# TODO: need to address single quotes within filename
if not owner:
c.sudo("chown root: '{}'".format(temp_path))
c.sudo("mv '{}' '{}'".format(temp_path, remote_path))
c.run('rmdir {}'.format(tempdir))
else:
c.put(local_path, remote_path, **kwargs)
# set owner / mode if needed
func = c.sudo if use_sudo else c.run
if owner:
if ':' not in owner:
owner = '{}:'.format(owner)
func("chown {} '{}'".format(owner, remote_path))
if mode:
func("chmod {} '{}'".format(mode, remote_path))
def upload_mako_template(c, local_path, remote_path, context={},
encoding='utf_8', **kwargs):
"""
Render a local file as a Mako template, and upload the result to the server.
"""
template = Template(filename=local_path)
temp_dir = tempfile.mkdtemp(prefix='rattail-fabric.')
temp_path = os.path.join(temp_dir, os.path.basename(local_path))
text = template.render(**context)
if six.PY3:
with open(temp_path, 'wt', encoding=encoding) as f:
f.write(text)
else:
with open(temp_path, 'wb') as f:
f.write(text.encode(encoding))
os.chmod(temp_path, os.stat(local_path).st_mode)
put(c, temp_path, remote_path, **kwargs)
os.remove(temp_path)
os.rmdir(temp_dir)
class Deployer(object):
def __init__(self, deploy_path, last_segment='deploy'):
if not os.path.isdir(deploy_path):
deploy_path = os.path.abspath(os.path.join(os.path.dirname(deploy_path), last_segment))
self.deploy_path = deploy_path
def __call__(self, c, local_path, remote_path, **kwargs):
self.deploy(c, local_path, remote_path, **kwargs)
def full_path(self, local_path):
return '{}/{}'.format(self.deploy_path, local_path)
def deploy(self, c, local_path, remote_path, context={}, **kwargs):
local_path = self.full_path(local_path)
if local_path.endswith('.template'):
raise NotImplementedError
elif local_path.endswith('.mako'):
upload_mako_template(c, local_path, remote_path, context=context, **kwargs)
else:
put(c, local_path, remote_path, **kwargs)
def sudoers(self, c, local_path, remote_path, owner='root:', mode='0440', **kwargs):
self.deploy(c, local_path, '/tmp/sudoers', owner=owner, mode=mode, use_sudo=True)
c.sudo('mv /tmp/sudoers {}'.format(remote_path))
def apache_site(self, c, local_path, name, **kwargs):
from rattail_fabric2.apache import deploy_site
kwargs['use_sudo'] = True
deploy_site(c, self, local_path, name, **kwargs)
def make_deploy(deploy_path, last_segment='deploy'):
"""
Make a ``deploy()`` function, for uploading files to the server.
During a deployment, one usually needs to upload certain additional files
to the server. It's also often necessary to dynamically define certain
settings etc. within these files. The :func:`upload_template()` and
:func:`put()` functions, respectively, handle uploading files which do and
do not require dynamic variable substitution.
The return value from ``make_deploy()`` is a function which will call
``put()`` or ``upload_template()`` based on whether or not the file path
ends with ``'.template'``.
To make the ``deploy()`` function even simpler for the caller, it will
assume a certain context for local file paths. This means one only need
provide a base file name when calling ``deploy()``, and it will be
interpreted as relative to the function's context path.
The ``deploy_path`` argument is used to establish the context path for the
function. If it is a folder path, it will be used as-is; otherwise it will
be constructed by joining the parent folder of ``deploy_path`` with the
value of ``last_segment``.
Typical usage then is something like::
from rattail_fabric import make_deploy
deploy = make_deploy(__file__)
deploy('rattail/init-filemon', '/etc/init.d/rattail-filemon',
mode='0755')
deploy('rattail/rattail.conf.template', '/etc/rattail.conf')
This shows what is intended to be typical, i.e. where ``__file__`` is the
only argument required for ``make_deploy()``. For the above to work will
require you to have something like this file structure, where
``fabfile.py`` is the script which contains the above code::
myproject/
|-- fabfile.py
|-- deploy/
`-- rattail/
|-- init-filemon
|-- rattail.conf.template
"""
return Deployer(deploy_path, last_segment)

View file

@ -0,0 +1,6 @@
[apt]
frontend=none
email_address=root
confirm=0
save_seen=/var/lib/apt/listchanges.db
which=news

View file

@ -0,0 +1,142 @@
#!/bin/sh
# -*- coding: utf-8; -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2018 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
# This script is mostly based on the ``/etc/init.d/skeleton`` file from a
# Debian 6 system.
DESC=${DESC:-"Rattail Email Bouncer"}
NAME=${NAME:-"rattail-bouncer"}
SCRIPTNAME=${SCRIPTNAME:-"/etc/init.d/$NAME"}
PIDFILE=${PIDFILE:-"/var/run/rattail/$NAME.pid"}
PYTHON=${PYTHON:-"/usr/bin/python"}
RATTAIL=${RATTAIL:-"/usr/local/bin/rattail"}
RATTAIL_ARGS=${RATTAIL_ARGS:-""}
BOUNCER_ARGS=${BOUNCER_ARGS:-"--pidfile=$PIDFILE"}
USER=${USER:-"rattail"}
GROUP=${GROUP:-"rattail"}
# Read configuration variable files if present.
[ -r /etc/default/rattail ] && . /etc/default/rattail
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
# Load the VERBOSE setting and other rcS variables.
. /lib/init/vars.sh
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions
create_pid_dir() {
PIDDIR=`dirname "$PIDFILE"`
if [ ! -d "$PIDDIR" ]; then
mkdir --parents "$PIDDIR"
fi
chown $USER:$GROUP "$PIDDIR"
}
#
# Function that starts the daemon/service
#
do_start()
{
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
create_pid_dir
start-stop-daemon --start --pidfile $PIDFILE --exec $PYTHON --user $USER --test --quiet > /dev/null \
|| return 1
start-stop-daemon --start --pidfile $PIDFILE --exec $PYTHON --startas $RATTAIL --chuid $USER --group $GROUP --quiet -- \
$RATTAIL_ARGS bouncer $BOUNCER_ARGS start \
|| return 2
}
#
# Function that stops the daemon/service
#
do_stop()
{
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred
sudo -u $USER $RATTAIL $RATTAIL_ARGS bouncer $BOUNCER_ARGS stop > /dev/null 2>&1
}
case "$1" in
start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
do_start
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
stop)
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
do_stop
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
status)
status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
;;
restart|force-reload)
log_daemon_msg "Restarting $DESC" "$NAME"
do_stop
case "$?" in
0|1)
do_start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
exit 3
;;
esac
:

View file

@ -0,0 +1,62 @@
#!/bin/sh
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2018 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
#
# This is a simple script which will output a single line of info, and exit
# with a return code which indicates status of a given Rattail daemon. It was
# designed for use with Shinken monitoring. Invoke like so:
#
# check-rattail-daemon NAME
#
# Where NAME refers to the service, e.g. 'rattail-datasync'. Exit code may be:
#
# * 0 = daemon is confirmed running
# * 2 = daemon is confirmed *not* running
# * 3 = unknown state
#
################################################################################
NAME="$1"
if [ "$NAME" = "" ]; then
echo "Usage: check-rattail-daemon NAME"
exit 3
fi
PIDFILE="/var/run/rattail/$NAME.pid"
if [ ! -f "$PIDFILE" ]; then
echo "PID file not found: $PIDFILE"
exit 2
fi
PID=`cat $PIDFILE`
if [ "$PID" = "" ]; then
echo "File exists but contains no PID: $PIDFILE"
exit 3
fi
ps -p $PID >/dev/null
if [ ! $? ]; then
echo "No process found for PID $PID; per file: $PIDFILE"
exit 2
fi
echo "Rattail daemon $NAME is running with PID $PID"
exit 0

View file

@ -0,0 +1,148 @@
#!/bin/sh
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2018 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
#
# Generic 'init' script for all Rattail daemons. Originally based on the
# skeleton file from a Debian 6 system, but tweaked plenty since...
#
# NOTE: The "calling" script *must* define $NAME and $DESC (at least).
#
################################################################################
# Files in /etc/default and/or the "calling" init script may define any of the
# following as necessary, to override:
SCRIPTNAME=${SCRIPTNAME:-"/etc/init.d/$NAME"}
PIDFILE=${PIDFILE:-"/var/run/rattail/$NAME.pid"}
PYTHON=${PYTHON:-"/usr/bin/python"}
RATTAIL=${RATTAIL:-"/usr/local/bin/rattail"}
RATTAIL_ARGS=${RATTAIL_ARGS:-""}
RATTAIL_SUBCMD=${RATTAIL_SUBCMD:-"${NAME##*-}"}
RATTAIL_SUBCMD_ARGS=${RATTAIL_SUBCMD_ARGS:-""}
USER=${USER:-"rattail"}
GROUP=${GROUP:-"rattail"}
# Read configuration variable files if present.
[ -r /etc/default/rattail ] && . /etc/default/rattail
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
# Load the VERBOSE setting and other rcS variables.
. /lib/init/vars.sh
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions
create_pid_dir() {
PIDDIR=`dirname "$PIDFILE"`
if [ ! -d "$PIDDIR" ]; then
mkdir --parents "$PIDDIR"
fi
chown $USER:$GROUP "$PIDDIR"
}
#
# Function that starts the daemon/service
#
do_start()
{
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
create_pid_dir
start-stop-daemon --start --pidfile "$PIDFILE" --exec "$PYTHON" --user $USER --test --quiet >/dev/null \
|| return 1
start-stop-daemon --start --pidfile "$PIDFILE" --exec "$PYTHON" --startas "$RATTAIL" --chuid $USER --group $GROUP --quiet -- \
$RATTAIL_ARGS $RATTAIL_SUBCMD --pidfile="$PIDFILE" $RATTAIL_SUBCMD_ARGS start \
|| return 2
}
#
# Function that stops the daemon/service
#
do_stop()
{
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred
/usr/local/bin/check-rattail-daemon "$NAME" >/dev/null \
|| return 1
sudo -u $USER "$RATTAIL" $RATTAIL_ARGS $RATTAIL_SUBCMD --pidfile="$PIDFILE" $RATTAIL_SUBCMD_ARGS stop >/dev/null 2>&1 \
|| return 2
}
case "$1" in
start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
do_start
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
stop)
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
do_stop
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
status)
/usr/local/bin/check-rattail-daemon "$NAME" && exit 0 || exit $?
;;
restart|force-reload)
log_daemon_msg "Restarting $DESC" "$NAME"
do_stop
case "$?" in
0|1)
do_start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
exit 3
;;
esac
:

View file

@ -0,0 +1,142 @@
#!/bin/sh
# -*- coding: utf-8; -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2018 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
# This script is mostly based on the ``/etc/init.d/skeleton`` file from a
# Debian 6 system.
DESC=${DESC:-"Rattail Data Synchronizer"}
NAME=${NAME:-"rattail-datasync"}
SCRIPTNAME=${SCRIPTNAME:-"/etc/init.d/$NAME"}
PIDFILE=${PIDFILE:-"/var/run/rattail/$NAME.pid"}
PYTHON=${PYTHON:-"/usr/bin/python"}
RATTAIL=${RATTAIL:-"/usr/local/bin/rattail"}
RATTAIL_ARGS=${RATTAIL_ARGS:-""}
DATASYNC_ARGS=${DATASYNC_ARGS:-"--pidfile=$PIDFILE"}
USER=${USER:-"rattail"}
GROUP=${GROUP:-"rattail"}
# Read configuration variable files if present.
[ -r /etc/default/rattail ] && . /etc/default/rattail
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
# Load the VERBOSE setting and other rcS variables.
. /lib/init/vars.sh
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions
create_pid_dir() {
PIDDIR=`dirname "$PIDFILE"`
if [ ! -d "$PIDDIR" ]; then
mkdir --parents "$PIDDIR"
fi
chown $USER:$GROUP "$PIDDIR"
}
#
# Function that starts the daemon/service
#
do_start()
{
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
create_pid_dir
start-stop-daemon --start --pidfile $PIDFILE --exec $PYTHON --user $USER --test --quiet > /dev/null \
|| return 1
start-stop-daemon --start --pidfile $PIDFILE --exec $PYTHON --startas $RATTAIL --chuid $USER --group $GROUP --quiet -- \
$RATTAIL_ARGS datasync $DATASYNC_ARGS start \
|| return 2
}
#
# Function that stops the daemon/service
#
do_stop()
{
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred
sudo -u $USER $RATTAIL $RATTAIL_ARGS datasync $DATASYNC_ARGS stop > /dev/null 2>&1
}
case "$1" in
start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
do_start
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
stop)
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
do_stop
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
status)
status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
;;
restart|force-reload)
log_daemon_msg "Restarting $DESC" "$NAME"
do_stop
case "$?" in
0|1)
do_start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
exit 3
;;
esac
:

View file

@ -0,0 +1,142 @@
#!/bin/sh
# -*- coding: utf-8; -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2018 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
# This script is mostly based on the ``/etc/init.d/skeleton`` file from a
# Debian 6 system.
DESC=${DESC:-"Rattail File Monitor"}
NAME=${NAME:-"rattail-filemon"}
SCRIPTNAME=${SCRIPTNAME:-"/etc/init.d/$NAME"}
PIDFILE=${PIDFILE:-"/var/run/rattail/$NAME.pid"}
PYTHON=${PYTHON:-"/usr/bin/python"}
RATTAIL=${RATTAIL:-"/usr/local/bin/rattail"}
RATTAIL_ARGS=${RATTAIL_ARGS:-""}
FILEMON_ARGS=${FILEMON_ARGS:-"--pidfile=$PIDFILE"}
USER=${USER:-"rattail"}
GROUP=${GROUP:-"rattail"}
# Read configuration variable files if present.
[ -r /etc/default/rattail ] && . /etc/default/rattail
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
# Load the VERBOSE setting and other rcS variables.
. /lib/init/vars.sh
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions
create_pid_dir() {
PIDDIR=`dirname "$PIDFILE"`
if [ ! -d "$PIDDIR" ]; then
mkdir --parents "$PIDDIR"
fi
chown $USER:$GROUP "$PIDDIR"
}
#
# Function that starts the daemon/service
#
do_start()
{
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
create_pid_dir
start-stop-daemon --start --pidfile $PIDFILE --exec $PYTHON --user $USER --test --quiet > /dev/null \
|| return 1
start-stop-daemon --start --pidfile $PIDFILE --exec $PYTHON --startas $RATTAIL --chuid $USER --group $GROUP --quiet -- \
$RATTAIL_ARGS filemon $FILEMON_ARGS start \
|| return 2
}
#
# Function that stops the daemon/service
#
do_stop()
{
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred
sudo -u $USER $RATTAIL $RATTAIL_ARGS filemon $FILEMON_ARGS stop > /dev/null 2>&1
}
case "$1" in
start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
do_start
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
stop)
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
do_stop
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
status)
status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
;;
restart|force-reload)
log_daemon_msg "Restarting $DESC" "$NAME"
do_stop
case "$?" in
0|1)
do_start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
exit 3
;;
esac
:

View file

@ -0,0 +1,127 @@
#!/bin/sh
# -*- coding: utf-8; -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2018 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
#
# Generic 'init' script for Luigi central scheduler (luigid)
#
################################################################################
# "calling" init script may define any of the following as necessary:
NAME=${NAME:-"rattail-luigid"}
SCRIPTNAME=${SCRIPTNAME:-"/etc/init.d/$NAME"}
DESC=${DESC:-"Luigi Scheduler for Rattail"}
DAEMON=${DAEMON:-"/usr/local/bin/luigid"}
PYTHON=${PYTHON:-"/usr/local/bin/python"}
USER=${USER:-"rattail"}
GROUP=${GROUP:-"rattail"}
PIDFILE=${PIDFILE:-"/var/run/rattail/$NAME.pid"}
LOGDIR=${LOGDIR:-"/var/log/rattail/$NAME"}
STATEFILE=${STATEFILE:-"/var/run/rattail/$NAME.pickle"}
ADDRESS=${ADDRESS:-"0.0.0.0"}
create_pid_dir() {
PIDDIR=`dirname "$PIDFILE"`
if [ ! -d "$PIDDIR" ]; then
mkdir --parents "$PIDDIR"
fi
chown $USER:$GROUP "$PIDDIR"
}
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
do_start() {
create_pid_dir
start-stop-daemon --test --start --exec $DAEMON --user $USER --pidfile $PIDFILE --quiet > /dev/null \
|| return 1
start-stop-daemon --start --chuid $USER --exec $DAEMON --pidfile $PIDFILE --background --quiet -- \
--background --pidfile $PIDFILE --logdir $LOGDIR --state-path $STATEFILE --address $ADDRESS \
|| return 2
}
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred
do_stop()
{
start-stop-daemon --test --stop --exec $PYTHON --user $USER --pidfile $PIDFILE --quiet > /dev/null \
|| return 1
start-stop-daemon --stop --exec $PYTHON --user $USER --pidfile $PIDFILE --quiet > /dev/null \
|| return 2
}
case "$1" in
start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
do_start
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
stop)
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
do_stop
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
status)
/usr/local/bin/check-rattail-daemon "$NAME" && exit 0 || exit $?
;;
restart|force-reload)
log_daemon_msg "Restarting $DESC" "$NAME"
do_stop
case "$?" in
0|1)
do_start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
exit 3
;;
esac
:

View file

@ -0,0 +1,125 @@
#!/bin/sh
# -*- coding: utf-8; -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2018 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
#
# Generic 'init' script for headless LibreOffice Writer
#
################################################################################
# "calling" init script may define any of the following as necessary:
NAME=${NAME:-"rattail-soffice"}
SCRIPTNAME=${SCRIPTNAME:-"/etc/init.d/$NAME"}
DESC=${DESC:-"LibreOffice for Rattail"}
DAEMON=${DAEMON:-"/usr/lib/libreoffice/program/soffice.bin"}
SOFFICE=${SOFFICE:-"/usr/bin/soffice"}
USER=${USER:-"rattail"}
GROUP=${GROUP:-"rattail"}
PIDFILE=${PIDFILE:-"/var/run/rattail/$NAME.pid"}
PORT=${PORT:-"2002"}
create_pid_dir() {
PIDDIR=`dirname "$PIDFILE"`
if [ ! -d "$PIDDIR" ]; then
mkdir --parents "$PIDDIR"
fi
chown $USER:$GROUP "$PIDDIR"
}
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
do_start() {
create_pid_dir
start-stop-daemon --test --start --exec $DAEMON --user $USER --pidfile $PIDFILE --quiet > /dev/null \
|| return 1
start-stop-daemon --start --chuid $USER --exec $DAEMON --startas $SOFFICE --pidfile $PIDFILE --background --quiet -- \
--headless --pidfile=$PIDFILE --accept="socket,host=localhost,port=$PORT;urp;" \
|| return 2
}
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred
do_stop()
{
start-stop-daemon --test --stop --exec $DAEMON --user $USER --pidfile $PIDFILE --quiet > /dev/null \
|| return 1
start-stop-daemon --stop --exec $DAEMON --user $USER --pidfile $PIDFILE --quiet > /dev/null \
|| return 2
}
case "$1" in
start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
do_start
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
stop)
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
do_stop
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
status)
/usr/local/bin/check-rattail-daemon "$NAME" && exit 0 || exit $?
;;
restart|force-reload)
log_daemon_msg "Restarting $DESC" "$NAME"
do_stop
case "$?" in
0|1)
do_start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
exit 3
;;
esac
:

View file

@ -0,0 +1,73 @@
# -*- coding: utf-8; -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2018 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Fabric library for Postfix
"""
from __future__ import unicode_literals, absolute_import
from rattail_fabric2 import apt
def install(c):
"""
Install the Postfix mail service
"""
apt.install(c, 'postfix')
apt.purge(c, 'exim4', 'exim4-base', 'exim4-config', 'exim4-daemon-light')
def alias(c, name, alias_to, path='/etc/aliases'):
"""
Set a mail alias for Postfix
"""
# does alias entry already exist?
if c.run("grep '^{}:' /etc/aliases".format(name), warn=True).failed:
# append new entry
c.sudo("""bash -c 'echo "{}: {}" >> /etc/aliases'""".format(name, alias_to))
else:
# update existing entry
c.sudo('sed -i.bak -e "s/^{}: .*/{}: {}/" /etc/aliases'.format(name, name, alias_to))
c.sudo('newaliases')
def restart(c):
"""
Restart the Postfix mail service
"""
c.sudo('systemctl restart postfix.service')
def set_config(c, setting, value):
"""
Configure the given setting with the given value.
"""
c.sudo("postconf -e '{}={}'".format(setting, value))
def set_relayhost(c, relayhost):
"""
Configure the 'relayhost' setting with the given string
"""
set_config(c, 'relayhost', relayhost)

View file

@ -0,0 +1,96 @@
# -*- coding: utf-8; -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2018 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Fabric Library for PostgreSQL
"""
from __future__ import unicode_literals, absolute_import
from rattail_fabric2 import apt
def install(c):
"""
Install the PostgreSQL database service
"""
apt.install(c, 'postgresql')
def sql(c, sql, database='', port=None):
"""
Execute some SQL as the 'postgres' user.
"""
cmd = 'sudo -u postgres psql {port} --tuples-only --no-align --command="{sql}" {database}'.format(
port='--port={}'.format(port) if port else '',
sql=sql, database=database)
return c.sudo(cmd, shell=False)
def user_exists(c, name, port=None):
"""
Determine if a given PostgreSQL user exists.
"""
user = sql(c, "SELECT rolname FROM pg_roles WHERE rolname = '{0}'".format(name), port=port).stdout.strip()
return bool(user)
def create_user(c, name, password=None, port=None, checkfirst=True, createdb=False):
"""
Create a PostgreSQL user account.
"""
if not checkfirst or not user_exists(c, name, port=port):
c.sudo('sudo -u postgres createuser {port} {createdb} --no-createrole --no-superuser {name}'.format(
port='--port={}'.format(port) if port else '',
createdb='--{}createdb'.format('' if createdb else 'no-'),
name=name))
if password:
set_user_password(c, name, password, port=port)
def set_user_password(c, name, password, port=None):
"""
Set the password for a PostgreSQL user account
"""
# TODO: probably should figure out how to suppress echo for this command
# with hide('running'):
sql(c, "ALTER USER \\\"{}\\\" PASSWORD '{}';".format(name, password), port=port)
def db_exists(c, name, port=None):
"""
Determine if a given PostgreSQL database exists.
"""
db = sql(c, "SELECT datname FROM pg_database WHERE datname = '{0}'".format(name), port=port).stdout.strip()
return db == name
def create_db(c, name, owner=None, port=None, checkfirst=True):
"""
Create a PostgreSQL database.
"""
if not checkfirst or not db_exists(c, name, port=port):
cmd = 'sudo -u postgres createdb {port} {owner} {name}'.format(
port='--port={}'.format(port) if port else '',
owner='--owner={}'.format(owner) if owner else '',
name=name)
c.sudo(cmd)

161
rattail_fabric2/python.py Normal file
View file

@ -0,0 +1,161 @@
# -*- coding: utf-8; -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2018 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Fabric Library for Python
"""
from __future__ import unicode_literals, absolute_import
from contextlib import contextmanager
import six
from rattail_fabric2 import apt, exists, mkdir
def install_pip(c, use_apt=False, eager=True):
"""
Install/upgrade the Pip installer for Python.
"""
if use_apt:
apt.install(c, 'python-pip')
else:
apt.install(c, 'build-essential', 'python-dev', 'libssl-dev', 'libffi-dev')
if c.run('which pip', warn=True).failed:
apt.install(c, 'python-pkg-resources', 'python-setuptools')
c.sudo('easy_install pip')
c.sudo('apt-get --assume-yes purge python-setuptools')
pip(c, 'setuptools')
pip(c, 'pip', upgrade=True)
kwargs = {}
if eager:
kwargs['upgrade_strategy'] = 'eager'
pip(c, 'setuptools', 'wheel', 'ndg-httpsclient', upgrade=True, **kwargs)
def pip(c, *packages, **kwargs):
"""
Install one or more packages via ``pip install``.
"""
upgrade = kwargs.pop('upgrade', False)
upgrade = '--upgrade' if upgrade else ''
upgrade_strategy = kwargs.pop('upgrade_strategy', None)
if upgrade and upgrade_strategy:
upgrade_strategy = '--upgrade-strategy {}'.format(upgrade_strategy)
else:
upgrade_strategy = ''
use_sudo = kwargs.pop('use_sudo', True)
runas_user = kwargs.pop('runas_user', None)
if kwargs:
raise RuntimeError("Unknown kwargs for pip(): {}".format(kwargs))
packages = ["'{}'".format(p) for p in packages]
cmd = 'pip install {} {} {}'.format(upgrade, upgrade_strategy, ' '.join(packages))
if use_sudo:
kw = {}
if runas_user:
kw['user'] = runas_user
c.sudo(cmd, **kw)
else:
c.run(cmd)
def install_virtualenvwrapper(c, workon_home='/srv/envs', user='root', use_apt=False, configure_me=True):
"""
Install the `virtualenvwrapper`_ system, with the given ``workon`` home,
owned by the given user.
"""
mkdir(c, workon_home, owner=user, use_sudo=True)
if use_apt:
apt.install(c, 'virtualenvwrapper')
else:
pip(c, 'virtualenvwrapper', upgrade=True)
configure_virtualenvwrapper(c, user, workon_home)
if configure_me:
# TODO
# configure_virtualenvwrapper(c, env.user, workon_home)
raise NotImplementedError
def configure_virtualenvwrapper(c, user, workon_home='/srv/envs', wrapper='/usr/local/bin/virtualenvwrapper.sh'):
"""
Configure virtualenvwrapper for the given user account.
"""
home = c.run('getent passwd {} | cut -d: -f6'.format(user)).stdout.strip()
home = home.rstrip('/')
def update(script):
script = '{}/{}'.format(home, script)
if not exists(c, script):
c.sudo('touch {}'.format(script))
c.sudo('chown {}: {}'.format(user, script))
if c.sudo("grep '^export WORKON_HOME.*' {}".format(script), warn=True).failed:
c.sudo("""bash -c 'echo "export WORKON_HOME={}" >> {}'""".format(workon_home, script))
c.sudo("""bash -c 'echo "source {}" >> {}'""".format(wrapper, script))
else:
c.sudo("sed -i.bak -e 's/^export WORKON_HOME=.*/export WORKON_HOME={}/' {}".format(
workon_home.replace('/', '\\/'), script))
update('.profile')
update('.bashrc')
c.sudo("bash -l -c 'whoami'", user=user)
def mkvirtualenv(c, name, workon_home='/srv/envs', python=None,
use_sudo=True, user=None, runas_user=None):
"""
Make a new Python virtual environment.
"""
cmd = 'mkvirtualenv {} {}'.format('--python={}'.format(python) if python else '', name)
if use_sudo:
kw = {}
if runas_user:
kw = {'user': runas_user}
c.sudo("bash -l -c '{}'".format(cmd), **kw)
else:
# TODO: need to use `bash -l` for this too?
c.run(cmd)
@contextmanager
def workon(c, name):
"""
Context manager to prefix your command(s) with the ``workon`` command.
"""
with c.prefix('workon {}'.format(name)):
yield
@contextmanager
def cdvirtualenv(c, name, subdirs=[], workon_home='/srv/envs'):
"""
Context manager to prefix your command(s) with the ``cdvirtualenv`` command.
"""
if isinstance(subdirs, six.string_types):
subdirs = [subdirs]
path = '{}/{}'.format(workon_home, name)
if subdirs:
path = '{}/{}'.format(path, '/'.join(subdirs))
with workon(c, name):
with c.cd(path):
yield

View file

@ -0,0 +1,56 @@
# -*- coding: utf-8; -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2018 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Fabric library for Rattail itself
"""
from __future__ import unicode_literals, absolute_import
import os
from rattail_fabric2 import make_deploy, make_system_user, mkdir
deploy = make_deploy(__file__)
def bootstrap_rattail(c, home='/var/lib/rattail', uid=None, shell='/bin/bash'):
"""
Bootstrap a basic Rattail software environment.
"""
make_system_user(c, 'rattail', home=home, uid=uid, shell=shell)
mkdir(c, os.path.join(home, '.ssh'), owner='rattail:', mode='0700', use_sudo=True)
mkdir(c, '/etc/rattail', use_sudo=True)
mkdir(c, '/srv/rattail', use_sudo=True)
mkdir(c, '/var/log/rattail', owner='rattail:rattail', mode='0775', use_sudo=True)
mkdir(c, '/srv/rattail/init', use_sudo=True)
deploy(c, 'daemon', '/srv/rattail/init/daemon', use_sudo=True)
deploy(c, 'check-rattail-daemon', '/usr/local/bin/check-rattail-daemon', use_sudo=True)
deploy(c, 'luigid', '/srv/rattail/init/luigid', use_sudo=True)
deploy(c, 'soffice', '/srv/rattail/init/soffice', use_sudo=True)
# TODO: deprecate / remove these
deploy(c, 'bouncer', '/srv/rattail/init/bouncer', use_sudo=True)
deploy(c, 'datasync', '/srv/rattail/init/datasync', use_sudo=True)
deploy(c, 'filemon', '/srv/rattail/init/filemon', use_sudo=True)

66
rattail_fabric2/ssh.py Normal file
View file

@ -0,0 +1,66 @@
# -*- coding: utf-8; -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2018 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Fabric Library for SSH
"""
from __future__ import unicode_literals, absolute_import
def cache_host_key(c, host, for_user='root'):
"""
Cache the SSH host key for the given host, for the given user.
"""
cmd = 'ssh -o StrictHostKeyChecking=no {} echo'.format(host)
user = None if for_user == 'root' else for_user
c.sudo(cmd, user=user, warn=True)
def restart(c):
"""
Restart the OpenSSH service
"""
c.sudo('systemctl restart ssh.service')
def configure(c, allow_root=False):
"""
Configure the OpenSSH service
"""
path = '/etc/ssh/sshd_config'
# PermitRootLogin no (or without-password)
if c.run("grep '^PermitRootLogin ' {}".format(path), warn=True).failed:
c.sudo('sed -i.bak -e "s/^#PermitRootLogin .*/PermitRootLogin {}/" {}'.format(
'without-password' if allow_root else 'no', path))
else:
c.sudo('sed -i.bak -e "s/^PermitRootLogin .*/PermitRootLogin {}/" {}'.format(
'without-password' if allow_root else 'no', path))
# PasswordAuthentication no
if c.run("grep '^PasswordAuthentication ' {}".format(path), warn=True).failed:
c.sudo('sed -i.bak -e "s/^#?PasswordAuthentication .*/PasswordAuthentication no/" {}'.format(path))
else:
c.sudo('sed -i.bak -e "s/^PasswordAuthentication .*/PasswordAuthentication no/" {}'.format(path))
restart(c)

75
rattail_fabric2/util.py Normal file
View file

@ -0,0 +1,75 @@
# -*- coding: utf-8; -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2018 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Misc. Utilities
"""
from __future__ import unicode_literals, absolute_import
def is_win(c):
"""
Return True if remote SSH server is running Windows, False otherwise.
The idea is based on echoing quoted text: \*NIX systems will echo quoted
text only, while Windows echoes quotation marks as well.
.. note::
This function is derived from one copied from fabric v1.
"""
result = c.run('echo "Will you echo quotation marks"', warn=True)
return '"' in result.stdout
def _expand_path(c, path):
"""
Return a path expansion
E.g. ~/some/path -> /home/myuser/some/path
/user/\*/share -> /user/local/share
More examples can be found here: http://linuxcommand.org/lc3_lts0080.php
.. versionchanged:: 1.0
Avoid breaking remote Windows commands which does not support expansion.
.. note::
This function is derived from one copied from fabric v1.
"""
return path if is_win(c) else '"$(echo %s)"' % path
def exists(c, path, use_sudo=False):
"""
Return True if given path exists on the current remote host.
If ``use_sudo`` is True, will use `sudo` instead of `run`.
.. note::
This function is derived from one copied from fabric v1.
"""
func = c.sudo if use_sudo else c.run
cmd = 'stat %s' % _expand_path(c, path)
return not func(cmd, warn=True).failed