Skip to content

Commit b8b423a

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 d6fb4a2 commit b8b423a

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
@@ -210,7 +210,7 @@ def _connect(self, client, host, port, sock=None, retries=1):
210210
# of SSH failure
211211
except paramiko.SSHException, ex:
212212
logger.error("General SSH error - %s", ex)
213-
raise SSHException(ex.message)
213+
raise SSHException(ex.message, host)
214214

215215
def exec_command(self, command, sudo=False, user=None, **kwargs):
216216
"""Wrapper to :mod:`paramiko.SSHClient.exec_command`
@@ -498,15 +498,13 @@ def run_command(self, *args, **kwargs):
498498
'cmd' : <greenlet>}}
499499
500500
"""
501-
stop_on_errors = kwargs['stop_on_errors'] \
502-
if 'stop_on_errors' in kwargs else True
503-
del kwargs['stop_on_errors']
501+
stop_on_errors = kwargs.pop('stop_on_errors', True)
504502
cmds = [self.pool.spawn(self._exec_command, host, *args, **kwargs)
505503
for host in self.hosts]
506504
output = {}
507505
for cmd in cmds:
508506
try:
509-
output = self.get_output(cmd)
507+
self.get_output(cmd, output)
510508
except Exception, ex:
511509
if stop_on_errors:
512510
raise ex
@@ -541,12 +539,11 @@ def _exec_command(self, host, *args, **kwargs):
541539
proxy_port=self.proxy_port)
542540
return self.host_clients[host].exec_command(*args, **kwargs)
543541

544-
def get_output(self, cmd):
542+
def get_output(self, cmd, output):
545543
"""Get output from command.
546544
547-
:param commands: (Optional) Override commands to get output from.\
548-
Uses running commands in pool if not given.
549-
:type commands: :mod:`gevent.Greenlet`
545+
:param cmd: Command to get output from
546+
:type cmd: :mod:`gevent.Greenlet`
550547
:rtype: Dictionary with host as key as in:
551548
552549
::
@@ -573,10 +570,15 @@ def get_output(self, cmd):
573570
>>> self.get_exit_code(output[host])
574571
0
575572
"""
576-
output = {}
577573
try:
578574
(channel, host, stdout, stderr) = cmd.get()
579575
except Exception, ex:
576+
try:
577+
host = ex.args[1]
578+
except IndexError:
579+
logger.error("Got exception with no host argument - cannot update output data with %s", ex)
580+
raise ex
581+
output.setdefault(host, {})
580582
output[host].update({'exit_code' : None,
581583
'channel' : None,
582584
'stdout' : None,
@@ -590,7 +592,6 @@ def get_output(self, cmd):
590592
'stdout' : stdout,
591593
'stderr' : stderr,
592594
'cmd' : cmd, })
593-
return output
594595

595596
def get_exit_code(self, host_output):
596597
"""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)