From 27d6b5a3b3bf0c617115e6de11cff778286c781f Mon Sep 17 00:00:00 2001 From: John Estabrook Date: Tue, 16 Mar 2021 10:32:52 -0500 Subject: configd: T3411: revert to using explicit_print on ConfigError --- src/services/vyos-configd | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/services/vyos-configd b/src/services/vyos-configd index 1e60e53df..f06711831 100755 --- a/src/services/vyos-configd +++ b/src/services/vyos-configd @@ -108,6 +108,12 @@ conf_mode_scripts = dict(zip(imports, modules)) exclude_set = {key_name_from_file_name(f) for f in filenames if f not in include} include_set = {key_name_from_file_name(f) for f in filenames if f in include} +def explicit_print(path, mode, msg): + try: + with open(path, mode) as f: + f.write(f"\n{msg}\n\n") + except OSError: + logger.critical("error explicit_print") def run_script(script, config) -> int: config.set_level([]) @@ -120,8 +126,7 @@ def run_script(script, config) -> int: script.apply(c) except ConfigError as e: logger.critical(e) - with open(session_out, session_mode) as f, redirect_stdout(f): - print(f"{e}\n") + explicit_print(session_out, session_mode, str(e)) return R_ERROR_COMMIT except Exception as e: logger.critical(e) -- cgit v1.2.3 From fb2480e8a5c538c90e1b3d8c0bb23e23374dc13e Mon Sep 17 00:00:00 2001 From: John Estabrook Date: Tue, 16 Mar 2021 10:33:23 -0500 Subject: configd: T3411: revise redirect to capture stdout from subprocesses --- src/services/vyos-configd | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/src/services/vyos-configd b/src/services/vyos-configd index f06711831..8547b5f56 100755 --- a/src/services/vyos-configd +++ b/src/services/vyos-configd @@ -25,7 +25,7 @@ import logging import signal import importlib.util import zmq -from contextlib import redirect_stdout, redirect_stderr +from contextlib import contextmanager from vyos.defaults import directories from vyos.configsource import ConfigSourceString, ConfigSourceError @@ -108,6 +108,23 @@ conf_mode_scripts = dict(zip(imports, modules)) exclude_set = {key_name_from_file_name(f) for f in filenames if f not in include} include_set = {key_name_from_file_name(f) for f in filenames if f in include} +@contextmanager +def stdout_redirected(filename, mode): + saved_stdout_fd = None + destination_file = None + try: + sys.stdout.flush() + saved_stdout_fd = os.dup(sys.stdout.fileno()) + destination_file = open(filename, mode) + os.dup2(destination_file.fileno(), sys.stdout.fileno()) + yield + finally: + if saved_stdout_fd is not None: + os.dup2(saved_stdout_fd, sys.stdout.fileno()) + os.close(saved_stdout_fd) + if destination_file is not None: + destination_file.close() + def explicit_print(path, mode, msg): try: with open(path, mode) as f: @@ -118,12 +135,10 @@ def explicit_print(path, mode, msg): def run_script(script, config) -> int: config.set_level([]) try: - with open(session_out, session_mode) as f, redirect_stdout(f): - with redirect_stderr(f): - c = script.get_config(config) - script.verify(c) - script.generate(c) - script.apply(c) + c = script.get_config(config) + script.verify(c) + script.generate(c) + script.apply(c) except ConfigError as e: logger.critical(e) explicit_print(session_out, session_mode, str(e)) @@ -206,7 +221,8 @@ def process_node_data(config, data) -> int: if script_name in exclude_set: return R_PASS - result = run_script(conf_mode_scripts[script_name], config) + with stdout_redirected(session_out, session_mode): + result = run_script(conf_mode_scripts[script_name], config) return result -- cgit v1.2.3