change instance disk net and pci logic to fabric

This commit is contained in:
alu
2025-02-21 21:54:02 +00:00
parent 7d2db031a9
commit 4bd4e136dd
4 changed files with 153 additions and 142 deletions
+71 -82
View File
@@ -1,5 +1,4 @@
import { Router } from "express";
import { token } from "morgan";
export const router = Router({ mergeParams: true }); ;
const checkAuth = global.utils.checkAuth;
@@ -23,30 +22,28 @@ router.get("/:hostpci", async (req, res) => {
node: req.params.node,
type: req.params.type,
vmid: req.params.vmid,
hostpci: req.params.hostpci.replace("hostpci", "")
hostpci: Number(req.params.hostpci.replace("hostpci", ""))
};
// check hostpci is a valid number
if (isNaN(params.hostpci)) {
res.status(500).send({ error: `Hostpci id must be a number, got ${req.params.hostpci}.` });
res.end();
return;
}
// check auth for specific instance
const vmpath = `/nodes/${params.node}/${params.type}/${params.vmid}`;
const auth = await checkAuth(req.cookies, res, vmpath);
if (!auth) {
return;
}
// check device is in instance config
const config = (await global.pve.requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
if (!config[`hostpci${params.hostpci}`]) {
res.status(500).send({ error: `Could not find hostpci${params.hostpci} in ${params.vmid}.` });
res.end();
return;
}
const device = config[`hostpci${params.hostpci}`].split(",")[0];
// get node's pci devices
const deviceData = await global.pve.getDeviceInfo(params.node, device);
if (!deviceData) {
// get device
const device = await global.pve.getDevice(params.node, params.vmid, params.hostpci);
if (!device) {
res.status(500).send({ error: `Could not find hostpci${params.hostpci}=${device} in ${params.node}.` });
res.end();
return;
}
res.status(200).send(deviceData);
res.status(200).send(device);
res.end();
});
@@ -71,13 +68,16 @@ router.post("/:hostpci/modify", async (req, res) => {
node: req.params.node,
type: req.params.type,
vmid: req.params.vmid,
hostpci: req.params.hostpci.replace("hostpci", ""),
hostpci: Number(req.params.hostpci.replace("hostpci", "")),
device: req.body.device,
pcie: req.body.pcie
};
const userObj = global.utils.getUserObjFromUsername(req.cookies.username);
// check hostpci is a valid number
if (isNaN(params.hostpci)) {
res.status(500).send({ error: `Hostpci id must be a number, got ${req.params.hostpci}.` });
res.end();
return;
}
// check if type is qemu
if (params.type !== "qemu") {
res.status(500).send({ error: "Type must be qemu (vm)." });
@@ -92,28 +92,33 @@ router.post("/:hostpci/modify", async (req, res) => {
}
// force all functions
params.device = params.device.split(".")[0];
// get instance config to check if device has not changed
const config = (await global.pve.requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { token: true })).data.data;
const currentDeviceData = await global.pve.getDeviceInfo(params.node, config[`hostpci${params.hostpci}`].split(",")[0]);
if (!currentDeviceData) {
// device must exist to be modified
const existingDevice = await global.pve.getDevice(params.node, params.vmid, params.hostpci);
if (!existingDevice) {
res.status(500).send({ error: `No device in hostpci${params.hostpci}.` });
res.end();
return;
}
// only check user and node availability if base id is different
if (currentDeviceData.id.split(".")[0] !== params.device) {
// only check user and node availability if base id is different, we do the split in case of existing partial-function hostpci
const userObj = global.utils.getUserObjFromUsername(req.cookies.username);
if (existingDevice.device_id.split(".")[0] !== params.device) {
// setup request
const deviceData = await global.pve.getDeviceInfo(params.node, params.device);
const request = { pci: deviceData.device_name };
const node = await global.pve.getNode(params.node);
const requestedDevice = node.devices[`${params.device}`];
const request = { pci: requestedDevice.device_name };
if (!requestedDevice) {
res.status(500).send({ request, error: `Could not fulfil request for ${params.device}.` });
res.end();
return;
}
// check resource approval
if (!await approveResources(req, userObj, request, params.node)) {
res.status(500).send({ request, error: `Could not fulfil request for ${deviceData.device_name}.` });
res.status(500).send({ request, error: `Could not fulfil request for ${requestedDevice.device_name}.` });
res.end();
return;
}
// check node availability
const nodeAvailPci = await global.pve.getNodeAvailDevices(params.node, req.cookies);
if (!nodeAvailPci.some(element => element.id.split(".")[0] === params.device)) {
if (!Object.values(node.devices).some(element => element.device_id.split(".")[0] === params.device && element.reserved === false)) {
res.status(500).send({ error: `Device ${params.device} is already in use on ${params.node}.` });
res.end();
return;
@@ -123,18 +128,9 @@ router.post("/:hostpci/modify", async (req, res) => {
const action = {};
action[`hostpci${params.hostpci}`] = `${params.device},pcie=${params.pcie}`;
// commit action
const rootauth = await global.pve.requestPVE("/access/ticket", "POST", null, global.config.backends.pve.config.root);
if (!(rootauth.status === 200)) {
res.status(rootauth.status).send({ auth: false, error: "API could not authenticate as root user." });
res.end();
return;
}
const rootcookies = {
PVEAuthCookie: rootauth.data.data.ticket,
CSRFPreventionToken: rootauth.data.data.CSRFPreventionToken
};
const result = await global.pve.requestPVE(`${vmpath}/config`, "POST", { cookies: rootcookies }, action);
const result = await global.pve.requestPVE(`${vmpath}/config`, "POST", { root: true }, action);
await global.pve.handleResponse(params.node, result, res);
await global.pve.syncNode(params.node);
});
/**
@@ -151,18 +147,22 @@ router.post("/:hostpci/modify", async (req, res) => {
* - 500: {request: Object, error: string}
* - 500: PVE Task Object
*/
router.post("/create", async (req, res) => {
router.post("/:hostpci/create", async (req, res) => {
req.params = Object.assign({}, req.routeparams, req.params);
const params = {
node: req.params.node,
type: req.params.type,
vmid: req.params.vmid,
hostpci: Number(req.params.hostpci.replace("hostpci", "")),
device: req.body.device,
pcie: req.body.pcie
};
const userObj = global.utils.getUserObjFromUsername(req.cookies.username);
// check hostpci is a valid number
if (isNaN(params.hostpci)) {
res.status(500).send({ error: `Hostpci id must be a number, got ${req.params.hostpci}.` });
res.end();
return;
}
// check if type is qemu
if (params.type !== "qemu") {
res.status(500).send({ error: "Type must be qemu (vm)." });
@@ -177,46 +177,38 @@ 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 = (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++;
// device must not exist to be added
const existingDevice = await global.pve.getDevice(params.node, params.vmid, params.hostpci);
if (existingDevice) {
res.status(500).send({ error: `Existing device in hostpci${params.hostpci}.` });
res.end();
return;
}
// setup request
const deviceData = await global.pve.getDeviceInfo(params.node, params.device);
const request = {
pci: deviceData.device_name
};
const node = await global.pve.getNode(params.node);
const requestedDevice = node.devices[`${params.device}`];
const request = { pci: requestedDevice.device_name };
// check resource approval
const userObj = global.utils.getUserObjFromUsername(req.cookies.username);
if (!await approveResources(req, userObj, request, params.node)) {
res.status(500).send({ request, error: `Could not fulfil request for ${deviceData.device_name}.` });
res.status(500).send({ request, error: `Could not fulfil request for ${requestedDevice.device_name}.` });
res.end();
return;
}
// check node availability
const nodeAvailPci = await global.pve.getNodeAvailDevices(params.node, req.cookies);
if (!nodeAvailPci.some(element => element.id.split(".")[0] === params.device)) {
// const node = await global.pve.getNode(params.node);
if (!Object.values(node.devices).some(element => element.device_id.split(".")[0] === params.device && element.reserved === false)) {
res.status(500).send({ error: `Device ${params.device} is already in use on ${params.node}.` });
res.end();
return;
}
// setup action
const action = {};
action[`hostpci${hostpci}`] = `${params.device},pcie=${params.pcie}`;
action[`hostpci${params.hostpci}`] = `${params.device},pcie=${params.pcie}`;
// commit action
const rootauth = await global.pve.requestPVE("/access/ticket", "POST", null, global.config.backends.pve.config.root);
if (!(rootauth.status === 200)) {
res.status(rootauth.status).send({ auth: false, error: "API could not authenticate as root user." });
res.end();
return;
}
const rootcookies = {
PVEAuthCookie: rootauth.data.data.ticket,
CSRFPreventionToken: rootauth.data.data.CSRFPreventionToken
};
const result = await global.pve.requestPVE(`${vmpath}/config`, "POST", { cookies: rootcookies }, action);
const result = await global.pve.requestPVE(`${vmpath}/config`, "POST", { root: true }, action);
await global.pve.handleResponse(params.node, result, res);
await global.pve.syncNode(params.node);
});
/**
@@ -238,8 +230,14 @@ router.delete("/:hostpci/delete", async (req, res) => {
node: req.params.node,
type: req.params.type,
vmid: req.params.vmid,
hostpci: req.params.hostpci.replace("hostpci", "")
hostpci: Number(req.params.hostpci.replace("hostpci", ""))
};
// check hostpci is a valid number
if (isNaN(params.hostpci)) {
res.status(500).send({ error: `Hostpci id must be a number, got ${req.params.hostpci}.` });
res.end();
return;
}
// check if type is qemu
if (params.type !== "qemu") {
res.status(500).send({ error: "Type must be qemu (vm)." });
@@ -253,8 +251,8 @@ router.delete("/:hostpci/delete", async (req, res) => {
return;
}
// check device is in instance config
const config = (await global.pve.requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
if (!config[`hostpci${params.hostpci}`]) {
const device = global.pve.getDevice(params.node, params.vmid, params.hostpci);
if (!device) {
res.status(500).send({ error: `Could not find hostpci${params.hostpci} in ${params.vmid}.` });
res.end();
return;
@@ -262,16 +260,7 @@ router.delete("/:hostpci/delete", async (req, res) => {
// setup action
const action = { delete: `hostpci${params.hostpci}` };
// commit action, need to use root user here because proxmox api only allows root to modify hostpci for whatever reason
const rootauth = await global.pve.requestPVE("/access/ticket", "POST", null, global.config.backends.pve.config.root);
if (!(rootauth.status === 200)) {
res.status(rootauth.status).send({ auth: false, error: "API could not authenticate as root user." });
res.end();
return;
}
const rootcookies = {
PVEAuthCookie: rootauth.data.data.ticket,
CSRFPreventionToken: rootauth.data.data.CSRFPreventionToken
};
const result = await global.pve.requestPVE(`${vmpath}/config`, "POST", { cookies: rootcookies }, action);
const result = await global.pve.requestPVE(`${vmpath}/config`, "POST", { root: true }, action);
await global.pve.handleResponse(params.node, result, res);
await global.pve.syncNode(params.node);
});