2023-04-26 01:18:05 +00:00
|
|
|
import axios from 'axios';
|
|
|
|
import { pveAPI, pveAPIToken } from "./vars.js";
|
2023-02-28 23:24:49 +00:00
|
|
|
|
2023-05-16 16:50:24 +00:00
|
|
|
export async function requestPVE(path, method, cookies, body = null, token = null) {
|
2023-02-28 23:24:49 +00:00
|
|
|
let url = `${pveAPI}${path}`;
|
|
|
|
let content = {
|
|
|
|
method: method,
|
|
|
|
mode: "cors",
|
|
|
|
credentials: "include",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/x-www-form-urlencoded"
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if (token) {
|
|
|
|
content.headers.Authorization = `PVEAPIToken=${token.user}@${token.realm}!${token.id}=${token.uuid}`;
|
|
|
|
}
|
|
|
|
else if (cookies) {
|
|
|
|
content.headers.CSRFPreventionToken = cookies.CSRFPreventionToken;
|
|
|
|
content.headers.Cookie = `PVEAuthCookie=${cookies.PVEAuthCookie}; CSRFPreventionToken=${cookies.CSRFPreventionToken}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (body) {
|
|
|
|
content.data = JSON.parse(body);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
let response = await axios.request(url, content);
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
return error.response;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-16 16:50:24 +00:00
|
|
|
export async function handleResponse(node, result, res) {
|
2023-02-28 23:24:49 +00:00
|
|
|
const waitFor = delay => new Promise(resolve => setTimeout(resolve, delay));
|
2023-06-03 00:10:33 +00:00
|
|
|
if (result.data.data && typeof(result.data.data) === "string" && result.data.data.startsWith("UPID:")) {
|
2023-04-19 01:03:55 +00:00
|
|
|
let upid = result.data.data;
|
2023-02-28 23:24:49 +00:00
|
|
|
while (true) {
|
|
|
|
let taskStatus = await requestPVE(`/nodes/${node}/tasks/${upid}/status`, "GET", null, null, pveAPIToken);
|
|
|
|
if (taskStatus.data.data.status === "stopped" && taskStatus.data.data.exitstatus === "OK") {
|
2023-05-18 08:09:18 +00:00
|
|
|
let result = taskStatus.data.data;
|
2023-05-19 20:03:38 +00:00
|
|
|
let taskLog = await requestPVE(`/nodes/${node}/tasks/${upid}/log`, "GET", null, null, pveAPIToken);
|
2023-05-18 08:09:18 +00:00
|
|
|
result.log = taskLog.data.data;
|
|
|
|
res.status(200).send(result);
|
2023-04-19 01:03:55 +00:00
|
|
|
res.end();
|
|
|
|
return;
|
2023-02-28 23:24:49 +00:00
|
|
|
}
|
|
|
|
else if (taskStatus.data.data.status === "stopped") {
|
2023-05-18 08:09:18 +00:00
|
|
|
let result = taskStatus.data.data;
|
2023-05-19 20:03:38 +00:00
|
|
|
let taskLog = await requestPVE(`/nodes/${node}/tasks/${upid}/log`, "GET", null, null, pveAPIToken);
|
2023-05-18 08:09:18 +00:00
|
|
|
result.log = taskLog.data.data;
|
|
|
|
res.status(500).send(result);
|
2023-04-19 01:03:55 +00:00
|
|
|
res.end();
|
|
|
|
return;
|
2023-02-28 23:24:49 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
await waitFor(1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2023-04-19 01:03:55 +00:00
|
|
|
res.status(result.status).send(result.data);
|
2023-05-18 08:09:18 +00:00
|
|
|
res.end();
|
2023-02-28 23:24:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-16 16:50:24 +00:00
|
|
|
export async function getUsedResources(req, resourceMeta) {
|
2023-04-19 01:03:55 +00:00
|
|
|
let response = await requestPVE("/cluster/resources", "GET", req.cookies);
|
|
|
|
let used = {};
|
|
|
|
let diskprefixes = [];
|
|
|
|
for (let resourceName of Object.keys(resourceMeta)) {
|
2023-04-20 20:43:05 +00:00
|
|
|
used[resourceName] = 0;
|
|
|
|
if (resourceMeta[resourceName].type === "storage") {
|
|
|
|
for (let diskPrefix of resourceMeta[resourceName].disks) {
|
|
|
|
diskprefixes.push(diskPrefix);
|
|
|
|
}
|
2023-04-19 01:03:55 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-26 01:18:05 +00:00
|
|
|
for (let instance of response.data.data) {
|
2023-04-19 01:03:55 +00:00
|
|
|
if (instance.type === "lxc" || instance.type === "qemu") {
|
|
|
|
let config = await requestPVE(`/nodes/${instance.node}/${instance.type}/${instance.vmid}/config`, "GET", req.cookies);
|
|
|
|
config = config.data.data;
|
2023-04-26 01:18:05 +00:00
|
|
|
for (let key of Object.keys(config)) {
|
2023-05-16 16:50:24 +00:00
|
|
|
if (Object.keys(used).includes(key) && resourceMeta[key].type === "numeric") {
|
|
|
|
used[key] += Number(config[key]);
|
2023-04-19 01:03:55 +00:00
|
|
|
}
|
|
|
|
else if (diskprefixes.some(prefix => key.startsWith(prefix))) {
|
|
|
|
let diskInfo = await getDiskInfo(instance.node, instance.type, instance.vmid, key);
|
2023-05-24 22:21:00 +00:00
|
|
|
if (diskInfo) { // only count if disk exists
|
|
|
|
used[diskInfo.storage] += Number(diskInfo.size);
|
|
|
|
}
|
2023-05-16 16:50:24 +00:00
|
|
|
}
|
|
|
|
else if (key.startsWith("net")) {
|
2023-05-24 22:21:00 +00:00
|
|
|
if (config[key].includes("rate=")) { // only count instances with a rate limit
|
|
|
|
used.network += Number(config[key].split("rate=")[1].split(",")[0]);
|
|
|
|
}
|
2023-04-19 01:03:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return used;
|
|
|
|
}
|
|
|
|
|
2023-05-16 16:50:24 +00:00
|
|
|
export async function getDiskInfo(node, type, vmid, disk) {
|
2023-05-18 08:09:18 +00:00
|
|
|
try {
|
|
|
|
let config = await requestPVE(`/nodes/${node}/${type}/${vmid}/config`, "GET", null, null, pveAPIToken);
|
|
|
|
let storageID = config.data.data[disk].split(":")[0];
|
|
|
|
let volID = config.data.data[disk].split(",")[0];
|
|
|
|
let volInfo = await requestPVE(`/nodes/${node}/storage/${storageID}/content/${volID}`, "GET", null, null, pveAPIToken);
|
|
|
|
volInfo.data.data.storage = storageID;
|
|
|
|
return volInfo.data.data;
|
|
|
|
}
|
|
|
|
catch {
|
|
|
|
return null;
|
|
|
|
}
|
2023-04-26 01:18:05 +00:00
|
|
|
}
|