diff --git a/src/main.rs b/src/main.rs index aa905d3..3169a66 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,6 +47,9 @@ static N: &str = "🔴"; #[derive(Parser, Debug)] #[command(version, about, long_about = None)] struct RockerArgs { + #[arg(long)] + id: Option, + // --wait/--log --run /bin/bash --image busybox #[arg(long)] run: Option, @@ -96,6 +99,7 @@ struct RockerArgs { #[arg(long)] cgroup: Option, + } @@ -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(), diff --git a/test/service/client.py b/test/service/client.py new file mode 100644 index 0000000..4d239e7 --- /dev/null +++ b/test/service/client.py @@ -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"]) + + diff --git a/test/service/server.py b/test/service/server.py new file mode 100644 index 0000000..2de20ca --- /dev/null +++ b/test/service/server.py @@ -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) \ No newline at end of file diff --git a/test/test.py b/test/test.py index 98bca46..be6b682 100755 --- a/test/test.py +++ b/test/test.py @@ -8,7 +8,7 @@ import os import threading import ctypes -cnt = 0 +cnt = 0 print(cnt, "hello", time.time()) while True: