[FEATURE] Migrated to new loading sequence (#6)
Co-authored-by: Dunemask <dunemask@gmail.com> Reviewed-on: https://gitea.dunemask.dev/elysium/minecluster/pulls/6
This commit is contained in:
parent
fb57c03ba7
commit
6eb4ed3e95
53 changed files with 1349 additions and 449 deletions
|
@ -10,7 +10,7 @@ import { sendError } from "../util/ExpressClientError.js";
|
|||
export async function listFiles(req, res) {
|
||||
const serverSpec = req.body;
|
||||
if (!serverSpec) return res.sendStatus(400);
|
||||
if (!serverSpec.name) return res.status(400).send("Server name required!");
|
||||
if (!serverSpec.id) return res.status(400).send("Server id missing!");
|
||||
listServerFiles(serverSpec)
|
||||
.then((f) => {
|
||||
const fileData = f.map((fi, i) => ({
|
||||
|
@ -29,7 +29,7 @@ export async function listFiles(req, res) {
|
|||
export async function createFolder(req, res) {
|
||||
const serverSpec = req.body;
|
||||
if (!serverSpec) return res.sendStatus(400);
|
||||
if (!serverSpec.name) return res.status(400).send("Server name required!");
|
||||
if (!serverSpec.id) return res.status(400).send("Server id missing!");
|
||||
if (!serverSpec.path) return res.status(400).send("Path required!");
|
||||
createServerFolder(serverSpec)
|
||||
.then(() => res.sendStatus(200))
|
||||
|
@ -39,7 +39,7 @@ export async function createFolder(req, res) {
|
|||
export async function deleteItem(req, res) {
|
||||
const serverSpec = req.body;
|
||||
if (!serverSpec) return res.sendStatus(400);
|
||||
if (!serverSpec.name) return res.status(400).send("Server name required!");
|
||||
if (!serverSpec.id) return res.status(400).send("Server id missing!");
|
||||
if (!serverSpec.path) return res.status(400).send("Path required!");
|
||||
if (serverSpec.isDir === undefined || serverSpec.isDir === null)
|
||||
return res.status(400).send("IsDIr required!");
|
||||
|
@ -50,7 +50,7 @@ export async function deleteItem(req, res) {
|
|||
|
||||
export async function uploadItem(req, res) {
|
||||
const serverSpec = req.body;
|
||||
if (!serverSpec.name) return res.status(400).send("Server name required!");
|
||||
if (!serverSpec.id) return res.status(400).send("Server id missing!");
|
||||
if (!serverSpec.path) return res.status(400).send("Path required!");
|
||||
uploadServerItem(serverSpec, req.file)
|
||||
.then(() => res.sendStatus(200))
|
||||
|
@ -59,7 +59,7 @@ export async function uploadItem(req, res) {
|
|||
|
||||
export async function getItem(req, res) {
|
||||
const serverSpec = req.body;
|
||||
if (!serverSpec.name) return res.status(400).send("Server name required!");
|
||||
if (!serverSpec.id) return res.status(400).send("Server id missing!");
|
||||
if (!serverSpec.path) return res.status(400).send("Path required!");
|
||||
getServerItem(serverSpec, res)
|
||||
.then(({ ds, ftpTransfer }) => {
|
||||
|
|
|
@ -6,43 +6,61 @@ import {
|
|||
getServerEntry,
|
||||
} from "../database/queries/server-queries.js";
|
||||
import { sendError } from "../util/ExpressClientError.js";
|
||||
import {
|
||||
startServerContainer,
|
||||
stopServerContainer,
|
||||
} from "../k8s/server-control.js";
|
||||
import { toggleServer } from "../k8s/k8s-server-control.js";
|
||||
|
||||
const dnsRegex = new RegExp(
|
||||
`^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])(\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*$`,
|
||||
);
|
||||
|
||||
function payloadFilter(req, res) {
|
||||
const serverSpec = req.body;
|
||||
if (!serverSpec) return res.sendStatus(400);
|
||||
const { name, host, version, serverType, difficulty, gamemode, memory } =
|
||||
const { name, host, version, serverType, memory } = serverSpec;
|
||||
const { backupHost, backupBucket, backupId, backupKey, backupInterval } =
|
||||
serverSpec;
|
||||
if (!name) return res.status(400).send("Server name is required!");
|
||||
if (!host) return res.status(400).send("Server host is required!");
|
||||
if (!dnsRegex.test(host)) return res.status(400).send("Hostname invalid!");
|
||||
if (!version) return res.status(400).send("Server version is required!");
|
||||
if (!difficulty)
|
||||
return res.status(400).send("Server difficulty is required!");
|
||||
if (!serverType) return res.status(400).send("Server type is required!");
|
||||
if (!gamemode) return res.status(400).send("Server Gamemode is required!");
|
||||
if (!memory) return res.status(400).send("Memory is required!");
|
||||
req.body.name = req.body.name.toLowerCase();
|
||||
// TODO: Impliment non creation time backups
|
||||
if (
|
||||
!!backupHost ||
|
||||
!!backupBucket ||
|
||||
!!backupId ||
|
||||
!!backupKey ||
|
||||
!!backupInterval
|
||||
) {
|
||||
// If any keys are required, all are required
|
||||
if (
|
||||
!(
|
||||
!!backupHost &&
|
||||
!!backupBucket &&
|
||||
!!backupId &&
|
||||
!!backupKey &&
|
||||
!!backupInterval
|
||||
)
|
||||
)
|
||||
return res.status(400).send("All backup keys are required!");
|
||||
if (!dnsRegex.test(backupHost))
|
||||
return res.status(400).send("Backup Host invalid!");
|
||||
}
|
||||
return "filtered";
|
||||
}
|
||||
|
||||
function checkServerName(serverSpec) {
|
||||
function checkServerId(serverSpec) {
|
||||
if (!serverSpec) throw new ExpressClientError({ c: 400 });
|
||||
if (!serverSpec.name)
|
||||
throw new ExpressClientError({ c: 400, m: "Server name required!" });
|
||||
if (!serverSpec.id)
|
||||
throw new ExpressClientError({ c: 400, m: "Server id missing!" });
|
||||
}
|
||||
|
||||
export async function createServer(req, res) {
|
||||
if (payloadFilter(req, res) !== "filtered") return;
|
||||
const serverSpec = req.body;
|
||||
try {
|
||||
const serverSpecs = await getServerEntry(serverSpec.name);
|
||||
if (serverSpecs.length !== 0) throw Error("Server already exists in DB!");
|
||||
await createServerResources(serverSpec);
|
||||
await createServerEntry(serverSpec);
|
||||
const serverEntry = await createServerEntry(serverSpec);
|
||||
await createServerResources(serverEntry);
|
||||
res.sendStatus(200);
|
||||
} catch (e) {
|
||||
sendError(res)(e);
|
||||
|
@ -53,11 +71,11 @@ export async function deleteServer(req, res) {
|
|||
// Ensure spec is safe
|
||||
const serverSpec = req.body;
|
||||
try {
|
||||
checkServerName(serverSpec);
|
||||
checkServerId(serverSpec);
|
||||
} catch (e) {
|
||||
return sendError(res)(e);
|
||||
}
|
||||
const deleteEntry = deleteServerEntry(serverSpec.name);
|
||||
const deleteEntry = deleteServerEntry(serverSpec.id);
|
||||
const deleteResources = deleteServerResources(serverSpec);
|
||||
Promise.all([deleteEntry, deleteResources])
|
||||
.then(() => res.sendStatus(200))
|
||||
|
@ -68,12 +86,12 @@ export async function startServer(req, res) {
|
|||
// Ensure spec is safe
|
||||
const serverSpec = req.body;
|
||||
try {
|
||||
checkServerName(serverSpec);
|
||||
checkServerId(serverSpec);
|
||||
} catch (e) {
|
||||
return sendError(res)(e);
|
||||
}
|
||||
const { name } = serverSpec;
|
||||
toggleServer(name, true)
|
||||
const { id } = serverSpec;
|
||||
toggleServer(id, true)
|
||||
.then(() => res.sendStatus(200))
|
||||
.catch(sendError(res));
|
||||
}
|
||||
|
@ -82,12 +100,12 @@ export async function stopServer(req, res) {
|
|||
// Ensure spec is safe
|
||||
const serverSpec = req.body;
|
||||
try {
|
||||
checkServerName(serverSpec);
|
||||
checkServerId(serverSpec);
|
||||
} catch (e) {
|
||||
return sendError(res)(e);
|
||||
}
|
||||
const { name } = serverSpec;
|
||||
toggleServer(name, false)
|
||||
const { id } = serverSpec;
|
||||
toggleServer(id, false)
|
||||
.then(() => res.sendStatus(200))
|
||||
.catch(sendError(res));
|
||||
}
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
import { getDeployments } from "../k8s/k8s-server-control.js";
|
||||
import { getInstances } from "../k8s/server-control.js";
|
||||
import { getInstances } from "../k8s/server-status.js";
|
||||
import { sendError } from "../util/ExpressClientError.js";
|
||||
|
||||
export function serverList(req, res) {
|
||||
getDeployments()
|
||||
.then((sd) => res.json(sd.map((s) => s.metadata.name.substring(4))))
|
||||
.catch((e) => {
|
||||
ERR("SERVER CONTROL", e);
|
||||
ERR("STATUS CONTROLLER", e);
|
||||
res.status(500).send("Couldn't get server list");
|
||||
});
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ import k8s from "@kubernetes/client-node";
|
|||
import { Rcon as RconClient } from "rcon-client";
|
||||
import stream from "stream";
|
||||
import { ERR, WARN } from "../../util/logging.js";
|
||||
import { getServerEntry } from "../../database/queries/server-queries.js";
|
||||
|
||||
// Kubernetes Configuration
|
||||
const kc = new k8s.KubeConfig();
|
||||
|
@ -12,8 +13,9 @@ const namespace = process.env.MCL_SERVER_NAMESPACE;
|
|||
|
||||
// Retrieves logs from the minecraft server container
|
||||
export async function webConsoleLogs(socket) {
|
||||
const { serverName } = socket.mcs;
|
||||
const podName = `mcl-${serverName}`;
|
||||
const { serverId } = socket.mcs;
|
||||
const server = await getServerEntry(serverId);
|
||||
const podName = `mcl-${server.mclName}`;
|
||||
const containerName = `${podName}-server`;
|
||||
const podResponse = await k8sCore.listNamespacedPod(namespace);
|
||||
const pods = podResponse.body.items.map((vp1) => vp1.metadata.name);
|
||||
|
@ -41,14 +43,15 @@ export async function webConsoleLogs(socket) {
|
|||
export async function webConsoleRcon(socket) {
|
||||
if (socket.rconClient)
|
||||
return VERB("RCON", "Socket already connected to RCON");
|
||||
const rconSecret = `mcl-${socket.mcs.serverName}-rcon-secret`;
|
||||
const { serverId } = socket.mcs;
|
||||
const server = await getServerEntry(serverId);
|
||||
const rconSecret = `mcl-${server.mclName}-rcon-secret`;
|
||||
const rconRes = await k8sCore.readNamespacedSecret(rconSecret, namespace);
|
||||
const rconPassword = Buffer.from(
|
||||
rconRes.body.data["rcon-password"],
|
||||
"base64",
|
||||
).toString("utf8");
|
||||
const { serverName } = socket.mcs;
|
||||
const rconHost = `mcl-${serverName}-rcon.${namespace}.svc.cluster.local`;
|
||||
const rconHost = `mcl-${server.mclName}-rcon.${namespace}.svc.cluster.local`;
|
||||
const rcon = new RconClient({
|
||||
host: rconHost,
|
||||
port: 25575,
|
||||
|
@ -58,7 +61,7 @@ export async function webConsoleRcon(socket) {
|
|||
try {
|
||||
await rcon.connect();
|
||||
} catch (error) {
|
||||
socket.emit("push", "Could not connect RCON Input to server!");
|
||||
socket.emit("rcon-error", "Could not connect RCON Input to server!");
|
||||
WARN("RCON", `Could not connect to '${rconHost}'`);
|
||||
}
|
||||
socket.rconClient = rcon;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue