Compare commits

...

4 Commits

Author SHA1 Message Date
88d790d42c provisionAgent fixed 12-21-25 2025-12-21 22:12:26 +00:00
43f1853e4f provisionAgent fix 12-20-25 2025-12-21 10:22:24 +00:00
28a5b80e01 provisionAgentfixed 12-20-25 2025-12-20 19:34:09 +00:00
b23d982428 Provisioning split 9-19-25 2025-12-19 19:13:35 +00:00
3 changed files with 219 additions and 325 deletions

View File

@ -0,0 +1,20 @@
// src/api/handlers/provisionDev.js
export function normalizeDevRequest(body = {}) {
if (!body.runtime) {
throw new Error("runtime is required for dev container");
}
if (!body.version) {
throw new Error("version is required for dev container");
}
return {
customerId: body.customerId,
runtime: body.runtime,
version: body.version,
memoryMiB: body.memoryMiB || 2048,
cpuCores: body.cpuCores || 2,
portsNeeded: body.portsNeeded || 0,
};
}

View File

@ -0,0 +1,22 @@
// src/api/handlers/provisionGame.js
export function normalizeGameRequest(body = {}) {
if (!body.game) {
throw new Error("game is required");
}
if (!body.variant) {
throw new Error("variant is required");
}
return {
customerId: body.customerId,
game: body.game,
variant: body.variant,
version: body.version,
world: body.world || "world",
memoryMiB: body.memoryMiB || 2048,
cpuCores: body.cpuCores || 2,
portsNeeded: body.portsNeeded || 0,
};
}

View File

