fix: t-001 a t-005 correctif

This commit is contained in:
2026-03-17 15:33:36 +01:00
parent 13457ceb5a
commit bdb65a09ff
6 changed files with 46 additions and 29 deletions

View File

@@ -1,10 +1,16 @@
import { exec } from "child_process"
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",
@@ -16,33 +22,28 @@ const diskSources: DiskSource[] = [
}
]
function getDefaultCommand(source: DiskSource) {
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 `cd ${localScriptDir} && bash check-storage.sh`
return {
command: "bash",
args: ["check-storage.sh"],
cwd: localScriptDir
}
}
return `ssh ${remoteHost} "cd ${remoteScriptDir} && ./check-storage.sh"`
return {
command: "ssh",
args: [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> {
function runCommand({ command, args, cwd }: CommandSpec): Promise<string> {
return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
execFile(command, args, { cwd }, (error, stdout, stderr) => {
if (error) {
reject(stderr || error.message)
return
@@ -56,12 +57,7 @@ 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)
const output = await runCommand(getCommand(source))
return {
key: source.key,
label: source.label,