You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.4 KiB
Python
52 lines
1.4 KiB
Python
from flask import Flask, request
|
|
import random
|
|
import os
|
|
import subprocess
|
|
import time
|
|
|
|
|
|
app = Flask(__name__)
|
|
USER_NAME = "ubuntu"
|
|
ROCKER_PATH = f"/home/{USER_NAME}/rocker/target/debug/rocker"
|
|
|
|
|
|
@app.route("/", methods=["POST"])
|
|
def main():
|
|
code = request.json.get("code", "")
|
|
|
|
_id = str(random.randint(8000_0000, 9000_0000))
|
|
user_path = f"/tmp/{_id}"
|
|
os.mkdir(user_path)
|
|
|
|
with open(f"{user_path}/main.py", "w") as f:
|
|
f.write(code)
|
|
|
|
try:
|
|
_ = subprocess.run(["sudo", ROCKER_PATH,
|
|
"--id", _id,
|
|
"--image", "ubuntu_pypy_numpy_pandas_user",
|
|
"--run", "python -u /tmp/main.py",
|
|
"--env", f"/home/{USER_NAME}/rocker/pypy.env",
|
|
"--volume", f"{user_path}:/tmp",
|
|
"--log"])
|
|
except Exception as e:
|
|
print(e)
|
|
|
|
time.sleep(1)
|
|
|
|
# 读取文件
|
|
out = ""
|
|
try:
|
|
log_path = f"/home/rocker/containers/{_id}/upper/logs/log"
|
|
with open(log_path) as f:
|
|
out = f.read()
|
|
except Exception as e:
|
|
print(e)
|
|
|
|
subprocess.run(["sudo", ROCKER_PATH,
|
|
"--rm", _id])
|
|
subprocess.run(["sudo", "rm", "-rf", user_path])
|
|
return {"msg": out, code: code, "id": _id}
|
|
|
|
if __name__ == "__main__":
|
|
app.run(host="0.0.0.0", port=8011, threaded=True) |