From ac9f2a499dc05b6b535c89b5d164d4fa7ff8a47f Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Thu, 10 Aug 2017 14:10:36 +0200 Subject: [PATCH] Applying changed configuration to named --- pp_lib/config_named_app.py | 162 ++++++++++++++++++++++++++++++++++++- 1 file changed, 160 insertions(+), 2 deletions(-) diff --git a/pp_lib/config_named_app.py b/pp_lib/config_named_app.py index 4d9f947..eee79b7 100644 --- a/pp_lib/config_named_app.py +++ b/pp_lib/config_named_app.py @@ -45,7 +45,7 @@ from .cfg_app import PpCfgAppError, PpConfigApplication from .pidfile import PidFileError, InvalidPidFileError, PidFileInUseError, PidFile -__version__ = '0.7.2' +__version__ = '0.7.3' LOG = logging.getLogger(__name__) @@ -874,6 +874,7 @@ class PpConfigNamedApp(PpConfigApplication): if not self.check_namedconf(): self.restore_configfiles() self.exit(99) + self.apply_config() except Exception: self.restore_configfiles() raise @@ -1725,7 +1726,7 @@ class PpConfigNamedApp(PpConfigApplication): 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)) + LOG.warn("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)) @@ -1735,6 +1736,163 @@ class PpConfigNamedApp(PpConfigApplication): return True + # ------------------------------------------------------------------------- + def apply_config(self): + + if not self.reload_necessary and not self.restart_necessary: + LOG.info("Reload or restart of named is not necessary.") + return + + running = self.named_running() + if not running: + LOG.warn("Named is not running, please start it manually.") + return + + if self.restart_necessary: + self.restart_named() + else: + self.reload_named() + + # ------------------------------------------------------------------------- + def named_running(self): + + LOG.debug("Checking, whether named is running ...") + + cmd = shlex.split(self.cmd_status) + 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:\n{}".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 + + # ------------------------------------------------------------------------- + def start_named(self): + + LOG.info("Starting named ...") + + cmd = shlex.split(self.cmd_start) + LOG.debug("Executing: {}".format(' '.join(cmd))) + + if self.simulate: + return + + 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=30) + 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:\n{}".format(s)) + if std_err and std_err.strip(): + s = to_str(std_err.strip()) + LOG.error("Output on STDERR: {}".format(s)) + + if ret_val: + return False + + return True + + # ------------------------------------------------------------------------- + def restart_named(self): + + LOG.info("Restarting named ...") + + cmd = shlex.split(self.cmd_restart) + LOG.debug("Executing: {}".format(' '.join(cmd))) + + if self.simulate: + return + + 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=30) + 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:\n{}".format(s)) + if std_err and std_err.strip(): + s = to_str(std_err.strip()) + LOG.error("Output on STDERR: {}".format(s)) + + if ret_val: + return False + + return True + + # ------------------------------------------------------------------------- + def reload_named(self): + + LOG.info("Reloading named ...") + + cmd = shlex.split(self.cmd_reload) + LOG.debug("Executing: {}".format(' '.join(cmd))) + + if self.simulate: + return + + 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=30) + 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:\n{}".format(s)) + if std_err and std_err.strip(): + s = to_str(std_err.strip()) + LOG.error("Output on STDERR: {}".format(s)) + + if ret_val: + return False + + return True # ============================================================================= -- 2.39.5