Skip to content

Commit 6a2bd05

Browse files
author
Dan
committed
Added backwards compatible host output object for storing host output. Updated get_output to use host output. Added embedded server race condition waiting time
1 parent 6425de3 commit 6a2bd05

File tree

3 files changed

+40
-14
lines changed

3 files changed

+40
-14
lines changed

embedded_server/embedded_server.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,9 @@ def _handle_ssh_connection(transport, fail_auth=False,
234234
return
235235
# *Important* Allow other greenlets to execute before establishing connection
236236
# which may be handled by said other greenlets
237-
gevent.sleep(1)
237+
gevent.sleep(2)
238238
channel = transport.accept(20)
239+
gevent.sleep(0)
239240
if not channel:
240241
logger.error("Could not establish channel")
241242
return
@@ -245,6 +246,7 @@ def _handle_ssh_connection(transport, fail_auth=False,
245246
while not channel.send_ready():
246247
gevent.sleep(.2)
247248
channel.close()
249+
gevent.sleep(0)
248250

249251
def handle_ssh_connection(sock,
250252
fail_auth=False, ssh_exception=False,

pssh/output.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class HostOutput(dict):
2+
__slots__ = ('host', 'cmd', 'channel', 'stdout', 'stderr', 'stdin', 'exit_code', 'exception')
3+
4+
def __init__(self, host, cmd, channel, stdout, stderr, stdin, exit_code=None, exception=None):
5+
dict.__init__(self, (('host', host), ('cmd', cmd), ('channel', channel),
6+
('stdout', stdout), ('stderr', stderr),
7+
('stdin', stdin), ('exit_code', exit_code),
8+
('exception', exception)))
9+
self.host = host
10+
self.cmd = cmd
11+
self.channel = channel
12+
self.stdout = stdout
13+
self.stderr = stderr
14+
self.stdin = stdin
15+
self.exception = exception
16+
self.exit_code = exit_code
17+
18+
def __setattr__(self, name, value):
19+
object.__setattr__(self, name, value)
20+
dict.__setitem__(self, name, value)
21+
22+
def update(self, update_dict):
23+
dict.update(self, update_dict)
24+
for key in update_dict:
25+
object.__setattr__(self, key, update_dict[key])
26+
27+
def __repr__(self):
28+
return "host=%s, cmd=%s, channel=%s, stdout=%s, stderr=%s, stdin=%s, \
29+
exception=%s" % (self.host, self.cmd, self.channel, self.stdout, self.stdin,
30+
self.stderr, self.exception,)

pssh/pssh_client.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from .exceptions import HostArgumentException
3535
from .constants import DEFAULT_RETRIES
3636
from .ssh_client import SSHClient
37+
from .output import HostOutput
3738

3839

3940
logger = logging.getLogger('pssh')
@@ -722,23 +723,16 @@ def _update_host_output(self, output, host, exit_code, channel, stdout,
722723
logger.warning("Already have output for host %s - changing host "
723724
"key for %s to %s", host, host, new_host)
724725
host = new_host
725-
output.setdefault(host, {})
726-
output[host].update({
727-
'exit_code': exit_code,
728-
'channel': channel,
729-
'stdout': stdout,
730-
'stderr': stderr,
731-
'stdin': stdin,
732-
'cmd': cmd,
733-
'exception': exception,})
726+
output[host] = HostOutput(host, cmd, channel, stdout, stderr, stdin,
727+
exception=exception)
734728

735729
def join(self, output):
736730
"""Block until all remote commands in output have finished
737731
and retrieve exit codes"""
738732
for host in output:
739-
output[host]['cmd'].join()
740-
if output[host]['channel']:
741-
output[host]['channel'].recv_exit_status()
733+
output[host].cmd.join()
734+
if output[host].channel:
735+
output[host].channel.recv_exit_status()
742736
self.get_exit_codes(output)
743737

744738
def finished(self, output):
@@ -761,7 +755,7 @@ def get_exit_codes(self, output):
761755
:rtype: None
762756
"""
763757
for host in output:
764-
output[host].update({'exit_code': self.get_exit_code(output[host])})
758+
output[host].exit_code = self.get_exit_code(output[host])
765759

766760
def get_exit_code(self, host_output):
767761
"""Get exit code from host output *if available*.

0 commit comments

Comments
 (0)