]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Start refactoring evaluating virtual alias files.
authorFrank Brehm <frank.brehm@pixelpark.com>
Wed, 24 May 2023 08:35:18 +0000 (10:35 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Wed, 24 May 2023 08:35:18 +0000 (10:35 +0200)
lib/pp_admintools/app/barracuda_sync.py

index 8a91543fbab88c99830d5b0c7d47c485c36999df..52c1eed628d459b47bdc96d0a1de296f607481ac 100644 (file)
@@ -9,7 +9,7 @@
 from __future__ import absolute_import
 
 # Standard modules
-import copy
+import copy
 import logging
 import re
 # import sys
@@ -20,13 +20,13 @@ from fb_tools.argparse_actions import DirectoryOptionAction
 from fb_tools.common import pp
 from fb_tools.handler import BaseHandler
 from fb_tools.multi_config import DEFAULT_ENCODING
-from fb_tools.xlate import format_list
+from fb_tools.xlate import format_list
 
 # Own modules
 from .ldap import BaseLdapApplication
 from ..xlate import XLATOR
 
-__version__ = '0.7.2'
+__version__ = '0.7.4'
 LOG = logging.getLogger(__name__)
 
 _ = XLATOR.gettext
@@ -45,8 +45,6 @@ class BarracudaSyncApp(BaseLdapApplication):
     postfix_config_dir = Path('/etc/postfix')
     postfix_maps_dir = postfix_config_dir / 'maps'
 
-    default_virtaliases_basename = 'virtual-aliases'
-    default_virtaliases_files = [postfix_maps_dir / default_virtaliases_basename, ]
     default_postfix_db_hashtype = 'hash'
 
     postfix_filetype_extensions = {
@@ -65,6 +63,9 @@ class BarracudaSyncApp(BaseLdapApplication):
     default_origin = 'pixelpark.com'
 
     re_virtaliases_line = re.compile(r'^([^#\s:]+)\s', re.MULTILINE)
+    re_pf_config = re.compile(r'^(?P<key>\S+)\s*=\s*(?P<value>\S.*)?')
+    re_pf_fieldsep = re.compile(r'\s*[,;]\s*')
+    re_empty_line = re.compile(r'^\s*(?:#.*|$)')
 
     default_postmap_command = 'postmap'
     default_postconf_command = 'postconf'
@@ -93,7 +94,7 @@ class BarracudaSyncApp(BaseLdapApplication):
     def __init__(self, appname=None, base_dir=None):
         """Constructz the application object."""
         self.barracuda_base_dn = self.default_barracuda_base_dn
-        self.virtaliases_files = copy.copy(self.default_virtaliases_files)
+        self.virtaliases_files = []
         self.postfix_db_hashtype = self.default_postfix_db_hashtype
         self.postconf_command = Path('/sbin') / self.default_postconf_command
         self.postmap_command = Path('/sbin') / self.default_postmap_command
@@ -129,21 +130,21 @@ class BarracudaSyncApp(BaseLdapApplication):
                 'It has to be exists. Default: {!r}.').format(str(self.postfix_maps_dir)),
         )
 
