-
Notifications
You must be signed in to change notification settings - Fork 0
/
mass-asg-rebuild.py
executable file
·298 lines (247 loc) · 10.5 KB
/
mass-asg-rebuild.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
#!/usr/bin/env python
"""
Name: mass-asg-rebuild
Author: shane.warner@fox.com
Synopsis: This tool automatically locates autoscaling enabled clusters in ASGARD, builds and bootstraps fresh nodes for them
via Chef, and creates AMI images for the resulting node builds for use with ASGARD and autoscaling groups.
"""
import chef
import boto
import sys
import copy
import time
import pprint
from datetime import datetime
from boto.ec2.blockdevicemapping import BlockDeviceType, BlockDeviceMapping
# Globals
imageId='ami-04fe926c'
failed_ids = []
class asg(object):
def __init__(self):
self.api = chef.autoconfigure()
self.bag = chef.DataBag('clusters')
try:
self.ec2 = boto.connect_ec2()
except Exception as e:
print e
self.threshold = 2400
def cleanup(self, instance_ids, terminate=True):
"""
Deletes the build nodes and clients out of the Chef server.
Also terminates all instances that were launched as a result of the build.
:param instances_ids: List of instance ids to terminate
"""
for instance in instance_ids:
self.terminate(instance)
for row in chef.Search('node', 'name:*.internal'):
node = chef.Node(row.object.name)
chef.Node.delete(node)
def stop_servers(self, instance_ids):
"""
Stops instances specified in list. It will also stop any servers listed in failed_ids as a cleanup measure.
:param instances_ids: List of instance ids to stop
"""
status = 0
stopped = []
print "-------------------------------"
print "Stopping instances"
print "-------------------------------"
for instance_id in instance_ids:
try:
self.ec2.create_tags(instance_id, {"Name": "chef-autobuild"})
except Exception as e:
print "Failed to add tag for {0}".format(instance_id)
time.sleep(1)
try:
self.ec2.stop_instances(instance_id.encode('ascii'))
except Exception as e:
print "Failed to issue stop command for {0}".format(instance_id.encode('ascii'))
print e
try:
reservations = self.ec2.get_all_reservations(filters={'reservation_id':failed_ids})
except Exception as e:
print "Failed to get instance reservations."
print e
instances = [i for r in reservations for i in r.instances]
for instance in instances:
try:
self.ec2.create_tags(instance.id, {"Name": "chef-autobuild"})
except Exception as e:
print "Failed to add tag for {0}".format(instance.id)
time.sleep(1)
try:
self.ec2.stop_instances(instance.id)
except Exception as e:
print "Failed to issue stop command for {0}".format(instance.id)
print e
while status == 0:
time.sleep(10)
for instance_id in instance_ids:
reservations = self.ec2.get_all_instances(instance_ids=[instance_id])
instance = reservations[0].instances[0]
if instance.update() == 'stopped':
print instance_id + " stopped."
instance_ids.remove(instance_id)
stopped.append((instance_id))
if not instance_ids:
status=1
return stopped
def build_list(self):
"""
Builds a list of cluster data for autoscaling clusters.
We query the Chef server for nodes with autoscaling enabled and add their properties to the list to be
returned.
:return:
"""
cluster_data = []
print "Using AMI ID: {0}".format(imageId)
print "-------------------------------"
print "Identifying autoscale clusters"
print "-------------------------------"
for name, item in self.bag.iteritems():
for row in chef.Search('node', 'cluster:' + name + " AND chef_environment:prod NOT cluster:splunk"):
node = chef.Node(row.object.name)
str = node['ec2']['userdata']
if str is not None and len(str) > 0:
pos = str.find('CLOUD_STACK=autoscale')
if pos >= 1:
roles = node['roles'][0]
if len(node['roles']) == 3:
for role in node['roles']:
if role != "base" and role != "lamp-afs":
roles = role
break
elif len(node['roles']) == 2:
for role in node['roles']:
if role != "base" and role != "lamp":
roles = role
break
print "{0}".format(name)
cluster_data.append((name, "stage", roles, node['ec2']['security_groups']))
break
return cluster_data
def build_servers(self, cluster_data):
"""
Bootstraps and builds autoscaling servers to be used for AMI imaging.
:param cluster_data: List of the following format: [(name,env,roles,securityGroups),]
"""
reservation_ids = []
instance_ids = []
status = 0
now = time.time()
timelimit = now + self.threshold
map = BlockDeviceMapping()
eph0 = BlockDeviceType()
eph1 = BlockDeviceType()
eph0.ephemeral_name = 'ephemeral0'
eph1.ephemeral_name = 'ephemeral1'
map['/dev/sdb'] = eph0
map['/dev/sdc'] = eph1
print "-------------------------------"
print "Launching Chef builds"
print "-------------------------------"
for cluster, env, role, securityGroups in cluster_data:
time.sleep(1)
userData = 'HOSTNAME=chef-autobuild01 ENV=stage CLUSTER=' + cluster + ' AUTOSCALE=1 AUTOBUILD=1 ROLES=' + role
try:
reservation = self.ec2.run_instances(image_id=imageId, key_name='ffe-ec2', security_groups=securityGroups,
instance_type='c3.xlarge', user_data=userData, block_device_map=map)
print "Launched " + reservation.id
reservation_ids.append((reservation.id))
except Exception as e:
print "Failed to launch instance for cluster: {0}".format(cluster)
print e
print "-------------------------------"
print "Waiting for builds to complete"
print "-------------------------------"
while status == 0:
time.sleep(10)
if time.time() >= timelimit:
for r_id in reservation_ids:
reservation_ids.remove(r_id)
failed_ids.append(r_id)
for r_id in reservation_ids:
for row in chef.Search('node', 'ec2_reservation_id:' + r_id + " AND chef_environment:stage", 1):
node = chef.Node(row.object.name)
if node is not None and len(row) > 0:
print node['ec2']['instance_id'] + " => " + node['cluster']
reservation_ids.remove(r_id)
instance_ids.append((node['ec2']['instance_id']))
if not reservation_ids:
status=1
return instance_ids
def create_images(self, stopped):
"""
Creates AMI images for the specified instances.
:param stopped: List of instances in the stopped state.
"""
completed = []
ami_ids = []
status = 0
now = time.time()
timelimit = now + self.threshold
print "-------------------------------"
print "Starting AMI imaging..."
print "-------------------------------"
timestamp = datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
for instance_id in stopped:
for row in chef.Search('node', 'ec2_instance_id:' + instance_id + " AND chef_environment:stage", 1):
time.sleep(1)
node = chef.Node(row.object.name)
if node is not None and len(row) > 0:
try:
ami = self.ec2.create_image(instance_id.encode('ascii'),node['cluster'] + "-autoscale-" + timestamp)
ami_ids.append((ami, node['cluster']))
except Exception as e:
print "Failed to issue create_image for {0}".format(instance_id)
print e
while status == 0:
for ami, cluster in ami_ids:
time.sleep(5)
ami_status = self.ec2.get_image(ami.encode('ascii'))
ami_status.update
if ami_status.state == 'available':
print ami + " completed."
completed.append((ami, cluster))
ami_ids.remove((ami, cluster))
if time.time() >= timelimit:
for ami, cluster in ami_ids:
ami_ids.remove((ami, cluster))
failed_ids.append(ami)
if not ami_ids:
status=1
return completed
def terminate(self, instance_id):
"""
Terminates the supplied instance ID.
:param instance_id: Instance ID to terminate.
:return: True or False
"""
try:
self.ec2.terminate_instances(instance_ids=instance_id)
except Exception as e:
print "Failed to terminate instance id {0}".format(instance_id)
print e
def main():
autoscale = asg()
cluster_data = autoscale.build_list()
instance_ids = autoscale.build_servers(cluster_data)
terminate_ids = copy.copy(instance_ids)
stopped = autoscale.stop_servers(instance_ids)
completed = autoscale.create_images(stopped)
autoscale.cleanup(terminate_ids)
print "Run complete."
print "SUMMARY:"
print "-------------------------------"
print " AMI => CLUSTER "
print "-------------------------------"
for ami, cluster in completed:
print ami + " => " + cluster
print "-------------------------------"
print "FAILED BUILDS/AMIS"
print "-------------------------------"
for failed_id in failed_ids:
print failed_id
print "-------------------------------"
if __name__ == "__main__":
main()