From 01723bcbc081c22588218090653cdb44ce80fe04 Mon Sep 17 00:00:00 2001 From: Sean Marlow Date: Mon, 17 Aug 2020 15:12:42 -0500 Subject: [PATCH] Break connection loop on certain exceptions. If ssh private key file does not exist or there is an authentication exception during establishment of ssh connection raise on first try. These are not recoverable so img-proof should not re-attempt connection. --- img_proof/ipa_utils.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/img_proof/ipa_utils.py b/img_proof/ipa_utils.py index 7e6321b6..593b4170 100644 --- a/img_proof/ipa_utils.py +++ b/img_proof/ipa_utils.py @@ -35,6 +35,7 @@ from contextlib import contextmanager from string import ascii_lowercase from tempfile import NamedTemporaryFile +from paramiko.ssh_exception import AuthenticationException from img_proof.ipa_constants import SYNC_POINTS from img_proof.ipa_exceptions import IpaSSHException, IpaUtilsException @@ -81,6 +82,8 @@ def establish_ssh_connection(ip, key_filename=ssh_private_key_file, timeout=timeout ) + except (FileNotFoundError, AuthenticationException): + raise except: # noqa: E722 attempts -= 1 time.sleep(10) @@ -281,6 +284,16 @@ def get_ssh_client(ip, timeout=wait_period ) execute_ssh_command(client, 'ls') + except FileNotFoundError: + raise IpaSSHException( + 'SSH private key file {key_file} not found.'.format( + key_file=ssh_private_key_file + ) + ) + except AuthenticationException: + raise IpaSSHException( + 'Authentication failed while establishing SSH connection.' + ) except: # noqa: E722 if client: client.close()