]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Adding additional checks to pp_lib/barracuda_sync_app.py
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 5 May 2017 09:09:44 +0000 (11:09 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 5 May 2017 09:09:44 +0000 (11:09 +0200)
pp_lib/barracuda_sync_app.py

index 217c40ea9ac31398f7a9a00da1710835f5ae1549..166ecf5619c7e897a30898103ab5688b86fa70c0 100644 (file)
@@ -30,7 +30,7 @@ from .ldap_app import PpLdapAppError, PpLdapApplication
 
 from .mailaddress import MailAddress
 
-__version__ = '0.4.3'
+__version__ = '0.4.4'
 LOG = logging.getLogger(__name__)
 
 
@@ -95,6 +95,7 @@ class PpBarracudaSyncApp(PpLdapApplication):
             cfg_stems='barracuda-sync'
         )
 
+        self._check_virtaliases_files()
         self._init_ignore_aliases_res()
         self.initialized = True
 
@@ -181,6 +182,16 @@ class PpBarracudaSyncApp(PpLdapApplication):
         if hasattr(self.args, 'postfix_dir') and self.args.postfix_dir:
             self._init_postfix_dir(self.args.postfix_dir)
 
+        if not os.path.isdir(self.postfix_config_dir):
+            LOG.error("Postfix directory {!r} does not exists or is not a directory.".format(
+                self.postfix_config_dir))
+            self.exit(1)
+
+        if not os.path.isdir(self.postfix_maps_dir):
+            LOG.error("Postfix maps directory {!r} does not exists or is not a directory.".format(
+                self.postfix_maps_dir))
+            self.exit(1)
+
         self._init_virtaliases_files(virtaliases_files)
 
     # -------------------------------------------------------------------------
@@ -230,6 +241,30 @@ class PpBarracudaSyncApp(PpLdapApplication):
             if afile not in self.virtaliases_files:
                 self.virtaliases_files.append(afile)
 
+    # -------------------------------------------------------------------------
+    def _check_virtaliases_files(self):
+
+        ok = True
+        for afile in self.virtaliases_files:
+
+            if not os.path.exists(afile):
+                LOG.error("Virtual aliases file {!r} does not exists.".format(afile))
+                ok = False
+                continue
+
+            if not os.path.isfile(afile):
+                LOG.error("Virtual aliases file {!r} is not a regular file.".format(afile))
+                ok = False
+                continue
+
+            if not os.access(afile, os.R_OK):
+                LOG.error("No read access to virtual aliases file {!r}.".format(afile))
+                ok = False
+                continue
+
+        if not ok:
+            self.exit(1)
+
     # -------------------------------------------------------------------------
     def _init_postfix_dir(self, value):
 
@@ -243,6 +278,9 @@ class PpBarracudaSyncApp(PpLdapApplication):
             self.default_virtaliases_files = [
                 os.path.join(self.postfix_maps_dir, 'virtual-aliases'),
             ]
+        else:
+            LOG.warn("Postfix directory {!r} does not exists or is not a directory.".format(
+                value))
 
     # -------------------------------------------------------------------------
     def _init_ignore_aliases_res(self):
@@ -272,6 +310,32 @@ class PpBarracudaSyncApp(PpLdapApplication):
 
         super(PpBarracudaSyncApp, self).pre_run()
 
+        self._check_ldap_barracuda_container()
+
+    # -------------------------------------------------------------------------
+    def _check_ldap_barracuda_container(self):
+
+        LOG.debug("Checking existence of Baracuda LDAP container {!r}.".format(
+            self.barracuda_base_dn))
+        query = '(objectclass=organizationalunit)'
+
+        self.ldap_connection.search(
+            search_base=self.barracuda_base_dn, search_filter=query,
+            search_scope=BASE, attributes='*')
+
+        LOG.debug("Found {} entries.".format(len(self.ldap_connection.response)))
+        if len(self.ldap_connection.response) < 1:
+            LOG.error((
+                "Did not found LDAP container {!r} for "
+                "Barracuda alias definitions.").format(
+                self.barracuda_base_dn))
+            self.exit(5)
+
+        entry = self.ldap_connection.response[0]
+        if self.verbose > 1:
+            LOG.debug("Container entry - class {cl!r}, content:\n{co}".format(
+                cl=entry.__class__.__name__, co=pp(entry)))
+
     # -------------------------------------------------------------------------
     def _run(self):