-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrp.py
594 lines (516 loc) · 21.5 KB
/
drp.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
import boto3
import logging
import json
import sys
import concurrent.futures
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
#################################################################
# #
# Title: drp_testing #
# Author: gmariette #
# Date: 17/03/2021 #
# Language: Python3 #
# Purpose: Fall AWS AZ of one region one by one #
# Scenario: #
# - Identify and modify all ASG to remove an AZ #
# - Terminate all EC2 of an AZ #
# - Failover a db if needed #
# - Create an NACL which deny all [in|e]gress of our AZ #
# - Rollback #
# #
#################################################################
# Setting the default log to info
# Configuring the output formatter
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
def waitUserInput():
answer = input('Do you want to continue ? (Y/N) \n')
if answer in ['y', 'Y']:
return True
elif answer in ['n', 'N']:
print('Exiting !')
sys.exit(0)
else:
waitUserInput()
class DRP:
def __init__(self, env):
self.aws_accounts = {
"MY_ACCOUNT": { "id": "xxxx", "role": "iam-role-name", "region": "eu-west-3" }
}
self.env = env
self.logger = logging.getLogger('DRP')
self.asg_backup = {}
self.nacl_backup = {}
self.df = pd.DataFrame()
def dumpConfigToDisk(self, comp, az):
'''
DOCSTRING: Dump config file to disk as backup
INPUT: Composant to backup
OUTPUT: None
'''
filename = self.env + '_backup_' + comp + '_' + az + '.json'
self.logger.info('Dumping backuped %ss config to file %s',comp.upper(), filename)
with open(filename, 'w+') as f:
if comp == 'asg':
f.write(json.dumps(self.asg_backup))
if comp == 'nacl':
f.write(json.dumps(self.nacl_backup))
def addActionToDf(self, action):
'''
DOCSTRING: Add action to DataFrame
INPUT: Action
OUTPUT: None
'''
changes = {
"action": action,
"timestamp": datetime.now().strftime("%d/%m/%Y %H:%M:%S")
}
self.df = self.df.append(changes, ignore_index=True)
self.logger.debug('Action %s has been added to the df', action)
def dumpDfToDisk(self):
self.df.to_json(self.env + '_df.json')
def graphPlots(self):
'''
DOCSTRING: Graph plot with our DF
INPUT: None
OUTPUT: None
'''
self.df.plot(kind="scatter", x="timestamp", y="action")
self.logger.info('Saving the plots to disk')
plt.savefig(self.env + 'timeline.jpg', bbox_inches='tight')
def assumeRole(self, action):
client = self.initStsClient()
assumedRoleObject = client.assume_role(
RoleArn="arn:aws:iam::"+self.aws_accounts[self.env]["id"]+":role/"+self.aws_accounts[self.env]["role"],
RoleSessionName="Drp-" + action
)
credentials=assumedRoleObject['Credentials']
ACCESS_KEY=credentials.get("AccessKeyId")
SECRET_KEY=credentials.get("SecretAccessKey")
SESSION_TOKEN=credentials.get("SessionToken")
return ACCESS_KEY, SECRET_KEY, SESSION_TOKEN
def initec2client(self, action):
'''
DOCSTRING: Return the ec2 client
INPUT: None
OUTPUT: boto3.client('ec2')
'''
ACCESS_KEY, SECRET_KEY, SESSION_TOKEN = self.assumeRole(action)
self.logger.debug('Launching ec2 client with role %s on the account %s', self.aws_accounts[self.env]["role"], self.aws_accounts[self.env]["id"])
return boto3.Session(
region_name=self.aws_accounts[self.env]["region"],
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY,
aws_session_token=SESSION_TOKEN
).client('ec2')
def initStsClient(self):
return boto3.client('sts')
def initeRdsClient(self, action):
'''
DOCSTRING: Return the rds client
INPUT: None
OUTPUT: boto3.client('rds')
'''
ACCESS_KEY, SECRET_KEY, SESSION_TOKEN = self.assumeRole(action)
self.logger.debug('Launching rds client with role %s on the account %s', self.aws_accounts[self.env]["role"], self.aws_accounts[self.env]["id"])
return boto3.Session(
region_name=self.aws_accounts[self.env]["region"],
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY,
aws_session_token=SESSION_TOKEN
).client('rds')
def initeASGClient(self, action):
'''
DOCSTRING: Return the autoscaling client
INPUT: None
OUTPUT: boto3.client('autoscaling')
'''
ACCESS_KEY, SECRET_KEY, SESSION_TOKEN = self.assumeRole(action)
self.logger.debug('Launching asg client with role %s on the account %s', self.aws_accounts[self.env]["role"], self.aws_accounts[self.env]["id"])
return boto3.Session(
region_name=self.aws_accounts[self.env]["region"],
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY,
aws_session_token=SESSION_TOKEN
).client('autoscaling')
def describeAZ(self):
'''
DOCSTRING: Return the AZ list for a region
INPUT: None
OUTPUT: AZ list
'''
# Client init
client = self.initec2client("DescribeAZ")
response = client.describe_availability_zones()['AvailabilityZones']
return [ x['ZoneName'] for x in response ]
def describeInstances(self, az=None):
'''
DOCSTRING: List instances running in an AZ
INPUT: Optionnal: az
OUTPUT: Instances id
'''
# Client init
client = self.initec2client("DescribeEC2")
# Put some filters ON
filters = []
filters.append( { 'Name': 'instance-state-name', 'Values': ['running' ] } )
if az is not None:
self.logger.info('Identifying ec2 instances running on az: %s', az)
filters.append( { 'Name': 'availability-zone', 'Values': [ az ] } )
else:
self.logger.info('Identifying ec2 instances running')
# Init our return list
instance_id_list = []
response = client.describe_instances(
Filters=filters,
)['Reservations']
for item in response:
for instance in item['Instances']:
instance_id_list.append(instance['InstanceId'])
for tag in instance['Tags']:
if tag['Key'] == 'Name':
self.logger.info('Instance id %s (%s) running on az %s', instance['InstanceId'], tag['Value'], az)
if instance_id_list:
self.logger.info('Found %s ec2 instances running on az: %s', len(instance_id_list), az)
return instance_id_list
self.logger.info('No ec2 instances running !')
return False
def terminateInstance(self, instances_list):
'''
DOCSTRING: Terminate ec2 instances
INPUT: instance_list
OUTPUT: None
'''
# Client init
client = self.initec2client("DescribeEC2")
if not instances_list:
self.logger.error('instance_list parameter not set !')
self.logger.info('Killing instances %s', ', '.join(instances_list))
response = client.terminate_instances(
InstanceIds=instances_list,
)['TerminatingInstances']
def describeASG(self):
'''
DOCSTRING: List available ASGs
INPUT: None
OUTPUT: ASGs available
'''
# Client init
client = self.initeASGClient("DescribeASG")
return client.describe_auto_scaling_groups()['AutoScalingGroups']
def saveASGConfig(self):
'''
DOCSTRING: Backup the ASGs configs
INPUT: None
OUTPUT: None
'''
self.logger.info('Backuping the current ASG state')
for asg in self.describeASG():
if self.asg_backup.get(asg['AutoScalingGroupName']) is None:
self.asg_backup[asg['AutoScalingGroupName']] = {}
self.asg_backup[asg['AutoScalingGroupName']]['AvailabilityZones'] = asg['AvailabilityZones']
self.asg_backup[asg['AutoScalingGroupName']]['VPCZoneIdentifier'] = asg['VPCZoneIdentifier']
self.asg_backup[asg['AutoScalingGroupName']]['Modified'] = False
def restoreASGConfig(self):
'''
DOCSTRING: Restore the asg configs
INPUT: None
OUTPUT: None
'''
self.logger.info('Restoring initial ASG config')
for asg in self.asg_backup:
if self.asg_backup[asg]['Modified']:
self.logger.info('Restoring ASG %s', asg)
self.updateASGAZ(asg, self.asg_backup[asg]['AvailabilityZones'], self.asg_backup[asg]['VPCZoneIdentifier'])
def updateASGAZ(self, asg, azs, vpc_zone_identifier):
'''
DOCSTRING: Update AZ for an asg
INPUT: None
OUTPUT: None
'''
# Client init
client = self.initeASGClient("UpdateASG")
self.logger.info('Modifying ASG %s - Setting new AZs: %s', asg, azs)
response = client.update_auto_scaling_group(
AutoScalingGroupName=asg,
AvailabilityZones=azs,
VPCZoneIdentifier=vpc_zone_identifier
)
self.asg_backup[asg]['Modified'] = True
return response
def describeSubnets(self, az):
'''
DOCSTRING: Describe subnets based on an az
INPUT: None
OUTPUT: Subnets
'''
# Client init
client = self.initec2client("DescribeSubnets")
subnet_list = []
response = client.describe_subnets(
Filters=[
{
'Name': 'availabilityZone',
'Values': [
az,
]
}
]
)['Subnets']
for item in response:
subnet_list.append(item['SubnetId'])
return subnet_list
def describeVPC(self):
'''
DOCSTRING: Describe VPC
INPUT: None
OUTPUT: VPCID
'''
# Client init
client = self.initec2client("DescribeVPC")
response = client.describe_vpcs()['Vpcs'][0]['VpcId']
return response
def describeNACL(self, subnets_list):
'''
DOCSTRING: Create an NACL
INPUT: None
OUTPUT: None
'''
# Client init
client = self.initec2client("DescribeNACL")
nacl_association_ids = []
response = client.describe_network_acls(
Filters=[
{
'Name': 'association.subnet-id',
'Values': subnets_list
},
]
)['NetworkAcls'][0]['Associations']
self.nacl_backup = response
initial_nacl_id = ""
for item in response:
if item['SubnetId'] in subnets_list:
nacl_association_ids.append({"NetworkAclAssociationId" : item['NetworkAclAssociationId'], "NetworkAclId": item['NetworkAclId'], 'SubnetId': item['SubnetId']})
if initial_nacl_id == "":
initial_nacl_id = item['NetworkAclId']
if initial_nacl_id == item['NetworkAclId']:
continue
if initial_nacl_id != item['NetworkAclId']:
self.logger.error('More than one NACL found !')
return initial_nacl_id, nacl_association_ids
def createNACL(self, vpc_id, az):
'''
DOCSTRING: Create an NACL
INPUT: None
OUTPUT: None
'''
# Client init
client = self.initec2client("CreateNACL")
response = client.create_network_acl(
VpcId=vpc_id,
TagSpecifications=[
{
'ResourceType': 'network-acl',
'Tags': [
{
'Key': 'Name',
'Value': 'DRP-NACL-FOR-AZ-'+ az.upper()
},
]
}
]
)['NetworkAcl']['NetworkAclId']
self.logger.info('NACL created: %s', response)
return response
def decribeNACLEntries(self, nacl_id):
'''
DOCSTRING: Describe NACL entries
INPUT: None
OUTPUT: None
'''
# Client init
client = self.initec2client("decribeNACLEntries")
response = client.describe_network_acls(
NetworkAclIds=[nacl_id]
)['NetworkAcls']
return response
def createDenyAllNACLEntry(self, nacl_id, egress=False):
'''
DOCSTRING: Create a NACL entry
INPUT: None
OUTPUT: None
'''
# Client init
client = self.initec2client("createDenyAllNACLEntry")
if egress:
self.logger.info('Creating a DENY ALL rule (INGRESS) on nacl: %s', nacl_id)
else:
self.logger.info('Creating a DENY ALL rule (EGRESS) on nacl: %s', nacl_id)
response = client.create_network_acl_entry(
CidrBlock='0.0.0.0/0',
Egress=egress,
NetworkAclId=nacl_id,
PortRange={
'From': 0,
'To': 65535
},
Protocol='-1',
RuleAction='deny',
RuleNumber=100
)
if response['ResponseMetadata']['HTTPStatusCode'] == 200:
self.logger.info('Rule created successfully')
return True
else:
self.logger.error('Problem while creating rule')
return False
def replaceNACLAssociation(self, nacl_id, association_id):
'''
DOCSTRING: Replace NACL association
INPUT: nacl_id, association_id
OUTPUT: None
'''
# Client init
client = self.initec2client("ReplaceNACLAssociation")
self.logger.info('Changing NACL association id %s to the NACL id %s', association_id, nacl_id)
response = client.replace_network_acl_association(
AssociationId=association_id,
NetworkAclId=nacl_id
)['NewAssociationId']
self.logger.info('New association id: %s', response)
return response
def deleteNACL(self, nacl_id):
'''
DOCSTRING: Delete an NACL
INPUT: None
OUTPUT: None
'''
# Client init
client = self.initec2client("createDenyAllNACLEntry")
response = client.delete_network_acl(
NetworkAclId=nacl_id
)
if response['ResponseMetadata']['HTTPStatusCode'] == 200:
self.logger.info('Deleted NACL %s', nacl_id)
else:
self.logger.error('Problem while deleting NACL %s', nacl_id)
def describeRDSinstances(self):
'''
DOCSTRING: List available rds instances
INPUT: None
OUTPUT: RDS db available
'''
# Client init
client = self.initeRdsClient("Describe")
self.logger.info('Describe RDS instance for env %s requested', self.env)
response = client.describe_db_instances()['DBInstances']
env_filter = ''.join(self.env.split('-')).lower()
db_list = { x['DBInstanceIdentifier'] : { 'MultiAZ': x['MultiAZ'], 'Subnet': x['AvailabilityZone'] } for x in response if env_filter in x['DBInstanceIdentifier'] }
return db_list
def restartRDSinstance(self, db_identifier, failover):
'''
DOCSTRING: Restart a db instance and force AZ failover
INPUT: None
OUTPUT: RDS db available
'''
# Client init
client = self.initeRdsClient("Restart")
self.logger.info('Restarting the RDS instance %s', db_identifier)
response = client.reboot_db_instance(
DBInstanceIdentifier=db_identifier,
ForceFailover=failover
)
def waitRDSavailable(self, db_identifier):
# Client init
client = self.initeRdsClient("Waiter")
waiter = client.get_waiter('db_instance_available')
self.logger.info('Waiting the good start of %s', db_identifier)
waiter.wait(
DBInstanceIdentifier=db_identifier
)
if __name__ == "__main__":
c = DRP("MY_ACCOUNT")
for az in c.describeAZ():
main_logger = logging.getLogger('MAIN')
main_logger.info('Begin of the operations on az: %s', az)
waitUserInput()
c.addActionToDf("[{} / {}] Begin of the operations".format(c.env, az))
# 0) Create a new az_list without our AZ
remaining_az = [ x for x in c.describeAZ() if x != az]
# 1) Identify the subnets from the az
subnet_list = c.describeSubnets(az)
main_logger.info('Following subnets (%s) will be removed from ASGs: %s',len(subnet_list), ' - '.join(subnet_list))
# 2) Create a NACL
# 2a) Identify VPC id
vpc_id = c.describeVPC()
# 2b) Creation of the network ACL
main_logger.info('Creating a network ACL to block all trafic from %s', az)
c.addActionToDf("[{} / {}] NACL creation".format(c.env, az))
drp_network_acl = c.createNACL(vpc_id, az)
# 2c) Get the current list of network ACL associations for these subnets
initial_nacl_id, initial_nacl_association_ids = c.describeNACL(subnet_list)
c.dumpConfigToDisk('nacl', az)
# 2d) Associate desirated subnets with new network ACL
new_nacl_association = []
for nacl_association_id in initial_nacl_association_ids:
new_nacl_association.append({"NetworkAclAssociationId" : c.replaceNACLAssociation(drp_network_acl, nacl_association_id['NetworkAclAssociationId']), "NetworkAclId": drp_network_acl, 'SubnetId': nacl_association_id['SubnetId']})
if c.createDenyAllNACLEntry(drp_network_acl):
main_logger.info('Ingress rule DENY all created for NACL %s', drp_network_acl)
if c.createDenyAllNACLEntry(drp_network_acl, egress=True):
main_logger.info('Egress rule DENY all created for NACL %s', drp_network_acl)
# 3a) Saving the ASGs configs
c.saveASGConfig()
c.addActionToDf("[{} / {}] Save of ASGs configs".format(c.env, az))
c.dumpConfigToDisk('asg', az)
# 3b) Update the ASGs by removing the subnets
for item in c.describeASG():
if c.env.replace('-','') in item['AutoScalingGroupName']:
asg_subnets = (item['VPCZoneIdentifier'].split(','))
common_subnet = ''.join([ x for x in asg_subnets if x in subnet_list])
new_subnet_list = ','.join([ x for x in asg_subnets if x not in subnet_list])
main_logger.info('The subnet %s will be removed from ASG %s', item['AutoScalingGroupName'], common_subnet)
c.updateASGAZ(item['AutoScalingGroupName'], remaining_az, new_subnet_list)
c.addActionToDf("[{} / {}] Update of ASG {} config".format(c.env, az, item['AutoScalingGroupName']))
# 4) Terminating instances on our AZ (force kill in case they are not part of an ASG !)
instances_list = c.describeInstances(az)
if instances_list:
c.terminateInstance(instances_list)
for instance in instances_list:
c.addActionToDf("[{} / {}] Terminating instance {}".format(c.env, az, instance))
# 5) Trigger DBs failovers which are in the AZ we want to stop (multithreaded)
db_list = c.describeRDSinstances()
restarted_dbs = []
with concurrent.futures.ThreadPoolExecutor() as executor:
for db in db_list:
if db_list[db]['Subnet'] == az:
main_logger.info('Database %s is in fall AZ !', db)
if db_list[db]['MultiAZ']:
main_logger.info('Triggering a db failover.')
c.addActionToDf("[{} / {}] Failover DB {}".format(c.env, az, db))
threadStartRDS = executor.submit(c.restartRDSinstance, db, True)
restarted_dbs.append(db)
else:
main_logger.error('This database is not multi AZ !')
if restarted_dbs:
with concurrent.futures.ThreadPoolExecutor() as executor:
for db in restarted_dbs:
threadWaitRDS = executor.submit(c.waitRDSavailable, db)
### ROLLBACK AZ
main_logger.info('Begin rollback of operations made on az: %s', az)
waitUserInput()
c.addActionToDf("[{} / {}] Begin rollback of the operations".format(c.env, az))
# 1) Restore NACL parameters
c.addActionToDf("[{} / {}] NACL rollback".format(c.env, az))
for nacl_association_id in new_nacl_association:
main_logger.info('Restoring subnet %s to main NACL (%s)', nacl_association_id['SubnetId'], initial_nacl_id)
c.replaceNACLAssociation(initial_nacl_id, nacl_association_id['NetworkAclAssociationId'])
# 2) Delete DRP_NACL
c.deleteNACL(drp_network_acl)
# 3) Restore ASG config
c.restoreASGConfig()
c.addActionToDf("[{} / {}] Restore ASGs configs".format(c.env, az))
c.dumpDfToDisk()
# Dump the DF thing
c.graphPlots()