Skip to content

Commit

Permalink
Timeout SSH wait after 5 minutes (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
jawang35 authored Nov 5, 2018
1 parent 7975e9c commit a74bf43
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
1 change: 0 additions & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ dependencies:
- boto3=1.9.31
- python=3.7.0
- fabric=2.4.0

15 changes: 8 additions & 7 deletions lib/ssh.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from io import StringIO
from time import sleep
from timeit import default_timer
from fabric import Connection
from paramiko import RSAKey
from paramiko.ssh_exception import NoValidConnectionsError
Expand All @@ -10,7 +11,7 @@ class SSH():
SSH context manager for creating an SSH connection.
On enter an SSH connection is attempted every 5 seconds until successful.
An exception is raised after 60 attempts.
An exception is raised after 5 minutes.
On exit the connection is closed.
Expand All @@ -26,7 +27,6 @@ def __init__(self, host, user, private_key):
self.host = host
self.user = user
self.private_key = RSAKey.from_private_key(StringIO(private_key))
self.wait_count = 0

def __enter__(self):
self.connection = Connection(
Expand All @@ -35,20 +35,21 @@ def __enter__(self):
connect_kwargs={'pkey': self.private_key},
)
print(f'Waiting for SSH to become available on {self.host}...')
self.wait_for_ssh()
self.wait_for_ssh(default_timer())
return self.connection

def __exit__(self, type, value, traceback):
print(f'Closing SSH connection to {self.host}...')
self.connection.close()

def wait_for_ssh(self):
self.wait_count += 1
def wait_for_ssh(self, start):
try:
self.connection.open()
except NoValidConnectionsError:
if self.wait_count >= 60:
# Error after 5 minutes. Otherwise retry.
now = default_timer()
if now - start > 300:
raise

sleep(5)
self.wait_for_ssh()
self.wait_for_ssh(start)

0 comments on commit a74bf43

Please sign in to comment.