-        sync_group.add_argument(
-            '-B', '--basename', '--virtalias-basename', nargs='*', dest='basename',
-            metavar=_('NAME'),
-            help=_(
-                'All possible basenames of the virtual aliases file below the latter '
-                'directory. All of these basenames are used as source of the virtual aliases. '
-                'Default: {!r}.').format(self.default_virtaliases_basename),
-        )
-
-        sync_group.add_argument(
-            '--type', dest='hashtype', metavar=_('TYPE'),
-            help=_(
-                'The used lookup table type of all virtual aliases table. '
-                'Default: {!r}.').format(self.default_postfix_db_hashtype),
-        )
+#        sync_group.add_argument(
+#            '-B', '--basename', '--virtalias-basename', nargs='*', dest='basename',
+#            metavar=_('NAME'),
+#            help=_(
+#                'All possible basenames of the virtual aliases file below the latter '
+#                'directory. All of these basenames are used as source of the virtual aliases. '
+#                'Default: {!r}.').format(self.default_virtaliases_basename),
+#        )
+
+#        sync_group.add_argument(
+#            '--type', dest='hashtype', metavar=_('TYPE'),
+#            help=_(
+#                'The used lookup table type of all virtual aliases table. '
+#                'Default: {!r}.').format(self.default_postfix_db_hashtype),
+#        )
 
         sync_group.add_argument(
             '--base-dn', dest='baase_dn', metavar='DN',
@@ -164,14 +165,16 @@ class BarracudaSyncApp(BaseLdapApplication):
         """Execute this method before calling run()."""
         super(BarracudaSyncApp, self).post_init()
 
-        self._check_postfix()
-        self._init_virtaliases_files()
-        self._check_virtaliases_files()
+        self._check_postfix_commands()
+        self._check_postfix_table_types()
+        self._get_postfix_default_db_type()
+        # self._init_virtaliases_files()
+        # self._check_virtaliases_files()
 
     # -------------------------------------------------------------------------
-    def _check_postfix(self):
-        """Check postfix commands and lookup table types."""
-        LOG.debug(_('Checking postfix commands and lookup table types ...'))
+    def _check_postfix_commands(self):
+        """Check postfix commands."""
+        LOG.debug(_('Checking postfix commands ...'))
 
         found_all_commands = True
 
@@ -191,6 +194,11 @@ class BarracudaSyncApp(BaseLdapApplication):
             LOG.err(_('Postfix seems not to be installed.'))
             self.exit(5)
 
+    # -------------------------------------------------------------------------
+    def _check_postfix_table_types(self):
+        """Check postfix lookup table types."""
+        LOG.debug(_('Checking postfix lookup table types ...'))
+
         LOG.debug(_('Evaluating lookup table types.'))
         handler = BaseHandler(appname=self.appname, verbose=self.verbose)
 
@@ -218,30 +226,35 @@ class BarracudaSyncApp(BaseLdapApplication):
             LOG.err(msg)
             self.exit(6)
 
-        if self.args.hashtype:
-            htype = self.args.hashtype
-            if htype not in self.lookup_table_types:
-                msg = _('Wrong lookup table type {!r} given. Valid types are:').format(htype)
-                msg += ' ' + format_list(self.lookup_table_types, do_repr=True)
-                LOG.error(msg)
-                self.exit(1)
-            self.postfix_db_hashtype = htype
-        elif self.postfix_db_hashtype not in self.lookup_table_types:
-            msg = _('Default lookup table type {!r} is invalid. Valid types are:').format(
-                self.postfix_db_hashtype)
-            LOG.error(msg)
-            self.exit(6)
-
-        if self.verbose > 1:
-            LOG.debug(_('Candidate for lookup table type: {!r}.').format(self.postfix_db_hashtype))
+    # -------------------------------------------------------------------------
+    def _get_postfix_default_db_type(self):
+        """Evaluate default postfix lookup table type."""
+        LOG.debug(_('Evaluating default postfix lookup table type ...'))
 
-        if self.postfix_db_hashtype not in self.usable_postfix_hashtypes:
-            msg = _(
-                'The lookup table type {!r} cannot be used to get all '
-                'database elements.').format(self.postfix_db_hashtype)
-            LOG.error(msg)
+        handler = BaseHandler(appname=self.appname, verbose=self.verbose)
+        pdata = handler.call([str(self.postconf_command), 'default_database_type'], quiet=True)
+        if pdata.returncode > 0:
+            msg = _('Error {} on evaluating default postfix lookup table type').format(
+                pdata.returncode)
+            if pdata.stderr:
+                msg += ': ' + pdata.stderr
+            else:
+                msg += '.'
+            LOG.err(msg)
             self.exit(6)
 
+        for line in pdata.stdout.splitlines():
+            if self.re_empty_line.match(line):
+                continue
+            m = self.re_pf_config.match(line)
+            if m and m.group('key') == 'default_database_type':
+                table_type = m.group('value')
+                if table_type:
+                    table_type = table_type.strip()
+                if table_type:
+                    self.postfix_db_hashtype = table_type
+                    self.debug(_('Found postfix default database type: {!r}.').format(table_type))
+
     # -------------------------------------------------------------------------
     def _init_virtaliases_files(self):
         """Collect all files used as database for virtual aliases."""