almost finish implementing resource check

Signed-off-by: Arthur Lu <learthurgo@gmail.com>
This commit is contained in:
Arthur Lu 2023-03-01 00:50:24 +00:00
parent 1f25cc1cd1
commit cf418bc610
2 changed files with 33 additions and 3 deletions

5
db.js
View File

@ -25,7 +25,10 @@ function init () {
function requestResources (user, resources) { function requestResources (user, resources) {
let approved = true; let approved = true;
Object.keys(resources).forEach((element) => { Object.keys(resources).forEach((element) => {
if (db[user][element] - resources[element] < 0) { if(!db[user][element]) { // if the resource does not exist in the user's entry, assume the user is not allowed to use it
approved = false;
}
else if (db[user][element] - resources[element] < 0) {
approved = false; approved = false;
} }
}); });

29
main.js
View File

@ -263,7 +263,6 @@ app.post("/api/resources", async (req, res) => {
let currentConfig = await requestPVE(`${vmpath}/config`, "GET", null, null, pveAPIToken); let currentConfig = await requestPVE(`${vmpath}/config`, "GET", null, null, pveAPIToken);
let request = {cores: req.body.cores - currentConfig.data.data.cores, memory: req.body.memory - currentConfig.data.data.memory}; let request = {cores: req.body.cores - currentConfig.data.data.cores, memory: req.body.memory - currentConfig.data.data.memory};
if (!requestResources(req.cookies.username, request)) { // check resource approval if (!requestResources(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;
@ -274,6 +273,7 @@ app.post("/api/resources", async (req, res) => {
let result = await requestPVE(`${vmpath}/config`, method, req.cookies, action, pveAPIToken); let result = await requestPVE(`${vmpath}/config`, method, req.cookies, action, pveAPIToken);
result = await handleResponse(req.body.node, result); result = await handleResponse(req.body.node, result);
// update resources
if (result.status === 200) { if (result.status === 200) {
allocateResources(req.cookies.username, request); allocateResources(req.cookies.username, request);
} }
@ -289,6 +289,9 @@ app.post("/api/instance", async (req, res) => {
return; return;
} }
// setup request
let request = {cores: req.body.cores, memory: 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];
@ -309,13 +312,27 @@ app.post("/api/instance", async (req, res) => {
action.password = req.body.password; action.password = req.body.password;
action.ostemplate = req.body.ostemplate; action.ostemplate = req.body.ostemplate;
action.rootfs = `${req.body.rootfslocation}:${req.body.rootfssize}`; action.rootfs = `${req.body.rootfslocation}:${req.body.rootfssize}`;
request[req.body.rootfslocation] = req.body.rootfssize;
} }
else { else {
action.name = req.body.name; action.name = req.body.name;
} }
// check resource approval
if (!requestResources(req.cookies.username, request)) { // check resource approval
res.status(500).send({auth: auth, data:{request: request, error: `Not enough resources to satisfy request.`}});
return;
}
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); result = await handleResponse(req.body.node, result);
//update resources
if (result.status === 200) {
allocateResources(req.cookies.username, request);
}
res.status(result.status).send({auth: auth, data: result.data}); res.status(result.status).send({auth: auth, data: result.data});
}); });
@ -329,8 +346,18 @@ app.delete("/api/instance", async (req, res) => {
return; return;
} }
// 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 result = await requestPVE(`${vmpath}`, "DELETE", req.cookies, null, pveAPIToken); let result = await requestPVE(`${vmpath}`, "DELETE", req.cookies, null, pveAPIToken);
result = await handleResponse(req.body.node, result); 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}); res.status(result.status).send({auth: auth, data: result.data});
}); });