-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclear_region.py
More file actions
executable file
·160 lines (87 loc) · 3.98 KB
/
clear_region.py
File metadata and controls
executable file
·160 lines (87 loc) · 3.98 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
#!/bin/env python
import sys
import pprint
import boto3
from botocore.exceptions import ClientError
class clear_region():
def __init__(self, region):
# This code delete / deregisters everything in a region associated
# with NAVO: instance, AMI, keypair and security group. It also
# checks to make sure there are no residual snapshots lying around.
# We explicitly check and disallow clean-up of our default us-west-2
# region.
pp = pprint.PrettyPrinter(indent=4)
if(region == 'us-west-2'):
print('Error: deleting base region disallowed.')
sys.exit(0)
#--------------------------------------------------------------
# Connect to Region
region_client = boto3.client ('ec2', region_name=region)
region_resource = boto3.resource('ec2', region_name=region)
#--------------------------------------------------------------
# INSTANCES, AMIS AND SNAPSHOTS:
response = region_client.describe_instances(
Filters = [
{
'Name': 'tag:Name',
'Values': ['NAVO']
}
])
reservations = response['Reservations']
if len(reservations) == 0:
print('No "NAVO" instances.')
else:
# Instances
print('')
for reservation in reservations:
instances = reservation['Instances']
for instance in instances:
instance_id = instance['InstanceId']
instance_object = region_resource.Instance(instance_id)
region_client.terminate_instances(InstanceIds=[instance_id])
instance_object.wait_until_terminated()
print('Instance ' + instance_id + ' terminated.')
# AMIs
response = region_client.describe_images(Owners=['self'],
Filters=[{'Name': 'name', 'Values':['NAVO']}])
images = response['Images']
for image in images:
ami_id = image['ImageId']
response = region_client.deregister_image(ImageId=ami_id)
print('"NAVO" AMI ID ' + ami_id + ' for region ' + region + ' deregistered.')
# Snapshots
snapshots = region_client.describe_snapshots(OwnerIds=['self'])['Snapshots']
for snapshot in snapshots:
if snapshot['Description'].find(ami_id) > 0:
snapshot_id = snapshot['SnapshotId']
response = region_client.delete_snapshot(SnapshotId=snapshot_id)
print('Snapshot ' + snapshot_id + ' deleted.')
#--------------------------------------------------------------
# KEYPAIRS:
response = region_client.describe_key_pairs(
Filters=[{'Name': 'key-name', 'Values': ['NAVO']}])
keypairs = response['KeyPairs']
for keypair in keypairs:
keypair_id = keypair['KeyPairId']
region_client.delete_key_pair(KeyPairId=keypair_id)
print('"NAVO" key pair ' + keypair_id + ' deleted.')
#--------------------------------------------------------------
# SECURITY GROUPS:
try:
response = region_client.describe_security_groups(GroupNames=['NAVO'])
security_groups = response['SecurityGroups']
for security_group in security_groups:
security_group_id = security_group['GroupId']
region_client.delete_security_group(GroupId=security_group_id)
print('"NAVO" security group ' + security_group_id + ' deleted.')
except:
pass
print('')
def main():
if len(sys.argv) < 2:
print('Usage: clear_region.py <region_name>\n')
sys.exit(0)
else:
clear_region(sys.argv[1])
if __name__ == "__main__":
main()