Update config of running Factorio instances

Monitor the factorio.settings config for changes and apply them to
running instances via the /config set command.
This commit is contained in:
Hornwitser committed 2020-05-31 22:28:51 +02:00
1 parent 5b043524b4
commit 931ad4816f
8 files changed
+274 -51

No files matched your search

+86 -1
View File
@@ -4,7 +4,7 @@ const path = require("path");
const validateHTML = require('html5-validator');
const parallel = require('mocha.parallel');
const { slowTest, get, exec, controlConfigPath, instancesDir } = require("./index");
const { slowTest, get, exec, sendRcon, controlConfigPath, instancesDir } = require("./index");
describe("Integration of Clusterio", function() {
@@ -82,6 +82,91 @@ describe("Integration of Clusterio", function() {
});
});
describe("set-instance-config-prop", function() {
it("applies factorio settings while running", async function() {
slowTest(this);
let testConfigs = [
// json name, value to set,
// /config name, expected result
[
"afk_autokick_interval", 2,
"afk-auto-kick", "Kick if AFK for more than 2 minutes.",
],
[
"allow_commands", true,
"allow-commands", "Yes",
],
[
"autosave_interval", 17,
"autosave-interval", "Autosave every 17 minutes.",
],
[
"autosave_only_on_server", false,
"autosave-only-on-server", "Autosave only on server: false.",
],
[
"description", "A test server blah blah",
"description", "Server description: A test server blah blah",
],
[
"ignore_player_limit_for_returning_players", true,
"ignore-player-limit-for-returning-players", "Ignore player limit for returning players: true."
],
[
"max_players", 11,
"max-players", "11",
],
[
"max_upload_slots", 7,
"max-upload-slots", "7 slots.",
],
[
"max_upload_in_kilobytes_per_second", 123,
"max-upload-speed", "123 kilobytes per second.",
],
[
"name", "A test",
"name", "Server name: A test",
],
[
"only_admins_can_pause_the_game", false,
"only-admins-can-pause", "Only admins can pause: false.",
],
[
"game_password", "secret",
"password", "The server currently has a password.",
],
[
"tags", ["clusterio", "test-tag"],
"tags", "Server tags: clusterio test-tag",
],
[
"visibility", { lan: false, public: false },
"visibility-lan", "LAN visibility: false.",
],
// Public visibility must be reset before verify can be reset
[
"require_user_verification", false,
"require-user-verification", "Verify user identity: false.",
],
];
for (let [prop, value, , ] of testConfigs) {
value = `"'${JSON.stringify(value).replace(/"/g, process.platform === "win32" ? '""' : '\\"')}'"`;
let args = `--instance test --field factorio.settings --prop ${prop} --value ${value}`;
await exec(`node clusterctl --config ${controlConfigPath} set-instance-config-prop ${args}`);
}
// Do this afterwards to leave enough to time for the
// changes to have propogated.
for (let [, , configName, expectedResult] of testConfigs) {
assert.equal(await sendRcon(44, `/config get ${configName}`), `${expectedResult}\n`);
}
});
});
describe("stop-instance", function() {
it("stops the instance", async function() {
slowTest(this);
+37
View File
@@ -6,9 +6,31 @@ const needle = require("needle");
const util = require("util");
const events = require("events");
const link = require("lib/link");
const server = require("lib/factorio/server");
class TestControl extends link.Link {
constructor(connector) {
super("control", "master", connector);
link.attachAllMessages(this);
}
async prepareDisconnectRequestHandler(message, request) {
this.connector.setClosing();
return await super.prepareDisconnectRequestHandler(message, request);
}
async debugWsMessageEventHandler() { }
async instanceOutputEventHandler() { }
}
class TestControlConnector extends link.WebSocketClientConnector {
register() {
this.sendHandshake("register_control", { token: this.token, agent: "clusterctl", version: "test" });
}
}
// Mark that this test takes a lot of time, or depeneds on a test
// that takes a lot of time.
function slowTest(test) {
@@ -29,6 +51,7 @@ async function get(path) {
let masterProcess;
let slaveProcess;
let control;
let url = "https://localhost:4443/";
let token = jwt.sign({ id: "api" }, "TestSecretDoNotUse");
@@ -43,6 +66,11 @@ async function exec(...args) {
return await util.promisify(child_process.exec)(...args);
}
async function sendRcon(instanceId, command) {
let response = await link.messages.sendRcon.send(control, { instance_id: instanceId, command })
return response.result;
}
function spawn(name, cmd, waitFor) {
return new Promise((resolve, reject) => {
console.log(cmd);
@@ -94,6 +122,11 @@ before(async function() {
masterProcess = await spawn("master:", `node master --config ${masterConfigPath} run`, "All plugins loaded");
slaveProcess = await spawn("slave:", `node slave --config ${slaveConfigPath} run`, "SOCKET | received ready from master");
let controlConnector = new TestControlConnector(url, 2);
controlConnector.token = token;
control = new TestControl(controlConnector);
await controlConnector.connect();
});
after(async function() {
@@ -118,9 +151,13 @@ process.on("exit", () => {
module.exports = {
TestControl,
TestControlConnector,
slowTest,
get,
exec,
sendRcon,
control,
url,
token,
+1 -24
View File
@@ -1,31 +1,8 @@
const assert = require("assert").strict;
const events = require("events");
const link = require("lib/link");
const { TestControlConnector, TestControl, get, exec, url, token } = require("./index");
const { get, exec, url, token } = require("./index");
class TestControl extends link.Link {
constructor(connector) {
super("control", "master", connector);
link.attachAllMessages(this);
}
async prepareDisconnectRequestHandler(message, request) {
this.connector.setClosing();
return await super.prepareDisconnectRequestHandler(message, request);
}
async debugWsMessageEventHandler() { }
async instanceOutputEventHandler() { }
}
class TestControlConnector extends link.WebSocketClientConnector {
register() {
this.sendHandshake("register_control", { token: this.token, agent: "clusterctl", version: "test" });
}
}
describe("Integration of lib/link", function() {
let control;