change instance disk net and pci logic to fabric
This commit is contained in:
parent
7d2db031a9
commit
4bd4e136dd
@ -98,17 +98,26 @@ router.get(`/:node(${nodeRegexP})/pci`, async (req, res) => {
|
|||||||
}
|
}
|
||||||
// get remaining user resources
|
// get remaining user resources
|
||||||
const userAvailPci = (await getUserResources(req, userObj)).pci.nodes[params.node];
|
const userAvailPci = (await getUserResources(req, userObj)).pci.nodes[params.node];
|
||||||
if (userAvailPci == undefined) { // user has no avaliable devices on this node, so send an empty list
|
if (userAvailPci === undefined) { // user has no avaliable devices on this node, so send an empty list
|
||||||
res.status(200).send([]);
|
res.status(200).send([]);
|
||||||
res.end();
|
res.end();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// get node avail devices
|
// get node avail devices
|
||||||
let nodeAvailPci = await global.pve.getNodeAvailDevices(params.node, req.cookies);
|
const node = await global.pve.getNode(params.node);
|
||||||
nodeAvailPci = nodeAvailPci.filter(nodeAvail => userAvailPci.some((userAvail) => {
|
let availableDevices = [];
|
||||||
|
// get each device and filter out only thise which are not reserved
|
||||||
|
for (const device of Object.values(node.devices)) {
|
||||||
|
if (device.reserved === false) {
|
||||||
|
availableDevices.push(device);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// further filter out only devices which the user has access to
|
||||||
|
availableDevices = availableDevices.filter(nodeAvail => userAvailPci.some((userAvail) => {
|
||||||
return nodeAvail.device_name && nodeAvail.device_name.includes(userAvail.match) && userAvail.avail > 0;
|
return nodeAvail.device_name && nodeAvail.device_name.includes(userAvail.match) && userAvail.avail > 0;
|
||||||
}));
|
}));
|
||||||
res.status(200).send(nodeAvailPci);
|
|
||||||
|
res.status(200).send(availableDevices);
|
||||||
res.end();
|
res.end();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -150,13 +159,13 @@ router.post(`${basePath}/resources`, async (req, res) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// get current config
|
// get current config
|
||||||
const currentConfig = await global.pve.requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { token: true });
|
const instance = await global.pve.getInstance(params.node, params.vmid);
|
||||||
const request = {
|
const request = {
|
||||||
cores: Number(params.cores) - Number(currentConfig.data.data.cores),
|
cores: Number(params.cores) - Number(instance.cores),
|
||||||
memory: Number(params.memory) - Number(currentConfig.data.data.memory)
|
memory: Number(params.memory) - Number(instance.memory)
|
||||||
};
|
};
|
||||||
if (params.type === "lxc") {
|
if (params.type === "lxc") {
|
||||||
request.swap = Number(params.swap) - Number(currentConfig.data.data.swap);
|
request.swap = Number(params.swap) - Number(instance.swap);
|
||||||
}
|
}
|
||||||
else if (params.type === "qemu") {
|
else if (params.type === "qemu") {
|
||||||
request.cpu = params.proctype;
|
request.cpu = params.proctype;
|
||||||
@ -180,6 +189,7 @@ router.post(`${basePath}/resources`, async (req, res) => {
|
|||||||
// commit action
|
// commit action
|
||||||
const result = await global.pve.requestPVE(`${vmpath}/config`, method, { token: true }, action);
|
const result = await global.pve.requestPVE(`${vmpath}/config`, method, { token: true }, action);
|
||||||
await global.pve.handleResponse(params.node, result, res);
|
await global.pve.handleResponse(params.node, result, res);
|
||||||
|
await global.pve.syncInstance(params.node, params.vmid);
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -301,6 +311,7 @@ router.post(`${basePath}/create`, async (req, res) => {
|
|||||||
// commit action
|
// commit action
|
||||||
const result = await global.pve.requestPVE(`/nodes/${params.node}/${params.type}`, "POST", { token: true }, action);
|
const result = await global.pve.requestPVE(`/nodes/${params.node}/${params.type}`, "POST", { token: true }, action);
|
||||||
await global.pve.handleResponse(params.node, result, res);
|
await global.pve.handleResponse(params.node, result, res);
|
||||||
|
await global.pve.syncNode(params.node);
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -329,4 +340,5 @@ router.delete(`${basePath}/delete`, async (req, res) => {
|
|||||||
// commit action
|
// commit action
|
||||||
const result = await global.pve.requestPVE(vmpath, "DELETE", { token: true });
|
const result = await global.pve.requestPVE(vmpath, "DELETE", { token: true });
|
||||||
await global.pve.handleResponse(params.node, result, res);
|
await global.pve.handleResponse(params.node, result, res);
|
||||||
|
await global.pve.syncNode(params.node);
|
||||||
});
|
});
|
||||||
|
@ -31,10 +31,9 @@ router.post("/:disk/detach", async (req, res) => {
|
|||||||
if (!auth) {
|
if (!auth) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// get current config
|
|
||||||
const config = (await global.pve.requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
|
|
||||||
// disk must exist
|
// disk must exist
|
||||||
if (!config[params.disk]) {
|
const disk = await global.pve.getDisk(params.node, params.vmid, params.disk);
|
||||||
|
if (!disk) {
|
||||||
res.status(500).send({ error: `Disk ${params.disk} does not exist.` });
|
res.status(500).send({ error: `Disk ${params.disk} does not exist.` });
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
@ -49,6 +48,7 @@ router.post("/:disk/detach", async (req, res) => {
|
|||||||
const method = params.type === "qemu" ? "POST" : "PUT";
|
const method = params.type === "qemu" ? "POST" : "PUT";
|
||||||
const result = await global.pve.requestPVE(`${vmpath}/config`, method, { token: true }, action);
|
const result = await global.pve.requestPVE(`${vmpath}/config`, method, { token: true }, action);
|
||||||
await global.pve.handleResponse(params.node, result, res);
|
await global.pve.handleResponse(params.node, result, res);
|
||||||
|
await global.pve.syncInstance(params.node, params.vmid);
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -80,29 +80,30 @@ router.post("/:disk/attach", async (req, res) => {
|
|||||||
if (!auth) {
|
if (!auth) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// get current config
|
|
||||||
const config = (await global.pve.requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
|
|
||||||
// disk must exist
|
// disk must exist
|
||||||
if (!config[`unused${params.source}`]) {
|
const disk = await global.pve.getDisk(params.node, params.vmid, `unused${params.source}`);
|
||||||
res.status(403).send({ error: `Requested disk unused${params.source} does not exist.` });
|
if (!disk) {
|
||||||
|
res.status(500).send({ error: `Requested disk unused${params.source} does not exist.` });
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// target disk must be allowed according to source disk's storage options
|
// target disk must be allowed according to source disk's storage options
|
||||||
const diskConfig = await global.pve.getDiskInfo(params.node, config, `unused${params.source}`); // get target disk
|
|
||||||
const resourceConfig = global.config.resources;
|
const resourceConfig = global.config.resources;
|
||||||
if (!resourceConfig[diskConfig.storage].disks.some(diskPrefix => params.disk.startsWith(diskPrefix))) {
|
if (!resourceConfig[disk.storage].disks.some(diskPrefix => params.disk.startsWith(diskPrefix))) {
|
||||||
res.status(500).send({ error: `Requested target ${params.disk} is not in allowed list [${resourceConfig[diskConfig.storage].disks}].` });
|
res.status(500).send({ error: `Requested target ${params.disk} is not in allowed list [${resourceConfig[disk.storage].disks}].` });
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// setup action using source disk info from vm config
|
// setup action using source disk info from vm config
|
||||||
const action = {};
|
const action = {};
|
||||||
action[params.disk] = config[`unused${params.source}`];
|
action[params.disk] = disk.volid;
|
||||||
const method = params.type === "qemu" ? "POST" : "PUT";
|
const method = params.type === "qemu" ? "POST" : "PUT";
|
||||||
|
|
||||||
// commit action
|
// commit action
|
||||||
const result = await global.pve.requestPVE(`${vmpath}/config`, method, { token: true }, action);
|
const result = await global.pve.requestPVE(`${vmpath}/config`, method, { token: true }, action);
|
||||||
await global.pve.handleResponse(params.node, result, res);
|
await global.pve.handleResponse(params.node, result, res);
|
||||||
|
await global.pve.syncInstance(params.node, params.vmid);
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -138,17 +139,15 @@ router.post("/:disk/resize", async (req, res) => {
|
|||||||
if (!auth) {
|
if (!auth) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// get current config
|
|
||||||
const config = (await global.pve.requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
|
|
||||||
// check disk existence
|
// check disk existence
|
||||||
const diskConfig = await global.pve.getDiskInfo(params.node, config, params.disk); // get target disk
|
const disk = await global.pve.getDisk(params.node, params.vmid, params.disk); // get target disk
|
||||||
if (!diskConfig) { // exit if disk does not exist
|
if (!disk) { // exit if disk does not exist
|
||||||
res.status(500).send({ error: `requested disk ${params.disk} does not exist.` });
|
res.status(500).send({ error: `requested disk ${params.disk} does not exist.` });
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// setup request
|
// setup request
|
||||||
const storage = diskConfig.storage; // get the storage
|
const storage = disk.storage; // get the storage
|
||||||
const request = {};
|
const request = {};
|
||||||
request[storage] = Number(params.size * 1024 ** 3); // setup request object
|
request[storage] = Number(params.size * 1024 ** 3); // setup request object
|
||||||
// check request approval
|
// check request approval
|
||||||
@ -161,6 +160,7 @@ router.post("/:disk/resize", async (req, res) => {
|
|||||||
const action = { disk: params.disk, size: `+${params.size}G` };
|
const action = { disk: params.disk, size: `+${params.size}G` };
|
||||||
const result = await global.pve.requestPVE(`${vmpath}/resize`, "PUT", { token: true }, action);
|
const result = await global.pve.requestPVE(`${vmpath}/resize`, "PUT", { token: true }, action);
|
||||||
await global.pve.handleResponse(params.node, result, res);
|
await global.pve.handleResponse(params.node, result, res);
|
||||||
|
await global.pve.syncInstance(params.node, params.vmid);
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -198,17 +198,15 @@ router.post("/:disk/move", async (req, res) => {
|
|||||||
if (!auth) {
|
if (!auth) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// get current config
|
|
||||||
const config = (await global.pve.requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
|
|
||||||
// check disk existence
|
// check disk existence
|
||||||
const diskConfig = await global.pve.getDiskInfo(params.node, config, params.disk); // get target disk
|
const disk = await global.pve.getDisk(params.node, params.vmid, params.disk); // get target disk
|
||||||
if (!diskConfig) { // exit if disk does not exist
|
if (!disk) { // exit if disk does not exist
|
||||||
res.status(500).send({ error: `requested disk ${params.disk} does not exist.` });
|
res.status(500).send({ error: `requested disk ${params.disk} does not exist.` });
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// setup request
|
// setup request
|
||||||
const size = parseInt(diskConfig.size); // get source disk size
|
const size = parseInt(disk.size); // get source disk size
|
||||||
const dstStorage = params.storage; // get destination storage
|
const dstStorage = params.storage; // get destination storage
|
||||||
const request = {};
|
const request = {};
|
||||||
if (!params.delete) { // if not delete, then request storage, otherwise it is net 0
|
if (!params.delete) { // if not delete, then request storage, otherwise it is net 0
|
||||||
@ -232,6 +230,7 @@ router.post("/:disk/move", async (req, res) => {
|
|||||||
// commit action
|
// commit action
|
||||||
const result = await global.pve.requestPVE(`${vmpath}/${route}`, "POST", { token: true }, action);
|
const result = await global.pve.requestPVE(`${vmpath}/${route}`, "POST", { token: true }, action);
|
||||||
await global.pve.handleResponse(params.node, result, res);
|
await global.pve.handleResponse(params.node, result, res);
|
||||||
|
await global.pve.syncInstance(params.node, params.vmid);
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -261,11 +260,10 @@ router.delete("/:disk/delete", async (req, res) => {
|
|||||||
if (!auth) {
|
if (!auth) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// get current config
|
|
||||||
const config = (await global.pve.requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
|
|
||||||
// disk must exist
|
// disk must exist
|
||||||
if (!config[params.disk]) {
|
const disk = await global.pve.getDisk(params.node, params.vmid, params.disk);
|
||||||
res.status(403).send({ error: `Requested disk unused${params.source} does not exist.` });
|
if (!disk) {
|
||||||
|
res.status(500).send({ error: `Disk ${params.disk} does not exist.` });
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -281,6 +279,7 @@ router.delete("/:disk/delete", async (req, res) => {
|
|||||||
// commit action
|
// commit action
|
||||||
const result = await global.pve.requestPVE(`${vmpath}/config`, method, { token: true }, action);
|
const result = await global.pve.requestPVE(`${vmpath}/config`, method, { token: true }, action);
|
||||||
await global.pve.handleResponse(params.node, result, res);
|
await global.pve.handleResponse(params.node, result, res);
|
||||||
|
await global.pve.syncInstance(params.node, params.vmid);
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -310,20 +309,17 @@ router.post("/:disk/create", async (req, res) => {
|
|||||||
size: req.body.size,
|
size: req.body.size,
|
||||||
iso: req.body.iso
|
iso: req.body.iso
|
||||||
};
|
};
|
||||||
|
|
||||||
const userObj = global.utils.getUserObjFromUsername(req.cookies.username);
|
const userObj = global.utils.getUserObjFromUsername(req.cookies.username);
|
||||||
|
|
||||||
// check auth for specific instance
|
// check auth for specific instance
|
||||||
const vmpath = `/nodes/${params.node}/${params.type}/${params.vmid}`;
|
const vmpath = `/nodes/${params.node}/${params.type}/${params.vmid}`;
|
||||||
const auth = await checkAuth(req.cookies, res, vmpath);
|
const auth = await checkAuth(req.cookies, res, vmpath);
|
||||||
if (!auth) {
|
if (!auth) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// get current config
|
|
||||||
const config = (await global.pve.requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
|
|
||||||
// disk must not exist
|
// disk must not exist
|
||||||
if (config[params.disk]) {
|
const disk = await global.pve.getDisk(params.node, params.vmid, params.disk);
|
||||||
res.status(403).send({ error: `Requested disk ${params.disk} already exists.` });
|
if (disk) {
|
||||||
|
res.status(500).send({ error: `Disk ${params.disk} does already exists.` });
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -361,4 +357,5 @@ router.post("/:disk/create", async (req, res) => {
|
|||||||
// commit action
|
// commit action
|
||||||
const result = await global.pve.requestPVE(`${vmpath}/config`, method, { token: true }, action);
|
const result = await global.pve.requestPVE(`${vmpath}/config`, method, { token: true }, action);
|
||||||
await global.pve.handleResponse(params.node, result, res);
|
await global.pve.handleResponse(params.node, result, res);
|
||||||
|
await global.pve.syncInstance(params.node, params.vmid);
|
||||||
});
|
});
|
||||||
|
@ -26,12 +26,16 @@ router.post("/:netid/create", async (req, res) => {
|
|||||||
node: req.params.node,
|
node: req.params.node,
|
||||||
type: req.params.type,
|
type: req.params.type,
|
||||||
vmid: req.params.vmid,
|
vmid: req.params.vmid,
|
||||||
netid: req.params.netid.replace("net", ""),
|
netid: Number(req.params.netid.replace("net", "")),
|
||||||
rate: req.body.rate,
|
rate: req.body.rate,
|
||||||
name: req.body.name
|
name: req.body.name
|
||||||
};
|
};
|
||||||
|
// check netid is a valid number
|
||||||
const userObj = global.utils.getUserObjFromUsername(req.cookies.username);
|
if (isNaN(params.netid)) {
|
||||||
|
res.status(500).send({ error: `Network interface id must be a number, got ${req.params.netid}.` });
|
||||||
|
res.end();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// check auth for specific instance
|
// check auth for specific instance
|
||||||
const vmpath = `/nodes/${params.node}/${params.type}/${params.vmid}`;
|
const vmpath = `/nodes/${params.node}/${params.type}/${params.vmid}`;
|
||||||
@ -39,10 +43,9 @@ router.post("/:netid/create", async (req, res) => {
|
|||||||
if (!auth) {
|
if (!auth) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// get current config
|
|
||||||
const currentConfig = await global.pve.requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { token: true });
|
|
||||||
// net interface must not exist
|
// net interface must not exist
|
||||||
if (currentConfig.data.data[`net${params.netid}`]) {
|
const net = await global.pve.getNet(params.node, params.vmid, params.netid);
|
||||||
|
if (net) {
|
||||||
res.status(500).send({ error: `Network interface net${params.netid} already exists.` });
|
res.status(500).send({ error: `Network interface net${params.netid} already exists.` });
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
@ -56,6 +59,7 @@ router.post("/:netid/create", async (req, res) => {
|
|||||||
network: Number(params.rate)
|
network: Number(params.rate)
|
||||||
};
|
};
|
||||||
// check resource approval
|
// check resource approval
|
||||||
|
const userObj = global.utils.getUserObjFromUsername(req.cookies.username);
|
||||||
if (!await approveResources(req, userObj, request, params.node)) {
|
if (!await approveResources(req, userObj, request, params.node)) {
|
||||||
res.status(500).send({ request, error: `Could not fulfil network request of ${params.rate}MB/s.` });
|
res.status(500).send({ request, error: `Could not fulfil network request of ${params.rate}MB/s.` });
|
||||||
res.end();
|
res.end();
|
||||||
@ -74,6 +78,7 @@ router.post("/:netid/create", async (req, res) => {
|
|||||||
// commit action
|
// commit action
|
||||||
const result = await global.pve.requestPVE(`${vmpath}/config`, method, { token: true }, action);
|
const result = await global.pve.requestPVE(`${vmpath}/config`, method, { token: true }, action);
|
||||||
await global.pve.handleResponse(params.node, result, res);
|
await global.pve.handleResponse(params.node, result, res);
|
||||||
|
await global.pve.syncInstance(params.node, params.vmid);
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -97,32 +102,33 @@ router.post("/:netid/modify", async (req, res) => {
|
|||||||
node: req.params.node,
|
node: req.params.node,
|
||||||
type: req.params.type,
|
type: req.params.type,
|
||||||
vmid: req.params.vmid,
|
vmid: req.params.vmid,
|
||||||
netid: req.params.netid.replace("net", ""),
|
netid: Number(req.params.netid.replace("net", "")),
|
||||||
rate: req.body.rate
|
rate: req.body.rate
|
||||||
};
|
};
|
||||||
|
// check netid is a valid number
|
||||||
const userObj = global.utils.getUserObjFromUsername(req.cookies.username);
|
if (isNaN(params.netid)) {
|
||||||
|
res.status(500).send({ error: `Network interface id must be a number, got ${req.params.netid}.` });
|
||||||
|
res.end();
|
||||||
|
return;
|
||||||
|
}
|
||||||
// check auth for specific instance
|
// check auth for specific instance
|
||||||
const vmpath = `/nodes/${params.node}/${params.type}/${params.vmid}`;
|
const vmpath = `/nodes/${params.node}/${params.type}/${params.vmid}`;
|
||||||
const auth = await checkAuth(req.cookies, res, vmpath);
|
const auth = await checkAuth(req.cookies, res, vmpath);
|
||||||
if (!auth) {
|
if (!auth) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// get current config
|
|
||||||
const currentConfig = await global.pve.requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { token: true });
|
|
||||||
// net interface must already exist
|
// net interface must already exist
|
||||||
if (!currentConfig.data.data[`net${params.netid}`]) {
|
const net = await global.pve.getNet(params.node, params.vmid, params.netid);
|
||||||
|
if (!net) {
|
||||||
res.status(500).send({ error: `Network interface net${params.netid} does not exist.` });
|
res.status(500).send({ error: `Network interface net${params.netid} does not exist.` });
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const currentNetworkConfig = currentConfig.data.data[`net${params.netid}`];
|
|
||||||
const currentNetworkRate = currentNetworkConfig.split("rate=")[1].split(",")[0];
|
|
||||||
const request = {
|
const request = {
|
||||||
network: Number(params.rate) - Number(currentNetworkRate)
|
network: Number(params.rate) - Number(net.rate)
|
||||||
};
|
};
|
||||||
// check resource approval
|
// check resource approval
|
||||||
|
const userObj = global.utils.getUserObjFromUsername(req.cookies.username);
|
||||||
if (!await approveResources(req, userObj, request, params.node)) {
|
if (!await approveResources(req, userObj, request, params.node)) {
|
||||||
res.status(500).send({ request, error: `Could not fulfil network request of ${params.rate}MB/s.` });
|
res.status(500).send({ request, error: `Could not fulfil network request of ${params.rate}MB/s.` });
|
||||||
res.end();
|
res.end();
|
||||||
@ -130,11 +136,12 @@ router.post("/:netid/modify", async (req, res) => {
|
|||||||
}
|
}
|
||||||
// setup action
|
// setup action
|
||||||
const action = {};
|
const action = {};
|
||||||
action[`net${params.netid}`] = currentNetworkConfig.replace(`rate=${currentNetworkRate}`, `rate=${params.rate}`);
|
action[`net${params.netid}`] = net.value.replace(`rate=${net.rate}`, `rate=${params.rate}`);
|
||||||
const method = params.type === "qemu" ? "POST" : "PUT";
|
const method = params.type === "qemu" ? "POST" : "PUT";
|
||||||
// commit action
|
// commit action
|
||||||
const result = await global.pve.requestPVE(`${vmpath}/config`, method, { token: true }, action);
|
const result = await global.pve.requestPVE(`${vmpath}/config`, method, { token: true }, action);
|
||||||
await global.pve.handleResponse(params.node, result, res);
|
await global.pve.handleResponse(params.node, result, res);
|
||||||
|
await global.pve.syncInstance(params.node, params.vmid);
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -156,18 +163,23 @@ router.delete("/:netid/delete", async (req, res) => {
|
|||||||
node: req.params.node,
|
node: req.params.node,
|
||||||
type: req.params.type,
|
type: req.params.type,
|
||||||
vmid: req.params.vmid,
|
vmid: req.params.vmid,
|
||||||
netid: req.params.netid.replace("net", "")
|
netid: Number(req.params.netid.replace("net", ""))
|
||||||
};
|
};
|
||||||
|
// check netid is a valid number
|
||||||
|
if (isNaN(params.netid)) {
|
||||||
|
res.status(500).send({ error: `Network interface id must be a number, got ${req.params.netid}.` });
|
||||||
|
res.end();
|
||||||
|
return;
|
||||||
|
}
|
||||||
// check auth for specific instance
|
// check auth for specific instance
|
||||||
const vmpath = `/nodes/${params.node}/${params.type}/${params.vmid}`;
|
const vmpath = `/nodes/${params.node}/${params.type}/${params.vmid}`;
|
||||||
const auth = await checkAuth(req.cookies, res, vmpath);
|
const auth = await checkAuth(req.cookies, res, vmpath);
|
||||||
if (!auth) {
|
if (!auth) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// get current config
|
|
||||||
const currentConfig = await global.pve.requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { token: true });
|
|
||||||
// net interface must already exist
|
// net interface must already exist
|
||||||
if (!currentConfig.data.data[`net${params.netid}`]) {
|
const net = await global.pve.getNet(params.node, params.vmid, params.netid);
|
||||||
|
if (!net) {
|
||||||
res.status(500).send({ error: `Network interface net${params.netid} does not exist.` });
|
res.status(500).send({ error: `Network interface net${params.netid} does not exist.` });
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
@ -177,4 +189,5 @@ router.delete("/:netid/delete", async (req, res) => {
|
|||||||
// commit action
|
// commit action
|
||||||
const result = await global.pve.requestPVE(`${vmpath}/config`, method, { token: true }, { delete: `net${params.netid}` });
|
const result = await global.pve.requestPVE(`${vmpath}/config`, method, { token: true }, { delete: `net${params.netid}` });
|
||||||
await global.pve.handleResponse(params.node, result, res);
|
await global.pve.handleResponse(params.node, result, res);
|
||||||
|
await global.pve.syncInstance(params.node, params.vmid);
|
||||||
});
|
});
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import { Router } from "express";
|
import { Router } from "express";
|
||||||
import { token } from "morgan";
|
|
||||||
export const router = Router({ mergeParams: true }); ;
|
export const router = Router({ mergeParams: true }); ;
|
||||||
|
|
||||||
const checkAuth = global.utils.checkAuth;
|
const checkAuth = global.utils.checkAuth;
|
||||||
@ -23,30 +22,28 @@ router.get("/:hostpci", async (req, res) => {
|
|||||||
node: req.params.node,
|
node: req.params.node,
|
||||||
type: req.params.type,
|
type: req.params.type,
|
||||||
vmid: req.params.vmid,
|
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
|
// check auth for specific instance
|
||||||
const vmpath = `/nodes/${params.node}/${params.type}/${params.vmid}`;
|
const vmpath = `/nodes/${params.node}/${params.type}/${params.vmid}`;
|
||||||
const auth = await checkAuth(req.cookies, res, vmpath);
|
const auth = await checkAuth(req.cookies, res, vmpath);
|
||||||
if (!auth) {
|
if (!auth) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// check device is in instance config
|
// get device
|
||||||
const config = (await global.pve.requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
|
const device = await global.pve.getDevice(params.node, params.vmid, params.hostpci);
|
||||||
if (!config[`hostpci${params.hostpci}`]) {
|
if (!device) {
|
||||||
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) {
|
|
||||||
res.status(500).send({ error: `Could not find hostpci${params.hostpci}=${device} in ${params.node}.` });
|
res.status(500).send({ error: `Could not find hostpci${params.hostpci}=${device} in ${params.node}.` });
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
res.status(200).send(deviceData);
|
res.status(200).send(device);
|
||||||
res.end();
|
res.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -71,13 +68,16 @@ router.post("/:hostpci/modify", async (req, res) => {
|
|||||||
node: req.params.node,
|
node: req.params.node,
|
||||||
type: req.params.type,
|
type: req.params.type,
|
||||||
vmid: req.params.vmid,
|
vmid: req.params.vmid,
|
||||||
hostpci: req.params.hostpci.replace("hostpci", ""),
|
hostpci: Number(req.params.hostpci.replace("hostpci", "")),
|
||||||
device: req.body.device,
|
device: req.body.device,
|
||||||
pcie: req.body.pcie
|
pcie: req.body.pcie
|
||||||
};
|
};
|
||||||
|
// check hostpci is a valid number
|
||||||
const userObj = global.utils.getUserObjFromUsername(req.cookies.username);
|
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
|
// check if type is qemu
|
||||||
if (params.type !== "qemu") {
|
if (params.type !== "qemu") {
|
||||||
res.status(500).send({ error: "Type must be qemu (vm)." });
|
res.status(500).send({ error: "Type must be qemu (vm)." });
|
||||||
@ -92,28 +92,33 @@ router.post("/:hostpci/modify", async (req, res) => {
|
|||||||
}
|
}
|
||||||
// force all functions
|
// force all functions
|
||||||
params.device = params.device.split(".")[0];
|
params.device = params.device.split(".")[0];
|
||||||
// get instance config to check if device has not changed
|
// device must exist to be modified
|
||||||
const config = (await global.pve.requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { token: true })).data.data;
|
const existingDevice = await global.pve.getDevice(params.node, params.vmid, params.hostpci);
|
||||||
const currentDeviceData = await global.pve.getDeviceInfo(params.node, config[`hostpci${params.hostpci}`].split(",")[0]);
|
if (!existingDevice) {
|
||||||
if (!currentDeviceData) {
|
|
||||||
res.status(500).send({ error: `No device in hostpci${params.hostpci}.` });
|
res.status(500).send({ error: `No device in hostpci${params.hostpci}.` });
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// only check user and node availability if base id is different
|
// only check user and node availability if base id is different, we do the split in case of existing partial-function hostpci
|
||||||
if (currentDeviceData.id.split(".")[0] !== params.device) {
|
const userObj = global.utils.getUserObjFromUsername(req.cookies.username);
|
||||||
|
if (existingDevice.device_id.split(".")[0] !== params.device) {
|
||||||
// setup request
|
// setup request
|
||||||
const deviceData = await global.pve.getDeviceInfo(params.node, params.device);
|
const node = await global.pve.getNode(params.node);
|
||||||
const request = { pci: deviceData.device_name };
|
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
|
// check resource approval
|
||||||
if (!await approveResources(req, userObj, request, params.node)) {
|
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();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// check node availability
|
// check node availability
|
||||||
const nodeAvailPci = await global.pve.getNodeAvailDevices(params.node, req.cookies);
|
if (!Object.values(node.devices).some(element => element.device_id.split(".")[0] === params.device && element.reserved === false)) {
|
||||||
if (!nodeAvailPci.some(element => element.id.split(".")[0] === params.device)) {
|
|
||||||
res.status(500).send({ error: `Device ${params.device} is already in use on ${params.node}.` });
|
res.status(500).send({ error: `Device ${params.device} is already in use on ${params.node}.` });
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
@ -123,18 +128,9 @@ router.post("/:hostpci/modify", async (req, res) => {
|
|||||||
const action = {};
|
const action = {};
|
||||||
action[`hostpci${params.hostpci}`] = `${params.device},pcie=${params.pcie}`;
|
action[`hostpci${params.hostpci}`] = `${params.device},pcie=${params.pcie}`;
|
||||||
// commit action
|
// commit action
|
||||||
const rootauth = await global.pve.requestPVE("/access/ticket", "POST", null, global.config.backends.pve.config.root);
|
const result = await global.pve.requestPVE(`${vmpath}/config`, "POST", { root: true }, action);
|
||||||
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);
|
|
||||||
await global.pve.handleResponse(params.node, result, res);
|
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: {request: Object, error: string}
|
||||||
* - 500: PVE Task Object
|
* - 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);
|
req.params = Object.assign({}, req.routeparams, req.params);
|
||||||
const params = {
|
const params = {
|
||||||
node: req.params.node,
|
node: req.params.node,
|
||||||
type: req.params.type,
|
type: req.params.type,
|
||||||
vmid: req.params.vmid,
|
vmid: req.params.vmid,
|
||||||
|
hostpci: Number(req.params.hostpci.replace("hostpci", "")),
|
||||||
device: req.body.device,
|
device: req.body.device,
|
||||||
pcie: req.body.pcie
|
pcie: req.body.pcie
|
||||||
};
|
};
|
||||||
|
// check hostpci is a valid number
|
||||||
const userObj = global.utils.getUserObjFromUsername(req.cookies.username);
|
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
|
// check if type is qemu
|
||||||
if (params.type !== "qemu") {
|
if (params.type !== "qemu") {
|
||||||
res.status(500).send({ error: "Type must be qemu (vm)." });
|
res.status(500).send({ error: "Type must be qemu (vm)." });
|
||||||
@ -177,46 +177,38 @@ router.post("/create", async (req, res) => {
|
|||||||
}
|
}
|
||||||
// force all functions
|
// force all functions
|
||||||
params.device = params.device.split(".")[0];
|
params.device = params.device.split(".")[0];
|
||||||
// get instance config to find next available hostpci slot
|
// device must not exist to be added
|
||||||
const config = (await global.pve.requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { token: true })).data.data;
|
const existingDevice = await global.pve.getDevice(params.node, params.vmid, params.hostpci);
|
||||||
let hostpci = 0;
|
if (existingDevice) {
|
||||||
while (config[`hostpci${hostpci}`]) {
|
res.status(500).send({ error: `Existing device in hostpci${params.hostpci}.` });
|
||||||
hostpci++;
|
res.end();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
// setup request
|
// setup request
|
||||||
const deviceData = await global.pve.getDeviceInfo(params.node, params.device);
|
const node = await global.pve.getNode(params.node);
|
||||||
const request = {
|
const requestedDevice = node.devices[`${params.device}`];
|
||||||
pci: deviceData.device_name
|
const request = { pci: requestedDevice.device_name };
|
||||||
};
|
|
||||||
// check resource approval
|
// check resource approval
|
||||||
|
const userObj = global.utils.getUserObjFromUsername(req.cookies.username);
|
||||||
if (!await approveResources(req, userObj, request, params.node)) {
|
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();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// check node availability
|
// check node availability
|
||||||
const nodeAvailPci = await global.pve.getNodeAvailDevices(params.node, req.cookies);
|
// const node = await global.pve.getNode(params.node);
|
||||||
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.status(500).send({ error: `Device ${params.device} is already in use on ${params.node}.` });
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// setup action
|
// setup action
|
||||||
const action = {};
|
const action = {};
|
||||||
action[`hostpci${hostpci}`] = `${params.device},pcie=${params.pcie}`;
|
action[`hostpci${params.hostpci}`] = `${params.device},pcie=${params.pcie}`;
|
||||||
// commit action
|
// commit action
|
||||||
const rootauth = await global.pve.requestPVE("/access/ticket", "POST", null, global.config.backends.pve.config.root);
|
const result = await global.pve.requestPVE(`${vmpath}/config`, "POST", { root: true }, action);
|
||||||
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);
|
|
||||||
await global.pve.handleResponse(params.node, result, res);
|
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,
|
node: req.params.node,
|
||||||
type: req.params.type,
|
type: req.params.type,
|
||||||
vmid: req.params.vmid,
|
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
|
// check if type is qemu
|
||||||
if (params.type !== "qemu") {
|
if (params.type !== "qemu") {
|
||||||
res.status(500).send({ error: "Type must be qemu (vm)." });
|
res.status(500).send({ error: "Type must be qemu (vm)." });
|
||||||
@ -253,8 +251,8 @@ router.delete("/:hostpci/delete", async (req, res) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// check device is in instance config
|
// check device is in instance config
|
||||||
const config = (await global.pve.requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
|
const device = global.pve.getDevice(params.node, params.vmid, params.hostpci);
|
||||||
if (!config[`hostpci${params.hostpci}`]) {
|
if (!device) {
|
||||||
res.status(500).send({ error: `Could not find hostpci${params.hostpci} in ${params.vmid}.` });
|
res.status(500).send({ error: `Could not find hostpci${params.hostpci} in ${params.vmid}.` });
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
@ -262,16 +260,7 @@ router.delete("/:hostpci/delete", async (req, res) => {
|
|||||||
// setup action
|
// setup action
|
||||||
const action = { delete: `hostpci${params.hostpci}` };
|
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
|
// 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);
|
const result = await global.pve.requestPVE(`${vmpath}/config`, "POST", { root: true }, action);
|
||||||
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);
|
|
||||||
await global.pve.handleResponse(params.node, result, res);
|
await global.pve.handleResponse(params.node, result, res);
|
||||||
|
await global.pve.syncNode(params.node);
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user