@ -1,13 +1,11 @@
// src/api/provisionAgent.js
// FINAL AGENT-DRIVEN PROVISIONING PIPELINE
// Supports: paper, vanilla, purpur, forge, fabric, neoforge + Steam creds passthrough
// FINAL AGENT-DRIVEN PROVISIONING PIPELINE (STABLE + SCALABLE)
import "dotenv/config";
import fetch from "node-fetch";
import crypto from "crypto";
import prisma from "../services/prisma.js";
import proxmox, {
import {
cloneContainer,
configureContainer,
startWithRetry,
@ -15,7 +13,6 @@ import proxmox, {
} from "../services/proxmoxClient.js";
import { getCtIpWithRetry } from "../services/getCtIp.js";
import { PortAllocationService } from "../services/portAllocator.js";
import {
allocateVmid,
confirmVmidAllocated,
@ -23,184 +20,131 @@ import {
} from "../services/vmidAllocator.js";
import { enqueuePublishEdge } from "../queues/postProvision.js";
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
import { normalizeGameRequest } from "./handlers/provisionGame.js";
import { normalizeDevRequest } from "./handlers/provisionDev.js";
const AGENT_TEMPLATE_VMID = Number(
process.env.AGENT_TEMPLATE_VMID ||
process.env.BASE_TEMPLATE_VMID ||
process.env.PROXMOX_AGENT_TEMPLATE_VMID ||
900
process.env.PROXMOX_AGENT_TEMPLATE_VMID
);
const AGENT_PORT = Number(process.env.ZLH_AGENT_PORT || 18888);
const AGENT_TOKEN = process.env.ZLH_AGENT_TOKEN || null;
/* -------------------------------------------------------------
VERSION PARSER
------------------------------------------------------------- */
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,
};
}
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const step = (name) =>
console.log(`[agentProvision] step=${name}`);
/* -------------------------------------------------------------
JAVA RUNTIME SELECTOR
HOSTNAME BUILDER
------------------------------------------------------------- */
function pickJavaRuntimeForMc(version) {
const { major, minor, patch } = parseMcVersion(version);
function buildHostname({ ctype, game, variant, vmid }) {
if (ctype === "dev") return `dev-${vmid}`;
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({ game, variant, vmid }) {
const g = (game || "").toLowerCase();
if (game === "minecraft") {
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;
if (v) return `mc-${v}-${vmid}`;
return `mc-${vmid}`;
}
return varPart ? `${prefix}-${varPart}-${vmid}` : `${prefix}-${vmid}`;
return `${game || "game"}-${vmid}`;
}
/* -------------------------------------------------------------
ADMIN PASSWORD GENERATOR
JAVA SELECTION (FIX)
------------------------------------------------------------- */
function generateAdminPassword() {
return crypto.randomBytes(12).toString("base64url");
}
function pickJavaForMinecraftVersion(version) {
// version like "1.21.7"
const parts = String(version).split(".");
const minor = Number(parts[1] || 0);
/* -------------------------------------------------------------
BUILD AGENT PAYLOAD
------------------------------------------------------------- */
function buildAgentPayload({
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 (paper, forge, fabric, vanilla, purpur)");
let art = artifactPath;
let jpath = javaPath;
// --------- VARIANT → ARTIFACT PATH ---------
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}`);
}
}
// --------- JAVA RUNTIME SELECTOR ----------
if (!jpath && g === "minecraft") {
const javaVersion = pickJavaRuntimeForMc(ver);
jpath =
javaVersion === 21
return minor >= 21
? "java/21/OpenJDK21.tar.gz"
: "java/17/OpenJDK17.tar.gz";
}
}
// --------- MEMORY DEFAULTS ----------
let mem = Number(memoryMiB) || 0;
if (mem <= 0) mem = ["forge", "neoforge"].includes(v) ? 4096 : 2048;
/* -------------------------------------------------------------
PAYLOAD BUILDERS
------------------------------------------------------------- */
// Steam + admin credentials (persisted, optional)
const resolvedSteamUser = steamUser || "anonymous";
const resolvedSteamPass = steamPass || "";
const resolvedSteamAuth = steamAuth || "";
const resolvedAdminUser = adminUser || "admin";
const resolvedAdminPass = adminPass || generateAdminPassword();
function buildDevAgentPayload({ vmid, runtime, version, memoryMiB }) {
if (!runtime) throw new Error("runtime required for dev container");
if (!version) throw new Error("version required for dev container");
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,
container_type: "dev",
runtime,
version,
memory_mb: Number(memoryMiB) || 2048,
};
}
steam_user: resolvedSteamUser,
steam_pass: resolvedSteamPass,
steam_auth: resolvedSteamAuth,
function buildGameAgentPayload(req) {
let javaPath = req.javaPath;
let artifactPath = req.artifactPath;
admin_user: resolvedAdminUser,
admin_pass: resolvedAdminPass,
// 🔧 FIXED JAVA LOGIC — NOTHING ELSE CHANGED
if (!javaPath && req.game === "minecraft") {
if (!req.version) {
throw new Error("minecraft version required for java selection");
}
javaPath = pickJavaForMinecraftVersion(req.version);
}
if (!artifactPath && req.game === "minecraft") {
switch (req.variant) {
case "forge":
artifactPath = `minecraft/forge/${req.version}/forge-installer.jar`;
break;
case "fabric":
artifactPath = `minecraft/fabric/${req.version}/fabric-server.jar`;
break;
case "neoforge":
artifactPath = `minecraft/neoforge/${req.version}/neoforge-installer.jar`;
break;
case "paper":
case "purpur":
case "vanilla":
artifactPath = `minecraft/${req.variant}/${req.version}/server.jar`;
break;
}
}
if (!javaPath) {
throw new Error(`BUG: java_path missing for ${req.game} ${req.variant}`);
}
if (!artifactPath) {
throw new Error(`BUG: artifact_path missing for ${req.game} ${req.variant}`);
}
return {
vmid: req.vmid,
container_type: "game",
game: req.game,
variant: req.variant,
version: req.version,
world: req.world,
ports: req.ports || [],
artifact_path: artifactPath,
java_path: javaPath,
memory_mb: req.memoryMiB,
admin_user: req.adminUser,
admin_pass: req.adminPass,
};
}
/* -------------------------------------------------------------
SEND CONFIG triggers async provision+start in agent
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",
headers,
body: JSON.stringify(payload),
@ -212,239 +156,147 @@ async function sendAgentConfig({ ip, payload }) {
}
}
/* -------------------------------------------------------------
WAIT FOR AGENT READY (poll /status)
------------------------------------------------------------- */
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}`;
async function waitForAgentTerminalState({ ip, timeoutMs = 10 * 60_000 }) {
const deadline = Date.now() + timeoutMs;
let last;
while (Date.now() < deadline) {
try {
const resp = await fetch(url, { headers });
if (!resp.ok) {
last = new Error(`/status HTTP ${resp.status}`);
} else {
const data = await resp.json().catch(() => ({}));
const state = (data.state || data.status || "").toLowerCase();
const res = await fetch(`http://${ip}:${AGENT_PORT}/status`);
if (res.ok) {
const data = await res.json();
// Agent's state machine:
// idle → installing → verifying → starting → running
if (state === "running") return { state: "running", raw: data };
if (state === "error" || state === "crashed") {
const msg = data.error || "";
throw new Error(`agent state=${state} ${msg ? `(${msg})` : ""}`);
}
if (data.state === "running") return;
last = new Error(`agent state=${state || "unknown"}`);
if (data.state === "error") {
throw new Error(data.error || "agent error");
}
} catch (err) {
last = err;
}
} catch {}
await sleep(3000);
}
throw last || new Error("Agent did not reach running state");
throw new Error("Agent did not reach running state");
}
/* -------------------------------------------------------------
MAIN PROVISION ENTRYPOINT
MAIN ENTRYPOINT
------------------------------------------------------------- */
export async function provisionAgentInstance(body = {}) {
const {
customerId,
game,
variant,
version,
world,
ctype: rawCtype,
name,
cpuCores,
memoryMiB,
diskGiB,
portsNeeded,
artifactPath,
javaPath,
const rawType =
body.container_type ??
body.containerType ??
body.ctype ??
"game";
// NEW optional fields
steamUser,
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 ctype = rawCtype || "game";
const isMinecraft = game.toLowerCase().includes("minecraft");
let vmid;
let allocatedPortsMap = null;
let gamePorts = [];
let ctIp;
let instanceHostname;
try {
console.log("[agentProvision] STEP 1: allocate VMID");
vmid = await allocateVmid(ctype);
instanceHostname = generateSystemHostname({ game, variant, vmid });
console.log("[agentProvision] STEP 2: port allocation");
if (!isMinecraft && (portsNeeded ?? 0) > 0) {
gamePorts = await PortAllocationService.reserve({
vmid,
count: portsNeeded,
portType: "game",
});
allocatedPortsMap = { game: gamePorts };
} else {
gamePorts = [25565];
allocatedPortsMap = { game: gamePorts };
if (!["game", "dev"].includes(rawType)) {
throw new Error(`invalid container type: ${rawType}`);
}
const node = process.env.PROXMOX_NODE || "zlh-prod1";
const bridge = ctype === "dev" ? "vmbr2" : "vmbr3";
const cpu = cpuCores ? Number(cpuCores) : 2;
const memory = memoryMiB ? Number(memoryMiB) : 2048;
const ctype = rawType;
console.log(`[agentProvision] starting ${ctype} provisioning`);
const description = name
? `${name} (customer=${customerId}; vmid=${vmid}; agent=v1)`
: `customer=${customerId}; vmid=${vmid}; agent=v1`;
const req =
ctype === "dev"
? normalizeDevRequest(body)
: normalizeGameRequest(body);
const tags = [
`cust-${customerId}`,
`type-${ctype}`,
`game-${game}`,
variant ? `var-${variant}` : null,
]
.filter(Boolean)
.join(",");
let vmid;
let ctIp;
console.log(
`[agentProvision] STEP 3: clone template ${AGENT_TEMPLATE_VMID} → vmid=${vmid}`
);
try {
step("allocate-vmid");
vmid = await allocateVmid(ctype);
const hostname = buildHostname({
ctype,
game: req.game,
variant: req.variant,
vmid,
});
step("clone-container");
await cloneContainer({
templateVmid: AGENT_TEMPLATE_VMID,
vmid,
name: instanceHostname,
name: hostname,
full: 1,
});
console.log("[agentProvision] STEP 4: configure CPU/mem/bridge/tags");
step("configure-container");
await configureContainer({
vmid,
cpu,
memory,
bridge,
description,
tags,
cpu: req.cpuCores || 2,
memory: req.memoryMiB || 2048,
bridge: ctype === "dev" ? "vmbr2" : "vmbr3",
});
console.log("[agentProvision] STEP 5: start container");
step("start-container");
await startWithRetry(vmid);
console.log("[agentProvision] STEP 6: detect container IP");
const ip = await getCtIpWithRetry(vmid, node, 12, 10_000);
if (!ip) throw new Error("Failed to detect container IP");
ctIp = ip;
step("wait-for-ip");
ctIp = await getCtIpWithRetry(vmid);
console.log(`[agentProvision] ctIp=${ctIp}`);
console.log("[agentProvision] STEP 7: build agent payload");
const payload = buildAgentPayload({
step("build-agent-payload");
const payload =
ctype === "dev"
? buildDevAgentPayload({
vmid,
game,
variant,
version,
world,
ports: gamePorts,
artifactPath,
javaPath,
memoryMiB,
runtime: body.runtime,
version: body.version,
memoryMiB: req.memoryMiB,
})
: buildGameAgentPayload({ ...req, vmid });
steamUser,
steamPass,
steamAuth,
adminUser,
adminPass,
});
console.log("[agentProvision] STEP 8: POST /config to agent (async provision+start)");
step("send-agent-config");
await sendAgentConfig({ ip: ctIp, payload });
console.log("[agentProvision] STEP 9: wait for agent to be running via /status");
const agentResult = await waitForAgentRunning({ ip: ctIp });
await waitForAgentTerminalState({ ip: ctIp });
console.log("[agentProvision] STEP 10: DB save");
const instance = await prisma.containerInstance.create({
step("persist-instance");
await prisma.containerInstance.create({
data: {
vmid,
customerId,
customerId: req.customerId,
ctype,
hostname: instanceHostname,
hostname,
ip: ctIp,
allocatedPorts: allocatedPortsMap,
payload,
agentState: agentResult.state,
agentState: "running",
agentLastSeen: new Date(),
},
});
console.log("[agentProvision] STEP 11: commit ports");
if (!isMinecraft && gamePorts.length) {
await PortAllocationService.commit({
if (ctype === "game") {
step("publish-edge");
const edgePorts =
req.ports?.length
? req.ports
: req.game === "minecraft"
? [25565]
: [];
await enqueuePublishEdge({
vmid,
ports: gamePorts,
portType: "game",
slotHostname: hostname,
ctIp,
game: req.game,
ports: edgePorts,
});
}
console.log("[agentProvision] STEP 12: publish edge");
await enqueuePublishEdge({
vmid,
slotHostname: instanceHostname,
instanceHostname,
ports: gamePorts,
ctIp,
game,
});
step("confirm-vmid");
await confirmVmidAllocated(vmid);
console.log("[agentProvision] COMPLETE");
return {
vmid,
ip: ctIp,
hostname: instanceHostname,
ports: gamePorts,
instance,
};
return { vmid, hostname, ip: ctIp };
} catch (err) {
console.error("[agentProvision] ERROR:", err.message);
try {
if (vmid) await PortAllocationService.releaseByVmid(vmid);
} catch {}
try {
if (vmid) await deleteContainer(vmid);
} catch {}
try {
if (vmid) await releaseVmid(vmid);
} catch {}
step("error-cleanup");
if (vmid) {
try { await deleteContainer(vmid); } catch {}
try { await releaseVmid(vmid); } catch {}
}
throw err;
}
}