-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresources.py
More file actions
725 lines (576 loc) · 23.9 KB
/
resources.py
File metadata and controls
725 lines (576 loc) · 23.9 KB
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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
__author__ = 'sebalopez'
import arn
import datetime
import dateutil.parser
import itertools
import logging
import boto3
import boto3_clients as clients
from botocore.exceptions import ClientError
from dateutil.tz import tzutc
logger = logging.getLogger(__name__)
class Resource(object):
def __init__(self, obj_id, obj_data):
self._id = obj_id
self._data = obj_data
self._tags = None
@property
def tags(self):
if self._tags is None:
if hasattr(self, '_describe_tags_func'):
describe_tags_fn = getattr(getattr(clients, self._type), self._describe_tags_func)
self._tags = {t['Key']: t['Value']
for t in self._get_tag_obj_from_api_response(
describe_tags_fn(**self._describe_tags_params))}
else:
self._tags = {t['Key']: t['Value'] for t in self._data.get('Tags', [])}
return self._tags
@property
def created_time(self):
raise NotImplementedError
@property
def name(self):
try:
return self.tags['Name']
except:
return ''
@property
def type(self):
return self._type
@property
def vpc(self):
raise NotImplementedError
@property
def groups(self):
return NotImplemented
def default_sg(self):
return clients.ec2.describe_security_groups(Filters=[
{'Name': 'vpc-id', 'Values': [self.vpc]},
{'Name': 'group-name', 'Values': ['default']}
])['SecurityGroups'][0]['GroupId']
def terminate(self):
terminate_fn = getattr(getattr(clients, self._type), self._terminate_func)
return terminate_fn(**self._terminate_params)
def __str__(self):
obj_str = '[%s]:%s' % (self.type, self._id)
if self.name:
obj_str += ' (%s)' % self.name
return obj_str
class EC2_Resource(Resource):
_type = 'ec2'
_default_sg = {}
_terminate_func = 'terminate_instances'
@classmethod
def get_default_sg(self, vpc):
if not EC2_Resource._default_sg:
logger.info('Retrieving default security groups')
groups = clients.ec2.describe_security_groups(
Filters=[{'Name': 'group-name', 'Values': ['default']}])['SecurityGroups']
for g in groups:
EC2_Resource._default_sg.setdefault(g['VpcId'], g['GroupId'])
if vpc in EC2_Resource._default_sg:
return EC2_Resource._default_sg[vpc]
else:
logger.error('Could not find default security group for vpc %s', vpc)
return None
def __init__(self, _id, _data=None):
self._terminate_params = {'InstanceIds': [_id], 'DryRun': False}
super(EC2_Resource, self).__init__(_id, _data)
@property
def vpc(self):
return self._data['VpcId']
@property
def status(self):
return self._data['State']['Name']
@property
def created_time(self):
return self._data['LaunchTime']
@property
def groups(self):
return [sg['GroupId'] for sg in self._data['SecurityGroups']]
@groups.setter
def groups(self, sgroups, **kwargs):
clients.ec2.modify_instance_attribute(InstanceId=self._id, Groups=sgroups)
self._data['SecurityGroups'] = [{'GroupName': 'default', 'GroupId': g} for g in sgroups]
def delete_tags(self, keys, **kwargs):
return clients.ec2.delete_tags(Resources=[self._id],
Tags=[{'Key': k} for k in keys],
**kwargs)
def terminate(self, clear_sg=True, DryRun=False):
if clear_sg:
logger.info('Setting security groups for instance %s to %s', self._id,
[EC2_Resource.get_default_sg(self.vpc)])
self.groups = [EC2_Resource.get_default_sg(self.vpc)]
# clear the Termination Protection first
clients.ec2.modify_instance_attribute(InstanceId=self._id,
DisableApiTermination={'Value': False},
DryRun=DryRun)
self._terminate_params['DryRun'] = DryRun
super(EC2_Resource, self).terminate()
def shutdown(self, DryRun=False):
clients.ec2.stop_instances(InstanceIds=[self._id], DryRun=DryRun)
@property
def uptime(self):
if self.status == 'running':
try:
return datetime.datetime.utcnow() - self.created_time.replace(tzinfo=None)
except ValueError:
return datetime.timedelta(0)
else:
return datetime.timedelta(0)
@property
def downtime(self):
if self.status == 'stopped':
try:
shutdown_time = datetime.datetime.strptime(
self._data['StateTransitionReason'].split('(')[1].split(' GMT)')[0],
'%Y-%m-%d %H:%M:%S'
).replace(tzinfo=None)
return datetime.datetime.utcnow() - shutdown_time
except ValueError:
return datetime.timedelta(0)
else:
return datetime.timedelta(0)
class RDS_Resource(Resource):
_type = 'rds'
_terminate_func = 'delete_db_instance'
_describe_tags_func = 'list_tags_for_resource'
def __init__(self, _id, _data=None):
self._terminate_params = {'DBInstanceIdentifier': _id, 'SkipFinalSnapshot': True}
self._name = _id
self._describe_tags_params = {'ResourceName': self.arn}
super(RDS_Resource, self).__init__(self.arn, _data)
@property
def name(self):
return self._data['DBInstanceIdentifier']
@property
def arn(self):
return arn.rds_instance(self._name)
@property
def vpc(self):
return self._data['DBSubnetGroup']['VpcId']
@property
def groups(self):
return [sg['VpcSecurityGroupId'] for sg in self._data['VpcSecurityGroups']]
@groups.setter
def groups(self, sgroups):
clients.rds.modify_db_instance(DBInstanceIdentifier=self._name, VpcSecurityGroupIds=sgroups)
self._data['VpcSecurityGroups'] = [{'Status': 'active', 'VpcSecurityGroupId': g}
for g in sgroups]
@property
def created_time(self):
return self._data['InstanceCreateTime']
@property
def status(self):
return self._data['DBInstanceStatus']
def _get_tag_obj_from_api_response(self, obj):
return obj['TagList']
def delete_tags(self, keys):
return clients.rds.remove_tags_from_resource(ResourceName=self._id, TagKeys=keys)
def shutdown(self, DryRun=False):
if DryRun:
raise ClientError({'Error': {'Code': 'DryRunOperation'}}, "")
else:
clients.rds.stop_db_instance(DBInstanceIdentifier=self.name)
def last_event(self):
# RDS only tracks events for 14 days
look_back_hours = 335
now = datetime.datetime.utcnow()
try:
return clients.rds.describe_events(
SourceIdentifier=self.name,
SourceType='db-instance',
StartTime=now - datetime.timedelta(hours=look_back_hours),
EndTime=now,
EventCategories=['creation', 'availability', 'notification', 'deletion']
)['Events'][-1]
except IndexError:
return 'NA'
@property
def uptime(self):
if self.status == 'available':
if self.last_event() == 'NA':
return datetime.timedelta(days=14)
else:
now = datetime.datetime.utcnow()
return now-self.last_event()['Date'].replace(tzinfo=None)
else:
return datetime.timedelta(0)
@property
def downtime(self):
if self.status == 'stopped':
if self.last_event() == 'NA':
return datetime.timedelta(days=14)
else:
now = datetime.datetime.utcnow()
return now-self.last_event()['Date'].replace(tzinfo=None)
else:
return datetime.timedelta(0)
def terminate(self, DryRun=False):
if DryRun:
raise ClientError({'Error': {'Code': 'DryRunOperation'}}, "")
else:
return super(RDS_Resource, self).terminate()
class RDSAurora_Resource(Resource):
_type = 'rds'
_terminate_func = 'delete_db_cluster'
def __init__(self, _id, _data=None):
super(RDSAurora_Resource, self).__init__(_id, _data)
self._terminate_params = {'DBClusterIdentifier': _id, 'SkipFinalSnapshot': True}
class ELB_Resource(Resource):
_type = 'elb'
_terminate_func = 'delete_load_balancer'
_describe_tags_func = 'describe_tags'
def __init__(self, _id, _data=None):
self._terminate_params = {'LoadBalancerName': _id}
self._describe_tags_params = {'LoadBalancerNames': [_id]}
super(ELB_Resource, self).__init__(_id, _data)
self._name = _id
@property
def vpc(self):
return self._data['VPCId']
@property
def groups(self):
return self._data['SecurityGroups']
@groups.setter
def groups(self, sggroups):
clients.elb.apply_security_groups_to_load_balancer(
LoadBalancerName=self._id, SecurityGroups=sggroups)
@property
def created_time(self):
return self._data['CreatedTime']
def _get_tag_obj_from_api_response(self, obj):
return obj['TagDescriptions'][0]['Tags']
class ELBv2_Resource(Resource):
_type = 'elbv2'
_terminate_func = 'delete_load_balancer'
_describe_tags_func = 'describe_tags'
def __init__(self, _id, _data=None):
self._terminate_params = {'LoadBalancerArn': _id}
self._describe_tags_params = {'ResourceArns': [_id]}
super(ELBv2_Resource, self).__init__(_id, _data)
@property
def vpc(self):
return self._data['VpcId']
@property
def groups(self):
return self._data['SecurityGroups']
@groups.setter
def groups(self, sggroups):
clients.elbv2.set_security_groups(LoadBalancerArn=self._id, SecurityGroups=sggroups)
@property
def created_time(self):
return self._data['CreatedTime']
def _get_tag_obj_from_api_response(self, obj):
return obj['TagDescriptions'][0]['Tags']
def terminate(self):
# clear deletion protection first
clients.elbv2.modify_load_balancer_attributes(
LoadBalancerArn=self._id, Attributes=[{'Key': 'deletion_protection.enabled',
'Value': 'false'}])
return super(ELBv2_Resource, self).terminate()
class AutoScalingGroup_Resource(Resource):
_type = 'autoscaling'
# NOTE: Deleting an AutoScaling Group automatically terminates all the instances under it.
# The Group cannot be deleted otherwise
_terminate_func = 'delete_auto_scaling_group'
def __init__(self, _id, _data=None):
self._terminate_params = {'AutoScalingGroupName': _id, 'ForceDelete': True}
super(AutoScalingGroup_Resource, self).__init__(_id, _data)
if _data is not None:
self.data = _data
else:
try:
self.data = clients.autoscaling.describe_auto_scaling_groups(AutoScalingGroupNames=[_id])['AutoScalingGroups'][0]
except IndexError:
logger.warn('no data found for group %s' %_id)
self.data = {}
self.valid_tags = []
@property
def name(self):
return self._id
@property
def created_time(self):
return self._data['CreatedTime']
def suspend(self, DryRun=False):
if DryRun:
raise ClientError({'Error': {'Code': 'DryRunOperation'}}, "")
else:
return clients.autoscaling.suspend_processes(
AutoScalingGroupName=self._id,
ScalingProcesses=['Launch', 'Terminate', 'ReplaceUnhealthy']
)
def terminate(self, DryRun=False):
if DryRun:
raise ClientError({'Error': {'Code': 'DryRunOperation'}}, "")
else:
return super(RDS_Resource, self).terminate()
# Shutdown for an AutoScaling group is taken as setting its desired capacity to 0.
# This terminates all its instances while not deleting the group itself
def shutdown(self, DryRun=False):
if DryRun:
raise ClientError({'Error': {'Code': 'DryRunOperation'}}, "")
else:
return clients.autoscaling.set_desired_capacity(AutoScalingGroupName=self.name, DesiredCapacity=0)
# This returns if the group's Launch process is suspended and when.
# It indicates whether stopping or deleting its instances would trigger a new one
@property
def scaling_state(self):
for process in self._data['SuspendedProcesses']:
if process['ProcessName'] == 'Launch':
return {
'Status': 'Suspended',
'SuspensionTime': datetime.datetime.strptime(
process['SuspensionReason'].split(' at ')[1], '%Y-%m-%dT%H:%M:%SZ'
)
}
return {
'Status': 'Active'
}
# Downtime for an ASG is the amount of time since it latest Terminate activity when the group size is 0
# if none found it will be the group creation time
@property
def downtime(self):
if self._data['DesiredCapacity'] == 0:
### TEMPORARYY SOOLUTION UNTIL WE CAN RESOLVE THE THROTTLING ISSUES
return datetime.datetime.utcnow() - self._data['CreatedTime'].replace(tzinfo=None)
# activities = clients.autoscaling.describe_scaling_activities(AutoScalingGroupName=self.name)['Activities']
# if len(activities) > 0:
# return datetime.datetime.utcnow() - activities[0]['StartTime'].replace(tzinfo=None)
# else:
# return datetime.datetime.utcnow() - self._data['CreatedTime'].replace(tzinfo=None)
else:
return datetime.timedelta(0)
# Uptime for an ASG will be the time since the last Launch activity if the group size is >0,
# if none found it will be the group creation time
@property
def uptime(self):
if self._data['DesiredCapacity'] == 0:
return datetime.timedelta(0)
else:
### TEMPORARYY SOOLUTION UNTIL WE CAN RESOLVE THE THROTTLING ISSUES
return datetime.datetime.utcnow() - self._data['CreatedTime'].replace(tzinfo=None)
# activities = clients.autoscaling.describe_scaling_activities(AutoScalingGroupName=self.name)['Activities']
# if len(activities) > 0:
# return datetime.datetime.utcnow() - activities[0]['StartTime'].replace(tzinfo=None)
# else:
# return datetime.datetime.utcnow() - self._data['CreatedTime'].replace(tzinfo=None)
class S3Bucket_Resource(Resource):
_type = 's3'
_describe_tags_func = 'get_bucket_tagging'
_terminate_func = 'delete_bucket'
def __init__(self, _id, _data=None):
self._describe_tags_params = {'Bucket': _id}
self._terminate_params = {'Bucket': _id}
super(S3Bucket_Resource, self).__init__(_id, _data)
self.valid_tags = []
@property
def created_time(self):
return self._data['CreationDate']
@property
def name(self):
return self._id
def _get_tag_obj_from_api_response(self, obj):
return obj['TagSet']
def terminate(self):
bucket = boto3.resource('s3').Bucket(self._id)
bucket.objects.all().delete()
bucket.object_versions.all().delete()
super(S3Bucket_Resource, self).terminate()
class LaunchConfiguration_Resource(Resource):
_type = 'autoscaling'
_terminate_func = 'delete_launch_configuration'
def _get_tag_obj_from_api_response(self, obj):
return []
def __init__(self, _id, _data=None):
self._terminate_params = {'LaunchConfigurationName': _id}
super(LaunchConfiguration_Resource, self).__init__(_id, _data)
class SecurityGroup_Resource(Resource):
_type = EC2_Resource._type
_terminate_func = 'delete_security_group'
def __init__(self, _id, _data=None):
self._terminate_params = {'GroupId': _id}
super(SecurityGroup_Resource, self).__init__(_id, _data)
self.isdefault = self.name == 'default'
@property
def vpc(self):
return self._data['VpcId']
@property
def name(self):
return self._data['GroupName']
@property
def type(self):
return 'securitygroup'
def terminate(self):
if self.name == 'default':
logger.warn("Refusing to remove default security group %s for VPC %s", self, self.vpc)
else:
try:
super(SecurityGroup_Resource, self).terminate()
except ClientError as e:
if e.response['Error']['Code'] == 'DependencyViolation':
logger.error('Could not delete security group: the group is still attached to other resources.')
else:
logger.exception('Failed to remove security group')
class EBSVolume_Resource(Resource):
_type = EC2_Resource._type
_terminate_func = 'delete_volume'
def __init__(self, _id, _data=None):
self._terminate_params = {'VolumeId': _id}
super(EBSVolume_Resource, self).__init__(_id, _data)
def terminate(self, DryRun=False):
self._terminate_params['DryRun'] = DryRun
super(EBSVolume_Resource, self).terminate()
class Snapshot_Resource(Resource):
_type = EC2_Resource._type
_terminate_func = 'delete_snapshot'
def __init__(self, _id, _data=None):
self._terminate_params = {'SnapshotId': _id}
super(Snapshot_Resource, self).__init__(_id, _data)
class AMI_Resource(Resource):
_type = EC2_Resource._type
_terminate_func = 'deregister_image'
@property
def name(self):
return self._data['Name']
@property
def created_time(self):
return dateutil.parser.parse(self._data['CreationDate'])
def __init__(self, _id, _data=None):
self._terminate_params = {'ImageId': _id}
super(AMI_Resource, self).__init__(_id, _data)
class ENI_Resource(Resource):
_type = EC2_Resource._type
_terminate_func = 'delete_network_interface'
def __init__(self, _id, _data=None):
self._terminate_params = {'NetworkInterfaceId': _id}
super(ENI_Resource, self).__init__(_id, _data)
class KeyPair_Resource(Resource):
_type = EC2_Resource._type
_terminate_func = 'delete_key_pair'
def __init__(self, _id, _data=None):
self._terminate_params = {'KeyName': _id}
super(KeyPair_Resource, self).__init__(_id, _data)
@property
def type(self):
return 'key'
class EFS_Resource(Resource):
_type = 'efs'
_terminate_func = 'delete_file_system'
_describe_tags_func = 'describe_tags'
def __init__(self, _id, _data=None):
self._terminate_params = {'FileSystemId': _id}
self._describe_tags_params = self._terminate_params
super(EFS_Resource, self).__init__(_id, _data)
self._mount_targets = self._get_mount_targets()
self._security_groups = self._get_security_groups()
self._vpc = None
def _get_mount_targets(self):
targets = []
try:
targets = clients.efs.describe_mount_targets(FileSystemId=self._id)['MountTargets']
except ClientError:
logger.exception('Failed to get mount targets for EFS %s', self._id)
return targets
def _get_security_groups(self):
groups = {}
for target in self._mount_targets:
try:
groups[target['MountTargetId']] = clients.efs.describe_mount_target_security_groups(
MountTargetId=target['MountTargetId'])['SecurityGroups']
except ClientError:
logger.exception('Failed to get security groups for mount target %s',
target['MountTargetId'])
return groups
@property
def created_time(self):
return self._data['CreationTime']
def _get_tag_obj_from_api_response(self, obj):
return obj['Tags']
@property
def name(self):
return self._data['Name']
@property
def vpc(self):
if self._vpc is None:
subnet_of_first_mount_target = self._mount_targets[0]['SubnetId']
vpc = clients.ec2.describe_subnets(SubnetIds=[subnet_of_first_mount_target])
self._vpc = vpc['Subnets'][0]['VpcId']
return self._vpc
@property
def groups(self):
return list(set(itertools.chain(*self._security_groups.values())))
@groups.setter
def groups(self, sggroups):
# To simplify, this assumes all mount points should get the same security groups.
for target in self._mount_targets:
try:
logger.info('Setting security groups for mount point %s', target['MountTargetId'])
clients.efs.modify_mount_target_security_groups(
MountTargetId=target['MountTargetId'], SecurityGroups=sggroups)
except ClientError:
logger.exception('Failed to set security groups')
def terminate(self):
if self._mount_targets:
for target in self._mount_targets:
logger.info('Deleting mount target %s', target['MountTargetId'])
try:
clients.efs.delete_mount_target(MountTargetId=target['MountTargetId'])
except ClientError:
logger.exception('Failed to delete mount target %s for filesystem %s',
target['MountTargetId'], self._id)
logger.error('Deletion of file system aborted, manual intervention is required')
logger.warn('The mount targets of filesystem %s were removed, but the FS itself was not. It will be removed next time if it still exists', self._id)
else:
super(EFS_Resource, self).terminate()
class DBSnapshot_Resource(Resource):
_type = RDS_Resource._type
_terminate_func = 'delete_db_snapshot'
_describe_tags_func = 'list_tags_for_resource'
@property
def created_time(self):
return self._data.get('SnapshotCreateTime', datetime.datetime.now(tzutc()))
@property
def arn(self):
return arn.rds_snapshot(self._id)
def __init__(self, _id, _data=None):
self._terminate_params = {'DBSnapshotIdentifier': _id}
super(DBSnapshot_Resource, self).__init__(_id, _data)
self._describe_tags_params = {'ResourceName': self.arn}
def _get_tag_obj_from_api_response(self, obj):
return obj['TagList']
class Lambda_Resource(Resource):
_type = 'lambda'
_terminate_func = 'delete_function'
_describe_tags_func = 'list_tags'
def __init__(self, _id, _data=None):
self._terminate_params = {'FunctionName': _id}
super(Lambda_Resource, self).__init__(_id, _data)
self._describe_tags_params = {'Resource': self.arn}
@property
def arn(self):
return arn.lambda_function(self._id)
@property
def name(self):
return self._id
@property
def tags(self):
if self._tags is None:
describe_tags_func = getattr(getattr(clients, self._type), self._describe_tags_func)
self._tags = describe_tags_func(**self._describe_tags_params)['Tags']
return self._tags
@property
def created_time(self):
return dateutil.parser.parse(self._data['LastModified'])
class CloudFormation_Resource(Resource):
_type = 'cloudformation'
_terminate_func = 'delete_stack'
def __init__(self, _id, _data=None):
super(CloudFormation_Resource, self).__init__(_id, _data)
self._terminate_params = {'StackName': self._id}
@property
def name(self):
return self._id
@property
def created_time(self):
return self._data.get('CreationTime', None)