-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathoutput.py
135 lines (116 loc) · 4.56 KB
/
output.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# This file is part of parallel-ssh.
#
# Copyright (C) 2014-2022 Panos Kittenis and contributors.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation, version 2.1.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
"""Output module of ParallelSSH"""
from os import linesep
from . import logger
class HostOutputBuffers(object):
__slots__ = ('stdout', 'stderr')
def __init__(self, stdout, stderr):
"""
:param stdout: Stdout data
:type stdout: :py:class:`BufferData`
:param stderr: Stderr data
:type stderr: :py:class:`BufferData`
"""
self.stdout = stdout
self.stderr = stderr
class BufferData(object):
__slots__ = ('reader', 'rw_buffer')
def __init__(self, reader, rw_buffer):
"""
:param reader: Greenlet reading data from channel and writing to rw_buffer
:type reader: :py:class:`gevent.Greenlet`
:param rw_buffer: Read/write buffer
:type rw_buffer: :py:class:`pssh.clients.reader.ConcurrentRWBuffer`
"""
self.reader = reader
self.rw_buffer = rw_buffer
class HostOutput(object):
"""Host output"""
__slots__ = ('host', 'channel', 'stdin',
'client', 'alias', 'exception',
'encoding', 'read_timeout', 'buffers',
)
def __init__(self, host, channel, stdin,
client, alias=None, exception=None, encoding='utf-8', read_timeout=None,
buffers=None):
"""
:param host: Host name output is for
:type host: str
:param channel: SSH channel used for command execution
:type channel: :py:class:`socket.socket` compatible object
:param stdin: Standard input buffer
:type stdin: :py:func:`file`-like object
:param client: `SSHClient` output is coming from.
:type client: :py:class:`pssh.clients.base.single.BaseSSHClient` or `None`.
:param alias: Host alias.
:type alias: str
:param exception: Exception from host if any
:type exception: :py:class:`Exception` or ``None``
:param read_timeout: Timeout in seconds for reading from buffers.
:type read_timeout: float
:param buffers: Host buffer data.
:type buffers: :py:class:`HostOutputBuffers`
"""
self.host = host
self.channel = channel
self.stdin = stdin
self.client = client
self.alias = alias
self.exception = exception
self.encoding = encoding
self.read_timeout = read_timeout
self.buffers = buffers
@property
def stdout(self):
if not self.client:
return
_stdout = self.client.read_output_buffer(
self.client.read_output(self.buffers.stdout.rw_buffer, timeout=self.read_timeout),
encoding=self.encoding)
return _stdout
@property
def stderr(self):
if not self.client:
return
_stderr = self.client.read_output_buffer(
self.client.read_stderr(self.buffers.stderr.rw_buffer, timeout=self.read_timeout),
encoding=self.encoding,
prefix='\t[err]')
return _stderr
@property
def exit_code(self):
if not self.client:
return
try:
return self.client.get_exit_status(self.channel)
except Exception as ex:
logger.error("Error getting exit status - %s", ex)
def __repr__(self):
return "\thost={host}{linesep}" \
"\talias={alias}{linesep}" \
"\texit_code={exit_code}{linesep}" \
"\tchannel={channel}{linesep}" \
"\texception={exception}{linesep}" \
"\tencoding={encoding}{linesep}" \
"\tread_timeout={read_timeout}".format(
host=self.host, alias=self.alias, channel=self.channel,
exception=self.exception, linesep=linesep,
exit_code=self.exit_code, encoding=self.encoding, read_timeout=self.read_timeout,
)
def __str__(self):
return self.__repr__()