-
Notifications
You must be signed in to change notification settings - Fork 38
/
wait-for-elb-instances-in-service.py
112 lines (101 loc) · 3.54 KB
/
wait-for-elb-instances-in-service.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python3
import argparse, re, time
from assume_role_lib import log, sts
from assume_role_lib.util import unwrap
from datetime import datetime, timedelta
from collections import Counter as counter
__version__ = '0.1'
logger = None
def add_arguments(argument_parser):
argument_parser.add_argument(
'--version', '-V',
action = 'version',
version = '%(prog)s {0}'.format(__version__),
help = 'Show version information and exit.',
)
argument_parser.add_argument(
'--timeout',
metavar = 'TIME',
default = '15m',
help = unwrap("""
Stack timeout: XXn; for n - d = days, h = hour, m = mins, s =
seconds.
""")
)
argument_parser.add_argument(
'elb',
metavar = 'NAME',
nargs = '+',
help = unwrap("""
The ELB to watch. Multiple are allowed.
""")
)
def parse_timeout(timeout):
days, hours, minutes, seconds = 0, 0, 0, 0
findall = re.findall(r'([0-9]+)([dhms])', timeout.lower())
for number, unit in findall:
number = int(number)
if unit == 'd':
days = number
elif unit == 'h':
hours = number
elif unit == 'm':
minutes = number
elif unit == 's':
seconds = number
return timedelta(days, seconds, 0, 0, minutes, hours)
def get_elb_instance_info(elb, elb_client):
instance_health = elb_client.describe_instance_health(
LoadBalancerName = elb,
).get('InstanceStates', [])
instance_states = counter(( i.get('State') for i in instance_health ))
return len(instance_health), instance_states
def main():
global logger
p = argparse.ArgumentParser(
description=unwrap("""
Wait for all instances added to an ELB to come into service. Exits
non-zero if there are no instances added to an ELB.
"""),
)
sts.add_arguments(p)
log.add_arguments(p)
add_arguments(p)
args = p.parse_args()
logger = log.get_logger(args)
logger.debug('Args: %r', args)
session = sts.get_session(args)
elb_client = session.client('elb')
elbs = set( args.elb )
complete_elbs = set()
remaining_elbs = elbs - complete_elbs
timeout = datetime.now() + parse_timeout(args.timeout)
while remaining_elbs:
for elb in remaining_elbs:
count, states = get_elb_instance_info(elb, elb_client)
logger.debug('%s: %r', elb, states)
if states.get('InService') == count:
complete_elbs.add(elb)
logger.info(
'%s has %d of %d instances in service',
elb, states.get('InService', 0), count,
)
remaining_elbs = elbs - complete_elbs
if remaining_elbs:
if datetime.now() > timeout:
logger.error(
'Timeout waiting for ELBs. %d have not completed.',
len(remaining_elbs),
)
for elb in remaining_elbs:
count, states = get_elb_instance_info(elb, elb_client)
logger.error(
'%s has only %d of %d instances in service',
elb, states.get('InService', 0), count,
)
raise SystemExit(2)
logger.info('Waiting for %d ELB\'s instances to come into service.', len(remaining_elbs))
logger.debug('%r', remaining_elbs)
time.sleep(15)
if __name__ == '__main__':
main()