summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Estabrook <jestabro@vyos.io>2021-02-18 14:07:46 -0600
committerGitHub <noreply@github.com>2021-02-18 14:07:46 -0600
commitd3c9a52e8788513c593fd872ecea18bd3dea2ffa (patch)
tree99435fecf1adf8b33baa47984766e1f0da2738f3
parentf67568bc2307706116f5509fca3a188dc4ab5d48 (diff)
parent8f50b3dd94f41a11ce4d0bb06d3506caaf2864b8 (diff)
downloadvyos-1x-d3c9a52e8788513c593fd872ecea18bd3dea2ffa.tar.gz
vyos-1x-d3c9a52e8788513c593fd872ecea18bd3dea2ffa.zip
Merge pull request #734 from jestabro/T3302
configd: T3302: redirect stdout/stderr from scripts to console
-rwxr-xr-xsrc/services/vyos-configd25
1 files changed, 11 insertions, 14 deletions
diff --git a/src/services/vyos-configd b/src/services/vyos-configd
index 5b1ab1f1f..3bd516463 100755
--- a/src/services/vyos-configd
+++ b/src/services/vyos-configd
@@ -25,6 +25,7 @@ import logging
import signal
import importlib.util
import zmq
+from contextlib import redirect_stdout, redirect_stderr
from vyos.defaults import directories
from vyos.configsource import ConfigSourceString, ConfigSourceError
@@ -104,27 +105,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}
-def explicit_print(t, m):
- try:
- with open(t, 'w') as f:
- f.write(m)
- f.write("\n")
- f.flush()
- except Exception:
- pass
def run_script(script, config) -> int:
config.set_level([])
try:
- c = script.get_config(config)
- script.verify(c)
- script.generate(c)
- script.apply(c)
+ with open(session_tty, 'w') as f, redirect_stdout(f):
+ with redirect_stderr(f):
+ c = script.get_config(config)
+ script.verify(c)
+ script.generate(c)
+ script.apply(c)
except ConfigError as e:
logger.critical(e)
- explicit_print(session_tty, str(e))
+ with open(session_tty, 'w') as f, redirect_stdout(f):
+ print(f"{e}\n")
return R_ERROR_COMMIT
- except Exception:
+ except Exception as e:
+ logger.critical(e)
return R_ERROR_DAEMON
return R_SUCCESS