-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
299 lines (256 loc) · 10.3 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
# coding=utf-8
# Developed by Nicholas Dehnen & Vincent Scharf.
# Copyright (c) 2019 Deutsche Telekom Intellectual Property.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from sanic import Sanic, exceptions, response
from jinja2 import Environment, PackageLoader
import os, uuid, json, asyncio
from utils import test_runner
from utils.git_provision import *
from utils.redis_layer import RedisRun, RedisGit, RedisId
from utils import redis_layer
app = Sanic(__name__)
env = Environment(loader=PackageLoader('app', 'templates'), trim_blocks=True)
upload_dir = "./uploads"
git_repo_dir = "./git"
app.static('/static', './static')
app.static('/favicon.ico', './static/favicon.ico')
base_url = "https://he.v4.nu:8914/" # should probably be set using env variables later on
@app.route('/')
async def index(request):
"""
Serves the index / home page of vvp-web.
:param request:
:return: HTML generated by Jinja2 from "index.html"
"""
template = env.get_template('index.html')
html = template.render()
return response.html(html)
@app.route("/upload", methods=['POST'])
async def upload_file(request):
"""
Handles file uploads on the home page. Files get stored in <upload_dir>/<uid>_filename, the uid and path get stored
in the database.
:param request:
:return: JSON data, empty on failure, containing uid on success
"""
if not os.path.exists(upload_dir):
os.makedirs(upload_dir)
extension = os.path.splitext(request.files["file"][0].name)[1]
if extension != '.zip':
return response.json(False, 400)
unique_id = str(uuid.uuid4())
path = upload_dir + "/" + unique_id + "_" + request.files["file"][0].name
f = open(path, "wb")
f.write(request.files["file"][0].body)
f.close()
RedisRun(unique_id).set_path(path)
print("Stored file '" + path + "' as: " + unique_id)
return response.json({'image_url': '/static/images/zip-file_graphical.svg',
'uid': unique_id})
@app.route("/next/<uuid>", methods=['GET', 'POST'])
async def process_heat(request, uuid):
"""
Displays the "next" page for a given uid. This page shows the validation progress and updates on its own using AJAX.
:param request:
:param uuid: Validation run uid
:return: HTML generated by Jinja2 from "progress.html" (or "error.html" on failure)
"""
rl = RedisRun(uuid)
path = rl.get_path()
if path:
path = os.path.abspath(path)
if rl.get_status()['progress'] <= 20 and 'is_git' not in rl.get():
rl.set_status("Starting test run..", 1, "running")
# TODO: Maybe take path out of this later on as its not required
asyncio.ensure_future(test_runner.run_tests(uuid, path))
else:
return response.html(env.get_template('error.html').render(
error="Could not find (path for) uuid. (status=" + rl.get_status() + ")"))
return response.html(env.get_template('progress.html').render(uid=uuid))
@app.route("/status/<uuid>", methods=['GET'])
async def return_status(request, uuid):
"""
Queries the database for the current status of a run. This only includes the state (running/success/error).
:param request:
:param uuid: Validation run uid
:return: JSON data, empty on failure, populated on success
"""
status = RedisRun(uuid).get_status()
if not status:
return response.json(False, 500)
return response.json(status)
@app.route("/status/<uuid>/full", methods=['GET'])
async def return_status(request, uuid):
"""
Queries the database for all data available for a given run id. This includes everything returned from /status/<uuid>,
as well as the file path and the names and outcomes of all tests which have been ran.
:param request:
:param uuid: Validation run uid
:return: JSON data, empty on failure, populated on success
"""
status = RedisRun(uuid).get()
if not status:
return response.json(False, 500)
return response.json(status)
@app.route("/status/<uuid>/slim", methods=['GET'])
async def return_status(request, uuid):
"""
Queries the database for data for a given run id. Returns a mix of the data returned by /status/<uuid>/full and
/status/<uuid>, specifically the uid, the state, the commit hash (if available) and the test results.
:param request:
:param uuid: Validation run uid
:return: JSON data, empty on failure, populated on success
"""
run = RedisRun(uuid)
d = {
'uid': uuid,
'status': run.get_status()['state'],
'commit': run.get()['commit_hash'],
'result': run.get_result()
}
if not d:
return response.json(False, 500)
return response.json(d)
@app.route("/result/<uuid>", methods=['GET', 'POST'])
async def show_results(request, uuid):
"""
Renders the result page for a given test uid, containing all information about test outcomes. This page is being
displayed after the /next/ stage finished, eg. the test run completed.
:param request:
:param uuid: Validation run uid
:return: HTML generated by Jinja2 from "results.html" (or "error.html" on failure)
"""
rl = RedisRun(uuid)
res = rl.get_result()
tests = rl.get_tests()
if not res:
return response.html(env.get_template('error.html').render(error="Could not find test results for uuid."))
return response.html(env.get_template('results.html').render(result=res, items=tests))
@app.route("/delete/<uuid>", methods=['POST'])
async def upload_file(request, uuid):
"""
Handles deletion of an uploaded file, only used on the homepage.
:param request:
:param uuid: Validation run uid
:return: JSON data, empty on failure, containing True on success.
"""
path = RedisRun(uuid).get_path()
if not uuid or not path:
print("Error: Could not get uuid '" + uuid + "' from Redis.")
return response.json(False, 400)
print("Deleting: '" + path + "' on user request.")
os.remove(path)
return response.json(True)
@app.route("/no_file_selected")
async def no_file_selected(request):
"""
Renders the "error.html" page with a custom message in case the user did nto upload a file.
:param request:
:return: HTML generated by Jinja2 from "error.html"
"""
template = env.get_template('error.html') #replace this with a nicer looking page later on
html = template.render(error="No file selected.")
return response.html(html)
@app.route("/git_init") # TODO: Make this whole routine async as well.
async def git_init(request):
"""
Starts the git repository initialization process in case the user wants to make use of the git history functionality.
:param request:
:return: JSON data, containing an error message on failure or the repository name / url on success.
"""
v = create_repo()
return response.json(v)
# deprecated, use /history/
@app.route("/runs/<id>")
async def git_runs(request, id):
"""
Returns all test runs for a given git repository id. Don't use this, use /history/ instead.
:param request:
:param id: Git repository id
:return: All test runs of the given Git repository id in JSON format
"""
return response.json(RedisGit(id).get_runs())
@app.route("/history/<id>")
async def git_history(request, id):
"""
Returns all test runs for a given git repository id in /status/<..>/slim format.
:param request:
:param id: Git respository id
:return: Run history data for the given repository in JSON format
"""
runs = RedisGit(id).get_runs()
d = {}
for c, (no, uid) in enumerate(runs.items()):
run = RedisRun(uid)
d[c] = {
'uid': uid,
'status': run.get_status()['state'],
'commit': run.get()['commit_hash'],
'result': run.get_result()
}
return response.json(d)
@app.route("/results/<id>")
async def run_result(request, id):
"""
Returns the results for a given test run id.
:param request:
:param id: Test run id
:return: Results of the given test run in JSON format
"""
return response.json(RedisRun(id).get())
# This response is shown in the users git client!
@app.route("/commit/<id>")
async def git_commit(request, id):
"""
Returns the status of a given commit id.
:param request:
:param id: Git repository commit id
:return: Status message for the given commit id
"""
print("New commit in repo: " + id)
unique_id = str(uuid.uuid4())
print("Generated uuid for run: " + unique_id)
d = await checkout_repo(unique_id, id)
return response.text("== vvp-web ==\n\nYour commit is now being processed.\nPlease visit " + base_url + "repo/" +
id + " for an overview of all test runs, or " + base_url + "next/" + unique_id +
" to follow up with the progress of the current test run.")
@app.route("/repo/<id>")
async def show_repo(request, id):
"""
Serves the repository summary page showing all test runs and run information in the /status/<..>/slim format
:param request:
:param id: Git respository id
:return: HTML generated by Jinja2 from "repo.html" (or "error.html" on failure)
"""
template = env.get_template('repo.html')
html = template.render()
return response.html(html)
# debug-ish function
@app.route("/uuid/<id>")
async def get_uuid(request, id):
v = RedisId(id).get()
return response.json(v)
@app.exception(exceptions.SanicException)
async def server_error(request, exception):
"""
Serves error page
:param request:
:param exception: Error message
:return: HTML generated by Jinja2 from "error.html"
"""
template = env.get_template('error.html')
html = template.render(error=str(exception))
return response.html(html)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8913, workers=2)