added caching for vms
This commit is contained in:
parent
286685d157
commit
81ad1f7622
2 changed files with 39 additions and 22 deletions
|
@ -1,6 +1,13 @@
|
|||
import { reactive } from "vue";
|
||||
import type { VM } from "~/types/VM"
|
||||
|
||||
|
||||
export const jwt_globals = reactive({
|
||||
secret: "",
|
||||
});
|
||||
|
||||
|
||||
|
||||
export const vm_cache = reactive({
|
||||
vms: [] as VM[],
|
||||
})
|
||||
|
|
|
@ -1,19 +1,27 @@
|
|||
|
||||
import {execSync} from 'child_process';
|
||||
import {VM} from "~/types/VM";
|
||||
import {settings} from "~/panel.config";
|
||||
import {vm_cache} from "~/core/globals";
|
||||
import Logger from "~/core/logger";
|
||||
|
||||
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 => {
|
||||
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 autostart = autostartValue === "enable";
|
||||
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';
|
||||
virtualMachines.push({
|
||||
vm_cache.vms.push({
|
||||
name: vm.name,
|
||||
os: vm.os,
|
||||
vCpuCount: vCpuCount,
|
||||
|
@ -22,5 +30,7 @@ export default defineEventHandler(() => {
|
|||
state: state
|
||||
});
|
||||
});
|
||||
return virtualMachines;
|
||||
}
|
||||
|
||||
return vm_cache.vms;
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue