]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Adding checking syntax of named.conf
authorFrank Brehm <frank.brehm@pixelpark.com>
Thu, 10 Aug 2017 11:52:54 +0000 (13:52 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Thu, 10 Aug 2017 11:52:54 +0000 (13:52 +0200)
pp_lib/config_named_app.py

index 120d2cf6f5b86d9b0a3233b3972bc7a5440a2d9d..4d9f947223fd7762fe35bcfcf50e74a1be860fde 100644 (file)
@@ -27,6 +27,9 @@ import textwrap
 import ipaddress
 import stat
 import shutil
+import shlex, subprocess
+
+from subprocess import Popen, TimeoutExpired, PIPE
 
 # Third party modules
 import six
@@ -36,13 +39,13 @@ import requests
 from six.moves.urllib.parse import urlunsplit
 
 # Own modules
-from .common import pp, to_bool, to_bytes
+from .common import pp, to_bool, to_bytes, to_str
 
 from .cfg_app import PpCfgAppError, PpConfigApplication
 
 from .pidfile import PidFileError, InvalidPidFileError, PidFileInUseError, PidFile
 
-__version__ = '0.7.1'
+__version__ = '0.7.2'
 LOG = logging.getLogger(__name__)
 
 
@@ -868,6 +871,9 @@ class PpConfigNamedApp(PpConfigApplication):
 
             try:
                 self.replace_configfiles()
+                if not self.check_namedconf():
+                    self.restore_configfiles()
+                    self.exit(99)
             except Exception:
                 self.restore_configfiles()
                 raise
@@ -1697,6 +1703,38 @@ class PpConfigNamedApp(PpConfigApplication):
                 shutil.rmtree(self.tempdir, False, emit_rm_err)
                 self.tempdir = None
 
+    # -------------------------------------------------------------------------
+    def check_namedconf(self):
+
+        LOG.info("Checking syntax correctness of named.conf ...")
+        cmd = shlex.split(self.cmd_checkconf)
+        LOG.debug("Executing: {}".format(' '.join(cmd)))
+
+        std_out = None
+        std_err = None
+        ret_val = None
+
+        with Popen(cmd, stdout=PIPE, stderr=PIPE) as proc:
+            try:
+                std_out, std_err = proc.communicate(timeout=10)
+            except TimeoutExpired:
+                proc.kill()
+                std_out, std_err = proc.communicate()
+            ret_val = proc.wait()
+
+        LOG.debug("Return value: {!r}".format(ret_val))
+        if std_out and std_out.strip():
+            s = to_str(std_out.strip())
+            LOG.debug("Output on STDOUT: {}".format(s))
+        if std_err and std_err.strip():
+            s = to_str(std_err.strip())
+            LOG.warn("Output on STDERR: {}".format(s))
+
+        if ret_val:
+            return False
+
+        return True
+
 
 # =============================================================================