-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathadmin.py
266 lines (206 loc) · 9 KB
/
admin.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
import os
import sys
import threading
import time
from datetime import datetime, timedelta
from flask import Blueprint, render_template, request, redirect, url_for
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)), '..'))
from server.aws import (get_instances_details,
get_all_regions, get_untagged_instances,
attach_tag_to_instances, get_instances_daily_cost,
get_instances_monthly_cost)
from models.instance import Instance
from models.ssh_keys import SSHKeys
from models.user import User, BlacklistToken
from models.cost_explorer import CostExplorer
from settings import db
admin_bp = Blueprint('admin', __name__, url_prefix='/admin')
region_name = os.getenv('REGION_NAME')
# creating user obj
user_obj = User()
instance_obj = Instance()
ssh_key_obj = SSHKeys()
ce_obj = CostExplorer()
@admin_bp.route('/instances', methods=['GET'])
def get_admin():
update_thread = threading.Thread(target=make_aws_call, args=())
update_thread.start()
return get_instances()
@admin_bp.route('/ssh-keys')
def get_ssh_keys():
keys_list, all_keys_list = ssh_key_obj.get_ssh_keys_from_db()
return render_template('ssh-keys.html',
keys_list=keys_list,
all_keys_list=all_keys_list)
@admin_bp.route('/users')
def get_users():
users_list = user_obj.get_all_users()
return render_template('user-management.html', users=users_list)
@admin_bp.route('/addkey', methods=['GET', 'POST'])
def register_key():
if request.method == "POST":
key_name = request.form.get('keyname')
key_value = request.form.get('keyvalue')
key_format = request.form.get('keyformat')
ssh_key_obj.add_ssh_key_value(key_name, key_value, key_format)
return redirect(url_for('admin.get_ssh_keys'))
@admin_bp.route('/adduser', methods=['GET', 'POST'])
def register_user():
if request.method == "POST":
username = request.form.get('username')
password = request.form.get('password')
admin = request.form.get('admin')
admin_flag = admin == 'on'
if is_valid_request():
# store them into db
user_obj.add_user(username=username, password=password, admin=admin_flag)
return redirect(url_for('admin.get_users'))
return redirect(url_for('auth.auth'))
return redirect(url_for('admin.get_users'))
@admin_bp.route('/assignInstance', methods=['POST'])
def assign_instance_to_user():
if request.method == "POST":
user_id = request.form.get('user_id')
insid = request.form.get('inst_id')
if is_valid_request():
user_id = get_user_id_from_db(user_id)
# store them into db
instance_obj.assign_instance_to_user(user_id=user_id, instance_id=insid)
return redirect(url_for('admin.get_admin'))
return redirect(url_for('auth.auth'))
return redirect(url_for('admin.get_admin'))
@admin_bp.route('/un_assignInstance', methods=['POST'])
def un_assign_instance_to_user():
if request.method == "POST":
user_id = request.form.get('un_user_id')
insid = request.form.get('un_inst_id')
if is_valid_request():
instance_obj.un_assign_instance_from_user(user_id=user_id, instance_id=insid)
return redirect(url_for('admin.get_admin'))
return redirect(url_for('auth.auth'))
return redirect(url_for('admin.get_admin'))
@admin_bp.route('/', methods=['POST'])
def instance_management():
if request.method == "POST":
if is_valid_request():
if request.form['ins_btn'] == 'assigned_ins':
assigned_instances = instance_obj.get_assigned_instances()
return render_template('admin.html', instances=assigned_instances)
return redirect(url_for('admin.get_admin'))
return redirect(url_for('auth.auth'))
return redirect(url_for('admin.get_admin'))
@admin_bp.route('/users/delete', methods=['GET', 'POST'])
def delete_user():
if request.method == "POST":
user_id = request.form['user_id']
if is_valid_request():
# delete user method call
instance_obj.delete_user(user_id)
return redirect(url_for('admin.get_users'))
return redirect(url_for('auth.auth'))
return redirect(url_for('admin.get_users'))
@admin_bp.route('/delete_key', methods=['GET', 'POST'])
def delete_key():
if request.method == "POST":
key_id = request.form['key_id']
if is_valid_request():
# delete key method call
ssh_key_obj.delete_key(key_id)
return redirect(url_for('admin.get_ssh_keys'))
return redirect(url_for('auth.auth'))
return redirect(url_for('admin.get_ssh_keys'))
@admin_bp.route('/logout', methods=['GET'])
def logout_admin():
# if is_valid_request():
admin_token = request.cookies.get('auth_token')
blacklisted_token = BlacklistToken(token=admin_token)
try:
db.session.add(blacklisted_token)
db.session.commit()
return render_template('login.html')
except Exception as db_exception:
print(db_exception)
return redirect(url_for('admin.get_admin'))
@admin_bp.route('/bill', methods=['GET'])
def get_admin_bill():
return get_cost_from_db()
def fetch_instances_cost_from_aws():
daily_cost = get_instances_daily_cost(str(get_yesterday_date()), str(get_today_date()))
store_instances_cost_into_db(daily_cost, str(get_today_date()))
def store_instances_cost_into_db(daily_cost, today_date):
month = datetime.utcnow().month
for d_cost in daily_cost:
ce_obj.add_daily_bill(d_cost['CE_INS_KEY'], month, today_date, d_cost['CE_INS_COST'])
def get_cost_from_db():
response = ce_obj.get_complete_bill_from_db()
return render_template('admin-billing.html', all_instances_cost=response)
def get_admin_id():
users = db.session.query(User)
for admin in users:
if admin.admin:
return admin.id
return "None"
def get_instances():
instances_list = instance_obj.get_all_instances_from_db()
# sending assigned user's data..
assigned_instances = instance_obj.get_assigned_instances()
return render_template('admin.html',
assigned_instances=assigned_instances,
instances=instances_list)
def is_valid_request():
user_token = request.cookies.get('auth_token')
user_id = get_admin_id()
if not isinstance(user_id, str):
return User.validate_token(user_token, user_id)
return False
def make_aws_call():
regions_list = get_all_regions()
for region in regions_list:
ins_list = get_instances_details(region)
delete_terminated_instances(ins_list, region)
store_instance_into_db(ins_list)
def store_instance_into_db(instances_list):
for instance in instances_list:
instance_obj.add_instance(instance['Id'],
instance['Name'],
instance['State'],
instance['PublicIP'],
instance['PrivateIP'],
instance['KeyName'],
instance['RegionName'])
def delete_terminated_instances(aws_instances_list, region):
db_instances_list = Instance.query.filter_by(region_name=region)
db_instances_ids = [instance.id for instance in db_instances_list]
aws_instances_ids = [instance['Id'] for instance in aws_instances_list]
ins_not_exists = list(set(db_instances_ids) - set(aws_instances_ids))
ins_not_exists = [db_ins_id for db_ins_id in db_instances_ids if db_ins_id not in aws_instances_ids]
if ins_not_exists:
instance_obj.delete_instance_from_db(ins_not_exists)
def delete_terminated_instances_cost(aws_monthly_cost_list):
db_instances_cost_list = CostExplorer.query.all()
db_instances_ids = [instance.ce_instance_id for instance in db_instances_cost_list]
aws_instances_ids = [instance['CE_INS_KEY'] for instance in aws_monthly_cost_list]
ins_not_exists = list(set(db_instances_ids) - set(aws_instances_ids))
ins_not_exists = [db_ins_id for db_ins_id in db_instances_ids if db_ins_id not in aws_instances_ids]
if ins_not_exists:
ce_obj.delete_instance_cost_from_db(ins_not_exists)
def get_user_id_from_db(username):
userobj = db.session.query(User)
for user in userobj:
if user.name == username:
return user.id
return None
def tag_ec2_instance_of_region(region):
instance_tags_list = []
instance_tags_list = get_untagged_instances(region)
attach_tag_to_instances(instance_tags_list)
def tag_all_ec2_instances():
regions_list = get_all_regions()
for region in regions_list:
tag_ec2_instance_of_region(region)
def get_today_date():
return datetime.utcnow().strftime('%Y-%m-%d')
def get_first_date():
return datetime.utcnow().replace(day=1).strftime('%Y-%m-%d')
def get_yesterday_date():
return (datetime.utcnow() - timedelta(days=1)).strftime('%Y-%m-%d')