Copy plugins on server startup

- Copying everything as we can't copy while the server is running, so we need
to either know which plugins changed or just copy it all (since it needs to
be copied to both places)
This commit is contained in:
Norbi Peti 2022-07-04 00:55:39 +02:00
parent 3d81bd3700
commit d584d7540e
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
5 changed files with 32 additions and 10 deletions

View file

@ -4,4 +4,4 @@ COPY .docker/server/start.sh ./
WORKDIR /mcserver
ENTRYPOINT ["sh", "/start.sh"]
ENTRYPOINT ["bash", "/start.sh"]

View file

@ -1,14 +1,20 @@
import fetch from 'node-fetch';
import { promises } from 'fs';
import { ChildProcess, exec, ExecException } from 'child_process';
import { exec, ExecException } from 'child_process';
import * as chokidar from 'chokidar';
import debounce from 'debounce';
const running: {
process: ChildProcess,
watch: chokidar.FSWatcher,
server1: boolean,
serverName: () => string,
prevServerName: () => string
} = {process: null, server1: false, serverName: () => 'server' + (running.server1 ? '1' : '2'), prevServerName: () => 'server' + (running.server1 ? '2' : '1')};
} = {
watch: null,
server1: false,
serverName: () => 'server' + (running.server1 ? '1' : '2'),
prevServerName: () => 'server' + (running.server1 ? '2' : '1')
};
async function main() {
console.log("Checking for server updates...", "MC", process.env.MC_VERSION);
@ -28,24 +34,28 @@ async function main() {
console.log("Build downloaded", lastBuild.downloads.application.name);
}
}
await startNextServer();
chokidar.watch(`/mcserver/${running.serverName()}/plugins/*.jar`).on('change', async () => { // TODO: Separate plugins dir
console.log("Plugin changes detected, restarting");
const listener = async (changedPath) => {
console.log(`Plugin change detected at ${changedPath}, switching to ${running.prevServerName()}`);
await startNextServer();
});
};
running.watch = chokidar.watch(`/mcserver/plugins/*.jar`).on('change', debounce(listener, 500));
await startNextServer();
}
async function startNextServer() {
running.server1 = !running.server1;
console.log("Copying plugin files to", running.serverName());
await promises.cp('/mcserver/plugins', `/mcserver/${running.serverName()}/plugins`, {recursive: true}); // TODO: Don't run servers as root
// TODO: Copy config files back to main plugins dir
console.log("Starting server", running.server1 ? 'one' : 'two');
exec('docker compose -f /docker-compose.server.yml up -d '+running.serverName(), loggingCallback);
exec('docker compose -f /docker-compose.server.yml up -d ' + running.serverName(), loggingCallback);
await waitForStartup();
await stopPrevServer();
}
async function stopPrevServer() {
console.log("Stopping previous server");
exec('docker compose -f /docker-compose.server.yml stop '+running.prevServerName(), loggingCallback);
exec('docker compose -f /docker-compose.server.yml stop ' + running.prevServerName(), loggingCallback);
}
function waitForStartup() {

11
src/package-lock.json generated
View file

@ -9,6 +9,7 @@
"version": "1.0.0",
"dependencies": {
"chokidar": "^3.5.3",
"debounce": "^1.2.1",
"node-fetch": "^3.2.6",
"read-last-lines": "^1.8.0"
},
@ -124,6 +125,11 @@
"node": ">= 12"
}
},
"node_modules/debounce": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/debounce/-/debounce-1.2.1.tgz",
"integrity": "sha512-XRRe6Glud4rd/ZGQfiV1ruXSfbvfJedlV9Y6zOlP+2K04vBYiJEte6stfFkCP03aMnY5tsipamumUjL14fofug=="
},
"node_modules/duplexer": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/duplexer/-/duplexer-0.1.2.tgz",
@ -653,6 +659,11 @@
"resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.0.tgz",
"integrity": "sha512-Vr3mLBA8qWmcuschSLAOogKgQ/Jwxulv3RNE4FXnYWRGujzrRWQI4m12fQqRkwX06C0KanhLr4hK+GydchZsaA=="
},
"debounce": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/debounce/-/debounce-1.2.1.tgz",
"integrity": "sha512-XRRe6Glud4rd/ZGQfiV1ruXSfbvfJedlV9Y6zOlP+2K04vBYiJEte6stfFkCP03aMnY5tsipamumUjL14fofug=="
},
"duplexer": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/duplexer/-/duplexer-0.1.2.tgz",

View file

@ -27,6 +27,7 @@
},
"dependencies": {
"chokidar": "^3.5.3",
"debounce": "^1.2.1",
"node-fetch": "^3.2.6",
"read-last-lines": "^1.8.0"
}