diff --git a/clusterctl.js b/clusterctl.js index 4680f6e8..f7dbee34 100644 --- a/clusterctl.js +++ b/clusterctl.js @@ -223,6 +223,26 @@ commands.push(new Command({ }, })); +commands.push(new Command({ + definition: ["set-instance-config-prop", "Set property of field in instance config", (yargs) => { + yargs.option({ + "instance": { describe: "Instance to set config on", nargs: 1, type: "string", demandOption: true }, + "field": { describe: "Field to set", nargs: 1, type: "string", demandOption: true }, + "prop": { describe: "Property to set", nargs: 1, type: "string", demandOption: true }, + "value": { describe: "JSON parsed value to set", nargs: 1, type: "string", demandOption: true }, + }); + }], + handler: async function(args, control) { + let instanceId = await resolveInstance(control, args.instance); + await link.messages.setInstanceConfigProp.send(control, { + instance_id: instanceId, + field: args.field, + prop: args.prop, + value: JSON.parse(args.value), + }); + }, +})); + commands.push(new Command({ definition: ['assign-instance', "Assign instance to a slave", (yargs) => { yargs.options({ diff --git a/lib/link/messages.js b/lib/link/messages.js index 408ae4b3..effda751 100644 --- a/lib/link/messages.js +++ b/lib/link/messages.js @@ -312,6 +312,17 @@ messages.setInstanceConfigField = new Request({ }, }); +messages.setInstanceConfigProp = new Request({ + type: "set_instance_config_prop", + links: ["control-master"], + requestProperties: { + "instance_id": { type: "integer" }, + "field": { type: "string" }, + "prop": { type: "string" }, + "value": {}, + }, +}); + messages.assignInstanceCommand = new Request({ type: 'assign_instance_command', links: ['control-master'], diff --git a/master.js b/master.js index 196b7c77..05a7340a 100644 --- a/master.js +++ b/master.js @@ -529,6 +529,19 @@ class ControlConnection extends BaseConnection { } } + async updateInstanceConfig(instance) { + let slaveId = instance.config.get("instance.assigned_slave"); + if (slaveId) { + let connection = slaveConnections.get(slaveId); + if (connection) { + await link.messages.assignInstance.send(connection, { + instance_id: instance.config.get("instance.id"), + serialized_config: instance.config.serialize(), + }); + } + } + } + async setInstanceConfigFieldRequestHandler(message) { let instance = db.instances.get(message.data.instance_id); if (!instance) { @@ -545,18 +558,18 @@ class ControlConnection extends BaseConnection { } instance.config.set(message.data.field, message.data.value); + await this.updateInstanceConfig(instance); + } - // Push updated config to slave - let slaveId = instance.config.get('instance.assigned_slave'); - if (slaveId) { - let connection = slaveConnections.get(slaveId); - if (connection) { - await link.messages.assignInstance.send(connection, { - instance_id: instance.config.get("instance.id"), - serialized_config: instance.config.serialize(), - }); - } + async setInstanceConfigPropRequestHandler(message) { + let instance = db.instances.get(message.data.instance_id); + if (!instance) { + throw new errors.RequestError(`Instance with ID ${message.data.instance_id} does not exist`); } + + let {field, prop, value} = message.data; + instance.config.setProp(field, prop, value); + await this.updateInstanceConfig(instance); } async assignInstanceCommandRequestHandler(message, request) { diff --git a/slave.js b/slave.js index 8932aacb..8b05fa4f 100644 --- a/slave.js +++ b/slave.js @@ -17,6 +17,7 @@ const events = require("events"); const pidusage = require("pidusage"); const setBlocking = require("set-blocking"); const phin = require("phin"); +const util = require("util"); const version = require("./package").version; // internal libraries @@ -55,6 +56,49 @@ const instanceFactorioAutosaveSize = new prometheus.Gauge( { labels: ["instance_id"] } ); +function applyAsConfig(name) { + return async function action(server, value) { + if (name === "tags" && value instanceof Array) { + // Replace spaces with non-break spaces and delimit by spaces. + // This does change the defined tags, but there doesn't seem to + // be a way to include a space into a tag from the console. + value = value.map(tag => tag.replace(/ /g, "\u00a0")).join(" "); + } + try { + await server.sendRcon(`/config set ${name} ${value}`); + } catch (err) { + console.error(`Error applying server setting ${name}`, err); + } + } +} + +const serverSettingsActions = { + "afk_autokick_interval": applyAsConfig("afk-auto-kick"), + "allow_commands": applyAsConfig("allow-commands"), + "autosave_interval": applyAsConfig("autosave-interval"), + "autosave_only_on_server": applyAsConfig("autosave-only-on-server"), + "description": applyAsConfig("description"), + "ignore_player_limit_for_returning_players": applyAsConfig("ignore-player-limit-for-returning-players"), + "max_players": applyAsConfig("max-players"), + "max_upload_slots": applyAsConfig("max-upload-slots"), + "max_upload_in_kilobytes_per_second": applyAsConfig("max-upload-speed"), + "name": applyAsConfig("name"), + "only_admins_can_pause_the_game": applyAsConfig("only-admins-can-pause"), + "game_password": applyAsConfig("password"), + "require_user_verification": applyAsConfig("require-user-verification"), + "tags": applyAsConfig("tags"), + "visibility": async function action(server, value) { + for (let scope of ["lan", "public", "steam"]) { + try { + let enabled = Boolean(value[scope]); + await server.sendRcon(`/config set visibility-${scope} ${enabled}`); + } catch (err) { + console.error(`Error applying visibility ${scope}`, err); + } + } + }, +}; + /** * Keeps track of the runtime parameters of an instance */ @@ -69,7 +113,13 @@ class Instance extends link.Link{ this.config = instanceConfig; this._configFieldChanged = (group, field, prev) => { - plugin.invokeHook(this.plugins, "onInstanceConfigFieldChanged", group, field, prev); + let hook = () => plugin.invokeHook(this.plugins, "onInstanceConfigFieldChanged", group, field, prev); + + if (group.name === "factorio" && field === "settings") { + this.updateFactorioSettings(group.get(field), prev).finally(hook); + } else { + hook(); + } }; this.config.on("fieldChanged", this._configFieldChanged); @@ -175,15 +225,16 @@ class Instance extends link.Link{ } /** - * Write the server-settings.json file + * Resolve the effective Factorio server settings * - * Generate the server-settings.json file from the example file in the - * data directory and override any settings configured in the instance's - * factorio_settings config entry. + * Use the example settings as the basis and override it with all the + * entries from the given settings object. + * + * @param {Object} overrides - Server settings to override. + * @returns server example settings with the given settings applied over it. */ - async writeServerSettings() { + async resolveServerSettings(overrides) { let serverSettings = await this.server.exampleSettings(); - let overrides = this.config.get('factorio.settings'); for (let [key, value] of Object.entries(overrides)) { if (!Object.hasOwnProperty.call(serverSettings, key)) { @@ -192,6 +243,18 @@ class Instance extends link.Link{ serverSettings[key] = value; } + return serverSettings; + } + + /** + * Write the server-settings.json file + * + * Generate the server-settings.json file from the example file in the + * data directory and override any settings configured in the instance's + * factorio_settings config entry. + */ + async writeServerSettings() { + let serverSettings = await this.resolveServerSettings(this.config.get("factorio.settings")); await fs.writeFile( this.server.writePath("server-settings.json"), JSON.stringify(serverSettings, null, 4) @@ -355,6 +418,17 @@ class Instance extends link.Link{ await this.server.sendRcon(`/sc clusterio_private.update_instance(${id}, "${name}")`, true); } + async updateFactorioSettings(current, previous) { + current = await this.resolveServerSettings(current); + previous = await this.resolveServerSettings(previous); + + for (let [key, action] of Object.entries(serverSettingsActions)) { + if (current[key] !== undefined && !util.isDeepStrictEqual(current[key], previous[key])) { + await action(this.server, current[key]); + } + } + } + /** * Stop the instance */ @@ -497,13 +571,15 @@ class Instance extends link.Link{ * Searches for instances in the provided directory * * Looks through all sub-dirs of the provided directory for valid - * instance definitions and returns a mapping of instance id to - * instance config objects. + * instance definitions and updates the mapping of instance id to + * instanceInfo objects. * - * @returns {Map} mapping between instance + * @param {Map} instanceInfos - + * mapping between instance id and information about this instance. + * @param {string} instancesDir - Directory containing instances + * @param logger - console like logging interface. */ -async function discoverInstances(instancesDir, logger) { - let instanceInfos = new Map(); +async function discoverInstances(instanceInfos, instancesDir, logger) { for (let entry of await fs.readdir(instancesDir, { withFileTypes: true })) { if (entry.isDirectory()) { let instanceConfig = new config.InstanceConfig(); @@ -521,6 +597,11 @@ async function discoverInstances(instancesDir, logger) { continue; } + // Ignore instances we have already discovered before. + if (instanceInfos.has(instanceConfig.get("instance.id"))) { + continue; + } + let instancePath = path.join(instancesDir, entry.name); logger.log(`found instance ${instanceConfig.get("instance.name")} in ${instancePath}`); instanceInfos.set(instanceConfig.get("instance.id"), { @@ -529,8 +610,6 @@ async function discoverInstances(instancesDir, logger) { }); } } - - return instanceInfos; } class InstanceConnection extends link.Link { @@ -891,7 +970,7 @@ class Slave extends link.Link { * the slave and master server with the new list of instances. */ async updateInstances() { - this.instanceInfos = await discoverInstances(this.config.get("slave.instances_directory"), console); + await discoverInstances(this.instanceInfos, this.config.get("slave.instances_directory"), console); let list = []; for (let [instanceId, instanceInfo] of this.instanceInfos) { let instanceConnection = this.instanceConnections.get(instanceId); diff --git a/test/integration/clusterio.js b/test/integration/clusterio.js index f48353b4..6b507dcb 100644 --- a/test/integration/clusterio.js +++ b/test/integration/clusterio.js @@ -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); diff --git a/test/integration/index.js b/test/integration/index.js index 49546554..05bb92f5 100644 --- a/test/integration/index.js +++ b/test/integration/index.js @@ -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, diff --git a/test/integration/link.js b/test/integration/link.js index a5f2b95a..ad9f2056 100644 --- a/test/integration/link.js +++ b/test/integration/link.js @@ -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; diff --git a/test/slave.js b/test/slave.js index d9309753..1ce8ed1c 100644 --- a/test/slave.js +++ b/test/slave.js @@ -157,7 +157,8 @@ describe("Slave testing", function() { it("should discover test instance", async function() { let logger = { log: () => {}, error: () => {} }; let instancePath = path.join("test", "file", "instances"); - let instanceInfos = await slave._discoverInstances(instancePath, logger); + let instanceInfos = new Map(); + await slave._discoverInstances(instanceInfos, instancePath, logger); let referenceConfig = new config.InstanceConfig(); await referenceConfig.init();