added caching for vms

This commit is contained in:
WeeXnes 2025-02-27 03:40:37 +01:00
parent 286685d157
commit 81ad1f7622
2 changed files with 39 additions and 22 deletions

View file

@ -1,6 +1,13 @@
import {reactive} from "vue"; import { reactive } from "vue";
import type { VM } from "~/types/VM"
export const jwt_globals = reactive({ export const jwt_globals = reactive({
secret: "", secret: "",
}); });
export const vm_cache = reactive({
vms: [] as VM[],
})

View file

@ -1,19 +1,27 @@
import {execSync} from 'child_process';
import { execSync } from 'child_process';
import {VM} from "~/types/VM";
import {settings} from "~/panel.config"; import {settings} from "~/panel.config";
import {vm_cache} from "~/core/globals";
import Logger from "~/core/logger";
export default defineEventHandler(() => { export default defineEventHandler(() => {
let vmNames = ["Gameserver", "Ubuntu_VM1"]
const virtualMachines: VM[] = []; 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();
vm.state = stateValue === "running" ? 'on' : 'off';
})
}else{
Logger.info("VMs havent been Loaded yet, loading now...")
settings.qemu_vms.forEach(vm => { settings.qemu_vms.forEach(vm => {
Logger.info("Loading " + vm.name)
const vCpuCount = parseInt(execSync(`LANG=C virsh dominfo ${vm.name} | grep 'CPU(s)' | awk '{print $2}'`).toString().trim()); 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 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 autostartValue = execSync(`LANG=C virsh dominfo ${vm.name} | grep 'Autostart' | awk '{print $2}'`).toString().trim();
const autostart = autostartValue === "enable"; const autostart = autostartValue === "enable";
const stateValue = execSync(`LANG=C virsh dominfo ${vm.name} | grep 'State' | awk '{print $2, $3}'`).toString().trim(); const stateValue = execSync(`LANG=C virsh dominfo ${vm.name} | grep 'State' | awk '{print $2, $3}'`).toString().trim();
const state: 'on' | 'off' = stateValue === "running" ? 'on' : 'off'; const state: 'on' | 'off' = stateValue === "running" ? 'on' : 'off';
virtualMachines.push({ vm_cache.vms.push({
name: vm.name, name: vm.name,
os: vm.os, os: vm.os,
vCpuCount: vCpuCount, vCpuCount: vCpuCount,
@ -22,5 +30,7 @@ export default defineEventHandler(() => {
state: state state: state
}); });
}); });
return virtualMachines; }
return vm_cache.vms;
}); });