provisionAgent fix 12-20-25

This commit is contained in:
jester 2025-12-21 10:22:24 +00:00
parent 28a5b80e01
commit 43f1853e4f
3 changed files with 88 additions and 398 deletions

View File

@ -1,46 +1,20 @@
// src/api/provisionDev.js // src/api/handlers/provisionDev.js
// DEV-SIDE request normalization + validation
export function normalizeDevRequest(body = {}) { export function normalizeDevRequest(body = {}) {
const { if (!body.runtime) {
customerId, throw new Error("runtime is required for dev container");
name, }
cpuCores,
memoryMiB,
diskGiB,
portsNeeded,
// dev fields if (!body.version) {
runtime, throw new Error("version is required for dev container");
}
// canonical runtime version field for dev (matches your curl)
version,
// legacy/alternate naming (optional)
runtimeVersion,
// optional addons
addons,
} = body;
if (!customerId) throw new Error("customerId required");
if (!runtime) throw new Error("runtime required");
const resolvedVersion = version || runtimeVersion;
if (!resolvedVersion) throw new Error("version required");
return { return {
customerId, customerId: body.customerId,
name, runtime: body.runtime,
cpuCores, version: body.version,
memoryMiB, memoryMiB: body.memoryMiB || 2048,
diskGiB, cpuCores: body.cpuCores || 2,
portsNeeded, portsNeeded: body.portsNeeded || 0,
runtime,
version: String(resolvedVersion),
addons: Array.isArray(addons) ? addons : undefined,
}; };
} }
export default { normalizeDevRequest };

View File

@ -1,60 +1,22 @@
// src/api/provisionGame.js // src/api/handlers/provisionGame.js
// GAME-SIDE request normalization + validation (no payload changes yet)
export function normalizeGameRequest(body = {}) { export function normalizeGameRequest(body = {}) {
const { if (!body.game) {
customerId, throw new Error("game is required");
game, }
variant,
version,
world,
name,
cpuCores,
memoryMiB,
diskGiB,
portsNeeded,
artifactPath,
javaPath,
// passthrough creds (kept here just for shaping; payload stays in provisionAgent.js for now) if (!body.variant) {
steamUser, throw new Error("variant is required");
steamPass, }
steamAuth,
adminUser,
adminPass,
} = body;
if (!customerId) throw new Error("customerId required");
if (!game) throw new Error("game required");
if (!variant) throw new Error("variant required");
const gameLower = String(game).toLowerCase();
const isMinecraft = gameLower.includes("minecraft");
return { return {
customerId, customerId: body.customerId,
game, game: body.game,
variant, variant: body.variant,
version, version: body.version,
world, world: body.world || "world",
memoryMiB: body.memoryMiB || 2048,
name, cpuCores: body.cpuCores || 2,
cpuCores, portsNeeded: body.portsNeeded || 0,
memoryMiB,
diskGiB,
portsNeeded,
artifactPath,
javaPath,
steamUser,
steamPass,
steamAuth,
adminUser,
adminPass,
isMinecraft,
}; };
} }
export default { normalizeGameRequest };

View File

@ -1,12 +1,5 @@
// src/api/provisionAgent.js // src/api/provisionAgent.js
// FINAL AGENT-DRIVEN PROVISIONING PIPELINE // FINAL AGENT-DRIVEN PROVISIONING PIPELINE (STABLE + SCALABLE)
// Supports: paper, vanilla, purpur, forge, fabric, neoforge + dev containers
//
// Updated (Dec 2025):
// - Keep V3 hostname behavior (FQDN: mc-vanilla-5072.zerolaghub.quest)
// - Decouple edge publishing from PortPool allocation
// - Minecraft does NOT allocate PortPool ports, but still publishes edge using routing port 25565
// - Preserve game/dev validation split (normalizeGameRequest / normalizeDevRequest)
import "dotenv/config"; import "dotenv/config";
import fetch from "node-fetch"; import fetch from "node-fetch";
@ -21,7 +14,6 @@ import {
} from "../services/proxmoxClient.js"; } from "../services/proxmoxClient.js";
import { getCtIpWithRetry } from "../services/getCtIp.js"; import { getCtIpWithRetry } from "../services/getCtIp.js";
import { PortAllocationService } from "../services/portAllocator.js";
import { import {
allocateVmid, allocateVmid,
confirmVmidAllocated, confirmVmidAllocated,
@ -33,7 +25,6 @@ import { enqueuePublishEdge } from "../queues/postProvision.js";
import { normalizeGameRequest } from "./handlers/provisionGame.js"; import { normalizeGameRequest } from "./handlers/provisionGame.js";
import { normalizeDevRequest } from "./handlers/provisionDev.js"; import { normalizeDevRequest } from "./handlers/provisionDev.js";
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const AGENT_TEMPLATE_VMID = Number( const AGENT_TEMPLATE_VMID = Number(
process.env.AGENT_TEMPLATE_VMID || process.env.AGENT_TEMPLATE_VMID ||
@ -45,179 +36,51 @@ const AGENT_TEMPLATE_VMID = Number(
const AGENT_PORT = Number(process.env.ZLH_AGENT_PORT || 18888); const AGENT_PORT = Number(process.env.ZLH_AGENT_PORT || 18888);
const AGENT_TOKEN = process.env.ZLH_AGENT_TOKEN || null; const AGENT_TOKEN = process.env.ZLH_AGENT_TOKEN || null;
// V3 behavior: slotHostname is FQDN built here const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const ZONE = process.env.TECHNITIUM_ZONE || "zerolaghub.quest";
/* ------------------------------------------------------------- /* -------------------------------------------------------------
VERSION PARSER (Minecraft only) PAYLOAD BUILDERS (CANONICAL)
------------------------------------------------------------- */ ------------------------------------------------------------- */
function parseMcVersion(ver) {
if (!ver) return { major: 0, minor: 0, patch: 0 };
const p = String(ver).split(".");
return {
major: Number(p[0]) || 0,
minor: Number(p[1]) || 0,
patch: Number(p[2]) || 0,
};
}
/* ------------------------------------------------------------- function buildDevAgentPayload({ vmid, runtime, version, memoryMiB }) {
JAVA RUNTIME SELECTOR (Minecraft only)
------------------------------------------------------------- */
function pickJavaRuntimeForMc(version) {
const { major, minor, patch } = parseMcVersion(version);
if (major > 1) return 21;
if (major === 1) {
if (minor >= 21) return 21;
if (minor === 20 && patch >= 5) return 21;
if (minor > 20) return 21;
return 17;
}
return 17;
}
/* -------------------------------------------------------------
HOSTNAME GENERATION
------------------------------------------------------------- */
function generateSystemHostname({ ctype, game, variant, vmid }) {
if (ctype === "dev") return `dev-${vmid}`;
const g = (game || "").toLowerCase();
const v = (variant || "").toLowerCase();
let prefix = "game";
if (g.includes("minecraft")) prefix = "mc";
else if (g.includes("terraria")) prefix = "terraria";
else if (g.includes("valheim")) prefix = "valheim";
else if (g.includes("rust")) prefix = "rust";
let varPart = "";
if (g.includes("minecraft")) {
if (
["paper", "forge", "fabric", "vanilla", "purpur", "neoforge"].includes(v)
) {
varPart = v;
}
}
return varPart ? `${prefix}-${varPart}-${vmid}` : `${prefix}-${vmid}`;
}
/* -------------------------------------------------------------
ADMIN PASSWORD GENERATOR
------------------------------------------------------------- */
function generateAdminPassword() {
return crypto.randomBytes(12).toString("base64url");
}
/* -------------------------------------------------------------
GAME PAYLOAD
------------------------------------------------------------- */
function buildGameAgentPayload({
vmid,
game,
variant,
version,
world,
ports,
artifactPath,
javaPath,
memoryMiB,
steamUser,
steamPass,
steamAuth,
adminUser,
adminPass,
}) {
const g = (game || "minecraft").toLowerCase();
const v = (variant || "").toLowerCase();
const ver = version || "1.20.1";
const w = world || "world";
if (!v) throw new Error("variant is required");
let art = artifactPath;
let jpath = javaPath;
if (!art && g === "minecraft") {
switch (v) {
case "paper":
case "vanilla":
case "purpur":
art = `minecraft/${v}/${ver}/server.jar`;
break;
case "forge":
art = `minecraft/forge/${ver}/forge-installer.jar`;
break;
case "fabric":
art = `minecraft/fabric/${ver}/fabric-server.jar`;
break;
case "neoforge":
art = `minecraft/neoforge/${ver}/neoforge-installer.jar`;
break;
default:
throw new Error(`Unsupported Minecraft variant: ${v}`);
}
}
if (!jpath && g === "minecraft") {
const javaVersion = pickJavaRuntimeForMc(ver);
jpath =
javaVersion === 21
? "java/21/OpenJDK21.tar.gz"
: "java/17/OpenJDK17.tar.gz";
}
let mem = Number(memoryMiB) || 0;
if (mem <= 0) mem = ["forge", "neoforge"].includes(v) ? 4096 : 2048;
return {
vmid,
game: g,
variant: v,
version: ver,
world: w,
ports: Array.isArray(ports) ? ports : [ports].filter(Boolean),
artifact_path: art,
java_path: jpath,
memory_mb: mem,
steam_user: steamUser || "anonymous",
steam_pass: steamPass || "",
steam_auth: steamAuth || "",
admin_user: adminUser || "admin",
admin_pass: adminPass || generateAdminPassword(),
};
}
/* -------------------------------------------------------------
DEV PAYLOAD
------------------------------------------------------------- */
function buildDevAgentPayload({ vmid, runtime, version, memoryMiB, ports }) {
if (!runtime) throw new Error("runtime required for dev container"); if (!runtime) throw new Error("runtime required for dev container");
if (!version) throw new Error("version required for dev container"); if (!version) throw new Error("version required for dev container");
return { return {
vmid, vmid,
ctype: "dev", ctype: "dev", // ← CRITICAL, AGENT CONTRACT
runtime, runtime,
version, version,
memory_mb: Number(memoryMiB) || 2048, memory_mb: Number(memoryMiB) || 2048,
ports: Array.isArray(ports) ? ports : [ports].filter(Boolean), };
}
function buildGameAgentPayload(req) {
// req already normalized by provisionGame
return {
vmid: req.vmid,
game: req.game,
variant: req.variant,
version: req.version,
world: req.world,
ports: req.ports || [],
artifact_path: req.artifactPath,
java_path: req.javaPath,
memory_mb: req.memoryMiB,
admin_user: req.adminUser,
admin_pass: req.adminPass,
}; };
} }
/* ------------------------------------------------------------- /* -------------------------------------------------------------
SEND CONFIG AGENT COMMUNICATION
------------------------------------------------------------- */ ------------------------------------------------------------- */
async function sendAgentConfig({ ip, payload }) {
const url = `http://${ip}:${AGENT_PORT}/config`;
const headers = { "Content-Type": "application/json" };
if (AGENT_TOKEN) headers["Authorization"] = `Bearer ${AGENT_TOKEN}`;
const resp = await fetch(url, { async function sendAgentConfig({ ip, payload }) {
const headers = { "Content-Type": "application/json" };
if (AGENT_TOKEN) headers.Authorization = `Bearer ${AGENT_TOKEN}`;
const resp = await fetch(`http://${ip}:${AGENT_PORT}/config`, {
method: "POST", method: "POST",
headers, headers,
body: JSON.stringify(payload), body: JSON.stringify(payload),
@ -229,50 +92,18 @@ async function sendAgentConfig({ ip, payload }) {
} }
} }
/* -------------------------------------------------------------
WAIT FOR AGENT READY
------------------------------------------------------------- */
async function waitForAgentRunning({ ip, timeoutMs = 10 * 60_000 }) { async function waitForAgentRunning({ ip, timeoutMs = 10 * 60_000 }) {
const url = `http://${ip}:${AGENT_PORT}/status`;
const headers = {};
if (AGENT_TOKEN) headers["Authorization"] = `Bearer ${AGENT_TOKEN}`;
const deadline = Date.now() + timeoutMs; const deadline = Date.now() + timeoutMs;
let lastLoggedStep = null;
while (Date.now() < deadline) { while (Date.now() < deadline) {
try { try {
const resp = await fetch(url, { headers }); const res = await fetch(`http://${ip}:${AGENT_PORT}/status`);
if (resp.ok) { if (res.ok) {
const data = await resp.json(); const data = await res.json();
const state = (data.state || "").toLowerCase(); if (data.state === "running") return;
const step = data.installStep || data.currentStep || "unknown"; if (data.state === "error") throw new Error(data.error || "agent error");
const progress = data.progress || "";
if (step !== lastLoggedStep) {
console.log(`[AGENT ${ip}] state=${state} step=${step} ${progress}`);
lastLoggedStep = step;
}
if (state === "running") {
console.log(`[AGENT ${ip}] ✓ Provisioning complete`);
return data;
}
if (state === "error") {
const errorMsg = data.error || "agent error";
console.error(`[AGENT ${ip}] ✗ ERROR:`, errorMsg);
throw new Error(errorMsg);
}
} }
} catch (err) { } catch {}
if (
!err.message.includes("ECONNREFUSED") &&
!err.message.includes("fetch failed")
) {
console.error(`[AGENT ${ip}] Poll error:`, err.message);
}
}
await sleep(3000); await sleep(3000);
} }
@ -282,63 +113,33 @@ async function waitForAgentRunning({ ip, timeoutMs = 10 * 60_000 }) {
/* ------------------------------------------------------------- /* -------------------------------------------------------------
MAIN ENTRYPOINT MAIN ENTRYPOINT
------------------------------------------------------------- */ ------------------------------------------------------------- */
export async function provisionAgentInstance(body = {}) { export async function provisionAgentInstance(body = {}) {
const ctype = body.ctype || "game"; const ctype = body.ctype || "game";
console.log(`[agentProvision] STEP 0: Starting ${ctype} container provisioning`); if (!["game", "dev"].includes(ctype)) {
throw new Error(`invalid ctype: ${ctype}`);
}
console.log(`[agentProvision] starting ${ctype} provisioning`);
// EARLY SPLIT — DO NOT MOVE
const req = const req =
ctype === "dev" ? normalizeDevRequest(body) : normalizeGameRequest(body); ctype === "dev"
? normalizeDevRequest(body)
const gameLower = String(req.game || "").toLowerCase(); : normalizeGameRequest(body);
const isMinecraft = ctype === "game" && gameLower.includes("minecraft");
let vmid; let vmid;
let ctIp; let ctIp;
let allocatedPorts = [];
let txnId = null;
try { try {
console.log("[agentProvision] STEP 1: allocate VMID");
vmid = await allocateVmid(ctype); vmid = await allocateVmid(ctype);
console.log(`[agentProvision] → Allocated vmid=${vmid}`); console.log(`[agentProvision] vmid=${vmid}`);
// Allocate ports if needed (Minecraft skips PortPool; uses 25565 via Velocity) const hostname =
if (!isMinecraft && req.portsNeeded && req.portsNeeded > 0) { ctype === "dev"
console.log("[agentProvision] STEP 2: port allocation"); ? `dev-${vmid}`
txnId = crypto.randomUUID(); : req.hostname || `game-${vmid}`;
const portObjs = await PortAllocationService.reserve({
game: req.game,
variant: req.variant,
customerId: req.customerId,
vmid,
purpose: ctype === "game" ? "game_main" : "dev",
txnId,
count: req.portsNeeded,
});
allocatedPorts = Array.isArray(portObjs)
? portObjs.map((p) => (typeof p === "object" ? p.port : p))
: [portObjs];
console.log(`[agentProvision] → Allocated ports: ${allocatedPorts.join(", ")}`);
} else {
console.log("[agentProvision] STEP 2: port allocation (skipped)");
}
const hostname = generateSystemHostname({
ctype,
game: req.game,
variant: req.variant,
vmid,
});
// V3 correct behavior: build FQDN here
const slotHostname = `${hostname}.${ZONE}`;
console.log(
`[agentProvision] STEP 3: clone template ${AGENT_TEMPLATE_VMID} → vmid=${vmid}`
);
await cloneContainer({ await cloneContainer({
templateVmid: AGENT_TEMPLATE_VMID, templateVmid: AGENT_TEMPLATE_VMID,
vmid, vmid,
@ -346,7 +147,6 @@ export async function provisionAgentInstance(body = {}) {
full: 1, full: 1,
}); });
console.log("[agentProvision] STEP 4: configure CPU/mem/bridge/tags");
await configureContainer({ await configureContainer({
vmid, vmid,
cpu: req.cpuCores || 2, cpu: req.cpuCores || 2,
@ -354,14 +154,9 @@ export async function provisionAgentInstance(body = {}) {
bridge: ctype === "dev" ? "vmbr2" : "vmbr3", bridge: ctype === "dev" ? "vmbr2" : "vmbr3",
}); });
console.log("[agentProvision] STEP 5: start container");
await startWithRetry(vmid); await startWithRetry(vmid);
console.log("[agentProvision] STEP 6: detect container IP");
ctIp = await getCtIpWithRetry(vmid); ctIp = await getCtIpWithRetry(vmid);
console.log(`[agentProvision] → ctIp=${ctIp}`);
console.log("[agentProvision] STEP 7: build agent payload");
const payload = const payload =
ctype === "dev" ctype === "dev"
? buildDevAgentPayload({ ? buildDevAgentPayload({
@ -369,22 +164,15 @@ export async function provisionAgentInstance(body = {}) {
runtime: body.runtime, runtime: body.runtime,
version: body.version, version: body.version,
memoryMiB: req.memoryMiB, memoryMiB: req.memoryMiB,
ports: allocatedPorts,
}) })
: buildGameAgentPayload({ : buildGameAgentPayload({ ...req, vmid });
vmid,
...req, console.log(`[agentProvision] payload:`);
// agent can still use ports; for minecraft, provide 25565 semantic port console.log(JSON.stringify(payload, null, 2));
ports: allocatedPorts.length > 0 ? allocatedPorts : isMinecraft ? [25565] : [],
});
console.log("[agentProvision] STEP 8: POST /config to agent (async provision+start)");
await sendAgentConfig({ ip: ctIp, payload }); await sendAgentConfig({ ip: ctIp, payload });
console.log("[agentProvision] STEP 9: wait for agent to be running via /status");
await waitForAgentRunning({ ip: ctIp }); await waitForAgentRunning({ ip: ctIp });
console.log("[agentProvision] STEP 10: DB save");
await prisma.containerInstance.create({ await prisma.containerInstance.create({
data: { data: {
vmid, vmid,
@ -392,67 +180,33 @@ export async function provisionAgentInstance(body = {}) {
ctype, ctype,
hostname, hostname,
ip: ctIp, ip: ctIp,
allocatedPorts, // matches schema
payload, payload,
agentState: "running", agentState: "running",
agentLastSeen: new Date(), agentLastSeen: new Date(),
}, },
}); });
// STEP 11: commit ports ONLY if allocated from PortPool if (!payload.ctype) {
if (allocatedPorts.length > 0) { throw new Error("Payload missing ctype (game|dev)");
console.log("[agentProvision] STEP 11: commit ports"); }
await PortAllocationService.commit({ vmid, ports: allocatedPorts });
} else {
console.log("[agentProvision] STEP 11: commit ports (skipped - none allocated)");
}
// STEP 12: publish edge for ALL game servers (Minecraft included)
if (ctype === "game") { if (ctype === "game") {
console.log("[agentProvision] STEP 12: publish edge");
const edgePorts =
allocatedPorts.length > 0 ? allocatedPorts : isMinecraft ? [25565] : [];
await enqueuePublishEdge({ await enqueuePublishEdge({
vmid, vmid,
slotHostname, // FQDN (V3 behavior) instanceHostname: hostname,
instanceHostname: hostname, // short (optional, kept for compatibility)
ports: edgePorts,
ctIp, ctIp,
game: req.game, game: req.game,
txnId,
}); });
} else {
console.log("[agentProvision] STEP 12: publish edge (skipped - dev container)");
} }
await confirmVmidAllocated(vmid); await confirmVmidAllocated(vmid);
console.log("[agentProvision] COMPLETE: success"); return { vmid, hostname, ip: ctIp };
return { vmid, hostname, ip: ctIp, ports: allocatedPorts };
} catch (err) { } catch (err) {
console.error("[agentProvision] ERROR:", err.message);
// Rollback ports on failure
if (vmid && allocatedPorts.length > 0) {
try {
await PortAllocationService.releaseByVmid(vmid);
console.log(`[agentProvision] → Rolled back ports for vmid=${vmid}`);
} catch (rollbackErr) {
console.error("[agentProvision] → Port rollback failed:", rollbackErr.message);
}
}
if (vmid) { if (vmid) {
try { try { await deleteContainer(vmid); } catch {}
await deleteContainer(vmid); try { await releaseVmid(vmid); } catch {}
} catch {}
try {
await releaseVmid(vmid);
} catch {}
} }
throw err; throw err;
} }
} }