fix: use env only
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -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<string> {
|
||||
function runCommand(command: string): Promise<string> {
|
||||
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,
|
||||
|
||||
@@ -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<string> {
|
||||
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<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
exec(command, (error, stdout, stderr) => {
|
||||
@@ -42,12 +44,14 @@ function runShellCommand(command: string): Promise<string> {
|
||||
|
||||
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,
|
||||
|
||||
@@ -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"
|
||||
]
|
||||
}
|
||||
]
|
||||
@@ -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"
|
||||
]
|
||||
}
|
||||
]
|
||||
31
server/utils/backup-scripts.ts
Normal file
31
server/utils/backup-scripts.ts
Normal file
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user