forked from HardBoiledSmith/johanna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_terminate_vpc.py
executable file
·371 lines (294 loc) · 13.6 KB
/
run_terminate_vpc.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
#!/usr/bin/env python3
from env import env
from run_common import AWSCli
from run_common import print_message
from run_common import print_session
if __name__ == "__main__":
from run_common import parse_args
parse_args()
aws_cli = AWSCli()
rds_subnet_name = env['rds']['DB_SUBNET_NAME']
cidr_vpc = aws_cli.cidr_vpc
################################################################################
#
# start
#
################################################################################
print_session('terminate vpc')
################################################################################
print_message('wait terminate rds')
aws_cli.wait_terminate_rds()
################################################################################
print_message('wait terminate elasticache')
aws_cli.wait_terminate_elasticache()
################################################################################
print_message('wait terminate eb')
aws_cli.wait_terminate_eb()
################################################################################
print_message('delete network interface')
cmd = ['ec2', 'describe-network-interfaces']
result = aws_cli.run(cmd, ignore_error=True)
for r in result['NetworkInterfaces']:
network_interface_id = r['NetworkInterfaceId']
if 'Attachment' in r:
attachment_id = r['Attachment']['AttachmentId']
cmd = ['ec2', 'detach-network-interface']
cmd += ['--attachment-id', attachment_id]
aws_cli.run(cmd, ignore_error=True)
cmd = ['ec2', 'delete-network-interface']
cmd += ['--network-interface-id', network_interface_id]
aws_cli.run(cmd, ignore_error=True)
################################################################################
print_message('get vpc id')
rds_vpc_id, eb_vpc_id = aws_cli.get_vpc_id()
################################################################################
print_message('delete vpc peering connection')
cmd = ['ec2', 'describe-vpc-peering-connections']
result = aws_cli.run(cmd, ignore_error=True)
for vpc_peer in result['VpcPeeringConnections']:
if vpc_peer['RequesterVpcInfo']['VpcId'] == rds_vpc_id and vpc_peer['AccepterVpcInfo']['VpcId'] == eb_vpc_id:
peering_connection_id = vpc_peer['VpcPeeringConnectionId']
print('delete vpc peering connnection (id: %s)' % peering_connection_id)
cmd = ['ec2', 'delete-vpc-peering-connection']
cmd += ['--vpc-peering-connection-id', peering_connection_id]
aws_cli.run(cmd, ignore_error=True)
################################################################################
print_message('revoke security group ingress')
security_group_id_1 = None
security_group_id_2 = None
cmd = ['ec2', 'describe-security-groups']
result = aws_cli.run(cmd, ignore_error=True)
for r in result['SecurityGroups']:
if r['VpcId'] != rds_vpc_id and r['VpcId'] != eb_vpc_id:
continue
if r['GroupName'] == 'eb_private':
security_group_id_1 = r['GroupId']
if r['GroupName'] == 'eb_public':
security_group_id_2 = r['GroupId']
if security_group_id_1 and security_group_id_2:
cmd = ['ec2', 'revoke-security-group-ingress']
cmd += ['--group-id', security_group_id_1]
cmd += ['--protocol', 'all']
cmd += ['--source-group', security_group_id_2]
cmd = ['ec2', 'revoke-security-group-ingress']
cmd += ['--group-id', security_group_id_2]
cmd += ['--protocol', 'all']
cmd += ['--source-group', security_group_id_1]
result = aws_cli.run(cmd, ignore_error=True)
################################################################################
print_message('delete security group')
cmd = ['ec2', 'describe-security-groups']
result = aws_cli.run(cmd, ignore_error=True)
for r in result['SecurityGroups']:
if r['VpcId'] != rds_vpc_id and r['VpcId'] != eb_vpc_id:
continue
if r['GroupName'] == 'default':
continue
print('delete security group (id: %s)' % r['GroupId'])
cmd = ['ec2', 'delete-security-group']
cmd += ['--group-id', r['GroupId']]
aws_cli.run(cmd, ignore_error=True)
################################################################################
print_message('delete route')
cmd = ['ec2', 'describe-route-tables']
result = aws_cli.run(cmd, ignore_error=True)
for r in result['RouteTables']:
if r['VpcId'] != rds_vpc_id and r['VpcId'] != eb_vpc_id:
continue
for route in r['Routes']:
if route['DestinationCidrBlock'] == '0.0.0.0/0':
print('delete route (route table id: %s)' % r['RouteTableId'])
cmd = ['ec2', 'delete-route']
cmd += ['--route-table-id', r['RouteTableId']]
cmd += ['--destination-cidr-block', '0.0.0.0/0']
aws_cli.run(cmd, ignore_error=True)
################################################################################
print_message('disassociate route table')
cmd = ['ec2', 'describe-route-tables']
result = aws_cli.run(cmd, ignore_error=True)
for r in result['RouteTables']:
if r['VpcId'] != rds_vpc_id and r['VpcId'] != eb_vpc_id:
continue
for association in r['Associations']:
if association['Main']:
continue
print('disassociate route table (route table id: %s, route table association id: %s)' %
(r['RouteTableId'], association['RouteTableAssociationId']))
cmd = ['ec2', 'disassociate-route-table']
cmd += ['--association-id', association['RouteTableAssociationId']]
aws_cli.run(cmd, ignore_error=True)
################################################################################
print_message('delete route table')
cmd = ['ec2', 'describe-route-tables']
result = aws_cli.run(cmd, ignore_error=True)
for r in result['RouteTables']:
if r['VpcId'] != rds_vpc_id and r['VpcId'] != eb_vpc_id:
continue
if len(r['Associations']) != 0:
continue
print('delete route table (route table id: %s)' % r['RouteTableId'])
cmd = ['ec2', 'delete-route-table']
cmd += ['--route-table-id', r['RouteTableId']]
aws_cli.run(cmd, ignore_error=True)
################################################################################
print_message('delete nat gateway')
cmd = ['ec2', 'describe-nat-gateways']
result = aws_cli.run(cmd, ignore_error=True)
for r in result['NatGateways']:
if r['VpcId'] != rds_vpc_id and r['VpcId'] != eb_vpc_id:
continue
print('delete nat gateway (nat gateway id: %s)' % r['NatGatewayId'])
cmd = ['ec2', 'delete-nat-gateway']
cmd += ['--nat-gateway-id', r['NatGatewayId']]
aws_cli.run(cmd, ignore_error=True)
################################################################################
print_message('wait delete nat gateway')
aws_cli.wait_delete_nat_gateway()
################################################################################
print_message('release eip')
cmd = ['ec2', 'describe-addresses']
result = aws_cli.run(cmd, ignore_error=True)
for r in result['Addresses']:
print('release address (address id: %s)' % r['AllocationId'])
cmd = ['ec2', 'release-address']
cmd += ['--allocation-id', r['AllocationId']]
aws_cli.run(cmd, ignore_error=True)
################################################################################
if env.get('elasticache'):
elasticache_subnet_name = env['elasticache']['CACHE_SUBNET_NAME']
print_message('delete cache subnet group')
cmd = ['elasticache', 'delete-cache-subnet-group']
cmd += ['--cache-subnet-group-name', elasticache_subnet_name]
aws_cli.run(cmd, ignore_error=True)
################################################################################
print_message('delete db subnet group')
cmd = ['rds', 'delete-db-subnet-group']
cmd += ['--db-subnet-group-name', rds_subnet_name]
aws_cli.run(cmd, ignore_error=True)
################################################################################
print_message('detach internet gateway')
cmd = ['ec2', 'describe-internet-gateways']
result = aws_cli.run(cmd, ignore_error=True)
for r in result['InternetGateways']:
if len(r['Attachments']) != 1:
continue
if r['Attachments'][0]['VpcId'] != eb_vpc_id:
continue
print('detach internet gateway (internet gateway id: %s)' % r['InternetGatewayId'])
cmd = ['ec2', 'detach-internet-gateway']
cmd += ['--internet-gateway-id', r['InternetGatewayId']]
cmd += ['--vpc-id', r['Attachments'][0]['VpcId']]
aws_cli.run(cmd, ignore_error=True)
################################################################################
print_message('delete internet gateway')
cmd = ['ec2', 'describe-internet-gateways']
result = aws_cli.run(cmd, ignore_error=True)
for r in result['InternetGateways']:
if len(r['Attachments']) != 0:
continue
print('delete internet gateway (internet gateway id: %s)' % r['InternetGatewayId'])
cmd = ['ec2', 'delete-internet-gateway']
cmd += ['--internet-gateway-id', r['InternetGatewayId']]
aws_cli.run(cmd, ignore_error=True)
################################################################################
print_message('delete subnet')
cmd = ['ec2', 'describe-subnets']
result = aws_cli.run(cmd, ignore_error=True)
for r in result['Subnets']:
if r['VpcId'] != rds_vpc_id and r['VpcId'] != eb_vpc_id:
continue
print('delete subnet (subnet id: %s)' % r['SubnetId'])
cmd = ['ec2', 'delete-subnet']
cmd += ['--subnet-id', r['SubnetId']]
aws_cli.run(cmd, ignore_error=True)
################################################################################
print_message('delete vpc')
if rds_vpc_id:
print('delete vpc (vpc id: %s)' % rds_vpc_id)
cmd = ['ec2', 'delete-vpc']
cmd += ['--vpc-id', rds_vpc_id]
aws_cli.run(cmd, ignore_error=True)
if eb_vpc_id:
print('delete vpc (vpc id: %s)' % eb_vpc_id)
cmd = ['ec2', 'delete-vpc']
cmd += ['--vpc-id', eb_vpc_id]
aws_cli.run(cmd, ignore_error=True)
################################################################################
#
# EB
#
################################################################################
print_session('terminate eb application')
################################################################################
print_message('delete application')
cmd = ['elasticbeanstalk', 'delete-application']
cmd += ['--application-name', env['elasticbeanstalk']['APPLICATION_NAME']]
aws_cli.run(cmd, ignore_error=True)
################################################################################
print_message('remove iam role from instance profile')
cmd = ['iam', 'remove-role-from-instance-profile']
cmd += ['--instance-profile-name', 'aws-elasticbeanstalk-ec2-role']
cmd += ['--role-name', 'aws-elasticbeanstalk-ec2-role']
aws_cli.run(cmd, ignore_error=True)
cmd = ['iam', 'remove-role-from-instance-profile']
cmd += ['--instance-profile-name', 'aws-elasticbeanstalk-ec2-worker-role']
cmd += ['--role-name', 'aws-elasticbeanstalk-ec2-worker-role']
aws_cli.run(cmd, ignore_error=True)
################################################################################
print_message('delete iam instance profile')
cmd = ['iam', 'delete-instance-profile']
cmd += ['--instance-profile-name', 'aws-elasticbeanstalk-ec2-role']
aws_cli.run(cmd, ignore_error=True)
cmd = ['iam', 'delete-instance-profile']
cmd += ['--instance-profile-name', 'aws-elasticbeanstalk-ec2-worker-role']
aws_cli.run(cmd, ignore_error=True)
################################################################################
print_message('delete iam role policy')
cmd = ['iam', 'delete-role-policy']
cmd += ['--role-name', 'aws-elasticbeanstalk-ec2-worker-role']
cmd += ['--policy-name', 'oneClick_aws-elasticbeanstalk-ec2-worker-role']
aws_cli.run(cmd, ignore_error=True)
cmd = ['iam', 'delete-role-policy']
cmd += ['--role-name', 'aws-elasticbeanstalk-service-role']
cmd += ['--policy-name', 'oneClick_aws-elasticbeanstalk-service-role']
aws_cli.run(cmd, ignore_error=True)
################################################################################
print_message('detach policies from iam role')
policy_list = list()
policy_list.append('arn:aws:iam::aws:policy/service-role/AWSElasticBeanstalkEnhancedHealth')
policy_list.append('arn:aws:iam::aws:policy/service-role/AWSElasticBeanstalkService')
for pp in policy_list:
cmd = ['iam', 'detach-role-policy']
cmd += ['--role-name', 'aws-elasticbeanstalk-service-role']
cmd += ['--policy-arn', pp]
aws_cli.run(cmd, ignore_error=True)
policy_list = list()
policy_list.append('arn:aws:iam::aws:policy/AWSElasticBeanstalkWebTier')
policy_list.append('arn:aws:iam::aws:policy/AWSElasticBeanstalkMulticontainerDocker')
policy_list.append('arn:aws:iam::aws:policy/AWSElasticBeanstalkWorkerTier')
for pp in policy_list:
cmd = ['iam', 'detach-role-policy']
cmd += ['--role-name', 'aws-elasticbeanstalk-ec2-worker-role']
cmd += ['--policy-arn', pp]
aws_cli.run(cmd, ignore_error=True)
for pp in policy_list:
cmd = ['iam', 'detach-role-policy']
cmd += ['--role-name', 'aws-elasticbeanstalk-ec2-role']
cmd += ['--policy-arn', pp]
aws_cli.run(cmd, ignore_error=True)
################################################################################
print_message('delete iam role')
cmd = ['iam', 'delete-role']
cmd += ['--role-name', 'aws-elasticbeanstalk-service-role']
aws_cli.run(cmd, ignore_error=True)
cmd = ['iam', 'delete-role']
cmd += ['--role-name', 'aws-elasticbeanstalk-ec2-worker-role']
aws_cli.run(cmd, ignore_error=True)
cmd = ['iam', 'delete-role']
cmd += ['--role-name', 'aws-elasticbeanstalk-ec2-role']
aws_cli.run(cmd, ignore_error=True)
################################################################################
print_message('delete key pair')
cmd = ['ec2', 'delete-key-pair']
cmd += ['--key-name', env['common']['AWS_KEY_PAIR_NAME']]
aws_cli.run(cmd, ignore_error=True)