Files
Supervisor/server/api/disk.get.ts
kevin 13457ceb5a
All checks were successful
Release / release (push) Successful in 26s
fix: readme
2026-03-17 14:26:49 +01:00

85 lines
2.1 KiB
TypeScript

import { exec } from "child_process"
type DiskSource = {
key: "remote" | "local"
label: string
}
const diskSources: DiskSource[] = [
{
key: "remote",
label: "Serveur distant"
},
{
key: "local",
label: "Machine locale"
}
]
function getDefaultCommand(source: DiskSource) {
const localScriptDir = process.env.DISK_LOCAL_SCRIPT_DIR || "/home/malio/Malio-ops/CheckStorage"
const remoteHost = process.env.DISK_REMOTE_HOST || "malio-b"
const remoteScriptDir = process.env.DISK_REMOTE_SCRIPT_DIR || "/home/malio-b/Malio-ops/CheckStorage"
if (source.key === "local") {
return `cd ${localScriptDir} && bash check-storage.sh`
}
return `ssh ${remoteHost} "cd ${remoteScriptDir} && ./check-storage.sh"`
}
function getEnvCommand(source: DiskSource) {
const envKey = `DISK_COMMAND_${source.key.toUpperCase()}`
const legacyEnvKey =
source.key === "remote" ? "DISK_REMOTE_COMMAND" : source.key === "local" ? "DISK_LOCAL_COMMAND" : ""
return (
process.env[envKey] ||
(legacyEnvKey ? process.env[legacyEnvKey] : undefined) ||
getDefaultCommand(source)
)
}
function runShellCommand(command: string): Promise<string> {
return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (error) {
reject(stderr || error.message)
return
}
resolve(stdout)
})
})
}
export default defineEventHandler(async () => {
const results = await Promise.all(
diskSources.map(async (source) => {
try {
const envCommand = getEnvCommand(source)
if (!envCommand) {
throw new Error(`Commande disque manquante pour ${source.key}`)
}
const output = await runShellCommand(envCommand)
return {
key: source.key,
label: source.label,
ok: true,
output
}
} catch (error) {
console.error(`Erreur disk source ${source.key}:`, error)
return {
key: source.key,
label: source.label,
ok: false,
output: "Erreur lors de l'opération"
}
}
})
)
return { results }
})