From c095d1fb3d32f8b1197f75382db47ef97d86ed55 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Thu, 10 Aug 2017 13:52:54 +0200 Subject: [PATCH] Adding checking syntax of named.conf --- pp_lib/config_named_app.py | 42 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/pp_lib/config_named_app.py b/pp_lib/config_named_app.py index 120d2cf..4d9f947 100644 --- a/pp_lib/config_named_app.py +++ b/pp_lib/config_named_app.py @@ -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 + # ============================================================================= -- 2.39.5