From 250afa120fe97c87e9ef4d13e62909c3b82a126c Mon Sep 17 00:00:00 2001 From: Aarsh2001 Date: Fri, 6 Oct 2023 21:29:17 +0100 Subject: [PATCH] minor modifications to gcp_auth --- .github/auth/vm_auth.py | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/.github/auth/vm_auth.py b/.github/auth/vm_auth.py index 06631aa8..43e780af 100644 --- a/.github/auth/vm_auth.py +++ b/.github/auth/vm_auth.py @@ -17,16 +17,27 @@ def _start_ssh_session(response, creds, username, passphrase): ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - try: - ssh.connect( - external_ip, - username=username, - key_filename=creds, - passphrase=passphrase, - ) - print("SSH session successful !") - except Exception as e: - print(e) + max_retries = 3 + retry_delay = 10 # seconds + + for _ in range(max_retries): + # ssh connection fails non-deterministically + try: + ssh.connect( + external_ip, + username=username, + key_filename=creds, + passphrase=passphrase, + ) + print("SSH session successful !") + except paramiko.ssh_exception.NoValidConnectionsError as e: + print(f"SSH Exception(NoValidConnectionsError): {e}") + time.sleep(retry_delay) + except paramiko.ssh_exception.SSHException as e: + print(f"SSH Exception(General): {e}") + except paramiko.ssh_exception.AuthenticationException as e: + print(f"Authentication failed: {e}") + return # Open an SSH session transport = ssh.get_transport()