From a3db738bfebdba47505c250b8498fe460c17b5d7 Mon Sep 17 00:00:00 2001 From: WeeXnes Date: Thu, 27 Feb 2025 07:34:35 +0100 Subject: [PATCH] changed shell execution to execa --- server/api/getVMs.ts | 84 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 72 insertions(+), 12 deletions(-) diff --git a/server/api/getVMs.ts b/server/api/getVMs.ts index c5161cf..98822ac 100644 --- a/server/api/getVMs.ts +++ b/server/api/getVMs.ts @@ -1,36 +1,96 @@ import {execSync} from 'child_process'; +import {execa} from 'execa' import {settings} from "~/panel.config"; import {vm_cache} from "~/core/globals"; import Logger from "~/core/logger"; -export default defineEventHandler(() => { +export default defineEventHandler(async () => { if(vm_cache.vms.length > 0){ Logger.info("VMs are cached, refreshing vm states...") - vm_cache.vms.forEach(vm=>{ - const stateValue = execSync(`LANG=C virsh dominfo ${vm.name} | grep 'State' | awk '{print $2, $3}'`).toString().trim(); + for (const vm of vm_cache.vms) { + const stateValue = await getStateValue(vm.name) vm.state = stateValue === "running" ? 'on' : 'off'; - }) + } }else{ Logger.info("VMs havent been Loaded yet, loading now...") - settings.qemu_vms.forEach(vm => { + for (const vm of settings.qemu_vms) { Logger.info("Loading " + vm.name) - const vCpuCount = parseInt(execSync(`LANG=C virsh dominfo ${vm.name} | grep 'CPU(s)' | awk '{print $2}'`).toString().trim()); - const maxMemory = parseInt(execSync(`LANG=C virsh dominfo ${vm.name} | grep 'Max memory' | awk '{print $3}'`).toString().trim()) / 1024; - const autostartValue = execSync(`LANG=C virsh dominfo ${vm.name} | grep 'Autostart' | awk '{print $2}'`).toString().trim(); + const vCpuCount = await getVcpuCount(vm.name); + const maxMemory = await getMaxMemory(vm.name); + const autostartValue = await getAutostartValue(vm.name); const autostart = autostartValue === "enable"; - const stateValue = execSync(`LANG=C virsh dominfo ${vm.name} | grep 'State' | awk '{print $2, $3}'`).toString().trim(); + const stateValue = await getStateValue(vm.name); const state: 'on' | 'off' = stateValue === "running" ? 'on' : 'off'; vm_cache.vms.push({ name: vm.name, os: vm.os, - vCpuCount: vCpuCount, - maxMemory: maxMemory, + vCpuCount: vCpuCount || 0, + maxMemory: maxMemory || 0, autostart: autostart, state: state }); - }); + } } return vm_cache.vms; }); + + +async function getVcpuCount(vmName: string): Promise { + try { + const { stdout } = await execa('virsh', ['dominfo', vmName], { + env: { LANG: 'C' } + }); + const vCpuCount = parseInt(stdout.split('\n').find(line => line.includes('CPU(s)'))?.split(':')[1]?.trim() || ''); + return isNaN(vCpuCount) ? null : vCpuCount; + } catch (error) { + console.error('Error getting vCPU count:', error); + return null; + } +} + +async function getMaxMemory(vmName: string): Promise { + try { + const { stdout } = await execa('virsh', ['dominfo', vmName], { + env: { LANG: 'C' } + }); + console.log(stdout) + const maxMemoryLine = stdout.split('\n').find(line => line.includes('Max memory')); + const maxMemory = maxMemoryLine ? parseInt(maxMemoryLine.split(':')[1].trim()) / 1024 : null; + return isNaN(maxMemory) ? null : maxMemory; + } catch (error) { + console.error('Error getting max memory:', error); + return null; + } +} + + +async function getAutostartValue(vmName: string): Promise { + try { + const { stdout } = await execa('virsh', ['dominfo', vmName], { + env: { LANG: 'C' } // Set LANG to C to ensure consistent output formatting + }); + + const autostartLine = stdout.split('\n').find(line => line.includes('Autostart')); + return autostartLine ? autostartLine.split(':')[1].trim() : null; + } catch (error) { + console.error('Error getting autostart value:', error); + return null; + } +} + + +async function getStateValue(vmName: string): Promise { + try { + const { stdout } = await execa('virsh', ['dominfo', vmName], { + env: { LANG: 'C' } + }); + const stateLine = stdout.split('\n').find(line => line.includes('State')); + const stateValue = stateLine ? stateLine.split(':')[1]?.trim() : null; + return stateValue; + } catch (error) { + console.error('Error getting state value:', error); + return null; + } +}