fix bug with adding multiple pci devices

This commit is contained in:
Arthur Lu 2025-01-06 20:33:15 +00:00
parent 7626dcf387
commit 3001febbc2
3 changed files with 20 additions and 11 deletions

View File

@ -98,13 +98,19 @@ router.get(`/:node(${nodeRegexP})/pci`, async (req, res) => {
}
// get remaining user resources
const userAvailPci = (await getUserResources(req, userObj)).pci.nodes[params.node];
// get node avail devices
let nodeAvailPci = await global.pve.getNodeAvailDevices(params.node, req.cookies);
nodeAvailPci = nodeAvailPci.filter(nodeAvail => userAvailPci.some((userAvail) => {
return nodeAvail.device_name && nodeAvail.device_name.includes(userAvail.match) && userAvail.avail > 0;
}));
res.status(200).send(nodeAvailPci);
res.end();
if (userAvailPci == undefined) { // user has no avaliable devices on this node, so send an empty list
res.status(200).send([]);
res.end();
}
else {
// get node avail devices
let nodeAvailPci = await global.pve.getNodeAvailDevices(params.node, req.cookies);
nodeAvailPci = nodeAvailPci.filter(nodeAvail => userAvailPci.some((userAvail) => {
return nodeAvail.device_name && nodeAvail.device_name.includes(userAvail.match) && userAvail.avail > 0;
}));
res.status(200).send(nodeAvailPci);
res.end();
}
});
/**

View File

@ -1,4 +1,5 @@
import { Router } from "express";
import { token } from "morgan";
export const router = Router({ mergeParams: true }); ;
const checkAuth = global.utils.checkAuth;
@ -177,7 +178,7 @@ router.post("/create", async (req, res) => {
// force all functions
params.device = params.device.split(".")[0];
// get instance config to find next available hostpci slot
const config = global.pve.requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { cookies: params.cookies });
const config = (await global.pve.requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { token: true })).data.data;
let hostpci = 0;
while (config[`hostpci${hostpci}`]) {
hostpci++;

View File

@ -258,11 +258,13 @@ export async function getUserResources (req, user) {
userResources.pci.nodes[nodeName][index].used++;
userResources.pci.nodes[nodeName][index].avail--;
}
// otherwise add the resource to the global pool
// otherwise try to add the resource to the global pool
else {
const index = userResources.pci.global.findIndex((availEelement) => deviceName.includes(availEelement.match));
userResources.pci.global[index].used++;
userResources.pci.global[index].avail--;
if (index >= 0) { // device resource is in the user's global list then increment it by 1
userResources.pci.global[index].used++;
userResources.pci.global[index].avail--;
}
}
const index = userResources.pci.total.findIndex((availEelement) => deviceName.includes(availEelement.match));
userResources.pci.total[index].used++;