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,26 +1,36 @@
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){
settings.qemu_vms.forEach(vm => { Logger.info("VMs are cached, refreshing vm states...")
const vCpuCount = parseInt(execSync(`LANG=C virsh dominfo ${vm.name} | grep 'CPU(s)' | awk '{print $2}'`).toString().trim()); vm_cache.vms.forEach(vm=>{
const maxMemory = parseInt(execSync(`LANG=C virsh dominfo ${vm.name} | grep 'Max memory' | awk '{print $3}'`).toString().trim()) / 1024; const stateValue = execSync(`LANG=C virsh dominfo ${vm.name} | grep 'State' | awk '{print $2, $3}'`).toString().trim();
const autostartValue = execSync(`LANG=C virsh dominfo ${vm.name} | grep 'Autostart' | awk '{print $2}'`).toString().trim(); vm.state = stateValue === "running" ? 'on' : 'off';
const autostart = autostartValue === "enable"; })
const stateValue = execSync(`LANG=C virsh dominfo ${vm.name} | grep 'State' | awk '{print $2, $3}'`).toString().trim(); }else{
const state: 'on' | 'off' = stateValue === "running" ? 'on' : 'off'; Logger.info("VMs havent been Loaded yet, loading now...")
virtualMachines.push({ settings.qemu_vms.forEach(vm => {
name: vm.name, Logger.info("Loading " + vm.name)
os: vm.os, const vCpuCount = parseInt(execSync(`LANG=C virsh dominfo ${vm.name} | grep 'CPU(s)' | awk '{print $2}'`).toString().trim());
vCpuCount: vCpuCount, const maxMemory = parseInt(execSync(`LANG=C virsh dominfo ${vm.name} | grep 'Max memory' | awk '{print $3}'`).toString().trim()) / 1024;
maxMemory: maxMemory, const autostartValue = execSync(`LANG=C virsh dominfo ${vm.name} | grep 'Autostart' | awk '{print $2}'`).toString().trim();
autostart: autostart, const autostart = autostartValue === "enable";
state: state 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';
vm_cache.vms.push({
name: vm.name,
os: vm.os,
vCpuCount: vCpuCount,
maxMemory: maxMemory,
autostart: autostart,
state: state
});
}); });
}); }
return virtualMachines;
return vm_cache.vms;
}); });