changed shell execution to execa
This commit is contained in:
parent
81ad1f7622
commit
a3db738bfe
1 changed files with 72 additions and 12 deletions
|
@ -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<number | null> {
|
||||
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<number | null> {
|
||||
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(<number>maxMemory) ? null : maxMemory;
|
||||
} catch (error) {
|
||||
console.error('Error getting max memory:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async function getAutostartValue(vmName: string): Promise<string | null> {
|
||||
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<string | null> {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue