完成一个测试服务器

main
阳光少年 1 year ago
parent 8db6ac26b9
commit dc3fc432b5

@ -47,6 +47,9 @@ static N: &str = "🔴";
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct RockerArgs {
#[arg(long)]
id: Option<String>,
// --wait/--log --run /bin/bash --image busybox
#[arg(long)]
run: Option<String>,
@ -96,6 +99,7 @@ struct RockerArgs {
#[arg(long)]
cgroup: Option<String>,
}
@ -699,8 +703,13 @@ fn main() -> Result<()>{
if args.image.is_some() || args.restart.is_some() {
let container_info = match (&args.run, &args.image, &args.restart) {
(Some(cmd), Some(image), None) => {
let container_id = args.id.unwrap_or(uuid::Uuid::new_v4().to_string()[0..8].to_string());
if get_container_info(&container_id).is_ok() {
return Err(RockerError::OtherError(format!("{N} 容器 {container_id} 已经存在")));
};
ContainerInfo {
id: uuid::Uuid::new_v4().to_string()[0..8].to_string(),
id: container_id,
run: cmd.clone(),
image: image.clone(),
volume: args.volume.clone().unwrap_or_default(),

@ -0,0 +1,19 @@
import requests
URL = "http://192.168.230.132:8011"
code = """
import time
print("hello world")
cnt = 0
while True:
print(cnt)
cnt += 1
time.sleep(0.1)
"""
resp = requests.post(URL, json={"code": code})
print(resp.json()["msg"])

@ -0,0 +1,48 @@
from flask import Flask, request
import random
import os
import subprocess
import time
app = Flask(__name__)
ROCKER_PATH = "/home/yanguangshaonian/rocker/target/debug/rocker"
@app.route("/", methods=["POST"])
def hello_world():
_id = str(random.randint(8000_0000, 9000_0000))
user_path = f"/tmp/{_id}"
os.mkdir(user_path)
code = request.json.get("code", "")
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", "/home/yanguangshaonian/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"
print(f"读取: {log_path}")
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)
Loading…
Cancel
Save