]> git-server-git.apps.pok.os.sepia.ceph.com Git - teuthology.git/commitdiff
connect(): Make retries optional
authorZack Cerza <zack@redhat.com>
Fri, 21 Aug 2015 16:06:47 +0000 (10:06 -0600)
committerLoic Dachary <ldachary@redhat.com>
Wed, 2 Sep 2015 22:02:39 +0000 (00:02 +0200)
Signed-off-by: Zack Cerza <zack@redhat.com>
teuthology/orchestra/connection.py

index b0c631fee6afa6fe85b85ad3838fb3d56c32d6ba..2c0dd485e05c6e27d4415116c5af9aed82a6f543 100644 (file)
@@ -38,7 +38,7 @@ def create_key(keytype, key):
 
 
 def connect(user_at_host, host_key=None, keep_alive=False, timeout=60,
-            _SSHClient=None, _create_key=None):
+            _SSHClient=None, _create_key=None, retry=True):
     """
     ssh connection routine.
 
@@ -48,6 +48,8 @@ def connect(user_at_host, host_key=None, keep_alive=False, timeout=60,
     :param timeout:    timeout in seconds
     :param _SSHClient: client, default is paramiko ssh client
     :param _create_key: routine to create a key (defaults to local reate_key)
+    :param retry:       Whether or not to retry failed connection attempts
+                        (eventually giving up if none succeed). Default is True
     :return: ssh connection.
     """
     user, host = split_user(user_at_host)
@@ -96,13 +98,17 @@ def connect(user_at_host, host_key=None, keep_alive=False, timeout=60,
 
     log.info(connect_args)
 
-    # just let the exceptions bubble up to caller
-    with safe_while(sleep=1, action='connect to ' + host) as proceed:
-        while proceed():
-            try:
-                ssh.connect(**connect_args)
-                break
-            except paramiko.AuthenticationException:
-                log.exception("Error connecting to {host}".format(host=host))
+    if not retry:
+        ssh.connect(**connect_args)
+    else:
+        # Retries are implemented using safe_while
+        with safe_while(sleep=1, action='connect to ' + host) as proceed:
+            while proceed():
+                try:
+                    ssh.connect(**connect_args)
+                    break
+                except paramiko.AuthenticationException:
+                    log.exception(
+                        "Error connecting to {host}".format(host=host))
     ssh.get_transport().set_keepalive(keep_alive)
     return ssh