cmdmod: reimplementation of #45932 for Oxygen

This commit is contained in:
Ronald van Zantvoort 2018-02-23 22:19:15 +01:00
parent 9b2bc1982c
commit beabf4f06b

View file

@ -285,6 +285,7 @@ def _run(cmd,
shell
)
output_loglevel = _check_loglevel(output_loglevel)
log_callback = _check_cb(log_callback)
if runas is None and '__context__' in globals():
@ -312,6 +313,10 @@ def _run(cmd,
# yaml-ified into non-string types
cwd = six.text_type(cwd)
if bg:
ignore_retcode = True
use_vt = False
if not salt.utils.platform.is_windows():
if not os.path.isfile(shell) or not os.access(shell, os.X_OK):
msg = 'The shell {0} is not available'.format(shell)
@ -368,7 +373,7 @@ def _run(cmd,
else:
return cmd
if _check_loglevel(output_loglevel) is not None:
if output_loglevel is not None:
# Always log the shell commands at INFO unless quiet logging is
# requested. The command output is what will be controlled by the
# 'loglevel' parameter.
@ -542,7 +547,7 @@ def _run(cmd,
msg = (
'Unable to run command \'{0}\' with the context \'{1}\', '
'reason: '.format(
cmd if _check_loglevel(output_loglevel) is not None
cmd if output_loglevel is not None
else 'REDACTED',
kwargs
)
@ -621,7 +626,7 @@ def _run(cmd,
to = ''
if timeout:
to = ' (timeout: {0}s)'.format(timeout)
if _check_loglevel(output_loglevel) is not None:
if output_loglevel is not None:
msg = 'Running {0} in VT{1}'.format(cmd, to)
log.debug(log_callback(msg))
stdout, stderr = '', ''
@ -694,6 +699,26 @@ def _run(cmd,
except NameError:
# Ignore the context error during grain generation
pass
# Log the output
if output_loglevel is not None:
if not ignore_retcode and ret['retcode'] != 0:
if output_loglevel < LOG_LEVELS['error']:
output_loglevel = LOG_LEVELS['error']
msg = (
'Command \'{0}\' failed with return code: {1}'.format(
cmd,
ret['retcode']
)
)
log.error(log_callback(msg))
if ret['stdout']:
log.log(output_loglevel, 'stdout: {0}'.format(log_callback(ret['stdout'])))
if ret['stderr']:
log.log(output_loglevel, 'stderr: {0}'.format(log_callback(ret['stderr'])))
if ret['retcode']:
log.log(output_loglevel, 'retcode: {0}'.format(ret['retcode']))
return ret
@ -1390,26 +1415,6 @@ def run_stdout(cmd,
password=password,
**kwargs)
log_callback = _check_cb(log_callback)
lvl = _check_loglevel(output_loglevel)
if lvl is not None:
if not ignore_retcode and ret['retcode'] != 0:
if lvl < LOG_LEVELS['error']:
lvl = LOG_LEVELS['error']
msg = (
'Command \'{0}\' failed with return code: {1}'.format(
cmd,
ret['retcode']
)
)
log.error(log_callback(msg))
if ret['stdout']:
log.log(lvl, 'stdout: %s', log_callback(ret['stdout']))
if ret['stderr']:
log.log(lvl, 'stderr: %s', log_callback(ret['stderr']))
if ret['retcode']:
log.log(lvl, 'retcode: %s', ret['retcode'])
return ret['stdout'] if not hide_output else ''
@ -1586,26 +1591,6 @@ def run_stderr(cmd,
password=password,
**kwargs)
log_callback = _check_cb(log_callback)
lvl = _check_loglevel(output_loglevel)
if lvl is not None:
if not ignore_retcode and ret['retcode'] != 0:
if lvl < LOG_LEVELS['error']:
lvl = LOG_LEVELS['error']
msg = (
'Command \'{0}\' failed with return code: {1}'.format(
cmd,
ret['retcode']
)
)
log.error(log_callback(msg))
if ret['stdout']:
log.log(lvl, 'stdout: %s', log_callback(ret['stdout']))
if ret['stderr']:
log.log(lvl, 'stderr: %s', log_callback(ret['stderr']))
if ret['retcode']:
log.log(lvl, 'retcode: %s', ret['retcode'])
return ret['stderr'] if not hide_output else ''
@ -1809,27 +1794,6 @@ def run_all(cmd,
encoded_cmd=encoded_cmd,
**kwargs)
log_callback = _check_cb(log_callback)
lvl = _check_loglevel(output_loglevel)
if lvl is not None:
if not ignore_retcode and ret['retcode'] != 0:
if lvl < LOG_LEVELS['error']:
lvl = LOG_LEVELS['error']
msg = (
'Command \'{0}\' failed with return code: {1}'.format(
cmd,
ret['retcode']
)
)
log.error(log_callback(msg))
if ret['stdout']:
log.log(lvl, 'stdout: %s', log_callback(ret['stdout']))
if ret['stderr']:
log.log(lvl, 'stderr: %s', log_callback(ret['stderr']))
if ret['retcode']:
log.log(lvl, 'retcode: %s', ret['retcode'])
if hide_output:
ret['stdout'] = ret['stderr'] = ''
return ret
@ -1990,22 +1954,6 @@ def retcode(cmd,
use_vt=use_vt,
password=password,
**kwargs)
log_callback = _check_cb(log_callback)
lvl = _check_loglevel(output_loglevel)
if lvl is not None:
if not ignore_retcode and ret['retcode'] != 0:
if lvl < LOG_LEVELS['error']:
lvl = LOG_LEVELS['error']
msg = (
'Command \'{0}\' failed with return code: {1}'.format(
cmd,
ret['retcode']
)
)
log.error(log_callback(msg))
log.log(lvl, 'output: %s', log_callback(ret['stdout']))
return ret['retcode']
@ -3578,7 +3526,6 @@ def run_bg(cmd,
output_loglevel='debug',
log_callback=None,
reset_system_locale=True,
ignore_retcode=False,
saltenv='base',
password=None,
prepend_path=None,
@ -3743,7 +3690,6 @@ def run_bg(cmd,
log_callback=log_callback,
timeout=timeout,
reset_system_locale=reset_system_locale,
ignore_retcode=ignore_retcode,
saltenv=saltenv,
password=password,
**kwargs