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

Last change on this file since a920eeb was a920eeb, checked in by Lance Edgar <lance@…>, 2 years ago

Use 0700 perms for backup-everything script

just in case it has secrets

  • 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                      config=None, everything=None, crontab=None, runat=UNSPECIFIED):
52    """
53    Make an app which can run backups for the server.
54    """
55    if not config:
56        path = '{}/rattail.conf'.format(envname)
57        if deploy.local_exists(path):
58            config = path
59        else:
60            raise ValueError("Must provide config path for backup app")
61
62    if runat is UNSPECIFIED:
63        runat = datetime.time(0) # defaults to midnight
64
65    # virtualenv
66    if mkvirtualenv:
67        python.mkvirtualenv(envname, python='/usr/bin/python3', upgrade_setuptools=False)
68    envpath = '/srv/envs/{}'.format(envname)
69    sudo('chown -R {}: {}'.format(user, envpath))
70    with cd(envpath):
71        mkdir('src', owner=user)
72        sudo('bin/pip install --upgrade pip', user=user)
73
74        # rattail
75        if not exists('src/rattail'):
76            sudo('git clone https://rattailproject.org/git/rattail.git src/rattail', user=user)
77        with cd('src/rattail'):
78            sudo('git pull', user=user)
79            deploy_generic('backup/git-exclude', '.git/info/exclude', owner=user)
80        sudo('bin/pip install --upgrade --upgrade-strategy eager --editable src/rattail', user=user)
81
82        # config
83        sudo('bin/rattail make-appdir', user=user)
84        deploy(config, 'app/rattail.conf', owner=user, mode='0600')
85        sudo('bin/rattail -c app/rattail.conf make-config -T quiet -O app/', user=user)
86        sudo('bin/rattail -c app/rattail.conf make-config -T silent -O app/', user=user)
87
88    # backup-everything script
89    everything_context = {
90        'envname': envname,
91        'user': user,
92    }
93    if everything:
94        deploy(everything, '/usr/local/bin/backup-everything', mode='0700', context=everything_context)
95    else:
96        deploy_backup_everything(**everything_context)
97
98    # crontab
99    if runat:
100        crontab_context = {
101            'envname': envname,
102            'pretty_time': runat.strftime('%I:%M %p'),
103            'cron_time': runat.strftime('%M %H'),
104        }
105        if crontab:
106            deploy(crontab, '/etc/cron.d/backup', context=crontab_context)
107        else:
108            deploy_generic('backup/crontab.mako', '/etc/cron.d/backup', context=crontab_context)
Note: See TracBrowser for help on using the repository browser.