From 38589b1b334318ce02f5b8b79ecd5fa0d6adb9f9 Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Wed, 1 Mar 2023 01:14:03 +0000 Subject: [PATCH] implement disk deallocation in delete instance, remove console.logs Signed-off-by: Arthur Lu --- main.js | 26 ++++++++++++++++++++++++++ pveutils.js | 2 -- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/main.js b/main.js index 13fe3f3..21be804 100644 --- a/main.js +++ b/main.js @@ -349,6 +349,32 @@ app.delete("/api/instance", async (req, res) => { // setup release TODO: add disk data here let currentConfig = await requestPVE(`${vmpath}/config`, "GET", null, null, pveAPIToken); let release = {cores: currentConfig.data.data.cores, memory: currentConfig.data.data.memory}; + let keys = Object.keys(currentConfig.data.data); + for (let i = 0; i < keys.length; i++) { + let element = keys[i]; + if (element.includes("sata") || element.includes("mp") || element.includes("rootfs")) { // if the disk is mounted, get its storage and size + let diskConfig = await getDiskConfig(req.body.node, req.body.type, req.body.vmid, element); + let storage = diskConfig.split(":")[0]; + let size = parseInt(diskConfig.split("size=")[1].split("G")[0]); + if (!release[storage]) { // if this is the first time storage has been seen, set storage to size + release[storage] = size; + } + else { // otherwise incrment storage by size + release[storage] += size; + } + } + else if (element.includes("unused")) { + let diskConfig = await getUnusedDiskData(req.body.node, req.body.type, req.body.vmid, element); // get disk config of unused disk + let storage = diskConfig.storage; // get disk storage + let size = diskConfig.size / (1024**3); // get disk size + if (!release[storage]) { // if this is the first time storage has been seen, set storage to size + release[storage] = size; + } + else { // otherwise incrment storage by size + release[storage] += size; + } + } + } let result = await requestPVE(`${vmpath}`, "DELETE", req.cookies, null, pveAPIToken); result = await handleResponse(req.body.node, result); diff --git a/pveutils.js b/pveutils.js index 935f9a6..8895e74 100644 --- a/pveutils.js +++ b/pveutils.js @@ -72,9 +72,7 @@ async function getUnusedDiskData (node, type, vmid, disk) { let storageData = await requestPVE(`/nodes/${node}/storage/${storageID}/content`, "GET", null, null, pveAPIToken); let diskDataStorage = null; storageData.data.data.forEach((element) => { - console.log(element) if (element.volid === diskDataConfig) { - console.log("match!!!") element.storage = storageID; diskDataStorage = element; }