81 lines
1.8 KiB
TypeScript
81 lines
1.8 KiB
TypeScript
import { execFile } from "node:child_process"
|
|
|
|
type DiskSource = {
|
|
key: "remote" | "local"
|
|
label: string
|
|
}
|
|
|
|
type CommandSpec = {
|
|
command: string
|
|
args: string[]
|
|
cwd?: string
|
|
}
|
|
|
|
const diskSources: DiskSource[] = [
|
|
{
|
|
key: "remote",
|
|
label: "Serveur distant"
|
|
},
|
|
{
|
|
key: "local",
|
|
label: "Machine locale"
|
|
}
|
|
]
|
|
|
|
function getCommand(source: DiskSource): CommandSpec {
|
|
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 {
|
|
command: "bash",
|
|
args: ["check-storage.sh"],
|
|
cwd: localScriptDir
|
|
}
|
|
}
|
|
|
|
return {
|
|
command: "ssh",
|
|
args: [remoteHost, `cd ${remoteScriptDir} && ./check-storage.sh`]
|
|
}
|
|
}
|
|
|
|
function runCommand({ command, args, cwd }: CommandSpec): Promise<string> {
|
|
return new Promise((resolve, reject) => {
|
|
execFile(command, args, { cwd }, (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 output = await runCommand(getCommand(source))
|
|
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 }
|
|
})
|