diff --git a/.env.example b/.env.example index 33c8e4b..915ab3e 100644 --- a/.env.example +++ b/.env.example @@ -16,16 +16,20 @@ SESSION_TTL_HOURS=12 # Profils de lancement SSH. # Chaque serveur dans storage/servers.json reference un profil via startProfile. -# Exemple: "startProfile": "GAME_1" utilise GAME_1_SSH_TARGET, GAME_1_SSH_PORT et GAME_1_START_COMMAND. +# Exemple: "startProfile": "GAME_1" utilise GAME_1_SSH_TARGET, GAME_1_SSH_PORT, +# GAME_1_SSH_KEY_FILE et GAME_1_START_COMMAND. # La cible peut etre un alias SSH, un hostname, une IP, ou user@host. # Exemples: # GAME_1_SSH_TARGET=game-server # GAME_1_SSH_PORT=2222 +# GAME_1_SSH_KEY_FILE=/home/app/.ssh/game-server-key # GAME_1_SSH_TARGET=user@192.0.2.10 GAME_1_SSH_TARGET=game-server-1 GAME_1_SSH_PORT= +GAME_1_SSH_KEY_FILE= GAME_1_START_COMMAND="bash /opt/game-server-1/start.sh" GAME_2_SSH_TARGET=game-server-2 GAME_2_SSH_PORT= +GAME_2_SSH_KEY_FILE= GAME_2_START_COMMAND="bash /opt/game-server-2/start.sh" diff --git a/.forgejo/workflows/deploy.yml b/.forgejo/workflows/deploy.yml index a3e92df..8f4828d 100644 --- a/.forgejo/workflows/deploy.yml +++ b/.forgejo/workflows/deploy.yml @@ -12,4 +12,15 @@ jobs: steps: - name: Deploy on WebApps run: | - sudo /usr/local/bin/deploy-gameservers-control + apt-get update + apt-get install -y openssh-client + + mkdir -p ~/.ssh + cp /keys/forgejo-runner ~/.ssh/id_ed25519 + chmod 600 ~/.ssh/id_ed25519 + + ssh -o StrictHostKeyChecking=no \ + -i ~/.ssh/id_ed25519 \ + -p 2911 \ + shinuwa@192.168.0.100 \ + "sudo /usr/local/bin/deploy-gameservers-control" \ No newline at end of file diff --git a/.gitignore b/.gitignore index e382713..7b5e9da 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,4 @@ node_modules/ .env npm-debug.log* -storage/groups.json -storage/launch-logs.json -storage/server-images.json -storage/server-images/ +storage/ diff --git a/README.md b/README.md index bcbd4b2..86e0454 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ Variables utiles: - `SESSION_TTL_HOURS`: duree des sessions, defaut `12`. - `*_SSH_TARGET`: cible SSH du profil de lancement, alias SSH ou `user@host`. - `*_SSH_PORT`: port SSH optionnel. Vide par defaut. +- `*_SSH_KEY_FILE`: chemin optionnel vers une cle SSH privee. - `*_START_COMMAND`: commande distante executee sur la cible SSH. Pour changer le port, modifier `.env`: @@ -31,6 +32,7 @@ Exemple de lancement distant via SSH: ```env GAME_1_SSH_TARGET=user@192.0.2.10 GAME_1_SSH_PORT=2222 +GAME_1_SSH_KEY_FILE=/home/app/.ssh/game-server-key GAME_1_START_COMMAND="bash /opt/game-server-1/start.sh" ``` diff --git a/src/services/serverLauncher.js b/src/services/serverLauncher.js index 5e38032..787be41 100644 --- a/src/services/serverLauncher.js +++ b/src/services/serverLauncher.js @@ -20,6 +20,7 @@ async function launchServer(server) { const target = process.env[`${profile}_SSH_TARGET`]; const port = process.env[`${profile}_SSH_PORT`]; + const keyFile = process.env[`${profile}_SSH_KEY_FILE`]; const command = process.env[`${profile}_START_COMMAND`]; if (!target || !command) { @@ -28,11 +29,7 @@ async function launchServer(server) { throw error; } - const args = []; - if (port) { - args.push("-p", port); - } - args.push(target, command); + const args = buildSshArgs({ target, port, keyFile, command }); return execFileAsync("ssh", args, { timeout: 60_000, @@ -40,9 +37,21 @@ async function launchServer(server) { }); } +function buildSshArgs({ target, port, keyFile, command }) { + const args = []; + if (port) { + args.push("-p", port); + } + if (keyFile) { + args.push("-i", keyFile); + } + args.push(target, command); + return args; +} + function getStartProfile(server) { if (server.startProfile) return server.startProfile; return ""; } -export { getStartProfile, launchServer }; +export { buildSshArgs, getStartProfile, launchServer }; diff --git a/test/serverLauncher.test.js b/test/serverLauncher.test.js new file mode 100644 index 0000000..585732f --- /dev/null +++ b/test/serverLauncher.test.js @@ -0,0 +1,34 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { buildSshArgs } from "../src/services/serverLauncher.js"; + +test("builds ssh args with optional port and key file", () => { + assert.deepEqual( + buildSshArgs({ + target: "user@192.0.2.10", + port: "2222", + keyFile: "/home/app/.ssh/game-key", + command: "bash /opt/game/start.sh", + }), + [ + "-p", + "2222", + "-i", + "/home/app/.ssh/game-key", + "user@192.0.2.10", + "bash /opt/game/start.sh", + ] + ); +}); + +test("builds ssh args without optional port and key file", () => { + assert.deepEqual( + buildSshArgs({ + target: "game-server", + port: "", + keyFile: "", + command: "bash /opt/game/start.sh", + }), + ["game-server", "bash /opt/game/start.sh"] + ); +});