diff --git a/server/api/backup-script.get.ts b/server/api/backup-script.get.ts index 53ba375..bba231c 100644 --- a/server/api/backup-script.get.ts +++ b/server/api/backup-script.get.ts @@ -1,16 +1,8 @@ -import scripts from "../config/backup-script.json" - -type BackupScript = { - key: string - label: string - icon?: string - downloadFolders?: string[] - command: string -} +import { backupScripts } from "../utils/backup-scripts" export default defineEventHandler(() => { return { - scripts: (scripts as BackupScript[]).map(({ key, label, icon, downloadFolders }) => ({ + scripts: backupScripts.map(({ key, label, icon, downloadFolders }) => ({ key, label, icon: icon || "mdi:play-circle-outline", diff --git a/server/api/backup-script.post.ts b/server/api/backup-script.post.ts index a018c74..f55edf1 100644 --- a/server/api/backup-script.post.ts +++ b/server/api/backup-script.post.ts @@ -1,17 +1,9 @@ -import { execFile } from "node:child_process" -import scripts from "../config/backup-script.json" +import { exec } from "node:child_process" +import { backupScripts, getBackupScriptCommand } from "../utils/backup-scripts" -type BackupScript = { - key: string - label: string - downloadFolders?: string[] - command: string - args?: string[] -} - -function runCommand(command: string, args: string[] = []): Promise { +function runCommand(command: string): Promise { return new Promise((resolve, reject) => { - execFile(command, args, { timeout: 10 * 60 * 1000 }, (error, stdout, stderr) => { + exec(command, { timeout: 10 * 60 * 1000 }, (error, stdout, stderr) => { if (error) { reject(stderr || error.message) return @@ -32,7 +24,7 @@ export default defineEventHandler(async (event) => { }) } - const script = (scripts as BackupScript[]).find((item) => item.key === key) + const script = backupScripts.find((item) => item.key === key) if (!script) { throw createError({ statusCode: 404, @@ -41,7 +33,15 @@ export default defineEventHandler(async (event) => { } try { - const output = await runCommand(script.command, script.args || []) + const command = getBackupScriptCommand(script.key) + if (!command) { + throw createError({ + statusCode: 500, + statusMessage: "Commande de script manquante" + }) + } + + const output = await runCommand(command) return { ok: true, key: script.key, diff --git a/server/api/disk.get.ts b/server/api/disk.get.ts index ff16fb6..dc11a36 100644 --- a/server/api/disk.get.ts +++ b/server/api/disk.get.ts @@ -1,5 +1,4 @@ -import { exec, execFile } from "child_process" -import diskSources from "../config/disk-commands.json" +import { exec } from "child_process" type DiskSource = { key: string @@ -8,6 +7,21 @@ type DiskSource = { args?: string[] } +const diskSources: DiskSource[] = [ + { + key: "remote", + label: "Serveur distant", + command: "ssh", + args: [] + }, + { + key: "local", + label: "Machine locale", + command: "bash", + args: [] + } +] + function getEnvCommand(source: DiskSource) { const envKey = `DISK_COMMAND_${source.key.toUpperCase()}` const legacyEnvKey = @@ -16,18 +30,6 @@ function getEnvCommand(source: DiskSource) { return process.env[envKey] || (legacyEnvKey ? process.env[legacyEnvKey] : undefined) || null } -function runCommand(command: string, args: string[] = []): Promise { - return new Promise((resolve, reject) => { - execFile(command, args, (error, stdout, stderr) => { - if (error) { - reject(stderr || error.message) - return - } - resolve(stdout) - }) - }) -} - function runShellCommand(command: string): Promise { return new Promise((resolve, reject) => { exec(command, (error, stdout, stderr) => { @@ -42,12 +44,14 @@ function runShellCommand(command: string): Promise { export default defineEventHandler(async () => { const results = await Promise.all( - (diskSources as DiskSource[]).map(async (source) => { + diskSources.map(async (source) => { try { const envCommand = getEnvCommand(source) - const output = envCommand - ? await runShellCommand(envCommand) - : await runCommand(source.command, source.args || []) + if (!envCommand) { + throw new Error(`Commande disque manquante pour ${source.key}`) + } + + const output = await runShellCommand(envCommand) return { key: source.key, label: source.label, diff --git a/server/config/backup-script.json b/server/config/backup-script.json deleted file mode 100644 index 6d6a054..0000000 --- a/server/config/backup-script.json +++ /dev/null @@ -1,34 +0,0 @@ -[ - { - "key": "backup-bdd-recette", - "label": "Backup BDD recette", - "icon": "mdi:database-export", - "downloadFolders": ["ferme", "inventory", "sirh", "user"], - "command": "ssh", - "args": [ - "ferme", - "cd /home/malio/Malio-ops/RecetteScripts && bash backup-bdd-recette.sh" - ] - }, - { - "key": "check-statut-recette", - "label": "Check statut recette", - "icon": "mdi:server-network", - "command": "ssh", - "args": [ - "ferme", - "cd /home/malio/Malio-ops/RecetteScripts && bash check-statut-recette.sh" - ] - }, - { - "key": "backup-vaultwarden", - "label": "Backup vaultwarden", - "icon": "mdi:data", - "downloadFolders": ["bitwarden"], - "command": "ssh", - "args": [ - "bitwarden", - "cd /home/matt/vaultwarden/Malio-ops/BackupVaultWarden && bash backup-vaultwarden.sh" - ] - } -] \ No newline at end of file diff --git a/server/config/disk-commands.json b/server/config/disk-commands.json deleted file mode 100644 index 3c2e664..0000000 --- a/server/config/disk-commands.json +++ /dev/null @@ -1,19 +0,0 @@ -[ - { - "key": "remote", - "label": "Serveur distant", - "command": "ssh", - "args": [ - "malio-b", - "cd /home/malio-b/Malio-ops/CheckStorage && bash check-storage.sh" - ] - }, - { - "key": "local", - "label": "Machine locale", - "command": "bash", - "args": [ - "/home/kevin/check_storage.sh" - ] - } -] \ No newline at end of file diff --git a/server/utils/backup-scripts.ts b/server/utils/backup-scripts.ts new file mode 100644 index 0000000..12b026d --- /dev/null +++ b/server/utils/backup-scripts.ts @@ -0,0 +1,31 @@ +export type BackupScript = { + key: string + label: string + icon?: string + downloadFolders?: string[] +} + +export const backupScripts: BackupScript[] = [ + { + key: "backup-bdd-recette", + label: "Backup BDD recette", + icon: "mdi:database-export", + downloadFolders: ["ferme", "inventory", "sirh", "user"] + }, + { + key: "check-statut-recette", + label: "Check statut recette", + icon: "mdi:server-network" + }, + { + key: "backup-vaultwarden", + label: "Backup vaultwarden", + icon: "mdi:data", + downloadFolders: ["bitwarden"] + } +] + +export function getBackupScriptCommand(key: string) { + const envKey = `BACKUP_SCRIPT_COMMAND_${key.toUpperCase().replace(/-/g, "_")}` + return process.env[envKey] || null +}