Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Paramiko backend try to reconnect if connection became closed #66

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 33 additions & 30 deletions testinfra/backend/paramiko.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,38 +46,41 @@ def __init__(self, hostspec, ssh_config=None, *args, **kwargs):

@property
def client(self):
if self._client is None:
if not HAS_PARAMIKO:
raise RuntimeError((
"You must install paramiko package (pip install paramiko) "
"to use the paramiko backend"))
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.WarningPolicy())
cfg = {
"hostname": self.host,
"port": int(self.port) if self.port else 22,
"username": self.user,
}
if self.ssh_config:
ssh_config = paramiko.SSHConfig()
with open(self.ssh_config) as f:
ssh_config.parse(f)
if (self._client is not None and
self._client.get_transport().is_active()):
return self._client

for key, value in ssh_config.lookup(self.host).items():
if key == "hostname":
cfg[key] = value
elif key == "user":
cfg["username"] = value
elif key == "port":
cfg[key] = int(value)
elif key == "identityfile":
cfg["key_filename"] = os.path.expanduser(value[0])
elif key == "stricthostkeychecking" and value == "no":
client.set_missing_host_key_policy(IgnorePolicy())
if not HAS_PARAMIKO:
raise RuntimeError((
"You must install paramiko package (pip install paramiko) "
"to use the paramiko backend"))
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.WarningPolicy())
cfg = {
"hostname": self.host,
"port": int(self.port) if self.port else 22,
"username": self.user,
}
if self.ssh_config:
ssh_config = paramiko.SSHConfig()
with open(self.ssh_config) as f:
ssh_config.parse(f)

client.connect(**cfg)
self._client = client
return self._client
for key, value in ssh_config.lookup(self.host).items():
if key == "hostname":
cfg[key] = value
elif key == "user":
cfg["username"] = value
elif key == "port":
cfg[key] = int(value)
elif key == "identityfile":
cfg["key_filename"] = os.path.expanduser(value[0])
elif key == "stricthostkeychecking" and value == "no":
client.set_missing_host_key_policy(IgnorePolicy())

client.connect(**cfg)
self._client = client
return client

def run(self, command, *args, **kwargs):
command = self.get_command(command, *args)
Expand Down