2023-02-28 23:24:49 +00:00
|
|
|
const axios = require('axios');
|
|
|
|
const {pveAPI, pveAPIToken} = require("./vars.js");
|
|
|
|
|
|
|
|
async function checkAuth (cookies, vmpath = null) {
|
|
|
|
if (vmpath) {
|
|
|
|
let result = await requestPVE(`/${vmpath}/config`, "GET", cookies);
|
|
|
|
return result.status === 200;
|
|
|
|
}
|
|
|
|
else { // if no path is specified, then do a simple authentication
|
|
|
|
let result = await requestPVE("/version", "GET", cookies);
|
|
|
|
return result.status === 200;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function requestPVE (path, method, cookies, body = null, token = null) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function handleResponse (node, response) {
|
|
|
|
const waitFor = delay => new Promise(resolve => setTimeout(resolve, delay));
|
|
|
|
if (response.data.data) {
|
|
|
|
let upid = response.data.data;
|
|
|
|
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") {
|
|
|
|
return {status: 200, data: taskStatus.data.data};
|
|
|
|
}
|
|
|
|
else if (taskStatus.data.data.status === "stopped") {
|
|
|
|
return {status: 500, data: taskStatus.data.data};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
await waitFor(1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-01 00:38:09 +00:00
|
|
|
async function getUnusedDiskData (node, type, vmid, disk) {
|
|
|
|
let diskDataConfig = await getDiskConfig(node, type, vmid, disk);
|
|
|
|
let storageID = diskDataConfig.split(":")[0];
|
2023-02-28 23:24:49 +00:00
|
|
|
let storageData = await requestPVE(`/nodes/${node}/storage/${storageID}/content`, "GET", null, null, pveAPIToken);
|
2023-03-01 00:38:09 +00:00
|
|
|
let diskDataStorage = null;
|
|
|
|
storageData.data.data.forEach((element) => {
|
|
|
|
console.log(element)
|
|
|
|
if (element.volid === diskDataConfig) {
|
|
|
|
console.log("match!!!")
|
|
|
|
element.storage = storageID;
|
|
|
|
diskDataStorage = element;
|
2023-02-28 23:24:49 +00:00
|
|
|
}
|
|
|
|
});
|
2023-03-01 00:38:09 +00:00
|
|
|
return diskDataStorage;
|
2023-02-28 23:24:49 +00:00
|
|
|
}
|
|
|
|
|
2023-02-28 23:33:00 +00:00
|
|
|
async function getDiskConfig (node, type, vmid, disk) {
|
2023-02-28 23:24:49 +00:00
|
|
|
let config = await requestPVE(`/nodes/${node}/${type}/${vmid}/config`, "GET", null, null, pveAPIToken);
|
|
|
|
|
|
|
|
return config.data.data[disk];
|
|
|
|
}
|
|
|
|
|
2023-02-28 23:33:00 +00:00
|
|
|
module.exports = {checkAuth, requestPVE, handleResponse, getUnusedDiskData, getDiskConfig};
|