-
Notifications
You must be signed in to change notification settings - Fork 3
/
asg-az-update.py
278 lines (221 loc) · 8.4 KB
/
asg-az-update.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import boto3
from botocore.exceptions import ClientError
import json
import logging
import os
import re
import getopt
import sys
logger = logging.getLogger()
logger.setLevel(logging.INFO)
# define boto3 clients
ec2_client = boto3.client('ec2')
asg_client = boto3.client('autoscaling')
def ignore_asg(asg_name, services):
for service in services:
service_pattern = '^{}-v[0-9]+$'.format(service)
result = re.search(service_pattern, asg_name)
if result:
return False
return True
def get_asgs():
try:
asg_describe = asg_client.describe_auto_scaling_groups()
# Make sure the Auto Scaling group exists
if len(asg_describe['AutoScalingGroups']) == 0:
raise ValueError("Empty Auto Scaling Group")
return asg_describe
except ClientError as e:
logging.error("Error retrieving Auto Scaling groups.")
raise e
def get_azs_for_asg(asg):
try:
if 'AvailabilityZones' in asg.keys():
return(asg['AvailabilityZones'])
else:
return(None)
except ClientError as e:
logging.error(
"Error getting Availibility Zones for {}".format(asg['AutoScalingGroupName']))
raise e
def get_subnets_for_asg(asg):
try:
if 'VPCZoneIdentifier' in asg.keys():
logging.info("Current AZs+subnets for ASG '{}' = {}:{}".format(
asg['AutoScalingGroupName'], asg['AvailabilityZones'], asg['VPCZoneIdentifier']))
return(asg['VPCZoneIdentifier'].split(','))
else:
return None
except ClientError as e:
logging.error(
"Error getting Subnets (VPCZoneIdentifier) for {}".format(asg['AutoScalingGroupName']))
raise e
def get_subnet_ids_for_az(az):
if not az:
return None
try:
subnet_describe = ec2_client.describe_subnets(
Filters=[
{
'Name': 'availabilityZone',
'Values': [az]
},
],
)
# Make sure the Auto Scaling group exists
if len(subnet_describe['Subnets']) == 0:
raise ValueError("Empty subnets for {}".format(az))
subnet_ids = []
for subnets in subnet_describe['Subnets']:
subnet_ids.append(subnets['SubnetId'])
logging.debug("subnets {}: {}".format(az, subnet_ids))
return (subnet_ids)
except ClientError as e:
logging.error(
"Error getting subnets {}".format(az))
raise e
def update_azs_for_asg(azs, subnets, asg):
asg_name = asg['AutoScalingGroupName']
max_size = asg['MaxSize']
desired_capacity = asg['DesiredCapacity'] + 1
if max_size < desired_capacity:
max_size = desired_capacity
try:
response = asg_client.update_auto_scaling_group(
AutoScalingGroupName=asg_name,
AvailabilityZones=azs,
VPCZoneIdentifier=subnets,
# DesiredCapacity=desired_capacity,
# MaxSize=max_size,
)
except ClientError as e:
logging.error(
"Error updating Availibility Zones for {}".format(asg_name))
raise e
# TODO: revise this - its a bit tricky as this will stop all instances in all ASGs!
# WARNING: do not use this if you don't know what you are doing!
'''
def trigger_auto_scaling_instance_refresh(asg_name, strategy="Rolling",
min_healthy_percentage=90, instance_warmup=300):
try:
response = asg_client.start_instance_refresh(
AutoScalingGroupName=asg_name,
Strategy=strategy,
Preferences={
'MinHealthyPercentage': min_healthy_percentage,
'InstanceWarmup': instance_warmup
})
logging.info("Triggered Instance Refresh {} for Auto Scaling "
"group {}".format(response['InstanceRefreshId'], asg_name))
except ClientError as e:
logging.error("Unable to trigger Instance Refresh for "
"Auto Scaling group {}".format(asg_name))
raise e
'''
def set_instances_unhealthy_for_azs(asg, azs):
count = 0
try:
for instance in asg['Instances']:
if instance['AvailabilityZone'] in azs:
instance['HealthStatus'] = 'Unhealthy'
logging.info(
"Setting instance {} unhealthy in {}({})".format(instance['InstanceId'], asg['AutoScalingGroupName'], instance['AvailabilityZone']))
response = asg_client.set_instance_health(
InstanceId=instance['InstanceId'],
HealthStatus='Unhealthy',
ShouldRespectGracePeriod=False # True
)
count += 1
except ClientError as e:
logging.error("Unable to update Instance health {}".format(e))
raise e
return count
# def lambda_handler(event, context):
def update_az(az, az_list, operation):
if not az:
return az_list
if not az_list:
return None
if az in az_list and operation == 'remove':
az_list.remove(az)
elif az not in az_list and operation == 'add':
az_list.append(az)
return az_list
def update_subnets(subnet_ids_to_update, subnet_id_list, operation):
if not subnet_ids_to_update:
return subnet_id_list
if not subnet_id_list:
return None
for id in subnet_ids_to_update:
if id in subnet_id_list and operation == 'remove':
subnet_id_list.remove(id)
elif id not in subnet_id_list and operation == 'add':
subnet_id_list.append(id)
return subnet_id_list
def ParseArgs(argv):
options, args = getopt.getopt(
argv[1:],
's:b:w:d',
['services=', 'blacklist-az=', 'whitelist-az=', 'dryrun'])
services = ""
blacklist_az = ""
whitelist_az = ""
dryrun = False
for option_key, option_value in options:
if option_key in ('-s', '--services'):
services = option_value
elif option_key in ('-b', '--blacklist-az'):
blacklist_az = option_value
elif option_key in ('-w', '--whitelist-az'):
blacklist_az = option_value
elif option_key in ('-d', '--dryrun'):
dryrun = True
return services, blacklist_az, whitelist_az, dryrun, args
def main(argv):
'''
dry_run = os.environ['DRYRUN']
blacklist_az = os.environ['BLACKLIST_AZ']
whitelist_az = os.environ['WHITELIST_AZ']
services = os.environ['SERVICE_NAMES']
'''
s, blacklist_az, whitelist_az, dryrun, args = ParseArgs(argv)
services = s.split(",")
# TODO: sanity check arguments
logging.info("services = {}".format(services))
logging.info("blacklist_az = {}".format(blacklist_az))
logging.info("whitelist_az = {}".format(whitelist_az))
logging.info("dryrun = {}".format(dryrun))
# map azs with subnet ids
blacklist_subnet_ids = get_subnet_ids_for_az(blacklist_az)
whitelist_subnet_ids = get_subnet_ids_for_az(whitelist_az)
logging.info("blacklist subnets ids = {}".format(blacklist_subnet_ids))
logging.info("whitelist subnets ids = {}".format(whitelist_subnet_ids))
asgs = get_asgs()
for asg in asgs['AutoScalingGroups']:
asg_name = asg['AutoScalingGroupName']
if ignore_asg(asg_name, services):
continue
azs = get_azs_for_asg(asg)
subnets = get_subnets_for_asg(asg)
if not azs and not subnets:
logging.error(
"Auto Scaling group {} doesn't have AZ/subnet info".format(asg_name))
continue
logging.debug("current azs for {} = {}".format(asg_name, azs))
update_az(blacklist_az, azs, 'remove')
update_subnets(blacklist_subnet_ids, subnets, 'remove')
update_az(whitelist_az, azs, 'add')
update_subnets(whitelist_subnet_ids, subnets, 'add')
# make sure that the az_list is non-empty
if not azs or not subnets:
logging.error(
"Cannot blacklist AZ as it resulted in empty AZ list: {}".format(asg_name))
continue
logging.info("Updating asg: {} with {}({})".format(
asg_name, azs, subnets))
if dryrun == False:
update_azs_for_asg(azs, ','.join(subnets), asg)
instances = set_instances_unhealthy_for_azs(asg, blacklist_az)
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
main(sys.argv)