-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdestroy-env.py
114 lines (98 loc) · 3.89 KB
/
destroy-env.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
#!/usr/bin/python
import os, json
# Measure how long the script takes
from datetime import datetime
startTime = datetime.now()
JSON_OUTPUT = 'output.json'
# AWS parameters
ELB_NAME = 'itmo544-elb'
AVAILABILITY_ZONE = os.popen('aws configure get region').read().replace('\n', '')
AMI_ID = 'ami-78c1ee1d'
# Autoscaling parameters
AUTOSCALING_CONFIGURATION = 'itmo544-autoscaling-config'
AUTOSCALING_GROUP = 'itmo544-autoscaling-group'
# RDS values
RDS_ID = 'itmo544-rds-fgarciadelacorte'
# S3 bucket values
S3_BUCKET_NAME = 'itmo544-image-bucket-fgarciadelacorte'
# SNS values
SNS_TOPIC = 'itmo544-topic-fgarciadelacorte'
def execCommand(commandString):
print('============================> Running command: ' + commandString + '\n')
os.system(commandString)
def extractJSON():
jsondict = {}
with open(JSON_OUTPUT) as jsonfile:
jsondict = json.load(jsonfile)
os.remove(JSON_OUTPUT)
return jsondict
# RDS
execCommand('aws rds delete-db-instance'
+ ' --db-instance-identifier ' + RDS_ID
+ ' --skip-final-snapshot')
# RDS ReadOnly replica
execCommand('aws rds delete-db-instance'
+ ' --db-instance-identifier read1-' + RDS_ID
+ ' --skip-final-snapshot')
# S3 buckets (empty and remove)
execCommand('aws s3 rm s3://' + S3_BUCKET_NAME +' --recursive')
execCommand('aws s3api delete-bucket'
+ ' --bucket ' + S3_BUCKET_NAME
+ ' --region ' + AVAILABILITY_ZONE)
execCommand('aws s3 rm s3://post-' + S3_BUCKET_NAME +' --recursive')
execCommand('aws s3api delete-bucket'
+ ' --bucket post-' + S3_BUCKET_NAME
+ ' --region ' + AVAILABILITY_ZONE)
# ELB
execCommand('aws elb delete-load-balancer'
+ ' --load-balancer-name ' + ELB_NAME)
# Autoscaling group (frontend)
execCommand('aws autoscaling delete-auto-scaling-group'
+ ' --auto-scaling-group-name ' + AUTOSCALING_GROUP
+ ' --force-delete')
execCommand('aws autoscaling delete-launch-configuration'
+ ' --launch-configuration-name ' + AUTOSCALING_CONFIGURATION)
# Other EC2 instances
execCommand('aws ec2 describe-instances'
+ ' --filters Name=image-id,Values=' + AMI_ID
+ ' > ' + JSON_OUTPUT)
ec2_instance_ids = [] # should be just one
for r in extractJSON()['Reservations']:
ec2_instance_ids.append(r['Instances'][0]['InstanceId'])
execCommand('aws ec2 terminate-instances'
+ ' --instance-ids ' + ' '.join(ec2_instance_ids))
# SQS
execCommand('aws sqs list-queues > ' + JSON_OUTPUT)
if (os.stat(JSON_OUTPUT).st_size > 0):
for queueURL in extractJSON()['QueueUrls']:
if 'itmo544-queue' in queueURL.split('/'):
execCommand('aws sqs get-queue-url'
+ ' --queue-name itmo544-queue'
+ ' > ' + JSON_OUTPUT)
execCommand('aws sqs delete-queue'
+ ' --queue-url ' + queueURL)
# SNS - we delete the right topic, in case any other topics exist, and unsubscribe all
execCommand('aws sns list-topics > ' + JSON_OUTPUT)
if (os.stat(JSON_OUTPUT).st_size > 0):
for topic in extractJSON()['Topics']:
if SNS_TOPIC in topic["TopicArn"].split(':'):
execCommand('aws sns delete-topic'
+ ' --topic-arn ' + topic["TopicArn"])
break
execCommand('aws sns list-subscriptions > ' + JSON_OUTPUT)
if (os.stat(JSON_OUTPUT).st_size > 0):
for sub in extractJSON()['Subscriptions']:
if (sub['SubscriptionArn'] != 'PendingConfirmation'):
execCommand('aws sns unsubscribe'
+ ' --subscription-arn ' + sub['SubscriptionArn'])
######################## WAITERS ########################
# The RDS instances are the ones that take the longest to be terminated.
execCommand('aws rds wait db-instance-deleted'
+ ' --db-instance-identifier ' + RDS_ID)
print('####################################################################################\n')
print('####################################################################################\n')
print('####################################################################################\n')
print('\n')
print('The scenario was succesfully destroyed.\n')
print('Execution time: ')
print(datetime.now() - startTime)