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

115 lines
5.7 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;
}
function positiveInt(name: string, fallback: number): number {
const value = num(name, fallback);
if (!Number.isSafeInteger(value) || value < 1) throw new Error(`env ${name} must be a positive integer`);
return value;
}
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'),
deployKeepDays: num('DEPLOY_KEEP_DAYS', 7),
restartConcurrency: positiveInt('DEPLOYMENT_RESTART_CONCURRENCY', 1),
createConcurrency: positiveInt('DEPLOYMENT_CREATE_CONCURRENCY', 1),
httpTimeoutMs: num('HTTP_TIMEOUT_MS', 10_000),
komodo: {
address: env.KOMODO_ADDRESS ?? '',
apiKey: env.KOMODO_API_KEY ?? '',
apiSecret: env.KOMODO_API_SECRET ?? '',
// Komodo server (host) new deployments land on. Required for the
// deploy flow — Komodo needs to know which machine to schedule the
// container onto. Look up the exact name in Komodo's UI (Servers page).
targetServer: env.KOMODO_TARGET_SERVER ?? '',
// Default image for freshly-provisioned T5 and T6 zombie servers.
// Overridable per-deploy in the UI later if we ever need to.
defaultImage: env.PLUTAINER_IMAGE ?? 'ghcr.io/ayymoss/plutainer:latest',
defaultNetwork: env.PLUTAINER_NETWORK ?? 'komodo_default',
// Named docker volume that holds the read-only game files layer.
gameFilesVolume: env.PLUTAINER_GAMEFILES_VOLUME ?? 't5_game_files',
t6GameFilesVolume: env.PLUTAINER_T6_GAMEFILES_VOLUME ?? 't6_game_files'
},
// Shared across every newly-provisioned server. Kept in sync with the
// fleet's RCON_PASSWORD (rcon.password) so freshly-created servers respond
// to the same rcon secret the sweep already uses.
// Port range Electric Cherry auto-allocates from when creating a new
// deployment. Update this when the host's firewall opens more.
portRange: {
min: num('SERVER_PORT_MIN', 28960),
max: num('SERVER_PORT_MAX', 28980)
},
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 komodoConfigured(): boolean {
return !!(config.komodo.address && config.komodo.apiKey && config.komodo.apiSecret);
}
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}`;