From 002728825710cb806c71da6d154538c51d3d0fcc Mon Sep 17 00:00:00 2001 From: Codex Bot Date: Fri, 2 Oct 2026 01:11:10 +0800 Subject: [PATCH] Automate ephemeral AutoDL Pro runners --- README.md | 45 +++ ops/__init__.py | 1 + ops/autodl-pro.example.json | 60 ++++ ops/autodl_pro_controller.py | 519 ++++++++++++++++++++++++++++ tests/test_autodl_pro_controller.py | 69 ++++ 5 files changed, 694 insertions(+) create mode 100644 ops/__init__.py create mode 100644 ops/autodl-pro.example.json create mode 100755 ops/autodl_pro_controller.py create mode 100644 tests/test_autodl_pro_controller.py diff --git a/README.md b/README.md index e400c85..68d94b8 100644 --- a/README.md +++ b/README.md @@ -142,3 +142,48 @@ uv run python -m eval.benchmark_extractor \ CI 的单 epoch smoke run 会执行 benchmark 来验证整条评测管线,但不会使用 `--enforce-gates`,因为 smoke 模型并不代表正式实验是否达标。正式训练完成后使用上述强制命令。 + +## AutoDL Pro 临时 Runner + +项目代码始终只在本地 Git 工作区和 Gitea 中维护。阿里云服务器是持久化的算力控制器与 +模型/产物中转站,AutoDL Pro 实例只是临时 Gitea Actions 执行节点。控制器不会把项目源码 +复制到任何服务器;ephemeral runner 接受任务后,workflow 才从 Gitea checkout 指定 commit。 + +`ops/autodl_pro_controller.py` 实现以下生命周期: + +1. 调用 AutoDL Pro API 创建一张 `PRO6000-96G`; +2. 等待 SSH 就绪,将模型数据和经过 SHA-256 固定的 `act_runner` 传入实例; +3. 注册带 `ubuntu-latest:host`、`ces-ci:host`、`gpu:host` 标签的一次性 runner; +4. 可选地通过 Gitea API dispatch `gpu-train.yaml`,也可保留为手动触发; +5. 等待 runner 完成一项任务并退出,将 checkpoint、benchmark 和 runner 日志回传阿里云; +6. 无论成功或失败都调用 API 关机;默认不释放实例,避免未经确认删除系统盘。 + +先把示例配置复制到阿里云控制器的受限目录并填写私有镜像 UUID: + +```bash +install -d -m 700 /root/.config/ces-controller/secrets +install -m 600 ops/autodl-pro.example.json /root/.config/ces-controller/config.json +``` + +AutoDL 开发者 Token、Gitea runner 注册 Token,以及可选的 Gitea API Token 必须分别放在 +配置指定的 root-only 文件中,不能写进配置、仓库、命令行或 AutoDL `start_command`。控制器 +还需要一把专用于 AutoDL 的 SSH 私钥;将其公钥预先添加到 AutoDL 账号的 SSH 公钥设置。 + +首次运行先检查不会产生费用的执行计划: + +```bash +python3 ops/autodl_pro_controller.py plan \ + --config /root/.config/ces-controller/config.json +``` + +确认镜像、GPU、系统盘扩容和是否自动 dispatch 后,再在 `tmux` 或 systemd 中运行付费流程: + +```bash +python3 ops/autodl_pro_controller.py run \ + --config /root/.config/ces-controller/config.json \ + --confirm-create +``` + +`run` 会真实创建按量计费实例。`release_after_run` 默认是 `false`;只有确认回传产物完整并愿意 +删除该 Pro 实例时才应改为 `true`。控制器只负责调度与数据搬运,训练步骤仍完全由 +`.gitea/workflows/gpu-train.yaml` 定义。 diff --git a/ops/__init__.py b/ops/__init__.py new file mode 100644 index 0000000..6c9ff02 --- /dev/null +++ b/ops/__init__.py @@ -0,0 +1 @@ +"""Operational helpers for reproducible research infrastructure.""" diff --git a/ops/autodl-pro.example.json b/ops/autodl-pro.example.json new file mode 100644 index 0000000..a4bc678 --- /dev/null +++ b/ops/autodl-pro.example.json @@ -0,0 +1,60 @@ +{ + "autodl": { + "base_url": "https://api.autodl.com", + "token_file": "/root/.config/ces-controller/secrets/autodl-token", + "data_center_list": [], + "req_gpu_amount": 1, + "gpu_spec_uuid": "pro6000-p", + "image_uuid": "REPLACE_WITH_PYTORCH_2_8_PRIVATE_IMAGE_UUID", + "cuda_v_from": 128, + "expand_system_disk_by_gb": 120, + "instance_name": "ces-gitea-runner", + "startup_timeout_seconds": 1800, + "shutdown_timeout_seconds": 600, + "max_runtime_hours": 24, + "release_after_run": false + }, + "ssh": { + "private_key": "/root/.config/ces-controller/autodl_ed25519", + "known_hosts": "/root/.config/ces-controller/known_hosts", + "connect_timeout_seconds": 600 + }, + "runner": { + "gitea_instance": "https://gitea.entroia.com/", + "registration_token_file": "/root/.config/ces-controller/secrets/gitea-runner-token", + "name_prefix": "ces-autodl-pro6000", + "labels": [ + "ubuntu-latest:host", + "ces-ci:host", + "gpu:host" + ], + "job_timeout": "24h", + "cache_path": "/var/cache/ces-controller/act_runner-0.2.13-linux-amd64", + "download_url": "https://dl.gitea.com/gitea-runner/0.2.13/act_runner-0.2.13-linux-amd64", + "sha256": "3acac8b506ac8cadc88a55155b5d6378f0fab0b8f62d1e0c0450f4ccd69733e2" + }, + "dispatch": { + "enabled": false, + "gitea_instance": "https://gitea.entroia.com", + "repository": "Zayden-Jung/composable-evidence-states-research", + "workflow": "gpu-train.yaml", + "ref": "main", + "token_file": "/root/.config/ces-controller/secrets/gitea-api-token" + }, + "transfer": { + "model_source": "/data/ces-models/siglip2-giant", + "model_destination": "/root/autodl-tmp/ces-models/siglip2-giant", + "model_required_files": [ + "config.json", + "model.safetensors.index.json", + "model-00001-of-00002.safetensors", + "model-00002-of-00002.safetensors", + "preprocessor_config.json", + "tokenizer.json", + "tokenizer.model", + "tokenizer_config.json" + ], + "artifact_source": "/root/autodl-tmp/ces-artifacts/siglip2-giant", + "artifact_destination": "/data/ces-artifacts" + } +} diff --git a/ops/autodl_pro_controller.py b/ops/autodl_pro_controller.py new file mode 100755 index 0000000..ec55b4d --- /dev/null +++ b/ops/autodl_pro_controller.py @@ -0,0 +1,519 @@ +#!/usr/bin/env python3 +"""Create an AutoDL Pro worker, attach an ephemeral Gitea runner, and tear it down. + +This program is intended to run on the persistent relay/controller host. Project +source is never copied by this program: Gitea Actions checks it out after the +runner accepts a job. Only model data, the runner binary, and runner credentials +are transferred to the temporary AutoDL worker. +""" + +from __future__ import annotations + +import argparse +import hashlib +import json +import shlex +import stat +import subprocess +import sys +import tempfile +import time +import urllib.error +import urllib.request +from dataclasses import dataclass +from pathlib import Path +from typing import Any + + +DEFAULT_RUNNER_URL = ( + "https://dl.gitea.com/gitea-runner/0.2.13/act_runner-0.2.13-linux-amd64" +) +DEFAULT_RUNNER_SHA256 = "3acac8b506ac8cadc88a55155b5d6378f0fab0b8f62d1e0c0450f4ccd69733e2" +DEFAULT_MODEL_FILES = [ + "config.json", + "model.safetensors.index.json", + "model-00001-of-00002.safetensors", + "model-00002-of-00002.safetensors", + "preprocessor_config.json", + "tokenizer.json", + "tokenizer.model", + "tokenizer_config.json", +] + + +class ControllerError(RuntimeError): + pass + + +def read_secret(path: str) -> str: + secret_path = Path(path).expanduser() + mode = stat.S_IMODE(secret_path.stat().st_mode) + if mode & 0o077: + raise ControllerError(f"secret file must not be group/world accessible: {secret_path}") + value = secret_path.read_text(encoding="utf-8").strip() + if not value: + raise ControllerError(f"secret file is empty: {secret_path}") + return value + + +class AutoDLProClient: + def __init__(self, base_url: str, token: str, timeout: int = 30): + self.base_url = base_url.rstrip("/") + self.token = token + self.timeout = timeout + + def request(self, method: str, path: str, body: dict[str, Any]) -> Any: + payload = json.dumps(body).encode("utf-8") + request = urllib.request.Request( + f"{self.base_url}{path}", + data=payload, + method=method, + headers={"Authorization": self.token, "Content-Type": "application/json"}, + ) + try: + with urllib.request.urlopen(request, timeout=self.timeout) as response: + parsed = json.loads(response.read().decode("utf-8")) + except (urllib.error.URLError, json.JSONDecodeError) as exc: + raise ControllerError(f"AutoDL API request failed: {method} {path}: {exc}") from exc + if parsed.get("code") != "Success": + raise ControllerError( + f"AutoDL API rejected {method} {path}: " + f"code={parsed.get('code')!r}, msg={parsed.get('msg')!r}" + ) + return parsed.get("data") + + def create(self, spec: dict[str, Any]) -> str: + return str(self.request("POST", "/api/v1/dev/instance/pro/create", spec)) + + def status(self, instance_uuid: str) -> str: + return str( + self.request( + "GET", "/api/v1/dev/instance/pro/status", {"instance_uuid": instance_uuid} + ) + ) + + def snapshot(self, instance_uuid: str) -> dict[str, Any]: + return dict( + self.request( + "GET", "/api/v1/dev/instance/pro/snapshot", {"instance_uuid": instance_uuid} + ) + ) + + def power_off(self, instance_uuid: str) -> None: + self.request( + "POST", "/api/v1/dev/instance/pro/power_off", {"instance_uuid": instance_uuid} + ) + + def release(self, instance_uuid: str) -> None: + self.request( + "POST", "/api/v1/dev/instance/pro/release", {"instance_uuid": instance_uuid} + ) + + +@dataclass(frozen=True) +class SshTarget: + host: str + port: int + key: Path + known_hosts: Path + + @classmethod + def from_snapshot(cls, snapshot: dict[str, Any], ssh_config: dict[str, Any]) -> "SshTarget": + host = str(snapshot.get("proxy_host", "")).strip() + port = int(snapshot.get("ssh_port", 0)) + if not host or port <= 0: + raise ControllerError("AutoDL snapshot did not contain a usable SSH endpoint") + return cls( + host=host, + port=port, + key=Path(ssh_config["private_key"]).expanduser(), + known_hosts=Path(ssh_config["known_hosts"]).expanduser(), + ) + + def options(self) -> list[str]: + return [ + "-F", + "/dev/null", + "-o", + "BatchMode=yes", + "-o", + "IdentitiesOnly=yes", + "-o", + "StrictHostKeyChecking=accept-new", + "-o", + f"UserKnownHostsFile={self.known_hosts}", + "-i", + str(self.key), + "-p", + str(self.port), + ] + + def ssh(self, command: str, *, check: bool = True) -> subprocess.CompletedProcess[str]: + return subprocess.run( + ["ssh", *self.options(), f"root@{self.host}", command], + check=check, + text=True, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + ) + + def rsync_to(self, source: Path, destination: str) -> None: + ssh_transport = ["ssh", *self.options()] + subprocess.run( + [ + "rsync", + "-ah", + "--partial", + "--append-verify", + "-e", + shlex.join(ssh_transport), + f"{source}/", + f"root@{self.host}:{destination.rstrip('/')}/", + ], + check=True, + ) + + def rsync_from(self, source: str, destination: Path) -> None: + destination.mkdir(parents=True, exist_ok=True) + ssh_transport = ["ssh", *self.options()] + subprocess.run( + [ + "rsync", + "-ah", + "--partial", + "-e", + shlex.join(ssh_transport), + f"root@{self.host}:{source.rstrip('/')}/", + f"{destination}/", + ], + check=True, + ) + + def scp_to(self, source: Path, destination: str) -> None: + options = self.options() + # scp uses uppercase -P for its port option. + options[options.index("-p")] = "-P" + subprocess.run( + ["scp", *options, str(source), f"root@{self.host}:{destination}"], check=True + ) + + +def load_config(path: Path) -> dict[str, Any]: + config = json.loads(path.read_text(encoding="utf-8")) + for section in ("autodl", "runner", "ssh", "transfer"): + if section not in config: + raise ControllerError(f"missing configuration section: {section}") + return config + + +def preflight(config: dict[str, Any]) -> None: + autodl = config["autodl"] + if str(autodl["image_uuid"]).startswith("REPLACE_"): + raise ControllerError("replace the placeholder AutoDL private image UUID") + read_secret(autodl["token_file"]) + read_secret(config["runner"]["registration_token_file"]) + if config.get("dispatch", {}).get("enabled", False): + read_secret(config["dispatch"]["token_file"]) + + private_key = Path(config["ssh"]["private_key"]).expanduser() + if not private_key.is_file(): + raise ControllerError(f"AutoDL SSH private key does not exist: {private_key}") + if stat.S_IMODE(private_key.stat().st_mode) & 0o077: + raise ControllerError(f"SSH private key must have mode 0600: {private_key}") + + model_source = Path(config["transfer"]["model_source"]).expanduser() + missing = [ + name + for name in config["transfer"].get("model_required_files", DEFAULT_MODEL_FILES) + if not (model_source / name).is_file() or (model_source / name).stat().st_size == 0 + ] + if missing: + raise ControllerError(f"model download is incomplete; file check failed: {missing}") + + +def download_runner(config: dict[str, Any]) -> Path: + runner = config["runner"] + cache_path = Path(runner["cache_path"]).expanduser() + expected = runner.get("sha256", DEFAULT_RUNNER_SHA256) + if cache_path.exists() and sha256(cache_path) == expected: + return cache_path + cache_path.parent.mkdir(parents=True, exist_ok=True) + url = runner.get("download_url", DEFAULT_RUNNER_URL) + print(f"Downloading pinned act_runner from {url}", flush=True) + with urllib.request.urlopen(url, timeout=300) as response, tempfile.NamedTemporaryFile( + dir=cache_path.parent, delete=False + ) as temporary: + while chunk := response.read(1024 * 1024): + temporary.write(chunk) + temporary_path = Path(temporary.name) + if sha256(temporary_path) != expected: + temporary_path.unlink(missing_ok=True) + raise ControllerError("downloaded act_runner checksum did not match the pinned checksum") + temporary_path.chmod(0o755) + temporary_path.replace(cache_path) + return cache_path + + +def sha256(path: Path) -> str: + digest = hashlib.sha256() + with path.open("rb") as source: + while chunk := source.read(1024 * 1024): + digest.update(chunk) + return digest.hexdigest() + + +def wait_for_status( + client: AutoDLProClient, instance_uuid: str, desired: str, timeout_seconds: int +) -> None: + deadline = time.monotonic() + timeout_seconds + last = "" + while time.monotonic() < deadline: + last = client.status(instance_uuid) + print(f"AutoDL instance {instance_uuid}: {last}", flush=True) + if last == desired: + return + if last in {"error", "released"}: + break + time.sleep(10) + raise ControllerError(f"instance did not reach {desired!r}; last status was {last!r}") + + +def wait_for_ssh(target: SshTarget, timeout_seconds: int) -> None: + target.known_hosts.parent.mkdir(parents=True, exist_ok=True) + target.known_hosts.touch(mode=0o600, exist_ok=True) + deadline = time.monotonic() + timeout_seconds + while time.monotonic() < deadline: + result = target.ssh("true", check=False) + if result.returncode == 0: + return + time.sleep(10) + raise ControllerError("SSH did not become ready before the timeout") + + +def runner_config(labels: list[str], timeout: str) -> str: + rendered_labels = "\n".join(f' - "{label}"' for label in labels) + return f"""log: + level: info +runner: + file: /opt/ces-runner/.runner + capacity: 1 + timeout: {timeout} + fetch_timeout: 5s + fetch_interval: 2s + labels: +{rendered_labels} +container: + docker_host: "-" +""" + + +def provision_worker(config: dict[str, Any], target: SshTarget, runner_binary: Path) -> None: + transfer = config["transfer"] + runner = config["runner"] + model_source = Path(transfer["model_source"]).expanduser() + if not model_source.is_dir(): + raise ControllerError(f"model source directory does not exist: {model_source}") + token_file = Path(runner["registration_token_file"]).expanduser() + read_secret(str(token_file)) # Validate permissions and contents before copying. + + target.ssh( + "install -d -m 700 /opt/ces-runner /run/ces-runner-secrets " + f"{shlex.quote(transfer['model_destination'])}" + ) + target.rsync_to(model_source, transfer["model_destination"]) + target.scp_to(runner_binary, "/opt/ces-runner/act_runner") + target.scp_to(token_file, "/run/ces-runner-secrets/registration-token") + + labels = list(runner["labels"]) + with tempfile.NamedTemporaryFile("w", encoding="utf-8", delete=False) as temporary: + temporary.write(runner_config(labels, runner.get("job_timeout", "24h"))) + config_path = Path(temporary.name) + try: + target.scp_to(config_path, "/opt/ces-runner/config.yaml") + finally: + config_path.unlink(missing_ok=True) + + name = f"{runner['name_prefix']}-{int(time.time())}" + labels_argument = ",".join(labels) + register = " ".join( + [ + "cd /opt/ces-runner && chmod 700 act_runner", + "&& chmod 600 /run/ces-runner-secrets/registration-token config.yaml", + "&& ./act_runner -c config.yaml register --no-interactive --ephemeral", + f"--instance {shlex.quote(runner['gitea_instance'])}", + "--token-file /run/ces-runner-secrets/registration-token", + f"--name {shlex.quote(name)}", + f"--labels {shlex.quote(labels_argument)}", + ] + ) + result = target.ssh(register, check=False) + if result.returncode != 0: + raise ControllerError(f"runner registration failed:\n{result.stdout}") + + start = ( + "cd /opt/ces-runner && " + "nohup sh -c './act_runner -c config.yaml daemon; " + 'code=$?; printf %s "$code" > runner.exit; ' + "rm -f /run/ces-runner-secrets/registration-token' " + "> runner.log 2>&1 < /dev/null & echo $! > runner.pid" + ) + target.ssh(start) + print(f"Registered ephemeral runner {name}", flush=True) + + +def dispatch_workflow(config: dict[str, Any]) -> None: + dispatch = config.get("dispatch", {}) + if not dispatch.get("enabled", False): + print("Workflow dispatch disabled; trigger Formal GPU training in Gitea now.", flush=True) + return + token = read_secret(dispatch["token_file"]) + body = json.dumps({"ref": dispatch.get("ref", "main")}).encode("utf-8") + url = ( + f"{dispatch['gitea_instance'].rstrip('/')}/api/v1/repos/" + f"{dispatch['repository']}/actions/workflows/{dispatch['workflow']}/dispatches" + ) + request = urllib.request.Request( + url, + data=body, + method="POST", + headers={"Authorization": f"token {token}", "Content-Type": "application/json"}, + ) + try: + with urllib.request.urlopen(request, timeout=30) as response: + if response.status not in {200, 201, 204}: + raise ControllerError(f"unexpected workflow dispatch status: {response.status}") + except urllib.error.URLError as exc: + raise ControllerError(f"Gitea workflow dispatch failed: {exc}") from exc + print(f"Dispatched {dispatch['workflow']} at {dispatch.get('ref', 'main')}", flush=True) + + +def wait_for_runner(target: SshTarget, max_runtime_hours: float) -> int: + deadline = time.monotonic() + max_runtime_hours * 3600 + while time.monotonic() < deadline: + result = target.ssh( + "test -f /opt/ces-runner/runner.exit && cat /opt/ces-runner/runner.exit", + check=False, + ) + if result.returncode == 0 and result.stdout.strip().isdigit(): + return int(result.stdout.strip()) + time.sleep(30) + raise ControllerError("runner exceeded the configured maximum runtime") + + +def create_spec(config: dict[str, Any]) -> dict[str, Any]: + autodl = config["autodl"] + spec = { + "req_gpu_amount": autodl.get("req_gpu_amount", 1), + "expand_system_disk_by_gb": autodl["expand_system_disk_by_gb"], + "gpu_spec_uuid": autodl["gpu_spec_uuid"], + "image_uuid": autodl["image_uuid"], + "cuda_v_from": autodl["cuda_v_from"], + "instance_name": autodl.get("instance_name", "ces-gitea-runner"), + } + if autodl.get("data_center_list"): + spec["data_center_list"] = autodl["data_center_list"] + return spec + + +def print_plan(config: dict[str, Any]) -> None: + safe = { + "create": create_spec(config), + "model": { + "source": config["transfer"]["model_source"], + "destination": config["transfer"]["model_destination"], + }, + "runner": { + "ephemeral": True, + "labels": config["runner"]["labels"], + "gitea_instance": config["runner"]["gitea_instance"], + }, + "dispatch": { + key: value + for key, value in config.get("dispatch", {}).items() + if key != "token_file" + }, + "cleanup": { + "power_off": True, + "release": config["autodl"].get("release_after_run", False), + }, + } + print(json.dumps(safe, indent=2, ensure_ascii=False)) + + +def run(config: dict[str, Any]) -> None: + preflight(config) + token = read_secret(config["autodl"]["token_file"]) + client = AutoDLProClient(config["autodl"].get("base_url", "https://api.autodl.com"), token) + runner_binary = download_runner(config) + instance_uuid: str | None = None + target: SshTarget | None = None + try: + instance_uuid = client.create(create_spec(config)) + print(f"Created AutoDL Pro instance {instance_uuid}", flush=True) + wait_for_status( + client, + instance_uuid, + "running", + int(config["autodl"].get("startup_timeout_seconds", 1800)), + ) + target = SshTarget.from_snapshot(client.snapshot(instance_uuid), config["ssh"]) + wait_for_ssh(target, int(config["ssh"].get("connect_timeout_seconds", 600))) + provision_worker(config, target, runner_binary) + dispatch_workflow(config) + runner_exit = wait_for_runner( + target, float(config["autodl"].get("max_runtime_hours", 24)) + ) + print(f"Ephemeral runner exited with code {runner_exit}", flush=True) + target.rsync_from( + config["transfer"]["artifact_source"], + Path(config["transfer"]["artifact_destination"]).expanduser() / instance_uuid, + ) + target.rsync_from( + "/opt/ces-runner", + Path(config["transfer"]["artifact_destination"]).expanduser() + / instance_uuid + / "runner", + ) + finally: + if instance_uuid is not None: + if target is not None: + target.ssh("rm -f /run/ces-runner-secrets/registration-token", check=False) + print(f"Powering off AutoDL Pro instance {instance_uuid}", flush=True) + client.power_off(instance_uuid) + if config["autodl"].get("release_after_run", False): + wait_for_status( + client, + instance_uuid, + "stopped", + int(config["autodl"].get("shutdown_timeout_seconds", 600)), + ) + client.release(instance_uuid) + print(f"Released AutoDL Pro instance {instance_uuid}", flush=True) + + +def main() -> int: + parser = argparse.ArgumentParser() + parser.add_argument("command", choices=("plan", "run")) + parser.add_argument("--config", type=Path, required=True) + parser.add_argument( + "--confirm-create", + action="store_true", + help="required with 'run'; acknowledges that a paid AutoDL instance will be created", + ) + args = parser.parse_args() + try: + config = load_config(args.config) + if args.command == "plan": + print_plan(config) + else: + if not args.confirm_create: + raise ControllerError("run requires --confirm-create because it starts billing") + run(config) + except (ControllerError, OSError, subprocess.CalledProcessError) as exc: + print(f"error: {exc}", file=sys.stderr) + return 1 + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tests/test_autodl_pro_controller.py b/tests/test_autodl_pro_controller.py new file mode 100644 index 0000000..fb28b40 --- /dev/null +++ b/tests/test_autodl_pro_controller.py @@ -0,0 +1,69 @@ +import json +import stat +from pathlib import Path + +import pytest + +from ops import autodl_pro_controller as controller + + +MODULE_PATH = Path(controller.__file__) + + +def test_create_spec_excludes_empty_data_centers(): + config = { + "autodl": { + "req_gpu_amount": 1, + "expand_system_disk_by_gb": 120, + "gpu_spec_uuid": "pro6000-p", + "image_uuid": "image-test", + "cuda_v_from": 128, + "data_center_list": [], + } + } + spec = controller.create_spec(config) + assert spec["gpu_spec_uuid"] == "pro6000-p" + assert "data_center_list" not in spec + + +def test_snapshot_to_ssh_target(tmp_path): + target = controller.SshTarget.from_snapshot( + {"proxy_host": "connect.example", "ssh_port": 32001}, + { + "private_key": str(tmp_path / "key"), + "known_hosts": str(tmp_path / "known_hosts"), + }, + ) + assert target.host == "connect.example" + assert target.port == 32001 + assert "IdentitiesOnly=yes" in target.options() + + +def test_runner_config_disables_docker_and_keeps_host_labels(): + rendered = controller.runner_config( + ["ubuntu-latest:host", "ces-ci:host", "gpu:host"], "24h" + ) + assert 'docker_host: "-"' in rendered + assert '"gpu:host"' in rendered + assert "capacity: 1" in rendered + + +def test_secret_permissions_are_enforced(tmp_path): + secret = tmp_path / "token" + secret.write_text("secret\n", encoding="utf-8") + secret.chmod(0o644) + with pytest.raises(controller.ControllerError): + controller.read_secret(str(secret)) + secret.chmod(0o600) + assert controller.read_secret(str(secret)) == "secret" + + +def test_example_config_is_valid_json(): + path = Path(__file__).parents[1] / "ops" / "autodl-pro.example.json" + config = json.loads(path.read_text(encoding="utf-8")) + assert config["runner"]["labels"] == [ + "ubuntu-latest:host", + "ces-ci:host", + "gpu:host", + ] + assert stat.S_IMODE(MODULE_PATH.stat().st_mode) in {0o644, 0o755}