13
13
from common import save_to_file , load_from_file
14
14
from s3 import get_all_buckets , reformat_buckets_data
15
15
from vpc import get_all_vpcs , delete_orphan_vpcs
16
+ from cloudformation import delete_stacks
16
17
17
18
# number of days to qualify an instance as old
18
19
OLD_INSTANCE_THRESHOLD = 30
@@ -92,9 +93,14 @@ def delete_vpcs():
92
93
deleted_vpcs = delete_orphan_vpcs (vpcs )
93
94
return deleted_vpcs
94
95
95
- def get_old_instances_email_summary (oldInstancesSheet , allInstancesSheet ):
96
+ def get_old_instances_email_summary (oldInstancesSheet , allInstancesSheet , summarySheet ):
96
97
sheet_link = os .environ ['SHEET_LINK' ]
97
98
old_instances = prepare_old_instances_data (allInstancesSheet , oldInstancesSheet )
99
+ if summarySheet is not None :
100
+ try :
101
+ total_ec2_deleted = summarySheet .read_custom ('J1' , 'J1' )[0 ][0 ]
102
+ except :
103
+ total_ec2_deleted = None
98
104
message = """
99
105
All,<br>
100
106
<br>
@@ -125,7 +131,7 @@ def get_old_instances_email_summary(oldInstancesSheet, allInstancesSheet):
125
131
126
132
inst_id = inst .get ('InstanceId' , '' )
127
133
owner = inst .get ('owner' , 'OwnerNotFound' )
128
- if owner != 'OwnerNotFound' :
134
+ if owner != 'OwnerNotFound' and owner != "" :
129
135
guid = inst .get ('guid' , '' )
130
136
if owner in unique_owners :
131
137
unique_owners [owner ]['count' ] += 1
@@ -142,9 +148,11 @@ def get_old_instances_email_summary(oldInstancesSheet, allInstancesSheet):
142
148
summary_email += "- {} unsaved instances owned by {} associated with GUIDs <b>{}</b><br>" \
143
149
.format (v .get ('count' ), k , v .get ('guids' ))
144
150
if len (orphan_instances ) > 0 :
145
- summary_email += "\n Following instances could not be associated with owners:\n "
151
+ summary_email += "<br><br> Following instances could not be associated with owners:<br> "
146
152
for inst in orphan_instances :
147
153
summary_email += "- Instance Id : {}, Region : {}" .format (inst ['InstanceId' ], inst ['AvailabilityZone' ])
154
+ if total_ec2_deleted is not None :
155
+ summary_email += "<br><br> Total EC2 instances deleted so far: <b>{}</b><br>" .format (str (total_ec2_deleted ))
148
156
return message .format (sheet_link , sheet_link , scheduled , summary_email )
149
157
150
158
if __name__ == "__main__" :
@@ -235,9 +243,10 @@ def get_old_instances_email_summary(oldInstancesSheet, allInstancesSheet):
235
243
elif args [1 ] == 'purge_instances' :
236
244
numberOfInstancesDeleted = terminate_instances (oldInstancesSheet , allInstancesSheet )
237
245
summaryRow ['EC2 Cleanup' ] = 'Deleted {} instances' .format (numberOfInstancesDeleted )
238
-
246
+ delete_stacks ()
247
+
239
248
elif args [1 ] == 'generate_ec2_deletion_summary' :
240
- summaryEmail = get_old_instances_email_summary (oldInstancesSheet , allInstancesSheet )
249
+ summaryEmail = get_old_instances_email_summary (oldInstancesSheet , allInstancesSheet , summarySheet )
241
250
if summaryEmail is not None :
242
251
smtp_addr = os .environ ['SMTP_ADDR' ]
243
252
smtp_username = os .environ ['SMTP_USERNAME' ]
0 commit comments