MotrixSimWeb/scripts/real-backend-dev.mjs

420 lines
14 KiB
JavaScript

#!/usr/bin/env node
import { access } from "node:fs/promises";
import { constants } from "node:fs";
import { spawn } from "node:child_process";
import { createConnection } from "node:net";
import path from "node:path";
import { fileURLToPath } from "node:url";
const rootDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
const command = process.argv[2] ?? "status";
const defaultKeyPath = "/Users/d-robotics/workSpace/document/80-private/orgs/drobotics/keys/wuying-39.106.101.95_ed25519";
const config = {
sshHost: process.env.WUYING_HOST ?? "39.106.101.95",
sshUser: process.env.WUYING_USER ?? "root",
sshKey: process.env.WUYING_SSH_KEY ?? defaultKeyPath,
localPort: Number(process.env.MOTRIXSIM_LOCAL_PORT ?? "18090"),
remoteHost: process.env.MOTRIXSIM_REMOTE_HOST ?? "127.0.0.1",
remotePort: Number(process.env.MOTRIXSIM_REMOTE_PORT ?? "18090"),
remoteDir: process.env.MOTRIXSIM_REMOTE_DIR ?? "/tmp/motrix-three-preview",
remotePython: process.env.MOTRIXSIM_REMOTE_PYTHON ?? "/opt/gs_playground_real2sim_48q/.venv/bin/python",
remoteScene: process.env.MOTRIXSIM_REMOTE_SCENE ?? "/tmp/motrixsim-web-composed/active_g1_store_scene.xml",
bridgeScript: process.env.MOTRIXSIM_BRIDGE_SCRIPT ?? "server/headless_motrix_bridge.py",
hz: Number(process.env.MOTRIXSIM_HZ ?? "30"),
verifyOutDir: process.env.MOTRIXSIM_VERIFY_OUT ?? "test-results/real-backend-editing",
};
function log(message) {
console.log(`[real-backend] ${message}`);
}
function run(args, options = {}) {
const [bin, ...rest] = args;
return new Promise((resolve, reject) => {
const child = spawn(bin, rest, {
cwd: rootDir,
stdio: options.capture ? ["ignore", "pipe", "pipe"] : "inherit",
...options.spawn,
});
let stdout = "";
let stderr = "";
child.stdout?.on("data", (chunk) => {
stdout += chunk;
});
child.stderr?.on("data", (chunk) => {
stderr += chunk;
});
child.on("error", reject);
child.on("close", (code) => {
if (code === 0) {
resolve({ stdout, stderr, code });
return;
}
const error = new Error(`${bin} exited with code ${code}${stderr ? `: ${stderr.trim()}` : ""}`);
error.code = code;
error.stdout = stdout;
error.stderr = stderr;
reject(error);
});
});
}
function sshBaseArgs() {
return [
"ssh",
"-i",
config.sshKey,
"-o",
"BatchMode=yes",
"-o",
"StrictHostKeyChecking=accept-new",
"-o",
"ConnectTimeout=8",
`${config.sshUser}@${config.sshHost}`,
];
}
async function ensureSshKey() {
try {
await access(config.sshKey, constants.R_OK);
} catch {
throw new Error(`SSH key is not readable: ${config.sshKey}. Set WUYING_SSH_KEY to the private key for ${config.sshHost}.`);
}
}
async function portOpen(host, port, timeoutMs = 700) {
return new Promise((resolve) => {
const socket = createConnection({ host, port });
const done = (value) => {
socket.destroy();
resolve(value);
};
socket.setTimeout(timeoutMs);
socket.once("connect", () => done(true));
socket.once("timeout", () => done(false));
socket.once("error", () => done(false));
});
}
async function fetchJson(url, timeoutMs = 5000) {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), timeoutMs);
try {
const response = await fetch(url, { cache: "no-store", signal: controller.signal });
const text = await response.text();
return { ok: response.ok, status: response.status, json: JSON.parse(text) };
} finally {
clearTimeout(timeout);
}
}
async function localBackendSummary() {
const base = `http://127.0.0.1:${config.localPort}`;
const portReady = await portOpen("127.0.0.1", config.localPort);
if (!portReady) {
return { portReady, health: undefined, manifest: undefined };
}
let health;
let manifest;
try {
health = await fetchJson(`${base}/health`);
} catch (error) {
health = { ok: false, error: error.message };
}
try {
const manifestResponse = await fetchJson(`${base}/manifest`, 15000);
const objects = manifestResponse.json?.objects ?? [];
manifest = {
ok: manifestResponse.ok,
status: manifestResponse.status,
name: manifestResponse.json?.name,
objectCount: objects.length,
editableGroupCount: new Set(objects.map((object) => object.editableGroup).filter(Boolean)).size,
groupedObjectCount: objects.filter((object) => object.editableGroup).length,
};
} catch (error) {
manifest = { ok: false, error: error.message };
}
return { portReady, health, manifest };
}
async function printStatus() {
const local = await localBackendSummary();
console.log(JSON.stringify({
config: {
sshHost: config.sshHost,
sshUser: config.sshUser,
localPort: config.localPort,
remotePort: config.remotePort,
remoteScene: config.remoteScene,
},
local,
}, null, 2));
}
function summarizeChecks(checks) {
const failed = checks.filter((item) => item.state === "fail").length;
const warned = checks.filter((item) => item.state === "warn").length;
const passed = checks.filter((item) => item.state === "pass").length;
return {
result: failed > 0 ? "fail" : warned > 0 ? "warn" : "pass",
passed,
warned,
failed,
total: checks.length,
};
}
function check(id, state, detail) {
return { id, state, detail };
}
function remoteDoctorCommand() {
const quoted = {
remoteDir: JSON.stringify(config.remoteDir),
remotePython: JSON.stringify(config.remotePython),
bridgeScript: JSON.stringify(config.bridgeScript),
remoteScene: JSON.stringify(config.remoteScene),
};
return [
"check_path() {",
" key=\"$1\"; kind=\"$2\"; target=\"$3\";",
" if [ \"$kind\" = \"dir\" ]; then test -d \"$target\"; ok=$?;",
" elif [ \"$kind\" = \"exec\" ]; then test -x \"$target\"; ok=$?;",
" else test -f \"$target\"; ok=$?; fi;",
" if [ \"$ok\" -eq 0 ]; then echo \"$key=pass:$target\"; else echo \"$key=fail:$target missing\"; fi;",
"}",
`check_path remote_dir dir ${quoted.remoteDir}`,
`check_path remote_python exec ${quoted.remotePython}`,
`check_path remote_scene file ${quoted.remoteScene}`,
`cd ${quoted.remoteDir}`,
`check_path bridge_script file ${quoted.bridgeScript}`,
`${quoted.remotePython} - <<'PY'`,
"import importlib.util",
"required = ['motrixsim', 'mujoco']",
"optional = ['torch']",
"for name in required:",
" state = 'pass' if importlib.util.find_spec(name) else 'fail'",
" print(f'remote_import_{name}={state}:{name}')",
"for name in optional:",
" state = 'pass' if importlib.util.find_spec(name) else 'warn'",
" print(f'remote_import_{name}={state}:{name}')",
"PY",
"process_line=$(ps -eo pid=,comm=,args= | awk '($2 ~ /^python/ || $3 ~ /python/) && /headless_motrix_bridge.py/ && /--port " + config.remotePort + "/ {print; exit}')",
"if [ -n \"$process_line\" ]; then echo \"remote_bridge_process=pass:$process_line\"; else echo \"remote_bridge_process=warn:not running\"; fi",
].join("\n");
}
function parseRemoteDoctor(stdout) {
const checks = [];
for (const rawLine of stdout.split(/\r?\n/)) {
const line = rawLine.trim();
if (!line) {
continue;
}
const match = /^([^=]+)=([^:]+):(.*)$/.exec(line);
if (!match) {
checks.push(check("remote_output", "warn", line));
continue;
}
checks.push(check(match[1], match[2], match[3]));
}
return checks;
}
async function doctor() {
const checks = [];
const nodeMajor = Number(process.versions.node.split(".")[0]);
checks.push(check("node", nodeMajor >= 20 ? "pass" : "fail", `node ${process.versions.node}`));
try {
await access(config.sshKey, constants.R_OK);
checks.push(check("ssh_key", "pass", config.sshKey));
} catch (error) {
checks.push(check("ssh_key", "fail", `${config.sshKey}: ${error.message}`));
}
if (checks.find((item) => item.id === "ssh_key")?.state === "pass") {
try {
await run([...sshBaseArgs(), "true"], { capture: true });
checks.push(check("ssh_connect", "pass", `${config.sshUser}@${config.sshHost}`));
const remote = await run([...sshBaseArgs(), remoteDoctorCommand()], { capture: true });
checks.push(...parseRemoteDoctor(remote.stdout));
} catch (error) {
checks.push(check("ssh_connect", "fail", error.message));
}
}
const local = await localBackendSummary();
checks.push(check("local_port", local.portReady ? "pass" : "warn", `127.0.0.1:${config.localPort}`));
checks.push(check(
"local_health",
local.health?.json?.ok === true ? "pass" : "warn",
local.health?.json?.scene ?? local.health?.error ?? "not ready",
));
checks.push(check(
"local_manifest_groups",
(local.manifest?.editableGroupCount ?? 0) > 0 ? "pass" : "warn",
`${local.manifest?.editableGroupCount ?? 0} groups / ${local.manifest?.groupedObjectCount ?? 0} objects`,
));
const summary = summarizeChecks(checks);
console.log(JSON.stringify({
schema: "motrixsim-web.real-backend-doctor.v1",
capturedAt: new Date().toISOString(),
config: {
sshHost: config.sshHost,
sshUser: config.sshUser,
localPort: config.localPort,
remotePort: config.remotePort,
remoteDir: config.remoteDir,
remotePython: config.remotePython,
remoteScene: config.remoteScene,
},
summary,
checks,
}, null, 2));
process.exitCode = summary.failed > 0 ? 1 : 0;
}
async function ensureTunnel() {
await ensureSshKey();
const summary = await localBackendSummary();
if (summary.health?.json?.ok === true) {
log(`local backend already responds on 127.0.0.1:${config.localPort}`);
return;
}
if (summary.portReady) {
throw new Error(`127.0.0.1:${config.localPort} is already listening but does not expose MotrixSim /health`);
}
log(`starting SSH tunnel 127.0.0.1:${config.localPort} -> ${config.sshHost}:${config.remotePort}`);
await run([
"ssh",
"-i",
config.sshKey,
"-fN",
"-o",
"BatchMode=yes",
"-o",
"StrictHostKeyChecking=accept-new",
"-o",
"ExitOnForwardFailure=yes",
"-L",
`${config.localPort}:${config.remoteHost}:${config.remotePort}`,
`${config.sshUser}@${config.sshHost}`,
], { capture: true });
const ready = await localBackendSummary();
if (ready.health?.json?.ok !== true) {
throw new Error(`SSH tunnel started, but /health did not become ready on 127.0.0.1:${config.localPort}`);
}
log("SSH tunnel ready");
}
async function syncBridgeScript() {
await ensureSshKey();
const localScript = path.join(rootDir, config.bridgeScript);
const remoteScript = `${config.sshUser}@${config.sshHost}:${config.remoteDir}/${config.bridgeScript}`;
log(`syncing ${config.bridgeScript} to ${config.sshHost}:${config.remoteDir}`);
await run([
"scp",
"-i",
config.sshKey,
"-o",
"BatchMode=yes",
"-o",
"StrictHostKeyChecking=accept-new",
localScript,
remoteScript,
]);
}
function remoteStartCommand() {
const quoted = {
remoteDir: JSON.stringify(config.remoteDir),
remotePython: JSON.stringify(config.remotePython),
bridgeScript: JSON.stringify(config.bridgeScript),
remoteScene: JSON.stringify(config.remoteScene),
remoteHost: JSON.stringify(config.remoteHost),
remotePort: JSON.stringify(String(config.remotePort)),
hz: JSON.stringify(String(config.hz)),
};
return [
"set -e",
`cd ${quoted.remoteDir}`,
"old_pids=$(ps -eo pid=,comm=,args= | awk '($2 ~ /^python/ || $3 ~ /python/) && /headless_motrix_bridge.py/ && /--port " + config.remotePort + "/ {print $1}')",
"if [ -n \"$old_pids\" ]; then kill $old_pids 2>/dev/null || true; sleep 1; fi",
`setsid ${quoted.remotePython} ${quoted.bridgeScript} --scene ${quoted.remoteScene} --host ${quoted.remoteHost} --port ${quoted.remotePort} --hz ${quoted.hz} --paused > /tmp/motrix_bridge_${config.remotePort}.log 2>&1 < /dev/null &`,
"sleep 4",
"ps -eo pid=,comm=,args= | awk '($2 ~ /^python/ || $3 ~ /python/) && /headless_motrix_bridge.py/ && /--port " + config.remotePort + "/ {print}'",
`tail -n 4 /tmp/motrix_bridge_${config.remotePort}.log`,
].join("\n");
}
async function remoteStart() {
await syncBridgeScript();
log(`starting remote bridge on ${config.sshHost}:${config.remotePort} using ${config.remoteScene}`);
await run([...sshBaseArgs(), remoteStartCommand()]);
}
async function verify() {
await run([
"node",
"scripts/verify-real-backend-editing.mjs",
`http://127.0.0.1:${config.localPort}/manifest`,
config.verifyOutDir,
]);
}
async function restoreDefaultScene() {
const composeUrl = `http://127.0.0.1:${config.localPort}/compose`;
log("restoring default backend scene without added test objects");
const response = await fetch(composeUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
scene: "g1-store-blocks-physics",
robot: "none",
robotPose: [1.85, -0.95, 0],
paused: true,
reset: true,
stockPhysics: "fixed",
objects: [],
}),
});
const payload = await response.json().catch(() => ({}));
if (!response.ok || payload.ok === false) {
throw new Error(`restore default scene failed: HTTP ${response.status} ${JSON.stringify(payload)}`);
}
log(`default backend scene ready: ${payload.objectCount ?? "?"} objects`);
}
async function up() {
await remoteStart();
await ensureTunnel();
await verify();
await restoreDefaultScene();
}
async function main() {
if (command === "status") {
await printStatus();
} else if (command === "doctor") {
await doctor();
} else if (command === "tunnel") {
await ensureTunnel();
} else if (command === "remote-start") {
await remoteStart();
} else if (command === "verify") {
await verify();
} else if (command === "up") {
await up();
} else {
console.error(`Unknown command: ${command}`);
console.error("Usage: node scripts/real-backend-dev.mjs [status|doctor|tunnel|remote-start|verify|up]");
process.exitCode = 2;
}
}
await main();