From 7873a4c32f0c4bcc569b206115f30dea80c68bdc Mon Sep 17 00:00:00 2001 From: Federico Ressi Date: Tue, 28 Jan 2020 10:57:27 +0100 Subject: [PATCH] Fix bug executing a command after loosing SSH connectivity to host Change-Id: I13b6f55a6271296a9667e4c354342512f69100f0 --- tobiko/shell/sh/_ssh.py | 22 ++++++++++++++++++---- tobiko/shell/ssh/_client.py | 2 +- tobiko/shell/ssh/config.py | 7 ++++--- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/tobiko/shell/sh/_ssh.py b/tobiko/shell/sh/_ssh.py index d7f085f91..9e6984da1 100644 --- a/tobiko/shell/sh/_ssh.py +++ b/tobiko/shell/sh/_ssh.py @@ -18,6 +18,7 @@ from __future__ import absolute_import from oslo_log import log import paramiko +from tobiko.shell.sh import _exception from tobiko.shell.sh import _execute from tobiko.shell.sh import _io from tobiko.shell.sh import _local @@ -83,9 +84,23 @@ class SSHShellProcessFixture(_process.ShellProcessFixture): if isinstance(ssh_client, ssh.SSHClientFixture): # Connect to SSH server ssh_client = ssh_client.connect() - process = ssh_client.get_transport().open_session() command = str(self.command) + timeout = self.timeout and float(self.timeout) + try: + process = ssh_client.get_transport().open_session(timeout=timeout) + except paramiko.SSHException as ex: + LOG.debug('Error executing command %r', command, exc_info=1) + error = str(ex) + if "Timeout opening channel." == error: + raise _exception.ShellTimeoutExpired(command=command, + stdin=None, + stdout=None, + stderr=None, + timeout=timeout) + else: + raise _exception.ShellError(error) + LOG.debug("Execute command %r on remote host (timeout=%r)...", command, self.timeout) if parameters.environment: @@ -109,9 +124,8 @@ class SSHShellProcessFixture(_process.ShellProcessFixture): buffer_size=self.parameters.buffer_size) def poll_exit_status(self): - process = self.process - exit_status = process.exit_status - if exit_status < 0: + exit_status = getattr(self.process, 'exit_status', None) + if exit_status and exit_status < 0: exit_status = None return exit_status diff --git a/tobiko/shell/ssh/_client.py b/tobiko/shell/ssh/_client.py index ec3bd27ee..43434da46 100644 --- a/tobiko/shell/ssh/_client.py +++ b/tobiko/shell/ssh/_client.py @@ -451,7 +451,7 @@ def ssh_connect(hostname, username=None, port=None, connection_interval=None, time.sleep(sleep_time) else: - LOG.info("Successfully logged it to %s", login) + LOG.debug("Successfully logged in to %s", login) return client, proxy_sock diff --git a/tobiko/shell/ssh/config.py b/tobiko/shell/ssh/config.py index 103f75dad..9d2048eb2 100644 --- a/tobiko/shell/ssh/config.py +++ b/tobiko/shell/ssh/config.py @@ -80,6 +80,7 @@ def setup_tobiko_config(conf): if not paramiko_logger.isEnabledFor(log.DEBUG): # Print paramiko debugging messages paramiko_logger.logger.setLevel(log.DEBUG) - elif paramiko_logger.isEnabledFor(log.DEBUG): - # Silence paramiko debugging messages - paramiko_logger.logger.setLevel(log.WARNING) + else: + if paramiko_logger.isEnabledFor(log.ERROR): + # Silence paramiko debugging messages + paramiko_logger.logger.setLevel(log.FATAL)