Skip to content

Commit 00af978

Browse files
author
Dan
committed
Updated get_output and exec_command for changed behaviour. Added host as argument to exceptions so that output can be updated correctly for the host the exception occured for. Updated test_pssh_client_run_command_get_output_explicit for changed get_output interface - Resolves #32
1 parent 30631c0 commit 00af978

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

pssh.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def _connect(self, client, host, port, sock=None, retries=1):
205205
# of SSH failure
206206
except paramiko.SSHException, ex:
207207
logger.error("General SSH error - %s", ex)
208-
raise SSHException(ex.message)
208+
raise SSHException(ex.message, host)
209209

210210
def exec_command(self, command, sudo=False, user=None, **kwargs):
211211
"""Wrapper to :mod:`paramiko.SSHClient.exec_command`
@@ -493,15 +493,13 @@ def run_command(self, *args, **kwargs):
493493
'cmd' : <greenlet>}}
494494
495495
"""
496-
stop_on_errors = kwargs['stop_on_errors'] \
497-
if 'stop_on_errors' in kwargs else True
498-
del kwargs['stop_on_errors']
496+
stop_on_errors = kwargs.pop('stop_on_errors', True)
499497
cmds = [self.pool.spawn(self._exec_command, host, *args, **kwargs)
500498
for host in self.hosts]
501499
output = {}
502500
for cmd in cmds:
503501
try:
504-
output = self.get_output(cmd)
502+
self.get_output(cmd, output)
505503
except Exception, ex:
506504
if stop_on_errors:
507505
raise ex
@@ -536,12 +534,11 @@ def _exec_command(self, host, *args, **kwargs):
536534
proxy_port=self.proxy_port)
537535
return self.host_clients[host].exec_command(*args, **kwargs)
538536

539-
def get_output(self, cmd):
537+
def get_output(self, cmd, output):
540538
"""Get output from command.
541539
542-
:param commands: (Optional) Override commands to get output from.\
543-
Uses running commands in pool if not given.
544-
:type commands: :mod:`gevent.Greenlet`
540+
:param cmd: Command to get output from
541+
:type cmd: :mod:`gevent.Greenlet`
545542
:rtype: Dictionary with host as key as in:
546543
547544
::
@@ -568,10 +565,15 @@ def get_output(self, cmd):
568565
>>> self.get_exit_code(output[host])
569566
0
570567
"""
571-
output = {}
572568
try:
573569
(channel, host, stdout, stderr) = cmd.get()
574570
except Exception, ex:
571+
try:
572+
host = ex.args[1]
573+
except IndexError:
574+
logger.error("Got exception with no host argument - cannot update output data with %s", ex)
575+
raise ex
576+
output.setdefault(host, {})
575577
output[host].update({'exit_code' : None,
576578
'channel' : None,
577579
'stdout' : None,
@@ -585,7 +587,6 @@ def get_output(self, cmd):
585587
'stdout' : stdout,
586588
'stderr' : stderr,
587589
'cmd' : cmd, })
588-
return output
589590

590591
def get_exit_code(self, host_output):
591592
"""Get exit code from host output if available

tests/test_pssh_client.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,9 @@ def test_pssh_client_run_command_get_output_explicit(self):
137137
pkey=self.user_key)
138138
out = client.run_command(self.fake_cmd)
139139
cmds = [cmd for host in out for cmd in [out[host]['cmd']]]
140-
output = client.get_output(commands=cmds)
140+
output = {}
141+
for cmd in cmds:
142+
client.get_output(cmd, output)
141143
expected_exit_code = 0
142144
expected_stdout = [self.fake_resp]
143145
expected_stderr = []
@@ -204,13 +206,8 @@ def test_pssh_client_hosts_list_part_failure(self):
204206
port=self.listen_port,
205207
pkey=self.user_key,
206208
)
207-
output = {}
208-
try:
209-
client.run_command(self.fake_cmd,
210-
output=output,
211-
stop_on_errors=False)
212-
except AuthenticationException:
213-
pass
209+
output = client.run_command(self.fake_cmd,
210+
stop_on_errors=False)
214211
self.assertTrue(hosts[0] in output,
215212
msg="Failed host does not exist in output - output is %s" % (output,))
216213
self.assertTrue(hosts[1] in output,

0 commit comments

Comments
 (0)