Download deployment/launch.py from clhfy45/ah: direct link, hf CLI and curl.
- Browser
- Download file 5.22 kB
-
https://huggingface.co/spaces/clhfy45/ah/resolve/main/deployment/launch.py
- Command line
-
hf download hf://spaces/clhfy45/ah/deployment/launch.py
-
curl -L -o launch.py https://huggingface.co/spaces/clhfy45/ah/resolve/main/deployment/launch.py
5.22 kB
| import os | |
| import re | |
| import signal | |
| import subprocess | |
| import sys | |
| import time | |
| import urllib.request | |
| from pathlib import Path | |
| from database_snapshot import DatabaseSnapshot | |
| def prepare(): | |
| os.umask(0o077) | |
| username = os.environ.get("COMFY_AUTH_USER", "") | |
| password = os.environ.get("COMFY_AUTH_PASSWORD", "") | |
| if not re.fullmatch(r"[A-Za-z0-9_.-]{1,64}", username): | |
| raise ValueError("HF Secret COMFY_AUTH_USER 必须为 1–64 位字母、数字、点、横线或下划线") | |
| if not password or len(password.encode()) > 72 or any(c in password for c in "\r\n\x00"): | |
| raise ValueError("HF Secret COMFY_AUTH_PASSWORD 必须非空、至多 72 字节且不含换行或 NUL") | |
| if not 30 <= int(os.environ.get("COMFY_REQUEST_TIMEOUT", "600")) <= 3600: | |
| raise ValueError("COMFY_REQUEST_TIMEOUT 必须在 30–3600 秒之间") | |
| data = Path(os.environ.get("COMFY_DATA_DIR", "/data")).resolve() | |
| if not data.is_dir() or not os.path.ismount(data): | |
| raise ValueError("COMFY_DATA_DIR 必须是已挂载的持久化卷;拒绝静默使用临时磁盘") | |
| for name in ("private", "user", "input", "output"): | |
| (data / name).mkdir(mode=0o700, exist_ok=True) | |
| probe = data / "private" / ".write-check" | |
| with probe.open("wb") as handle: | |
| handle.write(b"ok") | |
| handle.flush() | |
| os.fsync(handle.fileno()) | |
| probe.unlink() | |
| runtime = Path("/tmp/comfy-runtime") | |
| runtime.mkdir(mode=0o700, exist_ok=True) | |
| for name in ("client", "proxy", "fastcgi", "uwsgi", "scgi", "temp"): | |
| (runtime / name).mkdir(exist_ok=True) | |
| result = subprocess.run( | |
| ["htpasswd", "-niB", "-C", "10", username], | |
| input=password + "\n", | |
| text=True, | |
| stdout=subprocess.PIPE, | |
| stderr=subprocess.DEVNULL, | |
| check=True, | |
| ) | |
| (runtime / "htpasswd").write_text(result.stdout) | |
| subprocess.run(["nginx", "-t", "-c", "/opt/space/deployment/nginx.conf"], check=True) | |
| return data | |
| def main(): | |
| try: | |
| data = prepare() | |
| except Exception as exc: | |
| print(f"启动检查失败:{type(exc).__name__}。请核对 Secrets、Variables 和 Bucket 挂载。", file=sys.stderr) | |
| if isinstance(exc, ValueError): | |
| print(str(exc), file=sys.stderr) | |
| return 1 | |
| children = [] | |
| stopping = False | |
| snapshot = DatabaseSnapshot("/tmp/comfy-runtime/comfyui.db", data / "private" / "asset-index") | |
| snapshot.restore() | |
| def stop(*_): | |
| nonlocal stopping | |
| stopping = True | |
| for child in children: | |
| if child.poll() is None: | |
| child.terminate() | |
| signal.signal(signal.SIGTERM, stop) | |
| signal.signal(signal.SIGINT, stop) | |
| allowed = {"PATH", "LANG", "LC_ALL", "PYTHONUNBUFFERED", "PYTHONDONTWRITEBYTECODE", "SSL_CERT_FILE"} | |
| child_env = { | |
| k: v | |
| for k, v in os.environ.items() | |
| if k in allowed or k.startswith("UPSTREAM_KEY_") or k == "COMFY_REQUEST_TIMEOUT" | |
| } | |
| child_env["COMFY_DATA_DIR"] = str(data) | |
| command = [ | |
| sys.executable, | |
| "/opt/ComfyUI/main.py", | |
| "--cpu", | |
| "--listen", | |
| "127.0.0.1", | |
| "--port", | |
| "8188", | |
| "--disable-auto-launch", | |
| "--disable-metadata", | |
| "--enable-assets", | |
| "--disable-all-custom-nodes", | |
| "--whitelist-custom-nodes", | |
| "remote_bridge", | |
| "--user-directory", | |
| str(data / "user"), | |
| "--input-directory", | |
| str(data / "input"), | |
| "--output-directory", | |
| str(data / "output"), | |
| "--temp-directory", | |
| "/tmp/comfy-runtime/temp", | |
| "--database-url", | |
| "sqlite:////tmp/comfy-runtime/comfyui.db", | |
| ] | |
| exit_code = 1 | |
| try: | |
| children.append(subprocess.Popen(command, cwd="/opt/ComfyUI", env=child_env)) | |
| deadline = time.monotonic() + 600 | |
| while not stopping and children[0].poll() is None and time.monotonic() < deadline: | |
| try: | |
| with urllib.request.urlopen("http://127.0.0.1:8188/remote/config", timeout=2) as response: | |
| if response.status == 200: | |
| break | |
| except OSError: | |
| time.sleep(1) | |
| else: | |
| return 0 if stopping else 1 | |
| if stopping: | |
| return 0 | |
| children.append( | |
| subprocess.Popen( | |
| ["nginx", "-c", "/opt/space/deployment/nginx.conf", "-g", "daemon off;"], | |
| env={k: v for k, v in os.environ.items() if k in allowed}, | |
| ) | |
| ) | |
| print("ComfyUI 和鉴权入口已就绪。") | |
| last_snapshot = 0 | |
| while not stopping and all(child.poll() is None for child in children): | |
| if time.monotonic() - last_snapshot >= 30: | |
| snapshot.save() | |
| last_snapshot = time.monotonic() | |
| time.sleep(1) | |
| exit_code = 0 if stopping else 1 | |
| finally: | |
| stop() | |
| for child in children: | |
| try: | |
| child.wait(timeout=20) | |
| except subprocess.TimeoutExpired: | |
| child.kill() | |
| child.wait() | |
| snapshot.save() | |
| return exit_code | |
| if __name__ == "__main__": | |
| raise SystemExit(main()) | |