34 lines
2.3 KiB
TypeScript
34 lines
2.3 KiB
TypeScript
import { createHash, randomUUID } from 'node:crypto';
|
|
|
|
/** Seed verified host inventory without copying a complete game distribution. */
|
|
export async function prepareNativeHost(servers: string[] = []) {
|
|
const { config } = await import('../../src/lib/server/config.js');
|
|
const hosts = await import('../../src/lib/server/hosts/index.js');
|
|
const libraries = await import('../../src/lib/server/game-files/index.js');
|
|
const { db } = await import('../../src/lib/server/db/index.js');
|
|
const host = hosts.createHost({ name: 'Fixture host', portMin: 28960, portMax: 29060 });
|
|
const inventory = { protocolVersion: 1, cpuCount: 64, cpuUsagePercent: 0,
|
|
memoryTotal: 128 * 1024 ** 3, memoryAvailable: 120 * 1024 ** 3,
|
|
dockerRunning: true, directoriesWritable: true, storageAvailable: 100_000_000_000,
|
|
runtimeImages: [config.defaultRuntimeImage] };
|
|
hosts.enrollHost(hosts.createEnrollment(host.id).token, inventory);
|
|
const versions = new Map<string, string>();
|
|
for (const game of ['t5', 't6'] as const) {
|
|
const version = libraries.createLibraryImport({ hostId: host.id, game });
|
|
const manifest = JSON.stringify([{ path: 'zone/base.ff', size: 4, sha256: createHash('sha256').update('game').digest('hex') }]);
|
|
const hash = createHash('sha256').update(manifest).digest('hex');
|
|
db().prepare("UPDATE game_file_versions SET state='ready',manifest=?,manifest_sha256=?,size_bytes=4,published_at=? WHERE id=?")
|
|
.run(manifest, hash, new Date().toISOString(), version.id);
|
|
libraries.updateHostLibraryCaches(host.id, [{ libraryVersionId: version.id, status: 'ready', manifestSha256: hash }]);
|
|
versions.set(game, version.id);
|
|
}
|
|
const heartbeat = () => hosts.heartbeat(host.id, { ...inventory, libraries: [...versions.values()].map(id => ({ id, state: 'ready' as const })) });
|
|
heartbeat();
|
|
for (const [index, server] of servers.entries()) {
|
|
db().prepare(`INSERT INTO native_instances(id,server,host_id,library_id,image,port,created_at)
|
|
VALUES(?,?,?,?,?,?,?)`).run(randomUUID(), server, host.id, versions.get(server.startsWith('t6_') ? 't6' : 't5'),
|
|
config.defaultRuntimeImage, 28960 + index, new Date().toISOString());
|
|
}
|
|
return { host, versions, heartbeat };
|
|
}
|