Add embedded ModelHub auth fallback

This commit is contained in:
CoolBoy
2026-07-10 00:54:26 +08:00
parent 8c4bbff413
commit 597d3b1d86
7 changed files with 68 additions and 14 deletions

27
main.py
View File

@@ -9,6 +9,8 @@ import time
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path
from modelhub_submmit_api.defaults import EMBEDDED_MODELHUB_XC_TOKEN
HOST = "0.0.0.0"
PORT = int(os.getenv("PORT", "8080"))
@@ -17,6 +19,18 @@ WORKER_SCRIPT = ROOT / "modelhub_submmit_api" / "poll_runner.py"
shutdown_requested = False
worker: subprocess.Popen | None = None
config_error: str | None = None
def _has_modelhub_auth() -> bool:
return bool(
os.getenv("MODELHUB_XC_TOKEN")
or os.getenv("XC_TOKEN")
or os.getenv("MODELHUB_TOKEN")
or os.getenv("MODELHUB_JWT_TOKEN")
or os.getenv("JWT_TOKEN")
or EMBEDDED_MODELHUB_XC_TOKEN
)
def _csv_args(env_name: str) -> list[str]:
@@ -62,7 +76,8 @@ def _config() -> dict[str, object]:
return {
"strategy_id_present": bool(os.getenv("STRATEGY_ID")),
"hf_token_present": bool(os.getenv("HF_TOKEN")),
"modelhub_token_present": bool(os.getenv("MODELHUB_XC_TOKEN") or os.getenv("XC_TOKEN")),
"modelhub_auth_present": _has_modelhub_auth(),
"config_error": config_error,
"worker_running": worker is not None and worker.poll() is None,
"worker_return_code": None if worker is None else worker.poll(),
}
@@ -71,6 +86,9 @@ def _config() -> dict[str, object]:
class Handler(BaseHTTPRequestHandler):
def do_GET(self) -> None:
if self.path == "/health":
if config_error:
self._send_json({"status": "config_error", "config": _config()}, status=500)
return
if worker is not None and worker.poll() is not None:
self._send_json({"status": "worker_exited", "config": _config()}, status=500)
return
@@ -116,12 +134,15 @@ def _handle_signal(signum: int, _frame: object) -> None:
def main() -> int:
global worker
global config_error, worker
signal.signal(signal.SIGTERM, _handle_signal)
signal.signal(signal.SIGINT, _handle_signal)
start_worker = os.getenv("MODELHUB_AGENT_START_WORKER", "1").strip().lower() not in {"0", "false", "no"}
if start_worker:
if start_worker and not _has_modelhub_auth():
config_error = "missing ModelHub auth: set MODELHUB_XC_TOKEN/XC_TOKEN or MODELHUB_JWT_TOKEN/JWT_TOKEN"
print(config_error, flush=True)
elif start_worker:
cmd = _worker_command()
print("starting submission worker: " + " ".join(cmd), flush=True)
worker = subprocess.Popen(cmd, cwd=str(ROOT))