ssh: tidy up logs and stream names.

issue510
David Wilson 6 years ago
parent eb93f82d05
commit ec789513dc

@ -1703,7 +1703,7 @@ class Stream(BasicStream):
def __repr__(self): def __repr__(self):
cls = type(self) cls = type(self)
return '%s.%s(%r)' % (cls.__module__, cls.__name__, self.name) return "%s.%s('%s')" % (cls.__module__, cls.__name__, self.name)
class Context(object): class Context(object):

@ -66,9 +66,8 @@ class Stream(mitogen.parent.Stream):
if incorrect_prompts is not None: if incorrect_prompts is not None:
self.incorrect_prompts = map(str.lower, incorrect_prompts) self.incorrect_prompts = map(str.lower, incorrect_prompts)
def connect(self): def _get_name(self):
super(Stream, self).connect() return u'doas.' + mitogen.core.to_text(self.username)
self.name = u'doas.' + mitogen.core.to_text(self.username)
def get_boot_command(self): def get_boot_command(self):
bits = [self.doas_path, '-u', self.username, '--'] bits = [self.doas_path, '-u', self.username, '--']

@ -62,9 +62,8 @@ class Stream(mitogen.parent.Stream):
if username: if username:
self.username = username self.username = username
def connect(self): def _get_name(self):
super(Stream, self).connect() return u'docker.' + (self.container or self.image)
self.name = u'docker.' + (self.container or self.image)
def get_boot_command(self): def get_boot_command(self):
args = ['--interactive'] args = ['--interactive']

@ -52,9 +52,8 @@ class Stream(mitogen.parent.Stream):
if jexec_path: if jexec_path:
self.jexec_path = jexec_path self.jexec_path = jexec_path
def connect(self): def _get_name(self):
super(Stream, self).connect() return u'jail.' + self.container
self.name = u'jail.' + self.container
def get_boot_command(self): def get_boot_command(self):
bits = [self.jexec_path] bits = [self.jexec_path]

@ -55,9 +55,8 @@ class Stream(mitogen.parent.Stream):
self.kubectl_path = kubectl_path self.kubectl_path = kubectl_path
self.kubectl_args = kubectl_args or [] self.kubectl_args = kubectl_args or []
def connect(self): def _get_name(self):
super(Stream, self).connect() return u'kubectl.%s%s' % (self.pod, self.kubectl_args)
self.name = u'kubectl.%s%s' % (self.pod, self.kubectl_args)
def get_boot_command(self): def get_boot_command(self):
bits = [self.kubectl_path] + self.kubectl_args + ['exec', '-it', self.pod] bits = [self.kubectl_path] + self.kubectl_args + ['exec', '-it', self.pod]

