Require config files to be explicitly created (#1040)

The behaviour of creating the config file if it's missing while
convenient causes confusion and problems when the tools are run
in the wrong folder.  They end up creating a shadow installation
and misdirect the user with nonsensical error messages, instead
pointing to the real problem which is that the tool can't find
the configuration needed to actually run.

Require a config file to be present at startup and refuse to run if this
file is missing.  To support the odd use-case of creating the config
when missing a --create-config flag is added. To conveniently generate
a config file to edit out from the "config create" command is added.
This commit is contained in:
Hornwitser authored and GitHub committed 2026-09-15 22:16:54 +02:00
1 parent 5df5af7ca8
commit 2c52c265cf
15 files changed
+249 -75

No files matched your search

+10 -4
View File
@@ -212,26 +212,32 @@ describe("Integration of Clusterio", function() {
const lockFilePath = `${controllerConfigPath}.lock`;
await fs.copyFile(lockFilePath, "temp.lock");
try {
await execController("run --bypass-lock-file");
await execController("run --create-config --bypass-lock-file");
} catch (err) {
assert.equal(/Error: Server listening failed/.test(err.stderr), true);
}
await fs.rename("temp.lock", lockFilePath); // Replace the lockfile after it is deleted
});
it("should refuse to start when no users are loaded", async function() {
it("should refuse to start if the config is missing and --create-config is not passed", async function() {
const dir = path.join("temp", "test", "empty_controller");
await fs.rm(dir, { force: true, recursive: true, maxRetries: 10 });
await fs.mkdir(dir, { recursive: true });
await assert.rejects(execController("run", { cwd: dir }));
});
it("should start when no users are loaded if bypass option given", async function() {
it("should refuse to start when no users are loaded", async function() {
const dir = path.join("temp", "test", "empty_controller");
await fs.rm(dir, { force: true, recursive: true, maxRetries: 10 });
await fs.mkdir(dir, { recursive: true });
await assert.rejects(execController("run --create-config", { cwd: dir }));
});
it("should start when no users are loaded and bypass options are given", async function() {
slowTest(this);
const dir = path.join("temp", "test", "empty_controller");
await fs.rm(dir, { force: true, recursive: true, maxRetries: 10 });
await fs.mkdir(dir, { recursive: true });
const child = await spawnNode(
"altController",
"../../../packages/controller run --no-check-user-count",
"../../../packages/controller run --create-config --no-check-user-count",
/Started controller/,
{ cwd: dir },
);
+3 -1
View File
@@ -354,6 +354,7 @@ before(async function() {
});
console.log("Setting Controller Config");
await execController("config create");
await execController("config set controller.auth_secret TestSecretDoNotUse");
await execController("config set controller.http_port 8880");
await execController("config set controller.https_port 4443");
@@ -385,6 +386,7 @@ before(async function() {
const relativeFactorioDir = path.isAbsolute(factorioDir) ? factorioDir : path.join("..", "..", factorioDir);
await execCtlProcess("host create-config --id 4 --name host --generate-token");
await execHost("config create");
await execHost(`config set host.factorio_directory ${relativeFactorioDir}`);
hostProcess = await spawnNode("host:", "../../packages/host run", /Started host/);
@@ -394,7 +396,7 @@ before(async function() {
control = new TestControl(controlConnector);
await controlConnector.connect();
const initArgs = await initializeCtl(`--plugin-list ${pluginListPath} config list`, undefined, true);
const initArgs = await initializeCtl(`--plugin-list ${pluginListPath} config create`, undefined, true);
control.config = initArgs.ctlConfig;
control.hooks = initArgs.ctlHooks;
+97
View File
@@ -0,0 +1,97 @@
import assert from "node:assert/strict";
import fs from "node:fs/promises";
import path from "node:path";
import * as lib from "@clusterio/lib";
describe("lib/bootstrap", function() {
describe("loadConfigFromArgs()", function() {
const folder = path.join("temp", "test", "bootstrap");
const configPath = path.join(folder, "sample-config.json");
const noentPath = path.join(folder, "noent-config.json");
const createPath1 = path.join(folder, "create-config-1.json");
const createPath2 = path.join(folder, "create-config-2.json");
before(async function() {
await fs.mkdir(folder, { recursive: true });
await fs.writeFile(
configPath,
JSON.stringify({
"ctl.controller_url": "http://example/foo",
}),
{ encoding: "utf8" },
);
await fs.unlink(noentPath).catch(() => {});
await fs.unlink(createPath1).catch(() => {});
await fs.unlink(createPath2).catch(() => {});
});
it("should load an existing config", async function() {
const [config] = await lib.loadConfigFromArgs(
{
config: configPath,
bypassLockFile: false,
createConfig: false,
_: ["run"],
},
lib.CtlConfig,
"control",
);
assert.equal(config.get("ctl.controller_url"), "http://example/foo");
});
it("should reject if config does not exist", async function() {
await assert.rejects(
() => lib.loadConfigFromArgs(
{
config: noentPath,
bypassLockFile: false,
createConfig: false,
_: ["run"],
},
lib.CtlConfig,
"control",
),
/Error: Failed to load temp.test.bootstrap.noent-config\.json/
);
});
it("should create the config if it does not exist and createConfig is set", async function() {
const [config] = await lib.loadConfigFromArgs(
{
config: createPath1,
bypassLockFile: false,
createConfig: true,
_: ["run"],
},
lib.CtlConfig,
"control",
);
assert.equal(config.get("ctl.controller_url"), null);
await assert.doesNotReject(fs.access(createPath1));
});
it("should create the config if it does not exist and the command is config create", async function() {
const [config] = await lib.loadConfigFromArgs(
{
config: createPath2,
bypassLockFile: false,
createConfig: false,
_: ["config", "create"],
},
lib.CtlConfig,
"control",
);
assert.equal(config.get("ctl.controller_url"), null);
await assert.doesNotReject(fs.access(createPath2));
});
it("should use the existing config if it does not exist and createConfig is set", async function() {
const [config] = await lib.loadConfigFromArgs(
{
config: configPath,
bypassLockFile: false,
createConfig: true,
_: ["run"],
},
lib.CtlConfig,
"control",
);
assert.equal(config.get("ctl.controller_url"), "http://example/foo");
});
});
});