fully implement new db
This commit is contained in:
parent
dacbd965cf
commit
5862edcf24
41
db.js
41
db.js
@ -16,25 +16,48 @@ function init () {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getResourceMeta () {
|
function load () {
|
||||||
return db.resources;
|
db = JSON.parse(fs.readFileSync(filename));
|
||||||
}
|
}
|
||||||
|
|
||||||
function getUserMax (username) {
|
function save () {
|
||||||
return db.users[username].maximum;
|
fs.writeFileSync(filename, JSON.stringify(db));
|
||||||
|
}
|
||||||
|
|
||||||
|
function getResourceMeta () {
|
||||||
|
return db["resource-metadata"];
|
||||||
}
|
}
|
||||||
|
|
||||||
function getResourceUnits () {
|
function getResourceUnits () {
|
||||||
return db.units;
|
return db["resource-units"];
|
||||||
}
|
}
|
||||||
|
|
||||||
function putUserResources (username, used) {
|
function getUser (username) {
|
||||||
|
return db.users[username];
|
||||||
|
}
|
||||||
|
|
||||||
|
function setUsedResources (username, used) {
|
||||||
let userEntry = db.users[username];
|
let userEntry = db.users[username];
|
||||||
userEntry.used = used;
|
userEntry.used = used;
|
||||||
userEntry.avail = {};
|
userEntry.avail = {};
|
||||||
Object.keys(max).forEach((k) => {
|
Object.keys(userEntry.max).forEach((k) => {
|
||||||
userEntry.avail[k] = max[k] - used[k];
|
userEntry.avail[k] = userEntry.max[k] - userEntry.used[k];
|
||||||
});
|
});
|
||||||
|
save();
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {init, getResourceMeta, getUserMax, getResourceUnits, putUserResources};
|
async function approveResources (username, request) {
|
||||||
|
let approved = true;
|
||||||
|
let avail = db.users[username].avail;
|
||||||
|
Object.keys(request).forEach((key) => {
|
||||||
|
if (!(key in avail)) {
|
||||||
|
approved = false;
|
||||||
|
}
|
||||||
|
else if (avail[key] - request[key] < 0) {
|
||||||
|
approved = false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return approved;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {init, getResourceMeta, getResourceUnits, getUser, setUsedResources, approveResources};
|
287
main.js
287
main.js
@ -7,8 +7,8 @@ const morgan = require("morgan");
|
|||||||
var api = require("./package.json");
|
var api = require("./package.json");
|
||||||
|
|
||||||
const {pveAPIToken, listenPort, domain} = require("./vars.js");
|
const {pveAPIToken, listenPort, domain} = require("./vars.js");
|
||||||
const {checkAuth, requestPVE, handleResponse, getUsedResources} = require("./pveutils.js");
|
const {checkAuth, requestPVE, handleResponse, getUsedResources, getDiskInfo} = require("./pveutils.js");
|
||||||
const {init, getResourceMeta, getUserMax, getResourceUnits} = require("./db.js");
|
const {init, getResourceMeta, getUser, approveResources, setUsedResources} = require("./db.js");
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
app.use(helmet());
|
app.use(helmet());
|
||||||
@ -44,21 +44,18 @@ app.post("/api/proxmox/*", async (req, res) => { // proxy endpoint for POST prox
|
|||||||
});
|
});
|
||||||
|
|
||||||
app.get("/api/user/resources", async(req, res) => {
|
app.get("/api/user/resources", async(req, res) => {
|
||||||
|
// check auth
|
||||||
await checkAuth(req.cookies, res);
|
await checkAuth(req.cookies, res);
|
||||||
let rm = getResourceMeta();
|
let used = await getUsedResources(req, getResourceMeta());
|
||||||
let used = await getUsedResources(req, rm);
|
setUsedResources(req.cookies.username, used);
|
||||||
let max = await getUserMax(req.cookies.username);
|
let user = await getUser(req.cookies.username);
|
||||||
avail = {};
|
res.status(200).send(user);
|
||||||
Object.keys(max).forEach((k) => {
|
res.end();
|
||||||
avail[k] = max[k] - used[k];
|
|
||||||
});
|
|
||||||
let units = getResourceUnits();
|
|
||||||
res.status(200).send({used: used, maximum: max, available: avail, units: units});
|
|
||||||
return;
|
return;
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post("/api/disk/detach", async (req, res) => {
|
app.post("/api/disk/detach", async (req, res) => {
|
||||||
let vmpath = `/nodes/${req.body.node}/${req.body.type}/${req.body.vmid}`;
|
// check auth
|
||||||
await checkAuth(req.cookies, res);
|
await checkAuth(req.cookies, res);
|
||||||
if (req.body.disk.includes("unused")) {
|
if (req.body.disk.includes("unused")) {
|
||||||
res.status(500).send({auth: auth, data:{error: `Requested disk ${req.body.disk} cannot be unused. Use /disk/delete to permanently delete unused disks.`}});
|
res.status(500).send({auth: auth, data:{error: `Requested disk ${req.body.disk} cannot be unused. Use /disk/delete to permanently delete unused disks.`}});
|
||||||
@ -66,75 +63,61 @@ app.post("/api/disk/detach", async (req, res) => {
|
|||||||
}
|
}
|
||||||
let action = JSON.stringify({delete: req.body.disk});
|
let action = JSON.stringify({delete: req.body.disk});
|
||||||
let method = req.body.type === "qemu" ? "POST" : "PUT";
|
let method = req.body.type === "qemu" ? "POST" : "PUT";
|
||||||
let result = await requestPVE(`${vmpath}/config`, method, req.cookies, action, pveAPIToken);
|
let result = await requestPVE(`/nodes/${req.body.node}/${req.body.type}/${req.body.vmid}/config`, method, req.cookies, action, pveAPIToken);
|
||||||
await handleResponse(req.body.node, result, res);
|
await handleResponse(req.body.node, result, res);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post("/api/disk/attach", async (req, res) => {
|
app.post("/api/disk/attach", async (req, res) => {
|
||||||
let vmpath = `/nodes/${req.body.node}/${req.body.type}/${req.body.vmid}`;
|
// check auth
|
||||||
await checkAuth(req.cookies, res);
|
await checkAuth(req.cookies, res);
|
||||||
let action = {};
|
let action = {};
|
||||||
action[req.body.disk] = req.body.data;
|
action[req.body.disk] = req.body.data;
|
||||||
action = JSON.stringify(action);
|
action = JSON.stringify(action);
|
||||||
let method = req.body.type === "qemu" ? "POST" : "PUT";
|
let method = req.body.type === "qemu" ? "POST" : "PUT";
|
||||||
let result = await requestPVE(`${vmpath}/config`, method, req.cookies, action, pveAPIToken);
|
let result = await requestPVE(`/nodes/${req.body.node}/${req.body.type}/${req.body.vmid}/config`, method, req.cookies, action, pveAPIToken);
|
||||||
await handleResponse(req.body.node, result, res);
|
await handleResponse(req.body.node, result, res);
|
||||||
});
|
});
|
||||||
/*
|
|
||||||
app.post("/api/disk/resize", async (req, res) => {
|
app.post("/api/disk/resize", async (req, res) => {
|
||||||
let vmpath = `/nodes/${req.body.node}/${req.body.type}/${req.body.vmid}`;
|
|
||||||
|
|
||||||
// check auth
|
// check auth
|
||||||
let auth = await checkAuth(req.cookies, vmpath);
|
await checkAuth(req.cookies, res);
|
||||||
if (!auth) {
|
// update used resources
|
||||||
res.status(401).send({auth: auth});
|
let used = await getUsedResources(req, getResourceMeta());
|
||||||
return;
|
setUsedResources(req.cookies.username, used);
|
||||||
}
|
// check disk existence
|
||||||
|
let diskConfig = await getDiskInfo(req.body.node, req.body.type, req.body.vmid, req.body.disk); // get target disk
|
||||||
// check resource allocation
|
|
||||||
let diskConfig = await getDiskConfig(req.body.node, req.body.type, req.body.vmid, req.body.disk); // get target disk
|
|
||||||
if (!diskConfig) { // exit if disk does not exist
|
if (!diskConfig) { // exit if disk does not exist
|
||||||
res.status(500).send({auth: auth, data:{error: `requested disk ${req.body.disk} does not exist`}});
|
res.status(500).send({auth: auth, data:{error: `requested disk ${req.body.disk} does not exist`}});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let storage = diskConfig.split(":")[0]; // get the storage
|
let storage = diskConfig.storage; // get the storage
|
||||||
let request = {};
|
let request = {};
|
||||||
request[storage] = Number(req.body.size); // setup request object
|
request[storage] = Number(req.body.size * 1024 ** 3); // setup request object
|
||||||
if (!requestResources(req.cookies.username, request)) { // check request approval
|
// check request approval
|
||||||
res.status(500).send({auth: auth, data:{request: request, error: `Storage ${storage} could not fulfill request of size ${req.body.size}G.`}});
|
if (!await approveResources(req.cookies.username, request)) {
|
||||||
|
res.status(500).send({request: request, error: `Storage ${storage} could not fulfill request of size ${req.body.size}G.`});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// action approved, commit to action
|
||||||
let action = JSON.stringify({disk: req.body.disk, size: `+${req.body.size}G`});
|
let action = JSON.stringify({disk: req.body.disk, size: `+${req.body.size}G`});
|
||||||
let result = await requestPVE(`${vmpath}/resize`, "PUT", req.cookies, action, pveAPIToken);
|
let result = await requestPVE(`/nodes/${req.body.node}/${req.body.type}/${req.body.vmid}/resize`, "PUT", req.cookies, action, pveAPIToken);
|
||||||
result = await handleResponse(req.body.node, result);
|
await handleResponse(req.body.node, result, res);
|
||||||
|
|
||||||
// update resources
|
|
||||||
if (result.status === 200) {
|
|
||||||
allocateResources(req.cookies.username, request);
|
|
||||||
}
|
|
||||||
|
|
||||||
res.status(result.status).send({auth: auth, data: result.data, allocated: request});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post("/api/disk/move", async (req, res) => {
|
app.post("/api/disk/move", async (req, res) => {
|
||||||
let vmpath = `/nodes/${req.body.node}/${req.body.type}/${req.body.vmid}`;
|
|
||||||
let route = req.body.type === "qemu" ? "move_disk" : "move_volume";
|
|
||||||
|
|
||||||
// check auth
|
// check auth
|
||||||
let auth = await checkAuth(req.cookies, vmpath);
|
await checkAuth(req.cookies, res);
|
||||||
if (!auth) {
|
// update used resources
|
||||||
res.status(401).send({auth: auth});
|
let used = await getUsedResources(req, getResourceMeta());
|
||||||
|
setUsedResources(req.cookies.username, used);
|
||||||
|
// check disk existence
|
||||||
|
let diskConfig = await getDiskInfo(req.body.node, req.body.type, req.body.vmid, req.body.disk); // get target disk
|
||||||
|
if (!diskConfig) { // exit if disk does not exist
|
||||||
|
res.status(500).send({auth: auth, data:{error: `requested disk ${req.body.disk} does not exist`}});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
let size = parseInt(diskConfig.size); // get source disk size
|
||||||
// check resource allocation
|
let srcStorage = diskConfig.storage; // get source storage
|
||||||
let diskConfig = await getDiskConfig(req.body.node, req.body.type, req.body.vmid, req.body.disk); // get source disk
|
|
||||||
if (!diskConfig) { // exit if source disk does not exist
|
|
||||||
res.status(500).send({auth: auth, data:{error: `Requested disk ${req.body.disk} does not exist`}});
|
|
||||||
}
|
|
||||||
let size = parseInt(diskConfig.split("size=")[1].split("G")[0]); // get source disk size
|
|
||||||
let srcStorage = diskConfig.split(":")[0]; // get source storage
|
|
||||||
let dstStorage = req.body.storage; // get destination storage
|
let dstStorage = req.body.storage; // get destination storage
|
||||||
let request = {};
|
let request = {};
|
||||||
let release = {};
|
let release = {};
|
||||||
@ -142,8 +125,9 @@ app.post("/api/disk/move", async (req, res) => {
|
|||||||
release[srcStorage] = Number(size);
|
release[srcStorage] = Number(size);
|
||||||
}
|
}
|
||||||
request[dstStorage] = Number(size); // always decrease destination storage by size
|
request[dstStorage] = Number(size); // always decrease destination storage by size
|
||||||
if (!requestResources(req.cookies.username, request)) { // check resource approval
|
// check request approval
|
||||||
res.status(500).send({auth: auth, data:{request: request, release: release, error: `Storage ${dstStorage} could not fulfill request of size ${size}G.`}});
|
if (!await approveResources(req.cookies.username, request)) {
|
||||||
|
res.status(500).send({request: request, error: `Storage ${storage} could not fulfill request of size ${req.body.size}G.`});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,75 +139,44 @@ app.post("/api/disk/move", async (req, res) => {
|
|||||||
action.volume = req.body.disk
|
action.volume = req.body.disk
|
||||||
}
|
}
|
||||||
action = JSON.stringify(action);
|
action = JSON.stringify(action);
|
||||||
let result = await requestPVE(`${vmpath}/${route}`, "POST", req.cookies, action, pveAPIToken);
|
let route = req.body.type === "qemu" ? "move_disk" : "move_volume";
|
||||||
result = await handleResponse(req.body.node, result);
|
let result = await requestPVE(`/nodes/${req.body.node}/${req.body.type}/${req.body.vmid}/${route}`, "POST", req.cookies, action, pveAPIToken);
|
||||||
|
await handleResponse(req.body.node, result, res);
|
||||||
// update resources
|
|
||||||
if (result.status === 200) {
|
|
||||||
allocateResources(req.cookies.username, request);
|
|
||||||
releaseResources(req.cookies.username, release);
|
|
||||||
}
|
|
||||||
|
|
||||||
res.status(result.status).send({auth: auth, data: result.data, allocated: request, deallocated: release});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post("/api/disk/delete", async (req, res) => {
|
app.post("/api/disk/delete", async (req, res) => {
|
||||||
let vmpath = `/nodes/${req.body.node}/${req.body.type}/${req.body.vmid}`;
|
|
||||||
|
|
||||||
// check auth
|
// check auth
|
||||||
let auth = await checkAuth(req.cookies, vmpath);
|
await checkAuth(req.cookies, res);
|
||||||
if (!auth) {
|
// update used resources
|
||||||
res.status(401).send({auth: auth});
|
let used = await getUsedResources(req, getResourceMeta());
|
||||||
return;
|
setUsedResources(req.cookies.username, used);
|
||||||
}
|
// only ide or unused are allowed to be deleted
|
||||||
|
|
||||||
if (!req.body.disk.includes("unused") && !req.body.disk.includes("ide")) { // must be ide or unused
|
if (!req.body.disk.includes("unused") && !req.body.disk.includes("ide")) { // must be ide or unused
|
||||||
res.status(500).send({auth: auth, data:{error: `Requested disk ${req.body.disk} must be unused or ide. Use /disk/detach to detach disks in use.`}});
|
res.status(500).send({auth: auth, data:{error: `Requested disk ${req.body.disk} must be unused or ide. Use /disk/detach to detach disks in use.`}});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// setup release
|
|
||||||
let release = {};
|
|
||||||
if (!req.body.disk.includes("ide")) { // if disk type is ide, then dont bother checking for resources
|
|
||||||
let diskConfig = await getUnusedDiskData(req.body.node, req.body.type, req.body.vmid, req.body.disk); // get disk config of unused disk
|
|
||||||
let storage = diskConfig.storage; // get disk storage
|
|
||||||
let size = diskConfig.size / (1024**3); // get disk size
|
|
||||||
release[storage] = Number(size);
|
|
||||||
}
|
|
||||||
|
|
||||||
let action = JSON.stringify({delete: req.body.disk});
|
let action = JSON.stringify({delete: req.body.disk});
|
||||||
let method = req.body.type === "qemu" ? "POST" : "PUT";
|
let method = req.body.type === "qemu" ? "POST" : "PUT";
|
||||||
let result = await requestPVE(`${vmpath}/config`, method, req.cookies, action, pveAPIToken);
|
let result = await requestPVE(`/nodes/${req.body.node}/${req.body.type}/${req.body.vmid}/config`, method, req.cookies, action, pveAPIToken);
|
||||||
result = await handleResponse(req.body.node, result);
|
await handleResponse(req.body.node, result, res);
|
||||||
|
|
||||||
// update resources
|
|
||||||
if (result.status === 200) {
|
|
||||||
releaseResources(req.cookies.username, release);
|
|
||||||
}
|
|
||||||
|
|
||||||
res.status(result.status).send({auth: auth, data: result.data, deallocated: release});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post("/api/disk/create", async (req, res) => {
|
app.post("/api/disk/create", async (req, res) => {
|
||||||
let vmpath = `/nodes/${req.body.node}/${req.body.type}/${req.body.vmid}`;
|
|
||||||
|
|
||||||
// check auth
|
// check auth
|
||||||
let auth = await checkAuth(req.cookies, vmpath);
|
await checkAuth(req.cookies, res);
|
||||||
if (!auth) {
|
// update used resources
|
||||||
res.status(401).send({auth: auth});
|
let used = await getUsedResources(req, getResourceMeta());
|
||||||
return;
|
setUsedResources(req.cookies.username, used);
|
||||||
}
|
// check resource approval
|
||||||
|
|
||||||
// check resource allocation
|
|
||||||
let request = {};
|
let request = {};
|
||||||
if (!req.body.disk.includes("ide")) {
|
if (!req.body.disk.includes("ide")) {
|
||||||
request[req.body.storage] = Number(req.body.size); // setup request object
|
request[req.body.storage] = Number(req.body.size * 1024 ** 3); // setup request object
|
||||||
if (!requestResources(req.cookies.username, request)) { // check request approval
|
// check request approval
|
||||||
res.status(500).send({auth: auth, data:{request: request, error: `Storage ${storage} could not fulfill request of size ${req.body.size}G.`}});
|
if (!await approveResources(req.cookies.username, request)) {
|
||||||
|
res.status(500).send({request: request, error: `Storage ${storage} could not fulfill request of size ${req.body.size}G.`});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let action = {};
|
let action = {};
|
||||||
if (req.body.disk.includes("ide") && req.body.iso) {
|
if (req.body.disk.includes("ide") && req.body.iso) {
|
||||||
action[req.body.disk] = `${req.body.iso},media=cdrom`;
|
action[req.body.disk] = `${req.body.iso},media=cdrom`;
|
||||||
@ -236,64 +189,45 @@ app.post("/api/disk/create", async (req, res) => {
|
|||||||
}
|
}
|
||||||
action = JSON.stringify(action);
|
action = JSON.stringify(action);
|
||||||
let method = req.body.type === "qemu" ? "POST" : "PUT";
|
let method = req.body.type === "qemu" ? "POST" : "PUT";
|
||||||
let result = await requestPVE(`${vmpath}/config`, method, req.cookies, action, pveAPIToken);
|
let result = await requestPVE(`/nodes/${req.body.node}/${req.body.type}/${req.body.vmid}/config`, method, req.cookies, action, pveAPIToken);
|
||||||
result = await handleResponse(req.body.node, result);
|
await handleResponse(req.body.node, result, res);
|
||||||
|
|
||||||
// update resources
|
|
||||||
if (result.status === 200) {
|
|
||||||
allocateResources(req.cookies.username, request);
|
|
||||||
}
|
|
||||||
|
|
||||||
res.status(result.status).send({auth: auth, data: result.data, allocated: request});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post("/api/resources", async (req, res) => {
|
app.post("/api/resources", async (req, res) => {
|
||||||
let vmpath = `/nodes/${req.body.node}/${req.body.type}/${req.body.vmid}`;
|
|
||||||
|
|
||||||
// check auth
|
// check auth
|
||||||
let auth = await checkAuth(req.cookies, vmpath);
|
await checkAuth(req.cookies, res);
|
||||||
if (!auth) {
|
// update used resources
|
||||||
res.status(401).send({auth: auth});
|
let used = await getUsedResources(req, getResourceMeta());
|
||||||
return;
|
setUsedResources(req.cookies.username, used);
|
||||||
}
|
// get current config
|
||||||
|
let currentConfig = await requestPVE(`/nodes/${req.body.node}/${req.body.type}/${req.body.vmid}/config`, "GET", null, null, pveAPIToken);
|
||||||
let currentConfig = await requestPVE(`${vmpath}/config`, "GET", null, null, pveAPIToken);
|
|
||||||
let request = {
|
let request = {
|
||||||
cores: Number(req.body.cores) - Number(currentConfig.data.data.cores),
|
cores: Number(req.body.cores) - Number(currentConfig.data.data.cores),
|
||||||
memory: Number(req.body.memory) - Number(currentConfig.data.data.memory)
|
memory: Number(req.body.memory) - Number(currentConfig.data.data.memory)
|
||||||
};
|
};
|
||||||
if (!requestResources(req.cookies.username, request)) { // check resource approval
|
// check resource approval
|
||||||
res.status(500).send({auth: auth, data:{request: request, error: `Not enough resources to satisfy request.`}});
|
if (!await approveResources(req.cookies.username, request)) {
|
||||||
|
res.status(500).send({request: request, error: `Could not fulfil request`});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// commit action
|
||||||
let action = JSON.stringify({cores: req.body.cores, memory: req.body.memory});
|
let action = JSON.stringify({cores: req.body.cores, memory: req.body.memory});
|
||||||
let method = req.body.type === "qemu" ? "POST" : "PUT";
|
let method = req.body.type === "qemu" ? "POST" : "PUT";
|
||||||
let result = await requestPVE(`${vmpath}/config`, method, req.cookies, action, pveAPIToken);
|
let result = await requestPVE(`/nodes/${req.body.node}/${req.body.type}/${req.body.vmid}/config`, method, req.cookies, action, pveAPIToken);
|
||||||
result = await handleResponse(req.body.node, result);
|
await handleResponse(req.body.node, result, res);
|
||||||
|
|
||||||
// update resources
|
|
||||||
if (result.status === 200) {
|
|
||||||
allocateResources(req.cookies.username, request);
|
|
||||||
}
|
|
||||||
|
|
||||||
res.status(result.status).send({auth: auth, data: result.data, allocated: request});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post("/api/instance", async (req, res) => {
|
app.post("/api/instance", async (req, res) => {
|
||||||
// check auth
|
// check auth
|
||||||
let auth = await checkAuth(req.cookies);
|
await checkAuth(req.cookies, res);
|
||||||
if (!auth) {
|
// update used resources
|
||||||
res.status(401).send({auth: auth});
|
let used = await getUsedResources(req, getResourceMeta());
|
||||||
return;
|
setUsedResources(req.cookies.username, used);
|
||||||
}
|
|
||||||
|
|
||||||
// setup request
|
// setup request
|
||||||
let request = {
|
let request = {
|
||||||
cores: Number(req.body.cores),
|
cores: Number(req.body.cores),
|
||||||
memory: Number(req.body.memory)
|
memory: Number(req.body.memory)
|
||||||
};
|
};
|
||||||
|
|
||||||
// setup action
|
// setup action
|
||||||
let user = await requestPVE(`/access/users/${req.cookies.username}`, "GET", null, null, pveAPIToken);
|
let user = await requestPVE(`/access/users/${req.cookies.username}`, "GET", null, null, pveAPIToken);
|
||||||
let group = user.data.data.groups[0];
|
let group = user.data.data.groups[0];
|
||||||
@ -319,75 +253,24 @@ app.post("/api/instance", async (req, res) => {
|
|||||||
else {
|
else {
|
||||||
action.name = req.body.name;
|
action.name = req.body.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
// check resource approval
|
// check resource approval
|
||||||
if (!requestResources(req.cookies.username, request)) { // check resource approval
|
if (!approveResources(req.cookies.username, request)) { // check resource approval
|
||||||
res.status(500).send({auth: auth, data:{request: request, error: `Not enough resources to satisfy request.`}});
|
res.status(500).send({auth: auth, data:{request: request, error: `Not enough resources to satisfy request.`}});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// commit action
|
||||||
action = JSON.stringify(action);
|
action = JSON.stringify(action);
|
||||||
let result = await requestPVE(`/nodes/${req.body.node}/${req.body.type}`, "POST", req.cookies, action, pveAPIToken);
|
let result = await requestPVE(`/nodes/${req.body.node}/${req.body.type}`, "POST", req.cookies, action, pveAPIToken);
|
||||||
result = await handleResponse(req.body.node, result);
|
await handleResponse(req.body.node, result, res);
|
||||||
|
|
||||||
//update resources
|
|
||||||
if (result.status === 200) {
|
|
||||||
allocateResources(req.cookies.username, request);
|
|
||||||
}
|
|
||||||
|
|
||||||
res.status(result.status).send({auth: auth, data: result.data, allocated: request});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.delete("/api/instance", async (req, res) => {
|
app.delete("/api/instance", async (req, res) => {
|
||||||
let vmpath = `/nodes/${req.body.node}/${req.body.type}/${req.body.vmid}`;
|
|
||||||
|
|
||||||
// check auth
|
// check auth
|
||||||
let auth = await checkAuth(req.cookies, vmpath);
|
await checkAuth(req.cookies, res);
|
||||||
if (!auth) {
|
// commit action
|
||||||
res.status(401).send({auth: auth});
|
let result = await requestPVE(`/nodes/${req.body.node}/${req.body.type}/${req.body.vmid}`, "DELETE", req.cookies, null, pveAPIToken);
|
||||||
return;
|
await handleResponse(req.body.node, result, res);
|
||||||
}
|
});
|
||||||
|
|
||||||
// setup release TODO: add disk data here
|
|
||||||
let currentConfig = await requestPVE(`${vmpath}/config`, "GET", null, null, pveAPIToken);
|
|
||||||
let release = {cores: currentConfig.data.data.cores, memory: currentConfig.data.data.memory};
|
|
||||||
let keys = Object.keys(currentConfig.data.data);
|
|
||||||
for (let i = 0; i < keys.length; i++) {
|
|
||||||
let element = keys[i];
|
|
||||||
if (element.includes("sata") || element.includes("mp") || element.includes("rootfs")) { // if the disk is mounted, get its storage and size
|
|
||||||
let diskConfig = await getDiskConfig(req.body.node, req.body.type, req.body.vmid, element);
|
|
||||||
let storage = diskConfig.split(":")[0];
|
|
||||||
let size = parseInt(diskConfig.split("size=")[1].split("G")[0]);
|
|
||||||
if (!release[storage]) { // if this is the first time storage has been seen, set storage to size
|
|
||||||
release[storage] = size;
|
|
||||||
}
|
|
||||||
else { // otherwise incrment storage by size
|
|
||||||
release[storage] += size;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (element.includes("unused")) {
|
|
||||||
let diskConfig = await getUnusedDiskData(req.body.node, req.body.type, req.body.vmid, element); // get disk config of unused disk
|
|
||||||
let storage = diskConfig.storage; // get disk storage
|
|
||||||
let size = diskConfig.size / (1024**3); // get disk size
|
|
||||||
if (!release[storage]) { // if this is the first time storage has been seen, set storage to size
|
|
||||||
release[storage] = size;
|
|
||||||
}
|
|
||||||
else { // otherwise incrment storage by size
|
|
||||||
release[storage] += size;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let result = await requestPVE(`${vmpath}`, "DELETE", req.cookies, null, pveAPIToken);
|
|
||||||
result = await handleResponse(req.body.node, result);
|
|
||||||
|
|
||||||
//update resources
|
|
||||||
if (result.status === 200) {
|
|
||||||
releaseResources(req.cookies.username, release);
|
|
||||||
}
|
|
||||||
|
|
||||||
res.status(result.status).send({auth: auth, data: result.data, deallocated: release});
|
|
||||||
});*/
|
|
||||||
|
|
||||||
app.listen(listenPort, () => {
|
app.listen(listenPort, () => {
|
||||||
init();
|
init();
|
||||||
|
16
pveutils.js
16
pveutils.js
@ -114,16 +114,10 @@ async function getUsedResources (req, resourceMeta) {
|
|||||||
async function getDiskInfo (node, type, vmid, disk) {
|
async function getDiskInfo (node, type, vmid, disk) {
|
||||||
let config = await requestPVE(`/nodes/${node}/${type}/${vmid}/config`, "GET", null, null, pveAPIToken);
|
let config = await requestPVE(`/nodes/${node}/${type}/${vmid}/config`, "GET", null, null, pveAPIToken);
|
||||||
let storageID = config.data.data[disk].split(":")[0];
|
let storageID = config.data.data[disk].split(":")[0];
|
||||||
let volIDTarget = config.data.data[disk].split(",")[0];
|
let volID = config.data.data[disk].split(",")[0];
|
||||||
let storageData = await requestPVE(`/nodes/${node}/storage/${storageID}/content`, "GET", null, null, pveAPIToken);
|
let volInfo = await requestPVE(`/nodes/${node}/storage/${storageID}/content/${volID}`, "GET", null, null, pveAPIToken);
|
||||||
let diskInfo = null;
|
volInfo.data.data.storage = storageID;
|
||||||
storageData.data.data.forEach((element) => {
|
return volInfo.data.data;
|
||||||
if (element.volid === volIDTarget) {
|
|
||||||
element.storage = storageID;
|
|
||||||
diskInfo = element;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return diskInfo;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {checkAuth, requestPVE, handleResponse, getUsedResources};
|
module.exports = {checkAuth, requestPVE, handleResponse, getUsedResources, getDiskInfo};
|
Loading…
Reference in New Issue
Block a user