/
deploy.py
367 lines (310 loc) · 11.5 KB
/
deploy.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/usr/bin/env python
import argparse
import ConfigParser
import sys
import os
import logging
import time
class ElasticLoadBalancer(object):
def __init__(self, client, name):
self.client = client
self.name = name
self._update_details()
def _update_details(self):
"""Update cached details about this ELB instance"""
self.details = self.client.describe_load_balancers(
LoadBalancerNames=[self.name]
)['LoadBalancerDescriptions'][0]
def get_instance_health(self):
"""Get instance health for all instances"""
response = self.client.describe_instance_health(
LoadBalancerName=self.name
)
return response['InstanceStates']
def get_total_instance_count(self):
"""Get total instance count"""
return len([i for i in self.get_instance_health()])
def get_healthy_instance_count(self):
"""Get healthy instance count"""
healthy_instance_count = 0
for i in self.get_instance_health():
if i['State'] == 'InService':
healthy_instance_count += 1
return healthy_instance_count
class AutoScalingGroup(object):
def __init__(self, client, name):
self.processes = [
'AlarmNotification'
]
self.name = name
self.client = client
self._update_details()
def _update_details(self):
"""Update cached details about this ASG instance"""
self.details = self.client.describe_auto_scaling_groups(
AutoScalingGroupNames=[self.name],
MaxRecords=1
)['AutoScalingGroups'][0]
def resume_processes(self):
"""Enable normal operational processes"""
response = self.client.resume_processes(
AutoScalingGroupName=self.name,
ScalingProcesses=self.processes
)
def suspend_processes(self):
"""Suspend normal operational processes"""
self.client.suspend_processes(
AutoScalingGroupName=self.name,
ScalingProcesses=self.processes
)
def has_enough_capacity(self):
"""Check whether this ASG has enough capacity to double in size"""
return self.get_new_desired_capacity() <= self.get_max_capacity()
def get_desired_capacity(self):
"""Get current desired capacity"""
self._update_details()
desired_capacity = int(self.details["DesiredCapacity"])
return desired_capacity
def get_new_desired_capacity(self):
"""Get new desired capacity"""
self._update_details()
desired_capacity = int(self.details["DesiredCapacity"])
new_desired_capacity = 2 * desired_capacity
return new_desired_capacity
def get_max_capacity(self):
"""Get current max capacity"""
self._update_details()
max_capacity = int(self.details["MaxSize"])
return max_capacity
def get_load_balancer_name(self):
"""Get name of the first ELB associated with this ASG"""
self._update_details()
load_balancer_name = self.details['LoadBalancerNames'][0]
return load_balancer_name
def get_instance_ids(self):
"""Get the IDs of all instances in this ASG"""
self._update_details()
instance_ids = [i["InstanceId"] for i in self.details["Instances"]]
return instance_ids
def get_healthy_instance_count(self):
healthy_instance_count = 0
response = self.client.describe_auto_scaling_instances(
MaxRecords=50,
)['AutoScalingInstances']
for i in response:
if i['LifecycleState'] == 'InService':
healthy_instance_count += 1
return healthy_instance_count
def set_desired_capacity(self, new_desired_capacity):
"""Set new desired capacity of this ASG"""
response = self.client.set_desired_capacity(
AutoScalingGroupName=self.name,
DesiredCapacity=new_desired_capacity,
HonorCooldown=False
)
def double_in_size(self):
"""Double the desired capacity of this ASG"""
new_desired_capacity = self.get_new_desired_capacity()
self.set_desired_capacity(new_desired_capacity)
class EC2(object):
def __init__(self, client):
self.client = client
self._termination_tag = {
'Key': 'Terminate-After-Deploy',
'Value': 'true'
}
def tag_instances(self, instance_ids, tags):
"""Add a tag to a list of instances"""
response = self.client.create_tags(
DryRun=False,
Resources=instance_ids,
Tags=tags
)
def mark_instances_for_termination(self, instance_ids):
"""Mark a list of instances for termination"""
self.tag_instances(
instance_ids=instance_ids,
tags=[
self._termination_tag
]
)
def get_instances_marked_for_termination(self):
"""Get a list of instances marked for termination"""
try:
response = self.client.describe_instances(
DryRun=False,
Filters=[
{
'Name': 'tag:%s' % self._termination_tag['Key'],
'Values': [
self._termination_tag['Value']
]
}
]
)['Reservations'][0]['Instances']
return [i['InstanceId'] for i in response]
except:
return []
def terminate_instances(self, instance_ids):
"""Terminate a list of instances"""
if instance_ids and len(instance_ids) > 0:
response = self.client.terminate_instances(
DryRun=False,
InstanceIds=instance_ids
)
return response
def setup_clients(profile_name, region_name):
try:
import boto3
import botocore.session
except:
logging.error("Could not import \'boto3\' library. Please install.")
sys.exit(3)
session = boto3.session.Session(profile_name=profile_name)
asg_client = session.client('autoscaling', region_name=region_name)
ec2_client = session.client('ec2', region_name=region_name)
elb_client = session.client('elb', region_name=region_name)
return asg_client, ec2_client, elb_client
def deploy(auto_scaling_group_name, asg_client, ec2_client, elb_client):
asg = AutoScalingGroup(client=asg_client, name=auto_scaling_group_name)
ec2 = EC2(client=ec2_client)
elb_name = asg.get_load_balancer_name()
elb = ElasticLoadBalancer(client=elb_client, name=elb_name)
if not asg.has_enough_capacity():
logging.error(
"Not enough capacity in auto scaling group for deployment"
)
sys.exit(4)
original_desired_capacity = asg.get_desired_capacity()
logging.info("Original desired capacity is %d", original_desired_capacity)
logging.info("Marking existing instances for post-deployment termination")
existing_instances = asg.get_instance_ids()
ec2.mark_instances_for_termination(existing_instances)
logging.info("Suspending auto scaling group processes")
asg.suspend_processes()
logging.info("Doubling auto scaling group size")
asg.double_in_size()
current_desired_capacity = asg.get_desired_capacity()
time_spent_waiting = 0
# Wait for instances to be healthy and recognised by ASG
# Time out if not happening within 15 minutes
current_asg_healhty_instances = asg.get_healthy_instance_count()
while current_asg_healhty_instances < current_desired_capacity:
logging.info(
"Waiting for ASG to recognise %d healthy instances. Currently %d",
current_desired_capacity,
current_asg_healhty_instances
)
time.sleep(30)
time_spent_waiting += 30
if time_spent_waiting >= 900:
logging.error("New hosts not healthy within 15 minutes")
sys.exit(5)
current_asg_healhty_instances = asg.get_healthy_instance_count()
time_spent_waiting = 0
# Wait for instances to be healthy and recognised by ElasticLoadBalancer
# Time out if not happening within 15 minutes
current_elb_healthy_instances = elb.get_healthy_instance_count()
while current_elb_healthy_instances < current_desired_capacity:
logging.info(
"Waiting for ELB to recognise %d healthy instances. Currently %d",
current_desired_capacity,
current_elb_healthy_instances
)
time.sleep(30)
time_spent_waiting += 30
if time_spent_waiting >= 900:
logging.error("New hosts not healthy within 15 minutes")
sys.exit(5)
current_elb_healthy_instances = elb.get_healthy_instance_count()
logging.info(
"ELB has recognised %d healthy instances",
current_elb_healthy_instances
)
logging.info(
"ASG has recognised %d healthy instances",
current_asg_healhty_instances
)
# Terminate instances marked for termination
logging.info("Terminating previous instances")
instances_to_terminate = ec2.get_instances_marked_for_termination()
ec2.terminate_instances(instances_to_terminate)
# Set desired capacity to original
logging.info(
"Setting current capacity to original %d",
original_desired_capacity
)
asg.set_desired_capacity(original_desired_capacity)
logging.info("Resuming auto scaling group processes")
asg.resume_processes()
def setup_logging(debug=False):
root = logging.getLogger()
ch = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
root.addHandler(ch)
if debug:
ch.setLevel(logging.DEBUG)
root.setLevel(logging.DEBUG)
else:
ch.setLevel(logging.INFO)
root.setLevel(logging.INFO)
def parse_arguments():
"""Parse command line arguments"""
args_parser = argparse.ArgumentParser()
args_parser.add_argument(
"--debug",
help="Increase output verbosity",
action="store_true")
args_parser.add_argument(
"--profile",
required=False,
default="ec2",
help="AWS profile to use")
args_parser.add_argument(
"--region",
required=False,
default="eu-west-1",
help="AWS region to use")
args_parser.add_argument(
"--application",
required=True,
help="Application to deploy")
args_parser.add_argument(
"--config",
required=True,
help="Configuration file")
args = args_parser.parse_args()
return args
def main():
arguments = parse_arguments()
setup_logging(arguments.debug)
config = ConfigParser.ConfigParser()
try:
config.read(arguments.config)
except Exception as e:
logging.error(
"Could not parse configuration file %s: %s",
configuration_filename,
e
)
sys.exit(1)
try:
auto_scaling_group = config.get(
arguments.application,
"auto_scaling_group"
)
except Exception as e:
logging.error(
"Could not find configuration for application \'%s\'",
arguments.application
)
sys.exit(2)
asg_client, ec2_client, elb_client = setup_clients(
profile_name=arguments.profile,
region_name=arguments.region
)
deploy(auto_scaling_group, asg_client, ec2_client, elb_client)
if __name__ == '__main__':
main()