|
| 1 | +import os |
| 2 | +from secrets import token_urlsafe |
| 3 | +from flask import Blueprint, jsonify, request, redirect, abort |
| 4 | +from datetime import datetime |
| 5 | + |
| 6 | +from hwut_server.decorators import requires_authentication, check_authentication |
| 7 | +from hwut_server.models.jobs import Jobs, JobStatus |
| 8 | +from hwut_server.models.targets import Microcontrollers, Boards |
| 9 | +from hwut_server.database import db |
| 10 | +from hwut_server.utils import FILE_STORAGE |
| 11 | + |
| 12 | +mod = Blueprint('jobs', __name__, url_prefix='/jobs') |
| 13 | + |
| 14 | + |
| 15 | +@mod.route('/submit', methods=['PUT']) |
| 16 | +@requires_authentication |
| 17 | +def submit(): |
| 18 | + user = request.authorization.username |
| 19 | + |
| 20 | + duration_limit_seconds = 60 |
| 21 | + if request.args.get('duration_limit_seconds'): |
| 22 | + duration_limit_seconds = request.args.get('duration_limit_seconds') |
| 23 | + if duration_limit_seconds > 300: |
| 24 | + return abort(413, 'maximum execution time is 300 seconds') |
| 25 | + |
| 26 | + # board argument is optional |
| 27 | + board = request.args.get('board') |
| 28 | + if board: |
| 29 | + try: |
| 30 | + Boards.query.filter(Boards.name == board).one() |
| 31 | + except: |
| 32 | + return abort(400, 'board in unknown') |
| 33 | + |
| 34 | + microcontroller = request.args.get('microcontroller') |
| 35 | + if microcontroller is None: |
| 36 | + return abort(400, 'microcontroller must be specified') |
| 37 | + else: |
| 38 | + try: |
| 39 | + Microcontrollers.query.filter(Microcontrollers.name == microcontroller).one() |
| 40 | + except: |
| 41 | + return abort(400, 'microcontroller in unknown') |
| 42 | + |
| 43 | + filename_executable = token_urlsafe(32) |
| 44 | + size = request.content_length |
| 45 | + if size is None or size < 500: # minimum size 0.5kB |
| 46 | + return abort(400, 'executable file is missing or empty') |
| 47 | + if size is not None and size > 5000000: # Size limit 5MB |
| 48 | + return abort(413, 'maximum executable file size is 5MB') |
| 49 | + with open(os.path.join(FILE_STORAGE, filename_executable), 'wb') as f: |
| 50 | + f.write(request.stream.read()) |
| 51 | + |
| 52 | + try: |
| 53 | + job = Jobs( |
| 54 | + datetime.now(), |
| 55 | + JobStatus.WAITING, |
| 56 | + duration_limit_seconds, |
| 57 | + filename_executable, |
| 58 | + user, |
| 59 | + board, |
| 60 | + microcontroller, |
| 61 | + ) |
| 62 | + if request.args.get('comment'): |
| 63 | + job.comment = request.args.get('comment') |
| 64 | + db.session.add(job) |
| 65 | + db.session.commit() |
| 66 | + except: |
| 67 | + return abort(500, 'unable to create runner') |
| 68 | + return redirect(mod.url_prefix + '/' + str(job.id), 201) |
| 69 | + |
| 70 | + |
| 71 | +@mod.route('/<int:id>', methods=['GET']) |
| 72 | +def get(id): |
| 73 | + job = Jobs.query.filter(Jobs.id == id).one() |
| 74 | + auth = request.authorization |
| 75 | + extended = (auth and ((check_authentication(auth.username, auth.password, superuser=True)) or ( |
| 76 | + check_authentication(auth.username, auth.password) and auth.username == job.owner))) |
| 77 | + return jsonify(job.to_dict(extended)) |
| 78 | + |
| 79 | + |
| 80 | +@mod.route('/<int:id>', methods=['DELETE']) |
| 81 | +@requires_authentication |
| 82 | +def cancel(id): |
| 83 | + job = Jobs.query.filter(Jobs.id == id).one() |
| 84 | + auth = request.authorization |
| 85 | + if auth and ( |
| 86 | + (check_authentication(auth.username, auth.password, superuser=True)) |
| 87 | + or (check_authentication(auth.username, auth.password) and auth.username == job.owner)): |
| 88 | + if job.status == JobStatus.WAITING: |
| 89 | + job.status = JobStatus.CANCELED |
| 90 | + db.session.commit() |
| 91 | + return jsonify(job.to_dict(True)), 204 |
| 92 | + else: |
| 93 | + return abort(400, 'Unable to cancel job. Current job status: {}'.format(job.status.name)) |
| 94 | + else: |
| 95 | + return abort(401) |
| 96 | + |
| 97 | + |
| 98 | +@mod.route('', methods=['GET']) |
| 99 | +@requires_authentication |
| 100 | +def list_jobs(): |
| 101 | + auth = request.authorization |
| 102 | + if check_authentication(auth.username, auth.password, superuser=True): |
| 103 | + job_list = Jobs.query.all() |
| 104 | + else: |
| 105 | + job_list = Jobs.query.filter(Jobs.owner == auth.username).all() |
| 106 | + extended = False |
| 107 | + if request.args.get('extended') and request.args.get('extended') == '1': |
| 108 | + extended = True |
| 109 | + return jsonify([job.to_dict(extended) for job in job_list]) |
0 commit comments