diff --git a/deployment/idunn/recipe.toml b/deployment/idunn/recipe.toml index 2ef90ce..6e9fbff 100644 --- a/deployment/idunn/recipe.toml +++ b/deployment/idunn/recipe.toml @@ -94,9 +94,12 @@ executable = false executable_artifact = "heimdall-dist" arguments = [ { kind = "literal", value = "dist/src/index.js" }, + { kind = "literal", value = "--state-root" }, + { kind = "binding", name = "state_root" }, ] -transport = "rudp" +transport = "http" route_required = true +optional_environment = [] required_environment = [ "GAMECULT_IDUNN_RUNTIME_BUNDLE", "GAMECULT_IDUNN_CANDIDATE_BIND", diff --git a/src/config.ts b/src/config.ts index 03c08a2..ec48061 100644 --- a/src/config.ts +++ b/src/config.ts @@ -140,9 +140,38 @@ function readOptionalString(envValue: string | undefined): string | undefined { return value ? value : undefined; } -export function loadConfig(env: NodeJS.ProcessEnv = process.env): HeimdallConfig { +/** + * `--state-root PATH`, the directory Idunn assigns for this target's persistent + * state. Idunn owns that location: the recipe declares state slots relative to + * it and the operator binding supplies the absolute path, so the service is + * told rather than choosing. Absent outside Idunn, where GC_ACCESS_DATA_ROOT + * and the repo-local default still apply. + */ +function readStateRootArgument(argv: readonly string[]): string | undefined { + const index = argv.indexOf("--state-root"); + if (index === -1) { + return undefined; + } + + const value = argv[index + 1]; + if (!value || value.startsWith("--")) { + throw new Error("--state-root requires a path"); + } + + if (!path.isAbsolute(value)) { + throw new Error(`--state-root must be absolute: ${value}`); + } + + return value; +} + +export function loadConfig( + env: NodeJS.ProcessEnv = process.env, + argv: readonly string[] = process.argv.slice(2) +): HeimdallConfig { const sourceRoot = fileURLToPath(new URL("../", import.meta.url)); const workspaceRoot = path.resolve(sourceRoot, ".."); + const stateRootArgument = readStateRootArgument(argv); // Where Heimdall listens and what Heimdall advertises are two different // authorities, and Idunn is the reason they must not be conflated. Under // Idunn a candidate and the incumbent run at once, so the candidate is told @@ -163,7 +192,8 @@ export function loadConfig(env: NodeJS.ProcessEnv = process.env): HeimdallConfig const publicBaseUrl = trimTrailingSlash(env.GC_ACCESS_BASE_URL ?? `http://${host}:${port}`); const issuer = trimTrailingSlash(env.GC_ACCESS_ISSUER ?? publicBaseUrl); - const dataRoot = env.GC_ACCESS_DATA_ROOT ?? path.join(workspaceRoot, ".heimdall-data"); + const dataRoot = + stateRootArgument ?? env.GC_ACCESS_DATA_ROOT ?? path.join(workspaceRoot, ".heimdall-data"); const storageBackend = env.GC_ACCESS_STORAGE_BACKEND === "postgres" || env.GC_ACCESS_DATABASE_URL ? "postgres" : "memory"; const providersConfig = Object.fromEntries( diff --git a/tests/idunn-candidate-bind.test.ts b/tests/idunn-candidate-bind.test.ts index 5467f54..7e1c560 100644 --- a/tests/idunn-candidate-bind.test.ts +++ b/tests/idunn-candidate-bind.test.ts @@ -1,3 +1,4 @@ +import path from "node:path"; import { describe, expect, it } from "vitest"; import { loadConfig } from "../src/config.js"; @@ -65,3 +66,33 @@ describe("Idunn candidate bind", () => { expect(config.port).toBe(18833); }); }); + +describe("Idunn state root argument", () => { + it("takes --state-root over GC_ACCESS_DATA_ROOT", () => { + // Idunn owns where state lives: the recipe declares slots relative to + // state_root and the binding supplies the absolute path as a launch + // argument. The env var remains for runs outside Idunn. + const config = loadConfig({ GC_ACCESS_DATA_ROOT: "/tmp/ignored" }, [ + "--state-root", + "/var/lib/gamecult/heimdall", + ]); + + expect(config.dataRoot).toBe("/var/lib/gamecult/heimdall"); + expect(config.cultCachePath).toContain("heimdall.service.cc"); + expect(config.cultCachePath).toBe( + path.join(config.dataRoot, "cultcache", "heimdall.service.cc") + ); + }); + + it("falls back to GC_ACCESS_DATA_ROOT when the argument is absent", () => { + expect(loadConfig({ GC_ACCESS_DATA_ROOT: "/srv/heimdall/state" }, []).dataRoot).toBe( + "/srv/heimdall/state" + ); + }); + + it("rejects a missing or relative --state-root", () => { + expect(() => loadConfig({}, ["--state-root"])).toThrow(/requires a path/); + expect(() => loadConfig({}, ["--state-root", "--other"])).toThrow(/requires a path/); + expect(() => loadConfig({}, ["--state-root", "relative/path"])).toThrow(/must be absolute/); + }); +});