Skip to content

Commit 2034873

Browse files
committed
print minion output, mirror salt-ssh log level
1 parent 6463994 commit 2034873

File tree

5 files changed

+29
-21
lines changed

5 files changed

+29
-21
lines changed

changelog/66951.fixed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix salt-ssh minion logs to stdout

salt/client/ssh/__init__.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,6 +1446,7 @@ def _cmd_str(self):
14461446
OPTIONS.tty = {tty}
14471447
OPTIONS.cmd_umask = {cmd_umask}
14481448
OPTIONS.code_checksum = {code_checksum}
1449+
OPTIONS.log_level = '{log_level}'
14491450
ARGS = {arguments}\n'''.format(
14501451
config=self.minion_config,
14511452
delimeter=RSTR,
@@ -1459,6 +1460,7 @@ def _cmd_str(self):
14591460
cmd_umask=self.cmd_umask,
14601461
code_checksum=thin_code_digest,
14611462
arguments=self.argv,
1463+
log_level=self.opts["log_level"],
14621464
)
14631465
py_code = SSH_PY_SHIM.replace("#%%OPTS", arg_str)
14641466
py_code_enc = base64.encodebytes(py_code.encode("utf-8")).decode("utf-8")
@@ -1501,15 +1503,15 @@ def execute_script(self, script, extension="py", pre_dir="", script_args=None):
15011503

15021504
return ret
15031505

1504-
def shim_cmd(self, cmd_str, extension="py"):
1506+
def shim_cmd(self, cmd_str, extension="py", print_output=False):
15051507
"""
15061508
Run a shim command.
15071509
15081510
If tty is enabled, we must scp the shim to the target system and
15091511
execute it there
15101512
"""
15111513
if not self.tty and not self.winrm:
1512-
return self.shell.exec_cmd(cmd_str)
1514+
return self.shell.exec_cmd(cmd_str, print_output=print_output)
15131515

15141516
# Write the shim to a temporary file in the default temp directory
15151517
with tempfile.NamedTemporaryFile(mode="w+b", delete=False) as shim_tmp_file:
@@ -1537,7 +1539,7 @@ def shim_cmd(self, cmd_str, extension="py"):
15371539

15381540
return ret
15391541

1540-
def cmd_block(self, is_retry=False):
1542+
def cmd_block(self, is_retry=False, print_output=False):
15411543
"""
15421544
Prepare the pre-check command to send to the subsystem
15431545
@@ -1554,7 +1556,7 @@ def cmd_block(self, is_retry=False):
15541556
" ".join([str(arg) for arg in self.argv]),
15551557
)
15561558
cmd_str = self._cmd_str()
1557-
stdout, stderr, retcode = self.shim_cmd(cmd_str)
1559+
stdout, stderr, retcode = self.shim_cmd(cmd_str, print_output=print_output)
15581560

15591561
log.trace("STDOUT %s\n%s", self.target["host"], stdout)
15601562
log.trace("STDERR %s\n%s", self.target["host"], stderr)
@@ -1564,14 +1566,14 @@ def cmd_block(self, is_retry=False):
15641566
if error:
15651567
if error == "Python environment not found on Windows system":
15661568
saltwinshell.deploy_python(self)
1567-
stdout, stderr, retcode = self.shim_cmd(cmd_str)
1569+
stdout, stderr, retcode = self.shim_cmd(cmd_str, print_output=print_output)
15681570
while re.search(RSTR_RE, stdout):
15691571
stdout = re.split(RSTR_RE, stdout, 1)[1].strip()
15701572
while re.search(RSTR_RE, stderr):
15711573
stderr = re.split(RSTR_RE, stderr, 1)[1].strip()
15721574
elif error == "Undefined SHIM state":
15731575
self.deploy()
1574-
stdout, stderr, retcode = self.shim_cmd(cmd_str)
1576+
stdout, stderr, retcode = self.shim_cmd(cmd_str, print_output=print_output)
15751577
if not re.search(RSTR_RE, stdout) or not re.search(RSTR_RE, stderr):
15761578
# If RSTR is not seen in both stdout and stderr then there
15771579
# was a thin deployment problem.
@@ -1610,7 +1612,7 @@ def cmd_block(self, is_retry=False):
16101612
and retcode == salt.defaults.exitcodes.EX_THIN_DEPLOY
16111613
):
16121614
self.deploy()
1613-
stdout, stderr, retcode = self.shim_cmd(cmd_str)
1615+
stdout, stderr, retcode = self.shim_cmd(cmd_str, print_output=print_output)
16141616
if not re.search(RSTR_RE, stdout) or not re.search(RSTR_RE, stderr):
16151617
if not self.tty:
16161618
# If RSTR is not seen in both stdout and stderr then there
@@ -1622,7 +1624,7 @@ def cmd_block(self, is_retry=False):
16221624
stderr,
16231625
retcode,
16241626
)
1625-
return self.cmd_block()
1627+
return self.cmd_block(print_output=print_output)
16261628
elif not re.search(RSTR_RE, stdout):
16271629
# If RSTR is not seen in stdout with tty, then there
16281630
# was a thin deployment problem.
@@ -1642,7 +1644,7 @@ def cmd_block(self, is_retry=False):
16421644
stderr = re.split(RSTR_RE, stderr, 1)[1].strip()
16431645
elif "ext_mods" == shim_command:
16441646
self.deploy_ext()
1645-
stdout, stderr, retcode = self.shim_cmd(cmd_str)
1647+
stdout, stderr, retcode = self.shim_cmd(cmd_str, print_output=print_output)
16461648
if not re.search(RSTR_RE, stdout) or not re.search(RSTR_RE, stderr):
16471649
# If RSTR is not seen in both stdout and stderr then there
16481650
# was a thin deployment problem.

salt/client/ssh/shell.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ def exec_nb_cmd(self, cmd):
321321
yield None, None, None
322322
yield "".join(r_out), "".join(r_err), rcode
323323

324-
def exec_cmd(self, cmd):
324+
def exec_cmd(self, cmd, print_output=False):
325325
"""
326326
Execute a remote command
327327
"""
@@ -336,7 +336,7 @@ def exec_cmd(self, cmd):
336336
else:
337337
log.debug(logmsg)
338338

339-
ret = self._run_cmd(cmd)
339+
ret = self._run_cmd(cmd, print_output=print_output)
340340
return ret
341341

342342
def send(self, local, remote, makedirs=False):
@@ -384,7 +384,7 @@ def _split_cmd(self, cmd):
384384
cmd_lst.append(f"/bin/sh {cmd_part}")
385385
return cmd_lst
386386

387-
def _run_cmd(self, cmd, key_accept=False, passwd_retries=3):
387+
def _run_cmd(self, cmd, key_accept=False, passwd_retries=3, print_output=False):
388388
"""
389389
Execute a shell command via VT. This is blocking and assumes that ssh
390390
is being run
@@ -419,12 +419,16 @@ def _run_cmd(self, cmd, key_accept=False, passwd_retries=3):
419419
stdout = stdout.replace(self.passwd, ("*" * 6))
420420
ret_stdout += stdout
421421
buff = old_stdout + stdout
422+
if print_output:
423+
print(stdout, file=sys.stdout, end="")
422424
else:
423425
buff = stdout
424426
if stderr:
425427
if self.passwd:
426428
stderr = stderr.replace(self.passwd, ("*" * 6))
427429
ret_stderr += stderr
430+
if print_output:
431+
print(stderr, file=sys.stderr, end="")
428432
if buff and RSTR_RE.search(buff):
429433
# We're getting results back, don't try to send passwords
430434
send_password = False

salt/client/ssh/ssh_py_shim.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
helper script used by salt.client.ssh.Single. It is here, in a
88
separate file, for convenience of development.
99
"""
10+
1011
from __future__ import absolute_import, print_function
1112

1213
import hashlib
@@ -359,8 +360,8 @@ def main(argv): # pylint: disable=W0613
359360
"--metadata",
360361
"--out",
361362
"json",
362-
"-l",
363-
"quiet",
363+
"--log-level",
364+
OPTIONS.log_level,
364365
"-c",
365366
OPTIONS.saltdir,
366367
]

salt/client/ssh/wrapper/state.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def _ssh_state(chunks, st_kwargs, kwargs, pillar, test=False):
6060
**st_kwargs,
6161
)
6262
single.shell.send(trans_tar, "{}/salt_state.tgz".format(__opts__["thin_dir"]))
63-
stdout, stderr, retcode = single.cmd_block()
63+
stdout, stderr, retcode = single.cmd_block(print_output=True)
6464

6565
# Clean up our tar
6666
try:
@@ -234,7 +234,7 @@ def sls(mods, saltenv="base", test=None, exclude=None, **kwargs):
234234
**st_kwargs,
235235
)
236236
single.shell.send(trans_tar, "{}/salt_state.tgz".format(opts["thin_dir"]))
237-
stdout, stderr, retcode = single.cmd_block()
237+
stdout, stderr, retcode = single.cmd_block(print_output=True)
238238

239239
# Clean up our tar
240240
try:
@@ -372,7 +372,7 @@ def low(data, **kwargs):
372372
**st_kwargs,
373373
)
374374
single.shell.send(trans_tar, "{}/salt_state.tgz".format(__opts__["thin_dir"]))
375-
stdout, stderr, retcode = single.cmd_block()
375+
stdout, stderr, retcode = single.cmd_block(print_output=True)
376376

377377
# Clean up our tar
378378
try:
@@ -465,7 +465,7 @@ def high(data, **kwargs):
465465
**st_kwargs,
466466
)
467467
single.shell.send(trans_tar, "{}/salt_state.tgz".format(opts["thin_dir"]))
468-
stdout, stderr, retcode = single.cmd_block()
468+
stdout, stderr, retcode = single.cmd_block(print_output=True)
469469

470470
# Clean up our tar
471471
try:
@@ -716,7 +716,7 @@ def highstate(test=None, **kwargs):
716716
**st_kwargs,
717717
)
718718
single.shell.send(trans_tar, "{}/salt_state.tgz".format(opts["thin_dir"]))
719-
stdout, stderr, retcode = single.cmd_block()
719+
stdout, stderr, retcode = single.cmd_block(print_output=True)
720720

721721
# Clean up our tar
722722
try:
@@ -807,7 +807,7 @@ def top(topfn, test=None, **kwargs):
807807
**st_kwargs,
808808
)
809809
single.shell.send(trans_tar, "{}/salt_state.tgz".format(opts["thin_dir"]))
810-
stdout, stderr, retcode = single.cmd_block()
810+
stdout, stderr, retcode = single.cmd_block(print_output=True)
811811

812812
# Clean up our tar
813813
try:
@@ -1247,7 +1247,7 @@ def single(fun, name, test=None, **kwargs):
12471247
single.shell.send(trans_tar, "{}/salt_state.tgz".format(opts["thin_dir"]))
12481248

12491249
# Run the state.pkg command on the target
1250-
stdout, stderr, retcode = single.cmd_block()
1250+
stdout, stderr, retcode = single.cmd_block(print_output=True)
12511251

12521252
# Clean up our tar
12531253
try:

0 commit comments

Comments
 (0)