-
Notifications
You must be signed in to change notification settings - Fork 0
/
bp_openstack.py
261 lines (239 loc) · 8.79 KB
/
bp_openstack.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
from sanic import Blueprint
import asyncio
import datetime
import aiomysql
from sanic import Sanic
from sanic import response
from sanic.response import json
from db_support import db_select, db_insert, db_delete, db_update, alchemyencoder
from db_support import aio_post, aio_delete, aio_put, aio_patch
import db
import os
import aiofiles
import uuid
import xlrd
import hashlib
import aiohttp
from sanic.response import stream, text
import zipfile
import json as innerjson
import time
import signal
from log_support import do_log
from config import *
bp_openstack = Blueprint('bp_openstack')
#Monitor
@bp_openstack.get("/teacher/monitor")
async def get_userCourseOne(request):
get_info = request.raw_args
try:
id = int(get_info['id'])
info=await db_select("userTemplate",u_id=id)
template_id=[]
infoX = []
for item in info:
#print(item['template'])
if item['template'] == None or item['template'] == '':
pass
else:
infoX.append(item)
template_id.append(item['template'])
except Exception as e:
#print(e)
do_log(4, "userTemplate:数据库访问失败")
return json({'state': -1, 'info': 'userTemplate:数据库访问失败'})
if template_id == []:
return json({'state': 0, 'info': []})
try:
jsondata={"template_id":template_id}
resp = await aio_patch(vmURL+'serveraction', jsondata)
if resp['state'] != 1:
return json({'state': -1, 'info': '虚拟机交互失败'})
if resp['info'] == {}:
return json({'state': 0, 'info': []})
data = resp['info']
except:
do_log(4, "get_exp:虚拟机交互失败")
return json({'state': -1, 'info': '虚拟机交互失败'})
try:
result = []
for item in infoX:
now = item['template']
if data.get(str(now),'') == '':
# info.pop(info.index(item))
continue
students =[]
for stu in data[str(now)]:
user= await db_select("user",id=int(stu))
students.append(user[0])
item['parameter'] = students
result.append(item)
except:
do_log(4, "get_user:数据库错误")
return json({'state': -1, 'info': '数据库错误'})
do_log(4, "监控实验成功!")
return json({'state':0,'info':result})
#创建环境
@bp_openstack.get("/exp")
async def exp_post(request):
get_info = request.raw_args
try:
id = int(get_info['id'])
template = int(get_info['template'])
except:
do_log(4, "get_course:参数缺失或不规范")
return json({'state': -1, 'info': '参数缺失或不规范'})
try:
data={"user_id": id,"template_id": template}
resp = await aio_post(vmURL+'environment', data)
return json(resp)
except:
do_log(4, "get_exp:参数缺失或不规范")
return json({'state': -1, 'info': '虚拟机交互失败'})
#关闭环境
@bp_openstack.route("/exp/del", methods=['GET'])
async def exp_post(request):
get_info = request.raw_args
try:
id = int(get_info['id'])
template = int(get_info['template'])
except:
do_log(4, "get_course:参数缺失或不规范")
return json({'state': -1, 'info': '参数缺失或不规范'})
try:
resp = await aio_delete(vmURL+'environment?'+'template_id='+str(template)+'&user_id='+str(id))
return json(resp)
except Exception as e:
do_log(4, "get_exp:虚拟机交互失败")
return json({'state': -1, 'info': '虚拟机交互失败'})
#关闭环境。
@bp_openstack.route("/exp/state", methods=['PATCH'])
async def get_userCourseOne(request):
try:
info = request.json
resp = await aio_put(vmURL+'serveraction', info)
return json(resp)
except:
do_log(4, "get_server:OpenStack交互失败")
return json({'state': -1, 'info': 'OpenStack交互失败'})
#开启环境。
@bp_openstack.route("/exp/state", methods=['POST'])
async def get_userCourseOne(request):
try:
info = request.json
resp = await aio_post(vmURL+'serveraction', info)
return json(resp)
except:
do_log(4, "get_server:OpenStack交互失败")
return json({'state': -1, 'info': 'OpenStack交互失败'})
#获取console
@bp_openstack.route("/serveraction", methods=['PATCH'])
async def get_userCourseOne(request):
try:
info = request.json
resp = await aio_patch(vmURL+'serveraction', info)
return json(resp)
except:
do_log(4, "get_server:OpenStack交互失败")
return json({'state': -1, 'info': 'OpenStack交互失败'})
#创建镜像
@bp_openstack.route("/vm", methods=['POST'])
async def exp_post(request):
try:
info = request.json
resp = await aio_post(vmURL+'image', info)
return json(resp)
except:
do_log(4, "get_exp:OpenStack交互失败")
return json({'state': -1, 'info': 'OpenStack交互失败'})
@bp_openstack.route("/exp/closeAll", methods=['GET'])
async def delete_role(request):
try:
total = 0
success = 0
tids = await db_select("target", find=["template","user"], deleted=0)
except Exception as e:
do_log(4, "delete_user:用户存在未关闭的实验,且删除环境失败。")
return json({'state': -1, 'info': '用户存在未关闭的实验,且删除环境失败。'})
try:
data={"user_id_list": dictId}
resp = await aio_put(vmURL+'user', data)
if(resp['state'] != 1):
do_log(4, "删除OpenStack后台账户失败!")
return json({'state': -1, 'info':'删除OpenStack后台账户失败!'})
except:
do_log(4, "delete_user:OpenStack交互失败!")
return json({'state': -1, 'info': 'OpenStack交互失败!'})
do_log(2, "delete_role:批量删除成功[id=%d]" % (id,))
return json({'state': 0, 'info': '成功'})
@bp_openstack.route("/exp/closeOld", methods=['GET'])
async def delete_role(request):
get_info = request.raw_args
try:
id = int(get_info['id'])
except:
do_log(4, "get_course:参数缺失或不规范")
return json({'state': -1, 'info': '参数缺失或不规范'})
try:
total = 0
success = 0
tids = await db_select("target", find=["template","user"], deleted=0, user=id)
print(tids)
return json(tids)
except Exception as e:
do_log(4, "delete_user:用户存在未关闭的实验,且删除环境失败。")
return json({'state': -1, 'info': '用户存在未关闭的实验,且删除环境失败。'})
# try:
# data={"user_id_list": dictId}
# resp = await aio_put(vmURL+'user', data)
# if(resp['state'] != 1):
# do_log(4, "删除OpenStack后台账户失败!")
# return json({'state': -1, 'info':'删除OpenStack后台账户失败!'})
# except:
# do_log(4, "delete_user:OpenStack交互失败!")
# return json({'state': -1, 'info': 'OpenStack交互失败!'})
# do_log(2, "delete_role:批量删除成功[id=%d]" % (id,))
# return json({'state': 0, 'info': '成功'})
# async def exp_post(request):
# get_info = request.raw_args
# try:
# id = int(get_info['id'])
# template = int(get_info['template'])
# except:
# do_log(4, "get_course:参数缺失或不规范")
# return json({'state': -1, 'info': '参数缺失或不规范'})
# try:
# resp = await aio_delete(vmURL+'environment?'+'template_id='+str(template)+'&user_id='+str(id))
# return json(resp)
# except Exception as e:
# do_log(4, "get_exp:虚拟机交互失败")
# return json({'state': -1, 'info': '虚拟机交互失败'})
#CHECK镜像
@bp_openstack.route("/vm/check", methods=['GET'])
async def exp_post(request):
divide={
"0": ["RegionOne","RegionTwo","RegionThree"],
"1": ["RegionTwo","RegionThree"],
"2": ["RegionOne","RegionThree"],
"3": ["RegionThree"],
"4": ["RegionTwo","RegionOne"],
"5": ["RegionTwo"],
"6": ["RegionOne"]
}
result=[]
try:
info = await db_select("courseTemplate")
for inf in info:
if inf['state'] == '7':
continue
for div in divide[inf['state']]:
mid = innerjson.dumps(inf)
temp = innerjson.loads(mid)
temp["region"] = div
result.append(temp)
do_log(4, "get_exp:获取成功")
return json({'state': 0, 'info': result})
except Exception as e:
# print(e)
do_log(4, "get_exp:state解析错误")
return json({'state': -1, 'info': 'state解析错误'})