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