@ -60,9 +60,8 @@ class Stream(mitogen.parent.Stream):
if lxc_attach_path: if lxc_attach_path:
self.lxc_attach_path = lxc_attach_path self.lxc_attach_path = lxc_attach_path
def connect(self): def _get_name(self):
super(Stream, self).connect() return u'lxc.' + self.container
self.name = u'lxc.' + self.container
def get_boot_command(self): def get_boot_command(self):
bits = [ bits = [

@ -61,9 +61,8 @@ class Stream(mitogen.parent.Stream):
if lxc_path: if lxc_path:
self.lxc_path = lxc_path self.lxc_path = lxc_path
def connect(self): def _get_name(self):
super(Stream, self).connect() return u'lxd.' + self.container
self.name = u'lxd.' + self.container
def get_boot_command(self): def get_boot_command(self):
bits = [ bits = [

@ -1001,7 +1001,7 @@ class DiagLogStream(mitogen.core.BasicStream):
self.buf = '' self.buf = ''
def __repr__(self): def __repr__(self):
return 'mitogen.parent.DiagLogStream(fd=%r, %r)' % ( return "mitogen.parent.DiagLogStream(fd=%r, '%s')" % (
self.receive_side.fd, self.receive_side.fd,
self.stream.name, self.stream.name,
) )
@ -1017,11 +1017,11 @@ class DiagLogStream(mitogen.core.BasicStream):
return self.on_disconnect(broker) return self.on_disconnect(broker)
self.buf += buf.decode('utf-8', 'replace') self.buf += buf.decode('utf-8', 'replace')
while '\n' in self.buf: while u'\n' in self.buf:
lines = self.buf.split('\n') lines = self.buf.split('\n')
self.buf = lines[-1] self.buf = lines[-1]
for line in lines[:-1]: for line in lines[:-1]:
LOG.debug('%r: %r', self, line.rstrip()) LOG.debug('%s: %s', self.stream.name, line.rstrip())
class Stream(mitogen.core.Stream): class Stream(mitogen.core.Stream):
@ -1288,10 +1288,18 @@ class Stream(mitogen.core.Stream):
if self.eof_error_hint: if self.eof_error_hint:
e.args = ('%s\n\n%s' % (e.args[0], self.eof_error_hint),) e.args = ('%s\n\n%s' % (e.args[0], self.eof_error_hint),)
def _get_name(self):
"""
Called by :meth:`connect` after :attr:`pid` is known. Subclasses can
override it to specify a default stream name, or set
:attr:`name_prefix` to generate a default format.
"""
return u'%s.%s' % (self.name_prefix, self.pid)
def connect(self): def connect(self):
LOG.debug('%r.connect()', self) LOG.debug('%r.connect()', self)
self.pid, fd, diag_fd = self.start_child() self.pid, fd, diag_fd = self.start_child()
self.name = u'%s.%s' % (self.name_prefix, self.pid) self.name = self._get_name()
self.receive_side = mitogen.core.Side(self, fd) self.receive_side = mitogen.core.Side(self, fd)
self.transmit_side = mitogen.core.Side(self, os.dup(fd)) self.transmit_side = mitogen.core.Side(self, os.dup(fd))
if diag_fd is not None: if diag_fd is not None:
@ -1299,8 +1307,8 @@ class Stream(mitogen.core.Stream):
else: else:
self.diag_stream = None self.diag_stream = None
LOG.debug('%r.connect(): stdin=%r, stdout=%r, diag=%r', LOG.debug('%r.connect(): pid:%r stdin:%r, stdout:%r, diag:%r',
self, self.receive_side.fd, self.transmit_side.fd, self, self.pid, self.receive_side.fd, self.transmit_side.fd,
self.diag_stream and self.diag_stream.receive_side.fd) self.diag_stream and self.diag_stream.receive_side.fd)
try: try:

@ -223,11 +223,14 @@ class Stream(mitogen.parent.Stream):
def create_child(self, args): def create_child(self, args):
return mitogen.parent.create_child(args, preexec_fn=self.preexec_fn) return mitogen.parent.create_child(args, preexec_fn=self.preexec_fn)
def _get_name(self):
return u'setns.' + self.container
def connect(self): def connect(self):
self.name = self._get_name()
attr, func = self.GET_LEADER_BY_KIND[self.kind] attr, func = self.GET_LEADER_BY_KIND[self.kind]
tool_path = getattr(self, attr) tool_path = getattr(self, attr)
self.leader_pid = func(tool_path, self.container) self.leader_pid = func(tool_path, self.container)
LOG.debug('Leader PID for %s container %r: %d', LOG.debug('Leader PID for %s container %r: %d',
self.kind, self.container, self.leader_pid) self.kind, self.container, self.leader_pid)
super(Stream, self).connect() super(Stream, self).connect()
self.name = u'setns.' + self.container

@ -105,7 +105,7 @@ def filter_debug(stream, it):
if b('\n') not in buf: if b('\n') not in buf:
break break
line, _, buf = bytes_partition(buf, b('\n')) line, _, buf = bytes_partition(buf, b('\n'))
LOG.debug('%r: %s', stream, LOG.debug('%s: %s', stream.name,
mitogen.core.to_text(line.rstrip())) mitogen.core.to_text(line.rstrip()))
state = 'start_of_line' state = 'start_of_line'
elif state == 'in_plain': elif state == 'in_plain':
@ -239,11 +239,11 @@ class Stream(mitogen.parent.Stream):
base = super(Stream, self).get_boot_command() base = super(Stream, self).get_boot_command()
return bits + [shlex_quote(s).strip() for s in base] return bits + [shlex_quote(s).strip() for s in base]
def connect(self): def _get_name(self):
super(Stream, self).connect() s = u'ssh.' + mitogen.core.to_text(self.hostname)
self.name = u'ssh.' + mitogen.core.to_text(self.hostname)
if self.port: if self.port:
self.name += u':%s' % (self.port,) s += u':%s' % (self.port,)
return s
auth_incorrect_msg = 'SSH authentication is incorrect' auth_incorrect_msg = 'SSH authentication is incorrect'
password_incorrect_msg = 'SSH password is incorrect' password_incorrect_msg = 'SSH password is incorrect'
@ -261,7 +261,7 @@ class Stream(mitogen.parent.Stream):
def _host_key_prompt(self): def _host_key_prompt(self):
if self.check_host_keys == 'accept': if self.check_host_keys == 'accept':
LOG.debug('%r: accepting host key', self) LOG.debug('%s: accepting host key', self.name)
self.diag_stream.transmit_side.write(b('yes\n')) self.diag_stream.transmit_side.write(b('yes\n'))
return return
@ -273,7 +273,7 @@ class Stream(mitogen.parent.Stream):
def _connect_input_loop(self, it): def _connect_input_loop(self, it):
password_sent = False password_sent = False
for buf, partial in filter_debug(self, it): for buf, partial in filter_debug(self, it):
LOG.debug('%r: received %r', self, buf) LOG.debug('%s: stdout: %s', self.name, buf.rstrip())
if buf.endswith(self.EC0_MARKER): if buf.endswith(self.EC0_MARKER):
self._ec0_received() self._ec0_received()
return return
@ -295,7 +295,7 @@ class Stream(mitogen.parent.Stream):
elif partial and PASSWORD_PROMPT in buf.lower(): elif partial and PASSWORD_PROMPT in buf.lower():
if self.password is None: if self.password is None:
raise PasswordError(self.password_required_msg) raise PasswordError(self.password_required_msg)
LOG.debug('%r: sending password', self) LOG.debug('%s: sending password', self.name)
self.diag_stream.transmit_side.write( self.diag_stream.transmit_side.write(
(self.password + '\n').encode() (self.password + '\n').encode()
) )

@ -80,9 +80,8 @@ class Stream(mitogen.parent.Stream):
if incorrect_prompts is not None: if incorrect_prompts is not None:
self.incorrect_prompts = map(str.lower, incorrect_prompts) self.incorrect_prompts = map(str.lower, incorrect_prompts)
def connect(self): def _get_name(self):
super(Stream, self).connect() return u'su.' + mitogen.core.to_text(self.username)
self.name = u'su.' + mitogen.core.to_text(self.username)
def get_boot_command(self): def get_boot_command(self):
argv = mitogen.parent.Argv(super(Stream, self).get_boot_command()) argv = mitogen.parent.Argv(super(Stream, self).get_boot_command())

@ -140,9 +140,8 @@ class Stream(mitogen.parent.Stream):
self.selinux_role = option(self.selinux_role, selinux_role, opts.role) self.selinux_role = option(self.selinux_role, selinux_role, opts.role)
self.selinux_type = option(self.selinux_type, selinux_type, opts.type) self.selinux_type = option(self.selinux_type, selinux_type, opts.type)
def connect(self): def _get_name(self):
super(Stream, self).connect() return u'sudo.' + mitogen.core.to_text(self.username)
self.name = u'sudo.' + mitogen.core.to_text(self.username)
def get_boot_command(self): def get_boot_command(self):
# Note: sudo did not introduce long-format option processing until July # Note: sudo did not introduce long-format option processing until July

Loading…
Cancel
Save