Files @ 3c3d26f172cf
Branch filter:

Location: rattail-project/rattail/fabfile.py

Lance Edgar
Add `files.locking_copy_old()` function.

Something *did* appear to change regarding the behavior of the newer
`locking_copy()` function. Making the old one available again at least
temporarily, until the issue is settled.
# -*- coding: utf-8 -*-
################################################################################
#
#  Rattail -- Retail Software Framework
#  Copyright © 2010-2014 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 Affero 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 Affero General Public License for
#  more details.
#
#  You should have received a copy of the GNU Affero General Public License
#  along with Rattail.  If not, see <http://www.gnu.org/licenses/>.
#
################################################################################

from __future__ import unicode_literals

import shutil

from fabric.api import *
from fabric.contrib.files import exists, upload_template


@task
def release():
    """
    Release a new version of `rattail`.
    """

    shutil.rmtree('rattail.egg-info')
    local('python setup.py sdist --formats=gztar register upload')


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.
    """
    text = text.replace('_', ' ')
    text = text.replace('-', ' ')
    words = text.split()
    return ' '.join([x.capitalize() for x in words])


@task
def create_user(overwrite=None):
    """
    Create the 'rattail' user account.
    """

    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')


def configure_rattail_lib(overwrite=None):
    """
    Configure basic Rattail "library" files.
    """

    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/run/rattail'):
        sudo('mkdir /var/run/rattail')
        sudo('chown rattail: /var/run/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)
    configure_rattail_lib(overwrite)

    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('update-rc.d {0} defaults'.format(script))


@task
def configure_dbsync(name=None, desc=None, envdir=None, overwrite=None):
    """
    Configure a Rattail Database Synchonizer daemon.
    """

    if name is None:
        default_name = 'rattail-dbsync'
        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('dbsync', 'database-synchronizer'))
        desc = raw_input("Enter a description [{0}]: ".format(default_desc))
        desc = desc.strip() or default_desc

    if envdir is None:
        default_envdir = name.replace('-dbsync', '')
        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)
    configure_rattail_lib(overwrite)

    if not exists('/var/lib/rattail/init.d/dbsync') or overwrite:
        put('data/var/lib/rattail/init.d/dbsync', '/var/lib/rattail/init.d', use_sudo=True)
        sudo('chown rattail: /var/lib/rattail/init.d/dbsync')

    script = '/etc/init.d/{0}'.format(name)
    if not exists(script) or overwrite:
        upload_template('data/etc/init.d/rattail-dbsync_tmpl', script,
                        context=locals(), use_sudo=True, mode=0755)
        sudo('chown root: {0}'.format(script))
        sudo('update-rc.d {0} defaults'.format(script))