implement disk deallocation in delete instance,

remove console.logs

Signed-off-by: Arthur Lu <learthurgo@gmail.com>
This commit is contained in:
Arthur Lu 2023-03-01 01:14:03 +00:00
parent 364c268a64
commit 71b786792f
2 changed files with 26 additions and 2 deletions

26
main.js
View File

@ -349,6 +349,32 @@ app.delete("/api/instance", async (req, res) => {
// setup release TODO: add disk data here // setup release TODO: add disk data here
let currentConfig = await requestPVE(`${vmpath}/config`, "GET", null, null, pveAPIToken); let currentConfig = await requestPVE(`${vmpath}/config`, "GET", null, null, pveAPIToken);
let release = {cores: currentConfig.data.data.cores, memory: currentConfig.data.data.memory}; 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); let result = await requestPVE(`${vmpath}`, "DELETE", req.cookies, null, pveAPIToken);
result = await handleResponse(req.body.node, result); result = await handleResponse(req.body.node, result);

View File

@ -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 storageData = await requestPVE(`/nodes/${node}/storage/${storageID}/content`, "GET", null, null, pveAPIToken);
let diskDataStorage = null; let diskDataStorage = null;
storageData.data.data.forEach((element) => { storageData.data.data.forEach((element) => {
console.log(element)
if (element.volid === diskDataConfig) { if (element.volid === diskDataConfig) {
console.log("match!!!")
element.storage = storageID; element.storage = storageID;
diskDataStorage = element; diskDataStorage = element;
} }