delqhi/code-devops
0
1#!/usr/bin/env python32"""3A2A Consumer HF VM Bootstrap Script4Prepares the HF VM runtime environment for agent deployment.5No longer pulls OpenAI tokens — uses qwen3.6-plus-free via opencode CLI.6"""7 8from __future__ import annotations9 10import json11import os12import subprocess13import sys14from pathlib import Path15 16AGENT_ROOT = Path(__file__).resolve().parent.parent17AUTH_FILE = Path.home() / ".local" / "share" / "opencode" / "auth.json"18 19 20def bootstrap_hf_vm() -> bool:21 """Prepare the HF VM runtime for the agent."""22 print(f"[hf-pull] Bootstrapping HF VM for agent at {AGENT_ROOT}")23 24 # Ensure required directories exist25 for dirpath in [26 Path.home() / ".local" / "share" / "opencode",27 Path.home() / ".config" / "sin",28 AGENT_ROOT / "logs",29 AGENT_ROOT / "data",30 ]:31 dirpath.mkdir(parents=True, exist_ok=True)32 print(f"[hf-pull] Ensured directory: {dirpath}")33 34 # Write minimal auth.json with placeholder (opencode CLI handles auth)35 auth_data = {36 "openai": {37 "type": "oauth",38 "refresh": "opencode-managed"39 }40 }41 AUTH_FILE.parent.mkdir(parents=True, exist_ok=True)42 AUTH_FILE.write_text(json.dumps(auth_data, indent=2) + "\n")43 print(f"[hf-pull] Wrote placeholder auth to {AUTH_FILE}")44 45 # Verify node is available46 try:47 result = subprocess.run(["node", "--version"], capture_output=True, text=True, timeout=5)48 print(f"[hf-pull] Node.js available: {result.stdout.strip()}")49 except FileNotFoundError:50 print("[hf-pull] WARNING: node not found — install Node.js 18+")51 return False52 53 # Install dependencies if node_modules missing54 pkg_lock = AGENT_ROOT / "package-lock.json"55 node_modules = AGENT_ROOT / "node_modules"56 if pkg_lock.exists() and not node_modules.exists():57 print("[hf-pull] Installing dependencies...")58 result = subprocess.run(59 ["npm", "ci", "--production"],60 cwd=str(AGENT_ROOT),61 capture_output=True,62 text=True,63 timeout=30064 )65 if result.returncode != 0:66 print(f"[hf-pull] npm ci failed: {result.stderr}")67 return False68 print("[hf-pull] Dependencies installed")69 70 # Build if dist missing71 dist_dir = AGENT_ROOT / "dist"72 if not dist_dir.exists() and (AGENT_ROOT / "tsconfig.json").exists():73 print("[hf-pull] Building agent...")74 result = subprocess.run(75 ["npm", "run", "build"],76 cwd=str(AGENT_ROOT),77 capture_output=True,78 text=True,79 timeout=30080 )81 if result.returncode != 0:82 print(f"[hf-pull] Build failed: {result.stderr}")83 return False84 print("[hf-pull] Build complete")85 86 print("[hf-pull] HF VM bootstrap complete")87 return True88 89 90if __name__ == "__main__":91 success = bootstrap_hf_vm()92 sys.exit(0 if success else 1)93 