mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-16 23:30:41 +09:00
Implement symlinking of shared mods
Add symlinkMods function which takes an instance and a shared mods folder as input and updates or creates the instance mods folder from it by symlinking the mods in the shared mods folder to the instance mods folder. On Windows this uses hard links as the symlinks require elevated privileges.
This commit is contained in:
1 parent
2ff52a1cf2
commit
6a870be233
5 files changed
+135
-26
No files matched your search
@@ -41,6 +41,7 @@ static/pictures/spritesheet.js
|
||||
static/pictures/spritesheet.png
|
||||
static/external
|
||||
saves
|
||||
/test/temp/
|
||||
|
||||
# weird pattern to ignore sharedPlugins except not ignore some core modules
|
||||
!sharedPlugins/
|
||||
|
||||
@@ -13,6 +13,9 @@ Version 2.0.0
|
||||
- Added plugins directory to the views path. This makes it possible for
|
||||
plugins to render their own ejs views or pages in their own folders by
|
||||
using paths of the format "pluginName/path/to/page-or-view".
|
||||
- Replaced the per instance copy of the shared Factorio mods with
|
||||
symlinks. On Windows hard links are used instead due to the
|
||||
privileges requirements of symlinks.
|
||||
|
||||
### Breaking Changes
|
||||
|
||||
@@ -25,6 +28,9 @@ Version 2.0.0
|
||||
- Removed config management from the command line and the server manager.
|
||||
- Moved ejs templates into views folder and changed their extension to
|
||||
.ejs. Breaks playerManager.
|
||||
- Mods are no longer copied from the per instance instanceMods directory.
|
||||
If you need per instance mods you can now place them directly in the mods
|
||||
directory inside the instance folder.
|
||||
|
||||
|
||||
Version 1.2.2
|
||||
|
||||
@@ -10,7 +10,6 @@ const ncp = require('ncp').ncp;
|
||||
const Rcon = require('rcon-client-fork').Rcon;
|
||||
const deepmerge = require("deepmerge");
|
||||
const getMac = require('getmac').getMac;
|
||||
const rmdirSync = require('rmdir-sync');
|
||||
const ioClient = require("socket.io-client");
|
||||
const asTable = require("as-table").configure({delimiter: ' | '});
|
||||
const util = require("util");
|
||||
@@ -326,26 +325,84 @@ async function downloadMod() {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create and update symlinks for shared mods in an instance
|
||||
*
|
||||
* Creates symlinks for .zip and .dat files that are not present in the
|
||||
* instance mods directory but is present in the sharedMods directory,
|
||||
* and removes any symlinks that don't point to a file in the instance
|
||||
* mods directory. If the instance mods directory doesn't exist it will
|
||||
* be created.
|
||||
*
|
||||
* Note that on Windows this creates hard links instead of symbolic
|
||||
* links as the latter requires elevated privileges. This unfortunately
|
||||
* means the removal of mods from the shared mods dir can't be detected.
|
||||
*
|
||||
* @param {Instance} instance - Instance to link mods for
|
||||
* @param {string} sharedMods - Path to folder to link mods from.
|
||||
* @param {object} logger - console like logging interface.
|
||||
*/
|
||||
async function symlinkMods(instance, sharedMods, logger) {
|
||||
await fs.ensureDir(instance.path("mods"));
|
||||
|
||||
// Remove broken symlinks in instance mods.
|
||||
for (let entry of await fs.readdir(instance.path("mods"), { withFileTypes: true })) {
|
||||
if (entry.isSymbolicLink()) {
|
||||
if (!await fs.pathExists(instance.path("mods", entry.name))) {
|
||||
logger.log(`Removing broken symlink ${entry.name}`);
|
||||
await fs.unlink(instance.path("mods", entry.name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Link entries that are in sharedMods but not in instance mods.
|
||||
let instanceModsEntries = new Set(await fs.readdir(instance.path("mods")));
|
||||
for (let entry of await fs.readdir(sharedMods, { withFileTypes: true })) {
|
||||
if (entry.isFile()) {
|
||||
if (['.zip', '.dat'].includes(path.extname(entry.name))) {
|
||||
if (!instanceModsEntries.has(entry.name)) {
|
||||
logger.log(`linking ${entry.name} from ${sharedMods}`);
|
||||
let target = path.join(sharedMods, entry.name);
|
||||
let link = instance.path("mods", entry.name);
|
||||
|
||||
if (process.platform !== "win32") {
|
||||
await fs.symlink(path.relative(path.dirname(link), target), link);
|
||||
|
||||
// On Windows symlinks require elevated privileges, which is
|
||||
// not something we want to have. For this reason the mods
|
||||
// are hard linked instead. This has the drawback of not
|
||||
// being able to identify when mods are removed from the
|
||||
// sharedMods directory, or which mods are linked.
|
||||
} else {
|
||||
await fs.link(target, link);
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
logger.warning(`Warning: ignoring file '${entry.name}' in sharedMods`);
|
||||
}
|
||||
|
||||
} else {
|
||||
logger.warning(`Warning: ignoring non-file '${entry.name}' in sharedMods`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function createInstance(config, args, instance) {
|
||||
// if instance does not exist, create it
|
||||
console.log("Creating instance...");
|
||||
fs.mkdirSync(instance.path());
|
||||
fs.mkdirSync(instance.path("script-output"));
|
||||
fs.mkdirSync(instance.path("mods"));
|
||||
fs.mkdirSync(instance.path("instanceMods"));
|
||||
console.log(`Creating ${instance.path()}`);
|
||||
await fs.ensureDir(instance.path());
|
||||
await fs.ensureDir(instance.path("script-output"));
|
||||
fs.mkdirSync(instance.path("scenarios"));
|
||||
ncp("lib/scenarios", instance.path("scenarios"), err => {
|
||||
if (err) console.error(err)
|
||||
});
|
||||
|
||||
// fs.symlinkSync('../../../sharedMods', instance.path("mods"), 'junction') // This is broken because it can only take a file as first argument, not a folder
|
||||
fs.writeFileSync(instance.path("config.ini"), `[path]\r\n
|
||||
read-data=${ path.resolve(config.factorioDirectory, "data") }\r\n
|
||||
write-data=${ instance.path() }\r\n
|
||||
`);
|
||||
|
||||
// this line is probably not needed anymore but Im not gonna remove it
|
||||
fs.copySync('sharedMods', instance.path("mods"));
|
||||
await symlinkMods(instance, "sharedMods", console);
|
||||
let instconf = {
|
||||
"factorioPort": args.port || process.env.FACTORIOPORT || randomDynamicPort(),
|
||||
"clientPort": args["rcon-port"] || process.env.RCONPORT || randomDynamicPort(),
|
||||
@@ -462,21 +519,9 @@ async function startInstance(config, args, instance) {
|
||||
console.log(`Log rotated as ${logFilename}`);
|
||||
}
|
||||
}catch(e){}
|
||||
// Math.floor(Date.now()/1000)
|
||||
// (new Date).toGMTString()
|
||||
|
||||
// move mods from ./sharedMods to the instances mod directory
|
||||
try{fs.mkdirSync(instance.path("instanceMods"));}catch(e){}
|
||||
try{rmdirSync(instance.path("mods"));}catch(e){}
|
||||
try {
|
||||
// mods directory that will be emptied (deleted) when closing the server to facilitate seperation of instanceMods and sharedMods
|
||||
fs.mkdirSync(instance.path("mods"));
|
||||
} catch(e){}
|
||||
console.log("Clusterio | Moving shared mods from sharedMods/ to instance/mods...");
|
||||
fs.copySync('sharedMods', instance.path("mods"));
|
||||
console.log("Clusterio | Moving instance specific mods from instance/instanceMods to instance/mods...");
|
||||
fs.copySync(instance.path("instanceMods"), instance.path("mods"));
|
||||
|
||||
|
||||
await symlinkMods(instance, "sharedMods", console);
|
||||
|
||||
// Set paths for factorio so it reads and writes from the correct place even if the instance is imported from somewhere else
|
||||
fs.writeFileSync(instance.path("config.ini"), `[path]\r\n
|
||||
read-data=${ path.resolve(config.factorioDirectory, "data") }\r\n
|
||||
@@ -920,6 +965,7 @@ module.exports = {
|
||||
_checkFilename: checkFilename,
|
||||
_randomDynamicPort: randomDynamicPort,
|
||||
_generatePassword: generatePassword,
|
||||
_symlinkMods: symlinkMods,
|
||||
};
|
||||
|
||||
if (module === require.main) {
|
||||
|
||||
@@ -62,7 +62,6 @@
|
||||
"prom-client": "^10.2.3",
|
||||
"rcon-client-fork": "^3.2.0",
|
||||
"request": "^2.83.0",
|
||||
"rmdir-sync": "^1.0.1",
|
||||
"sanitizer": "^0.1.3",
|
||||
"simple-git": "^1.96.0",
|
||||
"single-line-log": "^1.1.2",
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
const assert = require('assert').strict;
|
||||
const path = require('path');
|
||||
const fs = require('fs-extra');
|
||||
|
||||
const client = require("../client");
|
||||
|
||||
@@ -105,4 +106,60 @@ describe("Client testing", function() {
|
||||
assert(/^[a-zA-Z0-9]+$/.test(password), `${password} failed test`);
|
||||
});
|
||||
});
|
||||
|
||||
describe("symlinkMods()", function() {
|
||||
// Remove previous test output
|
||||
let testDir = path.join("test", "temp", "symlink");
|
||||
fs.removeSync(testDir);
|
||||
|
||||
// Add some test mods
|
||||
fs.outputFileSync(path.join(testDir, "shared", "mod_a.zip"), "a");
|
||||
fs.outputFileSync(path.join(testDir, "shared", "mod_b.zip"), "b");
|
||||
fs.outputFileSync(path.join(testDir, "shared", "mod.dat"), "c");
|
||||
let instance = new client._Instance(path.join(testDir, "instance"), "test");
|
||||
fs.outputFileSync(instance.path("mods", "mod_i.zip"), "i");
|
||||
|
||||
let discardingLogger = {
|
||||
warning: function() { },
|
||||
log: function() { },
|
||||
}
|
||||
|
||||
it("should link mods and data files", async function() {
|
||||
await client._symlinkMods(instance, path.join(testDir, "shared"), discardingLogger);
|
||||
|
||||
assert.equal(await fs.readFile(instance.path("mods", "mod_a.zip"), "utf-8"), "a");
|
||||
assert.equal(await fs.readFile(instance.path("mods", "mod_b.zip"), "utf-8"), "b");
|
||||
assert.equal(await fs.readFile(instance.path("mods", "mod.dat"), "utf-8"), "c");
|
||||
assert.equal(await fs.readFile(instance.path("mods", "mod_i.zip"), "utf-8"), "i");
|
||||
});
|
||||
|
||||
it("should ignore directories", async function() {
|
||||
await fs.ensureDir(path.join(testDir, "shared", "dir"));
|
||||
await client._symlinkMods(instance, path.join(testDir, "shared"), discardingLogger);
|
||||
|
||||
assert(!await fs.exists(instance.path("mods", "dir"), "utf-8"), "dir was unxpectedly linked");
|
||||
});
|
||||
|
||||
it("should ignore files", async function() {
|
||||
await fs.outputFile(path.join(testDir, "shared", "file"), "f");
|
||||
await client._symlinkMods(instance, path.join(testDir, "shared"), discardingLogger);
|
||||
|
||||
assert(!await fs.exists(instance.path("mods", "file"), "utf-8"), "dir was unxpectedly linked");
|
||||
});
|
||||
|
||||
it("should unlink removed mods", async function() {
|
||||
// This does not work on Windows
|
||||
if (process.platform == "win32") {
|
||||
this.skip();
|
||||
}
|
||||
|
||||
await fs.unlink(path.join(testDir, "shared", "mod_a.zip"));
|
||||
await client._symlinkMods(instance, path.join(testDir, "shared"), discardingLogger);
|
||||
|
||||
await assert.rejects(fs.lstat(instance.path("mods", "mod_a.zip")), { code: "ENOENT" });
|
||||
assert.equal(await fs.readFile(instance.path("mods", "mod_b.zip"), "utf-8"), "b");
|
||||
assert.equal(await fs.readFile(instance.path("mods", "mod.dat"), "utf-8"), "c");
|
||||
assert.equal(await fs.readFile(instance.path("mods", "mod_i.zip"), "utf-8"), "i");
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user