From 99071f01b15a44d3482770265738447016532dc1 Mon Sep 17 00:00:00 2001 From: Marc Aymerich Date: Tue, 4 Aug 2015 10:50:05 +0000 Subject: [PATCH] Added support for phplist cron configuration --- TODO.md | 18 ------- orchestra/contrib/saas/backends/phplist.py | 61 ++++++++++++++++++---- orchestra/contrib/saas/settings.py | 20 ++++++- 3 files changed, 69 insertions(+), 30 deletions(-) diff --git a/TODO.md b/TODO.md index 2f3ad993..c2727678 100644 --- a/TODO.md +++ b/TODO.md @@ -427,30 +427,12 @@ Case # round decimals on every billing operation -# Serie1 - -# cleanup monitor data: helpers.functions into ServiceMonitor methods - # Add SPF record type # OVZ TRAFFIC ACCOUNTING!! # PHPlist cron, bounces and traffic (maybe specific mail script with sitename) - 'crontab': settings.SAAS_PHPLIST_CRONTAB.replace('$', '$$') -} -*/10 * * * * PHPLIST=%(php_list_path)s; export SITE="%(site)s"; php $PHPLIST/admin/index.php -c $PHPLIST/config/config.php -p processqueue > /dev/null -*/40 * * * * PHPLIST=%(php_list_path)s; export SITE="%(site)s"; php $PHPLIST/admin/index.php -c $PHPLIST/config/config.php -p processbounces > /dev/null -if settings.SAAS_PHPLIST_CRONTAB: - self.append(textwrap.dedent(""" - # Configuring phpList crontabs -if [[ ! $(crontab -l | grep "^%(site)s") ]]; then -cat << EOF | crontab -$(crontab -l) -# %(banner)s -%(crontab)s -EOF -fi # use "su $user --shell /bin/bash" on backends for security : MKDIR -p... diff --git a/orchestra/contrib/saas/backends/phplist.py b/orchestra/contrib/saas/backends/phplist.py index e29e96d1..667498a0 100644 --- a/orchestra/contrib/saas/backends/phplist.py +++ b/orchestra/contrib/saas/backends/phplist.py @@ -19,7 +19,10 @@ class PhpListSaaSBackend(ServiceController): but all sites share the same code. // config/config.php - $site = array_shift((explode(".",$_SERVER['HTTP_HOST']))); + $site = getenv("SITE"); + if ( $site == '' ) { + $site = array_shift((explode(".",$_SERVER['HTTP_HOST']))); + } $database_name = "phplist_mu_{$site}"; """ verbose_name = _("phpList SaaS") @@ -57,16 +60,8 @@ class PhpListSaaSBackend(ServiceController): else: md5_password = hashlib.md5() md5_password.update(saas.password.encode('ascii')) - context = { - 'name': saas.name, - 'site_name': saas.name, - 'db_user': settings.SAAS_PHPLIST_DB_USER, - 'db_pass': settings.SAAS_PHPLIST_DB_PASS, - 'db_name': settings.SAAS_PHPLIST_DB_NAME, - 'db_host': settings.SAAS_PHPLIST_DB_HOST, - 'digest': md5_password.hexdigest(), - } - context['db_name'] = context['db_name'] % context + context = self.get_context(saas) + context['digest'] = md5_password.hexdigest() cmd = textwrap.dedent("""\ mysql \\ --host=%(db_host)s \\ @@ -81,3 +76,47 @@ class PhpListSaaSBackend(ServiceController): def save(self, saas): if hasattr(saas, 'password'): self.append(self._save, saas) + context = self.get_context(saas) + if context['crontab']: + context['escaped_crontab'] = context['crontab'].replace('$', '\\$') + self.append(textwrap.dedent("""\ + # Configuring phpList crontabs + if [[ ! $(crontab -u %(user)s -l | grep 'phpList:"%(site_name)s"') ]]; then + cat << EOF | su %(user)s --shell /bin/bash -c 'crontab' + $(crontab -u %(user)s -l) + + # %(banner)s - phpList:"%(site_name)s" + %(escaped_crontab)s + EOF + fi""") % context + ) + + def delete(self, saas): + context = self.get_context(saas) + if context['crontab']: + context['crontab_regex'] = '\\|'.join(context['crontab'].splitlines()) + context['crontab_regex'] = context['crontab_regex'].replace('*', '\\*') + self.append(textwrap.dedent("""\ + crontab -u %(user)s -l \\ + | grep -v 'phpList:"%(site_name)s"\\|%(crontab_regex)s' \\ + | su %(user)s --shell /bin/bash -c 'crontab' + """) % context + ) + + def get_context(self, saas): + context = { + 'banner': self.get_banner(), + 'name': saas.name, + 'site_name': saas.name, + 'phplist_path': settings.SAAS_PHPLIST_PATH, + 'user': settings.SAAS_PHPLIST_SYSTEMUSER, + 'db_user': settings.SAAS_PHPLIST_DB_USER, + 'db_pass': settings.SAAS_PHPLIST_DB_PASS, + 'db_name': settings.SAAS_PHPLIST_DB_NAME, + 'db_host': settings.SAAS_PHPLIST_DB_HOST, + } + context.update({ + 'crontab': settings.SAAS_PHPLIST_CRONTAB % context, + 'db_name': context['db_name'] % context, + }) + return context diff --git a/orchestra/contrib/saas/settings.py b/orchestra/contrib/saas/settings.py index ac15c90b..6feeb5d6 100644 --- a/orchestra/contrib/saas/settings.py +++ b/orchestra/contrib/saas/settings.py @@ -110,9 +110,27 @@ SAAS_PHPLIST_BASE_DOMAIN = Setting('SAAS_PHPLIST_BASE_DOMAIN', SAAS_PHPLIST_VERIFY_SSL = Setting('SAAS_PHPLIST_VERIFY_SSL', True, - help_text="Verify SSL certificate on the HTTP requests performed by the backend.", + help_text=_("Verify SSL certificate on the HTTP requests performed by the backend."), ) +SAAS_PHPLIST_PATH = Setting('SAAS_PHPLIST_PATH', + '/var/www/phplist-mu', + help_text=_("Filesystem path to the phpList source code installed on the server. " + "Used by SAAS_PHPLIST_CRONTAB.") +) + +SAAS_PHPLIST_SYSTEMUSER = Setting('SAAS_PHPLIST_SYSTEMUSER', + 'root', + help_text=_("System user running phpList on the server." + "Used by SAAS_PHPLIST_CRONTAB.") +) + +SAAS_PHPLIST_CRONTAB = Setting('SAAS_PHPLIST_CRONTAB', + ('*/10 * * * * PHPLIST=%(phplist_path)s; export SITE="%(site_name)s"; php $PHPLIST/admin/index.php -c $PHPLIST/config/config.php -p processqueue > /dev/null\n' + '*/40 * * * * PHPLIST=%(phplist_path)s; export SITE="%(site_name)s"; php $PHPLIST/admin/index.php -c $PHPLIST/config/config.php -p processbounces > /dev/null'), + help_text=_("processqueue and processbounce phpList cron execution. " + "Left blank if you don't want crontab to be configured") +) SAAS_SEAFILE_DOMAIN = Setting('SAAS_SEAFILE_DOMAIN', 'seafile.{}'.format(ORCHESTRA_BASE_DOMAIN),