diff --git a/fabfile.py b/fabfile.py index a457184d007566d7283a953ea98fe7bc89f40bf3..4a4ba4961f88d3a3bfbdaf6daf8b157d40b65dcf 100644 --- a/fabfile.py +++ b/fabfile.py @@ -26,11 +26,41 @@ import os.path import shutil from fabric.api import * +from fabric.contrib.files import exists, upload_template execfile(os.path.join(os.path.dirname(__file__), 'rattail', '_version.py')) +def get_overwrite(overwrite=None): + """ + Obtain a canonical "overwrite" directive. + """ + + if overwrite in (True, False): + return overwrite + + if overwrite is None: + overwrite = raw_input("Overwrite as needed? [y/N]: ") + overwrite = overwrite.strip().lower() + else: + overwrite = overwrite.lower() + return overwrite.startswith('y') + + +def prettify(text): + """ + Return a "prettified" version of text. + + This is basically copied from ``edbob.util``. + """ + + text = text.replace('_', ' ') + text = text.replace('-', ' ') + words = text.split() + return ' '.join([x.capitalize() for x in words]) + + @task def release(): """ @@ -49,9 +79,69 @@ def release(): @task -def create_user(): +def create_user(overwrite=None): """ Create the 'rattail' user account. """ - sudo('adduser --system --home /var/lib/rattail --group rattail') + overwrite = get_overwrite(overwrite) + + with settings(warn_only=True): + result = run('id rattail') + + if result.return_code: # user doesn't exist + sudo('adduser --system --home /var/lib/rattail --group rattail') + + elif overwrite: + sudo('usermod --home /var/lib/rattail --move-home --shell /bin/false rattail') + + +@task +def configure_filemon(name=None, desc=None, envdir=None, overwrite=None): + """ + Configure a Rattail File Monitor daemon. + """ + + if name is None: + default_name = 'rattail-filemon' + name = raw_input("Enter a name for the service [{0}]: ".format(default_name)) + name = name.strip() or default_name + + if desc is None: + default_desc = prettify(name.replace('filemon', 'file-monitor')) + desc = raw_input("Enter a description [{0}]: ".format(default_desc)) + desc = desc.strip() or default_desc + + if envdir is None: + default_envdir = name.replace('-filemon', '') + default_envdir = default_envdir.replace('-', '.') + default_envdir = '/srv/envs/{0}'.format(default_envdir) + envdir = raw_input("Enter a virtual environment path [{0}]: ".format(default_envdir)) + envdir = envdir.strip() or default_envdir + + overwrite = get_overwrite(overwrite) + + if not exists('/etc/default/rattail') or overwrite: + put('data/etc/default/rattail', '/etc/default', use_sudo=True) + sudo('chown root: /etc/default/rattail') + + if not exists('/var/lib/rattail'): + create_user(overwrite=True) + + if not exists('/var/lib/rattail/init.d'): + sudo('mkdir /var/lib/rattail/init.d', user='rattail') + + if not exists('/var/lib/rattail/init.d/filemon') or overwrite: + put('data/var/lib/rattail/init.d/filemon', '/var/lib/rattail/init.d', use_sudo=True) + sudo('chown rattail: /var/lib/rattail/init.d/filemon') + + script = '/etc/init.d/{0}'.format(name) + if not exists(script) or overwrite: + upload_template('data/etc/init.d/rattail-filemon_tmpl', script, + context=locals(), use_sudo=True, mode=0755) + sudo('chown root: {0}'.format(script)) + sudo('insserv {0}'.format(script)) + + if not exists('/var/run/rattail'): + sudo('mkdir /var/run/rattail') + sudo('chown rattail: /var/run/rattail')