Mehrere Docker-Hosts: Matches verteilt auf die Hosts starten
CS2_DOCKER_HOSTS nimmt eine Liste von Docker-Hosts. Jedes Match läuft auf dem Host mit den meisten freien Ports und merkt sich seinen Host, damit Status, Konfiguration und Stopp dort landen. Ohne die Variable bleibt alles beim bisherigen einzelnen Host. deploy/docker-host-einrichten.sh richtet einen Host per SSH ein.
This commit is contained in:
1 parent
1a304cb398
commit
eda15bc585
9 files changed
+297
-5
No files matched your search
@@ -29,7 +29,7 @@
|
||||
*/
|
||||
|
||||
import { DemoServerAdapter } from './demoAdapter.js';
|
||||
import { DockerServerAdapter } from './dockerAdapter.js';
|
||||
import { DockerHostPool } from './dockerAdapter.js';
|
||||
import { StaticPoolServerAdapter } from './staticPoolAdapter.js';
|
||||
|
||||
export function createServerAdapter(config, deps) {
|
||||
@@ -37,7 +37,7 @@ export function createServerAdapter(config, deps) {
|
||||
case 'demo':
|
||||
return new DemoServerAdapter(config.demo);
|
||||
case 'docker':
|
||||
return new DockerServerAdapter(config.docker, deps);
|
||||
return new DockerHostPool(config.docker, deps);
|
||||
case 'static':
|
||||
return new StaticPoolServerAdapter(config.staticPool, deps);
|
||||
default:
|
||||
|
||||
@@ -37,7 +37,8 @@ export class DockerServerAdapter {
|
||||
|
||||
async docker(args, timeout = 60_000) {
|
||||
if (this.runDocker) return this.runDocker(args);
|
||||
const { stdout } = await exec(this.options.bin, args, { timeout, maxBuffer: 4 * 1024 * 1024 });
|
||||
const env = this.options.dockerHost ? { ...process.env, DOCKER_HOST: this.options.dockerHost } : process.env;
|
||||
const { stdout } = await exec(this.options.bin, args, { timeout, maxBuffer: 4 * 1024 * 1024, env });
|
||||
return stdout.trim();
|
||||
}
|
||||
|
||||
@@ -137,7 +138,14 @@ export class DockerServerAdapter {
|
||||
await this.docker(args, 15 * 60_000);
|
||||
|
||||
return {
|
||||
handle: { container, rconHost: this.options.rconHost, port, startedAt: Date.now(), rconPassword: secrets.rconPassword },
|
||||
handle: {
|
||||
container,
|
||||
hostName: this.options.name,
|
||||
rconHost: this.options.rconHost,
|
||||
port,
|
||||
startedAt: Date.now(),
|
||||
rconPassword: secrets.rconPassword,
|
||||
},
|
||||
host: this.options.host,
|
||||
port,
|
||||
tvPort,
|
||||
@@ -192,3 +200,99 @@ export class DockerServerAdapter {
|
||||
await this.docker(['rm', '-f', handle.container]).catch(() => {});
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Mehrere Docker-Hosts. Jedes Match läuft auf dem Host mit den meisten freien Port-Paaren,
|
||||
* der Host steht im Handle und damit im Match. Start, Status, Konfiguration und Stopp gehen
|
||||
* immer an denselben Host. Ohne CS2_DOCKER_HOSTS gibt es genau einen Host: den bisherigen.
|
||||
*/
|
||||
export class DockerHostPool {
|
||||
constructor(options, { getUsedPorts, runDocker } = {}) {
|
||||
this.name = 'docker';
|
||||
this.simulated = false;
|
||||
this.depsGetUsedPorts = getUsedPorts;
|
||||
const hosts = options.hosts ?? [{
|
||||
name: 'default',
|
||||
dockerHost: process.env.DOCKER_HOST || null,
|
||||
publicHost: options.host,
|
||||
rconHost: options.rconHost,
|
||||
portRangeStart: options.portRangeStart,
|
||||
portRangeEnd: options.portRangeEnd,
|
||||
volume: options.volume,
|
||||
}];
|
||||
// Fehlende Angaben kommen aus den allgemeinen Docker-Einstellungen
|
||||
this.hosts = hosts.map((h) => {
|
||||
const host = {
|
||||
name: h.name,
|
||||
dockerHost: h.dockerHost ?? null,
|
||||
publicHost: h.publicHost ?? options.host,
|
||||
rconHost: h.rconHost ?? options.rconHost,
|
||||
portRangeStart: h.portRangeStart ?? options.portRangeStart,
|
||||
portRangeEnd: h.portRangeEnd ?? options.portRangeEnd,
|
||||
volume: h.volume ?? options.volume,
|
||||
};
|
||||
return { ...options, ...host, host: host.publicHost };
|
||||
});
|
||||
this.adapters = new Map(this.hosts.map((h) => [h.name, new DockerServerAdapter(h, {
|
||||
getUsedPorts: () => (getUsedPorts ? getUsedPorts(h.publicHost) : new Set()),
|
||||
runDocker,
|
||||
})]));
|
||||
}
|
||||
|
||||
/** Freie Port-Paare auf einem Host. */
|
||||
freeSlots(host) {
|
||||
const used = this.getUsedPorts(host.publicHost);
|
||||
let free = 0;
|
||||
for (let p = host.portRangeStart; p + 1 <= host.portRangeEnd; p += 2) {
|
||||
if (!used.has(p) && !used.has(p + 1)) free += 1;
|
||||
}
|
||||
return free;
|
||||
}
|
||||
|
||||
getUsedPorts(publicHost) {
|
||||
return this.depsGetUsedPorts?.(publicHost) ?? new Set();
|
||||
}
|
||||
|
||||
pickHost() {
|
||||
const best = this.hosts
|
||||
.map((h) => ({ host: h, free: this.freeSlots(h) }))
|
||||
.filter((x) => x.free > 0)
|
||||
.sort((a, b) => b.free - a.free)[0];
|
||||
if (!best) throw new Error('Kein Docker-Host mit freien Ports');
|
||||
return best.host;
|
||||
}
|
||||
|
||||
async start(args) {
|
||||
return this.adapters.get(this.pickHost().name).start(args);
|
||||
}
|
||||
|
||||
adapterFor(handle) {
|
||||
const name = handle?.hostName ?? this.hosts[0].name;
|
||||
const adapter = this.adapters.get(name);
|
||||
if (!adapter) throw new Error(`Docker-Host "${name}" ist nicht (mehr) konfiguriert`);
|
||||
return adapter;
|
||||
}
|
||||
|
||||
async status(handle) {
|
||||
return this.adapterFor(handle).status(handle);
|
||||
}
|
||||
|
||||
async configure(handle, plan) {
|
||||
return this.adapterFor(handle).configure(handle, plan);
|
||||
}
|
||||
|
||||
async listRoundBackups(handle, matchId) {
|
||||
return this.adapterFor(handle).listRoundBackups(handle, matchId);
|
||||
}
|
||||
|
||||
async loadRoundBackup(handle, file) {
|
||||
return this.adapterFor(handle).loadRoundBackup(handle, file);
|
||||
}
|
||||
|
||||
async sendCommands(handle, commands) {
|
||||
return this.adapterFor(handle).sendCommands(handle, commands);
|
||||
}
|
||||
|
||||
async stop(handle) {
|
||||
return this.adapterFor(handle).stop(handle);
|
||||
}
|
||||
}
|
||||
@@ -160,6 +160,16 @@ export class ServerManager {
|
||||
this.service.changed(matchId);
|
||||
}
|
||||
|
||||
/** Ports, die auf einem bestimmten Docker-Host belegt sind. */
|
||||
usedPortsOn(host) {
|
||||
const used = new Set();
|
||||
for (const r of this.db.prepare(`SELECT port, tv_port FROM servers WHERE host = ? AND status != 'stopped' AND port IS NOT NULL`).all(host)) {
|
||||
used.add(r.port);
|
||||
used.add(r.tv_port);
|
||||
}
|
||||
return used;
|
||||
}
|
||||
|
||||
usedPorts() {
|
||||
const used = new Set();
|
||||
for (const r of this.db.prepare(`SELECT host, port, tv_port FROM servers WHERE status != 'stopped' AND port IS NOT NULL`).all()) {
|
||||
|
||||
Reference in new issue
Block a user