source: rattail-fabric/rattail_fabric/backup.py @ f20c7e4

Last change on this file since f20c7e4 was f20c7e4, checked in by Lance Edgar <ledgar@…>, 18 months ago

Allow alternate python3 for backup app environment

  • Property mode set to 100644
File size: 3.8 KB
Line 
1# -*- coding: utf-8; -*-
2################################################################################
3#
4#  Rattail -- Retail Software Framework
5#  Copyright © 2010-2018 Lance Edgar
6#
7#  This file is part of Rattail.
8#
9#  Rattail is free software: you can redistribute it and/or modify it under the
10#  terms of the GNU General Public License as published by the Free Software
11#  Foundation, either version 3 of the License, or (at your option) any later
12#  version.
13#
14#  Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
15#  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16#  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17#  details.
18#
19#  You should have received a copy of the GNU General Public License along with
20#  Rattail.  If not, see <http://www.gnu.org/licenses/>.
21#
22################################################################################
23"""
24Fabric library for Backup app
25"""
26
27from __future__ import unicode_literals, absolute_import
28
29import datetime
30
31from fabric.api import cd, sudo
32from fabric.contrib.files import exists
33
34from rattail_fabric import make_deploy, mkdir, python, UNSPECIFIED
35
36
37deploy_generic = make_deploy(__file__)
38
39
40def deploy_backup_everything(**context):
41    """
42    Deploy the generic `backup-everything` script
43    """
44    context.setdefault('envname', 'backup')
45    context.setdefault('user', 'rattail')
46    deploy_generic('backup/backup-everything.mako', '/usr/local/bin/backup-everything', mode='0700',
47                   context=context)
48
49
50def deploy_backup_app(deploy, envname, mkvirtualenv=True, user='rattail',
51                      python_exe='/usr/bin/python3',
52                      config=None, everything=None, crontab=None, runat=UNSPECIFIED):
53    """
54    Make an app which can run backups for the server.
55    """
56    if not config:
57        path = '{}/rattail.conf'.format(envname)
58        if deploy.local_exists(path):
59            config = path
60        else:
61            raise ValueError("Must provide config path for backup app")
62
63    if runat is UNSPECIFIED:
64        runat = datetime.time(0) # defaults to midnight
65
66    # virtualenv
67    if mkvirtualenv:
68        python.mkvirtualenv(envname, python=python_exe, upgrade_setuptools=False)
69    envpath = '/srv/envs/{}'.format(envname)
70    sudo('chown -R {}: {}'.format(user, envpath))
71    with cd(envpath):
72        mkdir('src', owner=user)
73        sudo('bin/pip install --upgrade pip', user=user)
74
75        # rattail
76        if not exists('src/rattail'):
77            sudo('git clone https://rattailproject.org/git/rattail.git src/rattail', user=user)
78        with cd('src/rattail'):
79            sudo('git pull', user=user)
80            deploy_generic('backup/git-exclude', '.git/info/exclude', owner=user)
81        sudo('bin/pip install --upgrade --upgrade-strategy eager --editable src/rattail', user=user)
82
83        # config
84        sudo('bin/rattail make-appdir', user=user)
85        deploy(config, 'app/rattail.conf', owner=user, mode='0600')
86        sudo('bin/rattail -c app/rattail.conf make-config -T quiet -O app/', user=user)
87        sudo('bin/rattail -c app/rattail.conf make-config -T silent -O app/', user=user)
88
89    # backup-everything script
90    everything_context = {
91        'envname': envname,
92        'user': user,
93    }
94    if everything:
95        deploy(everything, '/usr/local/bin/backup-everything', mode='0700', context=everything_context)
96    else:
97        deploy_backup_everything(**everything_context)
98
99    # crontab
100    if runat:
101        crontab_context = {
102            'envname': envname,
103            'pretty_time': runat.strftime('%I:%M %p'),
104            'cron_time': runat.strftime('%M %H'),
105        }
106        if crontab:
107            deploy(crontab, '/etc/cron.d/backup', context=crontab_context)
108        else:
109            deploy_generic('backup/crontab.mako', '/etc/cron.d/backup', context=crontab_context)
Note: See TracBrowser for help on using the repository browser.