forked from 1injex/azure-manager
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathapp.py
307 lines (222 loc) · 10.6 KB
/
app.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
import threading
from flask import Flask, render_template, request, url_for, redirect, flash, make_response,session,jsonify
import function
from flask_sqlalchemy import SQLAlchemy
import os
import re
defaultadmin ='admin'
defaultpass = 'admin123'
app = Flask(__name__)
app.jinja_env.filters['zip'] = zip
# 请将 xxx 替换为随机字符
app.config['SECRET_KEY'] = os.environ.get('random_key','c2jf932hibfiuebvwievubheriuvberv')
DATABASE_URL = os.environ.get('DATABASE_URL', 'sqlite:///flask_app.db')
DATABASE_URL = DATABASE_URL.replace("postgres://", "postgresql://", 1)
app.config['SQLALCHEMY_DATABASE_URI'] = DATABASE_URL
db = SQLAlchemy(app)
class User(db.Model):
account = db.Column(db.String(100),primary_key=True)
client_id = db.Column(db.String(100))
client_secret = db.Column(db.String(100))
tenant_id = db.Column(db.String(100))
subscription_id = db.Column(db.String(100))
def __init__(self, account, client_id,client_secret,tenant_id,subscription_id):
self.account = account
self.client_id = client_id
self.client_secret = client_secret
self.tenant_id = tenant_id
self.subscription_id = subscription_id
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
#logger.debug("login post method")
username = request.form['userName']
password = request.form['passWord']
if username == os.environ.get('adminname',defaultadmin) and password == os.environ.get('adminpassword',defaultpass):
#if username == 'adminusername' and password == 'adminpassword':
session['username'] = username
session['password'] = password
resp = make_response(render_template('index.html', users=User.query.all()))
resp.set_cookie('username', username)
return resp
#return jsonify({'status': '0', 'errmsg': 'login success!'})
else:
# return redirect(url_for('error'))
return jsonify({'status': '-1', 'errmsg': 'error account!'})
#logger.debug("login get method")
return render_template('login.html')
@app.route('/', methods=['GET', 'POST'])
def index():
#logger.debug("index page")
#logger.debug("cookie name %s" % request.cookies.get('username'))
if 'username' in session:
#logger.debug("login user is %s" % flask_login.current_user)
#logger.debug('Logged in as %s' % escape(session['username']))
return render_template('index.html', users=User.query.all())
else:
#logger.debug("you are not logged in")
return redirect(url_for('login'))
@app.route('/account/add', methods=['GET', 'POST'])
def accountadd():
if 'username' in session:
if request.method == 'POST': # 判断是否是 POST 请求
# 获取表单数据
account = request.form.get('account')
client_id = request.form.get('client_id')
client_secret = request.form.get('client_secret')
tenant_id = request.form.get('tenant_id')
subscription_id = request.form.get('subscription_id')
# 验证数据
if not account or not client_id or not client_secret or not tenant_id or not subscription_id:
flash('输入错误') # 显示错误提示
return redirect(url_for('index')) # 重定向回主页
# 保存表单数据到cookie
u = User(account, client_id,client_secret,tenant_id,subscription_id)
db.session.add(u)
db.session.commit()
resp = make_response(redirect(url_for('index')))
flash('添加管理账户成功')
return resp
return render_template('account.html')
else:
#logger.debug("you are not logged in")
return redirect(url_for('login'))
@app.route('/account/delete')
def accountdel():
if 'username' in session:
account = request.args.get('account')
dele = User.query.filter(User.account == account).first()
db.session.delete(dele)
db.session.commit()
flash('删除账户成功')
resp = make_response(redirect(url_for('index')))
return resp
else:
#logger.debug("you are not logged in")
return redirect(url_for('login'))
@app.route('/account/list')
def list():
if 'username' in session:
account = request.args.get('account')
result = User.query.filter(User.account == account).all()
client_id = result[0].client_id
client_secret = result[0].client_secret
tenant_id = result[0].tenant_id
subscription_id = result[0].subscription_id
credential = function.create_credential_object(tenant_id, client_id, client_secret)
dict, subscription_list = function.list(subscription_id, credential)
return render_template('list.html',dict=dict, subscription_list=subscription_list, account=account)
else:
#logger.debug("you are not logged in")
return redirect(url_for('login'))
@app.route('/account/vm/create', methods=['GET', 'POST'])
def create_vm():
if 'username' in session:
account = request.args.get('account')
print(account)
if request.method == 'POST':
result = User.query.filter(User.account == account).all()
client_id = result[0].client_id
client_secret = result[0].client_secret
tenant_id = result[0].tenant_id
subscription_id = result[0].subscription_id
credential = function.create_credential_object(tenant_id, client_id, client_secret)
tag = request.form.get('tag')
location = request.form.get('location')
size = request.form.get('size')
os = request.form.get('os')
set = request.form.get('set')
rootpwd=request.form.get('rootpwd')
storgesize=request.form.get('storgesize')
## 此处为VM默认登陆密码
username = request.form.get('vmusername')#"defaultuser"
password = request.form.get('vmpasswd')#"Thisisyour.password1"
char = re.findall(r'[a-z]', password)
bigchar = re.findall(r'[A-Z]', password)
num = re.findall(r'[0-9]', password)
if len(num) * len(bigchar) * len(char) == 0 or len(password) < 12 or len(password) > 72:
flash('VM密码不合规,请输入至少12位密码,且包含大小写字母和数字')
else:
for i in range(int(set)):
name = (tag + str(i + 1))
function.create_resource_group(subscription_id, credential, name, location)
threading.Thread(target=function.create_or_update_vm, args=(
subscription_id, credential, name, location, username, password, size, os,rootpwd,storgesize)).start()
flash('创建中,请耐心等待VM创建完成,大约需要1-3分钟')
return render_template('vm.html', account=account)
else:
#logger.debug("you are not logged in")
return redirect(url_for('login'))
@app.route('/account/vm/delete/<string:tag>')
def delete_vm(tag):
if 'username' in session:
account = request.args.get('account')
result = User.query.filter(User.account == account).all()
client_id = result[0].client_id
client_secret = result[0].client_secret
tenant_id = result[0].tenant_id
subscription_id = result[0].subscription_id
credential = function.create_credential_object(tenant_id, client_id, client_secret)
threading.Thread(target=function.delete_vm, args=(subscription_id, credential, tag)).start()
flash("删除中,请耐心等待1-3分钟")
dict, subscription_list = function.list(subscription_id, credential)
return render_template('list.html', dict=dict, subscription_list=subscription_list, account=account)
else:
#logger.debug("you are not logged in")
return redirect(url_for('login'))
@app.route('/account/vm/start/<string:tag>')
def start_vm(tag):
if 'username' in session:
account = request.args.get('account')
result = User.query.filter(User.account == account).all()
client_id = result[0].client_id
client_secret = result[0].client_secret
tenant_id = result[0].tenant_id
subscription_id = result[0].subscription_id
credential = function.create_credential_object(tenant_id, client_id, client_secret)
threading.Thread(target=function.start_vm, args=(subscription_id, credential, tag)).start()
flash("开机中,请耐心等待1-3分钟")
dict, subscription_list = function.list(subscription_id, credential)
return render_template('list.html', dict=dict, subscription_list=subscription_list, account=account)
else:
#logger.debug("you are not logged in")
return redirect(url_for('login'))
@app.route('/account/vm/stop/<string:tag>')
def stop_vm(tag):
if 'username' in session:
account = request.args.get('account')
result = User.query.filter(User.account == account).all()
client_id = result[0].client_id
client_secret = result[0].client_secret
tenant_id = result[0].tenant_id
subscription_id = result[0].subscription_id
credential = function.create_credential_object(tenant_id, client_id, client_secret)
threading.Thread(target=function.stop_vm, args=(subscription_id, credential, tag)).start()
flash("关机中,请耐心等待1-3分钟")
dict, subscription_list = function.list(subscription_id, credential)
return render_template('list.html', dict=dict, subscription_list=subscription_list, account=account)
else:
#logger.debug("you are not logged in")
return redirect(url_for('login'))
@app.route('/account/vm/changeip/<string:tag>')
def changeip_vm(tag):
if 'username' in session:
account = request.args.get('account')
result = User.query.filter(User.account == account).all()
client_id = result[0].client_id
client_secret = result[0].client_secret
tenant_id = result[0].tenant_id
subscription_id = result[0].subscription_id
credential = function.create_credential_object(tenant_id, client_id, client_secret)
try:
threading.Thread(target=function.change_ip, args=(subscription_id, credential, tag)).start()
flash("更换IP进行中,请耐心等待1-3分钟")
return redirect(url_for('index'))
except:
flash("出现未知错误,请重试")
else:
#logger.debug("you are not logged in")
return redirect(url_for('login'))
if __name__ == '__main__':
db.create_all()
app.run()