2022-01-28 15:29:32 -06:00
|
|
|
# -*- coding: utf-8; -*-
|
|
|
|
################################################################################
|
|
|
|
#
|
|
|
|
# Rattail -- Retail Software Framework
|
2023-01-08 12:41:25 -06:00
|
|
|
# Copyright © 2010-2023 Lance Edgar
|
2022-01-28 15:29:32 -06:00
|
|
|
#
|
|
|
|
# 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 Luigi apps
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
2023-01-09 09:00:50 -06:00
|
|
|
import re
|
|
|
|
from pkg_resources import parse_version
|
2022-01-28 15:29:32 -06:00
|
|
|
|
2023-01-08 14:55:33 -06:00
|
|
|
from rattail_fabric2 import postgresql, make_deploy, mkdir
|
2022-01-28 15:29:32 -06:00
|
|
|
|
|
|
|
|
|
|
|
deploy_common = make_deploy(__file__)
|
|
|
|
|
|
|
|
|
2023-01-08 14:55:33 -06:00
|
|
|
def install_luigi(c, envroot, luigi='luigi', user='rattail',
|
|
|
|
db=False, dbuser='rattail', dbpass='TODO_PASSWORD',
|
|
|
|
db_connection=None,
|
|
|
|
supervisor=False, autostart=False,
|
2023-01-08 13:04:46 -06:00
|
|
|
crontab=False, crontab_mailto=None):
|
2022-01-28 15:29:32 -06:00
|
|
|
"""
|
|
|
|
Install and configure Luigi to the given virtualenv.
|
|
|
|
"""
|
|
|
|
envroot = envroot.rstrip('/')
|
|
|
|
envname = os.path.basename(envroot)
|
|
|
|
appdir = '{}/app'.format(envroot)
|
|
|
|
|
|
|
|
# package
|
2022-03-18 21:46:41 -05:00
|
|
|
c.sudo("""bash -lc "workon {} && pip install '{}'" """.format(envname, luigi),
|
2022-01-28 15:29:32 -06:00
|
|
|
user=user)
|
|
|
|
|
2023-01-09 09:00:50 -06:00
|
|
|
# detect luigi version
|
|
|
|
LUIGI2 = False
|
|
|
|
output = c.sudo("bash -lc 'workon {} && pip show luigi | grep Version'".format(envname), user=user).stdout.strip()
|
|
|
|
match = re.match(r'^Version: (\d+\S+)$', output)
|
|
|
|
if match:
|
|
|
|
if parse_version(match.group(1)) < parse_version('3'):
|
|
|
|
LUIGI2 = True
|
|
|
|
|
2022-01-28 15:29:32 -06:00
|
|
|
# dirs
|
|
|
|
mkdir(c, ['{}/luigi'.format(appdir),
|
|
|
|
'{}/luigi/log'.format(appdir),
|
|
|
|
'{}/luigitasks'.format(appdir),
|
|
|
|
], use_sudo=True, owner=user)
|
|
|
|
|
|
|
|
# tasks
|
|
|
|
c.sudo('touch {}/luigitasks/__init__.py'.format(appdir),
|
|
|
|
user=user)
|
|
|
|
|
2023-01-08 14:55:33 -06:00
|
|
|
# database
|
|
|
|
if db:
|
|
|
|
postgresql.create_db(c, 'luigi', owner=dbuser)
|
|
|
|
if not db_connection:
|
|
|
|
db_connection = 'postgresql://{}:{}@localhost/luigi'.format(
|
|
|
|
dbuser, dbpass)
|
|
|
|
|
2022-01-28 15:29:32 -06:00
|
|
|
# config
|
|
|
|
deploy_common(c, 'luigi/luigi.cfg.mako', '{}/luigi/luigi.cfg'.format(appdir),
|
|
|
|
use_sudo=True, owner=user, mode='0640',
|
|
|
|
context={'appdir': appdir,
|
2023-03-09 15:31:03 -06:00
|
|
|
'LUIGI2': LUIGI2,
|
2022-01-28 15:29:32 -06:00
|
|
|
'db_connection': db_connection})
|
|
|
|
deploy_common(c, 'luigi/logging.conf.mako', '{}/luigi/logging.conf'.format(appdir),
|
|
|
|
use_sudo=True, owner=user,
|
|
|
|
context={'appdir': appdir})
|
|
|
|
|
2023-01-08 14:55:33 -06:00
|
|
|
# supervisor
|
|
|
|
if supervisor:
|
|
|
|
c.sudo('supervisorctl stop luigi:', warn=True)
|
|
|
|
deploy_common(c, 'luigi/supervisor.conf.mako',
|
|
|
|
'/etc/supervisor/conf.d/luigi.conf',
|
|
|
|
use_sudo=True,
|
|
|
|
context={'envroot': envroot,
|
|
|
|
'appdir': appdir,
|
|
|
|
'user': user,
|
|
|
|
'autostart': autostart})
|
|
|
|
c.sudo('supervisorctl update')
|
|
|
|
if autostart:
|
|
|
|
c.sudo('supervisorctl start luigi:')
|
|
|
|
|
2022-01-28 15:29:32 -06:00
|
|
|
# logrotate
|
|
|
|
deploy_common(c, 'luigi/luigi-logrotate.conf.mako', '{}/luigi/logrotate.conf'.format(appdir),
|
|
|
|
use_sudo=True, owner='root:', # must be owned by root (TODO: why is that again?)
|
|
|
|
context={'appdir': appdir})
|
|
|
|
deploy_common(c, 'luigi/rotate-logs.sh.mako', '{}/rotate-luigi-logs.sh'.format(appdir),
|
|
|
|
use_sudo=True, owner=user,
|
|
|
|
context={'appdir': appdir})
|
2023-01-08 12:41:25 -06:00
|
|
|
if crontab:
|
|
|
|
deploy_common(c, 'luigi/crontab.mako', '/etc/cron.d/luigi',
|
2023-01-08 13:04:46 -06:00
|
|
|
use_sudo=True, context={'appdir': appdir,
|
|
|
|
'mailto': crontab_mailto})
|
2022-01-28 15:29:32 -06:00
|
|
|
|
|
|
|
|
2022-03-21 17:54:23 -05:00
|
|
|
def install_overnight_script(c, envroot, user='rattail', automation='All',
|
2022-11-20 20:02:48 -06:00
|
|
|
email_key=None,
|
2022-11-27 12:47:39 -06:00
|
|
|
luigi=False,
|
|
|
|
cron=True, cron_conf='app/cron.conf',
|
|
|
|
restart=True, restart_conf='app/silent.conf'):
|
2022-01-28 15:29:32 -06:00
|
|
|
"""
|
|
|
|
Install an overnight automation script
|
|
|
|
"""
|
|
|
|
envroot = envroot.rstrip('/')
|
|
|
|
appdir = '{}/app'.format(envroot)
|
|
|
|
|
|
|
|
# overnight-*.sh
|
2022-11-20 20:02:48 -06:00
|
|
|
if luigi:
|
|
|
|
filename = 'overnight-{}.sh'.format(automation.lower())
|
|
|
|
deploy_common(c, 'luigi/overnight.sh.mako',
|
|
|
|
'{}/{}'.format(appdir, filename),
|
|
|
|
use_sudo=True, owner=user, mode='0755',
|
|
|
|
context={'envroot': envroot, 'appdir': appdir,
|
|
|
|
'automation': automation})
|
2022-01-28 15:29:32 -06:00
|
|
|
|
|
|
|
# cron-overnight-*.sh
|
2022-11-20 20:02:48 -06:00
|
|
|
if cron:
|
|
|
|
filename = 'cron-overnight-{}.sh'.format(automation.lower())
|
|
|
|
deploy_common(c, 'luigi/cron-overnight.sh.mako',
|
|
|
|
'{}/{}'.format(appdir, filename),
|
|
|
|
use_sudo=True, owner=user, mode='0755',
|
|
|
|
context={'envroot': envroot,
|
2022-11-27 12:47:39 -06:00
|
|
|
'overnight_conf': cron_conf,
|
2022-11-20 20:02:48 -06:00
|
|
|
'automation': automation,
|
|
|
|
'email_key': email_key})
|
2022-01-28 15:29:32 -06:00
|
|
|
|
|
|
|
# restart-overnight-*.sh
|
2022-11-20 20:02:48 -06:00
|
|
|
if restart:
|
|
|
|
filename = 'restart-overnight-{}.sh'.format(automation.lower())
|
|
|
|
deploy_common(c, 'luigi/restart-overnight.sh.mako',
|
|
|
|
'{}/{}'.format(appdir, filename),
|
|
|
|
use_sudo=True, owner=user, mode='0755',
|
|
|
|
context={'envroot': envroot,
|
|
|
|
'appdir': appdir,
|
2022-11-27 12:47:39 -06:00
|
|
|
'overnight_conf': restart_conf,
|
2022-11-20 20:02:48 -06:00
|
|
|
'automation': automation,
|
|
|
|
'email_key': email_key})
|
2022-01-28 15:29:32 -06:00
|
|
|
|
|
|
|
|
|
|
|
def register_with_supervisor(c, envroot, user='rattail', autostart=False):
|
|
|
|
"""
|
|
|
|
Register the Luigi scheduler daemon with supervisor
|
|
|
|
"""
|
|
|
|
envroot = envroot.rstrip('/')
|
|
|
|
appdir = '{}/app'.format(envroot)
|
|
|
|
|
|
|
|
deploy_common(c, 'luigi/supervisor.conf.mako',
|
|
|
|
'/etc/supervisor/conf.d/luigi.conf',
|
|
|
|
use_sudo=True,
|
|
|
|
context={'envroot': envroot,
|
|
|
|
'appdir': appdir,
|
|
|
|
'user': user,
|
|
|
|
'autostart': autostart})
|
|
|
|
c.sudo('supervisorctl update')
|
|
|
|
if autostart:
|
|
|
|
c.sudo('supervisorctl start luigi:')
|