Files
Kelx/utils/server-host-ports.js
2026-11-30 16:32:37 +00:00

44 lines
1.8 KiB
JavaScript

"use strict";
// adpanel:node-assignment - a legacy name or address cannot override an explicit assignment to another node.
const NODE_SERVER_ASSIGNMENT_WHERE = `servers.node_id = ?
OR (
(servers.node_id = ? OR LOWER(servers.node_id) = ? OR servers.ip = ?)
AND NOT EXISTS (
SELECT 1 FROM nodes n
WHERE (n.id = servers.node_id OR n.uuid = servers.node_id)
AND n.id <> ?
)
)`;
function nodeServerAssignmentParams(node) {
return [node.id, String(node.uuid || node.id).trim(), String(node.name || "").trim().toLowerCase(), String(node.address || "").trim(), node.id];
}
function assignedServerPortUses(entry) {
const uses = new Map();
const resources = entry.resources;
const addresses = new Map((Array.isArray(resources?.portAllocations) ? resources.portAllocations : [])
.map(allocation => [Number(allocation?.port), allocation?.ip]));
const add = (value, portType) => {
if (typeof value !== "number" && typeof value !== "string") return;
if (!/^\d{1,5}$/.test(String(value).trim())) return;
const port = Number(value);
if (!Number.isInteger(port) || port < 1 || port > 65535 || uses.has(port)) return;
uses.set(port, { port, portType, ip: String(addresses.get(port) || entry.ip || "").trim() });
};
add(entry.port, "primary");
if (!uses.size) add(entry.runtime?.port ?? entry.runtime?.ports?.[0], "runtime");
add(entry.reinstallPendingPort, "reinstall");
for (const port of Array.isArray(resources?.ports) ? resources.ports : []) {
add(port, "additional");
}
return Array.from(uses.values());
}
function serverEntryAssignedHostPorts(entry) {
return assignedServerPortUses(entry).map(use => use.port);
}
module.exports = { NODE_SERVER_ASSIGNMENT_WHERE, nodeServerAssignmentParams, assignedServerPortUses, serverEntryAssignedHostPorts };