Skip to content

Commit

Permalink
Paramiko: Handle closed session
Browse files Browse the repository at this point in the history
When the connection is dropped (for instance if the target host is
restarted between tests), paramiko raises
"paramiko.ssh_exception.SSHException: SSH session not active". In this
case we try to reconnect (once).
  • Loading branch information
philpep committed Feb 29, 2016
1 parent a99f831 commit 71eeb18
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions testinfra/backend/paramiko.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf8 -*-
# Copyright © 2015 Philippe Pepiot
# Copyright © 2015-2016 Philippe Pepiot
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -25,6 +25,7 @@
HAS_PARAMIKO = False
else:
HAS_PARAMIKO = True
import paramiko.ssh_exception

class IgnorePolicy(paramiko.MissingHostKeyPolicy):
"""Policy for ignoring missing host key."""
Expand Down Expand Up @@ -79,14 +80,27 @@ def client(self):
self._client = client
return self._client

def run(self, command, *args, **kwargs):
command = self.get_command(command, *args)
def _exec_command(self, command):
chan = self.client.get_transport().open_session()
command = self.encode(command)
chan.exec_command(command)
rc = chan.recv_exit_status()
stdout = b''.join(chan.makefile('rb'))
stderr = b''.join(chan.makefile_stderr('rb'))
return rc, stdout, stderr

def run(self, command, *args, **kwargs):
command = self.get_command(command, *args)
command = self.encode(command)
try:
rc, stdout, stderr = self._exec_command(command)
except paramiko.ssh_exception.SSHException:
if not self.client.get_transport().is_active():
# try to reinit connection (once)
self._client = None
rc, stdout, stderr = self._exec_command(command)
else:
raise

result = base.CommandResult(self, rc, stdout, stderr, command)
logger.info("RUN %s", result)
return result

0 comments on commit 71eeb18

Please sign in to comment.