-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathremove.py
executable file
·175 lines (150 loc) · 4.79 KB
/
remove.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
#!/usr/bin/python2.7
"""
# remove.py
# Author: Christopher Treadgold
# Date: N/D
# Edited: 07/08/2016 | Christopher Treadgold
"""
import json
import os
import sys
import time
import boto3
# AWS clients used by the AWSCMS
apigateway = boto3.client('apigateway')
lmda = boto3.client('lambda')
iam = boto3.client('iam')
dynamodb = boto3.client('dynamodb')
s3 = boto3.client('s3')
# Get installed cms'
if os.path.isfile("./installed.json"):
with open("installed.json", "r") as installed_file:
installed = json.loads(installed_file.read())
else:
print "No cms' are installed, exiting"
sys.exit()
# Ask the user which cms to remove
print "Installed cms':"
for key in installed.keys():
print "%s: %s" %(key, installed[key])
user_selection = str(input("Enter the number of the cms you would like to uninstall: "))
if user_selection not in installed.keys():
print "Invalid selection, exiting."
sys.exit()
else:
constants_file_name = "%s-constants.json" % (installed[user_selection])
with open(constants_file_name, "r") as const:
constants = json.loads(const.read())
del installed[user_selection]
rest_api_names = [
constants["REST_API"]
]
lmda_function_names = [
constants["LAMBDA_FUNCTION"]
]
role_names = [
constants["LAMBDA_ROLE"]
]
dynamodb_table_names = [
constants["BLOG_TABLE"],
constants["PAGE_TABLE"],
constants["TOKEN_TABLE"],
constants["USER_TABLE"],
constants["ROLE_TABLE"]
]
s3_bucket_names = [
constants["BUCKET"]
]
# Remove all AWSCMS api gateways
print 'Removing rest api'
rest_apis_deleted = 0
rest_apis = apigateway.get_rest_apis()['items']
for rest_api in rest_apis:
if rest_api['name'] in rest_api_names:
if rest_api != rest_apis[-1]:
time.sleep(30.5)
apigateway.delete_rest_api(restApiId=rest_api['id'])
rest_apis_deleted += 1
if rest_apis_deleted > 0:
print rest_apis_deleted, 'Rest api(s) removed'
else:
print 'No apis to remove'
# Remove all AWSCMS lambda funtions
print 'Removing lambda functions'
functions_removed = 0
for lmda_function in lmda.list_functions()['Functions']:
if lmda_function['FunctionName'] in lmda_function_names:
lmda.delete_function(
FunctionName=lmda_function['FunctionName'])
functions_removed += 1
if functions_removed > 0:
print functions_removed, 'lambda function(s) removed'
else:
print 'No lambda functions to remove'
# Remove all AWSCMS iam roles
print 'Removing iam roles'
roles_removed = 0
for role in iam.list_roles()['Roles']:
if role['RoleName'] in role_names:
# Detach all role policies
for policy in iam.list_attached_role_policies(RoleName=role['RoleName'])['AttachedPolicies']:
iam.detach_role_policy(
RoleName=role['RoleName'],
PolicyArn=policy['PolicyArn'])
for policy_name in iam.list_role_policies(RoleName=role['RoleName'])['PolicyNames']:
iam.delete_role_policy(
RoleName=role['RoleName'],
PolicyName=policy_name)
# Delete role
iam.delete_role(
RoleName=role['RoleName'])
roles_removed += 1
if roles_removed > 0:
print roles_removed, 'iam role(s) removed'
else:
print 'No iam roles to remove'
# Remove all AWSCMS dynamodb tables
print 'Removing dynamodb tables'
tables_removed = 0
for table_name in dynamodb.list_tables()['TableNames']:
if table_name in dynamodb_table_names:
dynamodb.delete_table(
TableName=table_name)
tables_removed += 1
if tables_removed > 0:
print tables_removed, 'dynamodb table(s) removed'
else:
print 'No dynamodb tables to remove'
# Remove all AWSCMS s3 buckets
print 'Removing S3 buckets'
buckets_deleted = 0
objects_deleted = 0
for bucket in s3.list_buckets()['Buckets']:
if bucket['Name'] in s3_bucket_names:
print 'Deleting bucket:', bucket['Name']
# Delete all bucket contents
if 'Contents' in s3.list_objects_v2(Bucket=bucket['Name']).keys():
for obj in s3.list_objects_v2(Bucket=bucket['Name'])['Contents']:
print 'Deleting object:', obj['Key']
s3.delete_object(
Bucket=bucket['Name'],
Key=obj['Key'])
print 'Object deleted'
objects_deleted += 1
# Delete bucket
s3.delete_bucket(
Bucket=bucket['Name'])
print 'Bucket deleted'
buckets_deleted += 1
if buckets_deleted > 0:
print buckets_deleted, 'S3 buckets removed'
print objects_deleted, 'objects deleted'
else:
print 'No S3 buckets to remove'
# Clean up files
if not installed:
os.remove("installed.json")
else:
with open("installed.json", "w") as inst:
inst.write(json.dumps(installed, indent=4, sort_keys=True))
os.remove(constants_file_name)