#!/usr/bin/env python3 """Hugging Face Jobs entrypoint for the AWS Steam GPU Launch Kit. This runs *inside* a Hub Job. It fetches this repo from the Hub, then either validates it (no credentials needed) or deploys it (AWS credentials injected as Hub secrets, so nothing long-lived is written to disk in the repo or the image). MODE=validate -> static checks only, no AWS calls MODE=deploy -> validate, then run launch.sh on Hub compute MODE=diagnose -> validate, then run the SSM diagnostic against an existing host Environment: LAUNCHKIT_REPO default Allszn/aws-steam-gpu-launchkit LAUNCHKIT_REV branch, tag or revision (default: main) MODE validate | deploy | diagnose (default: validate) plus everything launch.sh reads: INSTANCE_TYPE, KEY_NAME, ADMIN_CIDR, GAME_VOLUME_GIB, ARTIFACT_BUCKET, STACK_NAME, AWS_REGION, INSTANCE_ID """ from __future__ import annotations import os import subprocess import sys from pathlib import Path DEFAULT_REPO = "Allszn/aws-steam-gpu-launchkit" def log(msg: str) -> None: print(f"[hf-job] {msg}", flush=True) def fetch(repo_id: str, revision: str | None, token: str | None) -> Path: from huggingface_hub import snapshot_download log(f"fetching {repo_id}@{revision or 'main'} from the Hub") path = snapshot_download( repo_id=repo_id, repo_type="dataset", revision=revision, token=token, allow_patterns=[ "cloudformation/*", "scripts/*", "iam/*", "ssm/*", "launch.sh", "launch.py", "README.md", ], ) root = Path(path) log(f"artifacts at {root}") for f in sorted(root.rglob("*")): if f.is_file(): log(f" {f.relative_to(root)} ({f.stat().st_size} bytes)") return root def run(cmd: list[str], cwd: Path, env: dict[str, str]) -> int: log(f"$ {' '.join(cmd)}") proc = subprocess.run(cmd, cwd=str(cwd), env=env, text=True) return proc.returncode def main() -> int: repo_id = os.environ.get("LAUNCHKIT_REPO", DEFAULT_REPO) revision = os.environ.get("LAUNCHKIT_REV") or None mode = os.environ.get("MODE", "validate").strip().lower() token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGING_FACE_HUB_TOKEN") root = fetch(repo_id, revision, token) env = dict(os.environ) env.setdefault("AWS_REGION", "us-east-1") # ------------------------------------------------------------------ step 1 log("step 1/2 - static validation (no AWS credentials required)") rc = run([sys.executable, "scripts/validate.py", "--root", ".", "--pretty"], root, env) if rc != 0: log("validation FAILED - refusing to proceed to an AWS deploy") return rc if mode == "validate": log("done. Set MODE=deploy to apply, or MODE=diagnose to inspect a running host.") return 0 # ------------------------------------------------------------------ step 2 if not (env.get("AWS_ACCESS_KEY_ID") or env.get("AWS_PROFILE")): log("no AWS credentials found in the environment.") log("pass them as Hub secrets, for example:") log(" --secrets AWS_ACCESS_KEY_ID --secrets AWS_SECRET_ACCESS_KEY --secrets AWS_REGION") return 2 launch = root / "launch.sh" launch.chmod(0o755) if mode == "diagnose": instance_id = env.get("INSTANCE_ID") if not instance_id: log("MODE=diagnose requires INSTANCE_ID") return 2 log(f"step 2/2 - running the SSM diagnostic against {instance_id}") return run( [ "aws", "ssm", "send-command", "--document-name", "SteamGpu-Diagnose", "--instance-ids", instance_id, "--comment", "launchkit diagnose via Hugging Face Job", ], root, env, ) log("step 2/2 - deploying via launch.sh on Hub compute") return run(["bash", str(launch)], root, env) if __name__ == "__main__": sys.exit(main())