-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathawsutils.py
More file actions
77 lines (66 loc) · 2.45 KB
/
awsutils.py
File metadata and controls
77 lines (66 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import logging
import boto3
def wait_for_instances(instances):
"""
Wait until the given boto3 instance objects are running
"""
logging.info("Waiting for instances: {}".format([i.id for i in instances]))
for i in instances:
logging.info("Waiting for instance {}".format(i.id))
i.wait_until_running()
logging.info("Instance {} running".format(i.id))
client = boto3.client('ec2')
waiter = client.get_waiter('instance_status_ok')
logging.info("Waiting for instances to initialize (status ok): {}".format([i.id for i in instances]))
waiter.wait(InstanceIds=[i.id for i in instances])
logging.info("EC2 instances are ready to roll")
for i in instances:
i.reload()
def wait_port_open(server, port, timeout=None):
""" Wait for network service to appear
@param server: host to connect to (str)
@param port: port (int)
@param timeout: in seconds, if None or 0 wait forever
@return: True of False, if timeout is None may return only True or
throw unhandled network exception
"""
import socket
import errno
import time
sleep_s = 0
if timeout:
from time import time as now
# time module is needed to calc timeout shared between two exceptions
end = now() + timeout
while True:
logging.debug("Sleeping for %s second(s)", sleep_s)
time.sleep(sleep_s)
s = socket.socket()
try:
if timeout:
next_timeout = end - now()
if next_timeout < 0:
return False
else:
s.settimeout(next_timeout)
logging.info("connect %s %d", server, port)
s.connect((server, port))
except ConnectionError as err:
logging.debug("ConnectionError %s", err)
if sleep_s == 0:
sleep_s = 1
except socket.gaierror as err:
logging.debug("gaierror %s",err)
return False
except socket.timeout as err:
# this exception occurs only if timeout is set
if timeout:
return False
except TimeoutError as err:
# catch timeout exception from underlying network library
# this one is different from socket.timeout
raise
else:
s.close()
logging.info("wait_port_open: port %s:%s is open", server, port)
return True