Skip to content

Commit 32d61e5

Browse files
author
Gabriel Monroy
committed
add timeout/attempts to util.connect_ssh, use 120s by default
1 parent f0bb9fc commit 32d61e5

2 files changed

Lines changed: 6 additions & 6 deletions

File tree

celerytasks/ec2.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,9 @@ def launch_node(node_id, creds, params, init, ssh_username, ssh_private_key):
103103
node.fqdn = boto.public_dns_name
104104
node.metadata = format_metadata(boto)
105105
node.save()
106-
# wait 10 seconds for ssh daemon to come up
107-
time.sleep(10)
108106
# loop until cloud-init is finished
109107
ssh = util.connect_ssh(ssh_username, boto.public_dns_name, 22,
110-
ssh_private_key)
108+
ssh_private_key, timeout=120)
111109
initializing = True
112110
while initializing:
113111
time.sleep(10)

celerytasks/util.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,21 @@
77
import paramiko
88

99

10-
def connect_ssh(username, hostname, port, key):
10+
def connect_ssh(username, hostname, port, key,
11+
timeout=120, attempts=10):
1112
key_f = StringIO.StringIO(key)
1213
pkey = paramiko.RSAKey.from_private_key(key_f)
1314
ssh = paramiko.SSHClient()
1415
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
15-
for _ in range(10):
16+
interval = int(timeout / attempts)
17+
for _ in range(attempts):
1618
try:
1719
ssh.connect(hostname, username=username, pkey=pkey)
1820
break
1921
except paramiko.AuthenticationException as e:
2022
raise paramiko.AuthenticationException(e)
2123
except socket.error:
22-
time.sleep(3)
24+
time.sleep(interval)
2325
else:
2426
raise RuntimeError('SSH Connection Error')
2527
return ssh

0 commit comments

Comments
 (0)