Files
electric-cherry/src/lib/server/config.ts
T

86 lines
3.9 KiB
TypeScript

import { env } from 'node:process';
import { dirname, join } from 'node:path';
function req(name: string, fallback?: string): string {
const v = env[name] ?? fallback;
if (v === undefined) throw new Error(`missing env: ${name}`);
return v;
}
function num(name: string, fallback: number): number {
const raw = env[name];
if (!raw) return fallback;
const n = Number(raw);
if (!Number.isFinite(n)) throw new Error(`env ${name}=${raw} is not a number`);
return n;
}
export const config = {
serversRoot: req('SERVERS_ROOT', '/root/zombies/t5'),
t6ServersRoot:
env.T6_SERVERS_ROOT ?? join(dirname(req('SERVERS_ROOT', '/root/zombies/t5')), 't6'),
t6RelDestScript: env.T6_REL_DEST_SCRIPT ?? 'runtime/plutonium/storage/t6/scripts/zm',
t6ConfigTargetFile: env.T6_CONFIG_TARGET_FILE ?? 'dedicated_zm.cfg',
relDest: req('REL_DEST', 'runtime/plutonium/storage/t5/raw/scripts/sp'),
// Per-kind destination sub-paths under each server's directory. `relDest`
// above stays as the script fallback so existing deploys keep working.
relDestScript:
env.REL_DEST_SCRIPT ?? env.REL_DEST ?? 'runtime/plutonium/storage/t5/raw/scripts/sp',
relDestConfig: env.REL_DEST_CONFIG ?? 'configs',
relDestPlugin: env.REL_DEST_PLUGIN ?? 'runtime/plutonium/plugins',
// Legacy T5 config filename. T6 has its own target above.
configTargetFile: env.CONFIG_TARGET_FILE ?? 'dedicated_sp.cfg',
// Where on-disk copies of active beta files live for operator visibility.
// Cleared per-beta on promote/rollback.
betaArchiveDir: req('BETA_ARCHIVE_DIR', '/root/zombies/beta-archive'),
dbPath: req('DB_PATH', './data/cherry.db'),
httpTimeoutMs: num('HTTP_TIMEOUT_MS', 10_000),
defaultRuntimeImage: env.PLUTAINER_IMAGE ?? 'ghcr.io/ayymoss/plutainer:latest',
rcon: {
// Host every RCON packet is sent to. Defaults to the Docker
// host-gateway alias so the electric-cherry container can reach
// Plutonium servers running on the same host without knowing each
// container's internal IP.
host: env.RCON_HOST ?? 'host.docker.internal',
// Shared across the fleet — every server uses the same rcon_password.
password: env.RCON_PASSWORD ?? '',
timeoutMs: num('RCON_TIMEOUT_MS', 3_000)
}
} as const;
export function rconConfigured(): boolean {
return !!config.rcon.password;
}
export type ArtifactKind = 'script' | 'config' | 'plugin' | 'baseline';
export function relDestForKind(kind: ArtifactKind, game = 't5'): string {
switch (kind) {
case 'script':
return game === 't6' ? config.t6RelDestScript : config.relDestScript;
case 'config':
return config.relDestConfig;
case 'baseline':
return config.relDestConfig;
case 'plugin':
return config.relDestPlugin;
}
}
// Server IDs keep the game prefix even while creation is queued and no registry
// row exists yet. Legacy IDs retain the original T5 filesystem defaults.
export const gameOfServer = (server: string) => (server.startsWith('t6_') ? 't6' : 't5');
export const rootOfServer = (server: string) =>
gameOfServer(server) === 't6' ? config.t6ServersRoot : config.serversRoot;
export const serverDir = (server: string) => `${rootOfServer(server)}/${server}`;
export const configFileForGame = (game: string | null) =>
game === 't6' ? config.t6ConfigTargetFile : config.configTargetFile;
export const configFileOf = (server: string) => configFileForGame(gameOfServer(server));
export const destOfKind = (server: string, kind: ArtifactKind) =>
`${serverDir(server)}/${relDestForKind(kind, gameOfServer(server))}`;
// Back-compat: existing call sites written before the multi-kind refactor
// resolve to the script destination.
export const destOf = (server: string) => destOfKind(server, 'script');
export const betaArchiveOf = (betaId: number) => `${config.betaArchiveDir}/beta-${betaId}`;