update localdb.json.template,

fix tempalte resource checking for post /instance

Signed-off-by: Arthur Lu <learthurgo@gmail.com>
This commit is contained in:
Arthur Lu 2023-05-19 20:50:07 +00:00
parent c45f582fc1
commit e18c10ac35
2 changed files with 67 additions and 34 deletions

View File

@ -3,24 +3,28 @@
"cores": {
"type": "numeric",
"multiplier": 1,
"base": 1024,
"compact": false,
"unit": "Cores"
},
"memory": {
"type": "numeric",
"multiplier": 1048576,
"base": 1024,
"compact": true,
"unit": "B"
},
"swap": {
"type": "numeric",
"multiplier": 1048576,
"base": 1024,
"compact": true,
"unit": "B"
},
"local": {
"type": "storage",
"multiplier": 1,
"base": 1024,
"compact": true,
"unit": "B",
"disks": [
@ -33,6 +37,7 @@
"cephpl": {
"type": "storage",
"multiplier": 1,
"base": 1024,
"compact": true,
"unit": "B",
"disks": [
@ -45,25 +50,26 @@
"network": {
"type": "network",
"multiplier": 1000000,
"base": 1000,
"compact": true,
"unit": "MB/s"
"unit": "B/s"
}
},
"users": {
"exampleuser@realm": {
"resources": {
"max": {
"cores": 128,
"memory": 131072,
"swap": 131072,
"local": 1099511627776,
"cephpl": 1099511627776,
"network": 100000
"cores": 0,
"memory": 0,
"swap": 0,
"local": 0,
"cephpl": 0,
"network": 0
}
},
"nodes": [
"node1",
"node2"
"node-0-id",
"node-1-id"
],
"instances": {
"vmid": {
@ -73,10 +79,22 @@
"pool": "examplepool",
"templates": {
"lxc": {
"net0": "name=eth0,bridge=vmbr0,ip=dhcp,ip6=dhcp,tag=10,type=veth,rate=1000"
"net0": {
"value": "name=eth0,bridge=vmbr0,ip=dhcp,ip6=dhcp,tag=10,type=veth,rate=1000",
"resource": {
"name": "network",
"amount": 1000
}
}
},
"qemu": {
"net0": "virtio,bridge=vmbr0,tag=10,rate=1000"
"net0": {
"value": "virtio,bridge=vmbr0,tag=10,rate=1000",
"resource": {
"name": "network",
"amount": 1000
}
}
}
}
}

59
main.js
View File

@ -513,12 +513,7 @@ app.post("/api/instance", async (req, res) => {
// check auth
let auth = await checkAuth(req.cookies, res);
if (!auth) { return; }
// setup request
let request = {
cores: Number(req.body.cores),
memory: Number(req.body.memory)
};
// setup action
// get user db config
let user = await getUserConfig(req.cookies.username);
let vmid = Number.parseInt(req.body.vmid);
let vmid_min = user.instances.vmid.min;
@ -535,27 +530,25 @@ app.post("/api/instance", async (req, res) => {
res.end();
return;
}
let action = {
vmid: req.body.vmid,
cores: req.body.cores,
memory: req.body.memory,
pool: user.instances.pool
// setup request
let request = {
cores: Number(req.body.cores),
memory: Number(req.body.memory)
};
for (let key of Object.keys(user.instances.templates[req.body.type])) {
action[key] = user.instances.templates[req.body.type][key];
}
if (req.body.type === "lxc") {
action.swap = req.body.swap;
action.hostname = req.body.name;
action.unprivileged = 1;
action.features = "nesting=1";
action.password = req.body.password;
action.ostemplate = req.body.ostemplate;
action.rootfs = `${req.body.rootfslocation}:${req.body.rootfssize}`;
request.swap = req.body.swap;
request[req.body.rootfslocation] = req.body.rootfssize;
}
for (let key of Object.keys(user.instances.templates[req.body.type])) {
let item = user.instances.templates[req.body.type][key];
if (item.resource) {
if (request[item.resource.name]) {
request[item.resource.name] += item.resource.amount;
}
else {
action.name = req.body.name;
request[item.resource.name] = item.resource.amount;
}
}
}
// check resource approval
if (!await approveResources(req, req.cookies.username, request)) { // check resource approval
@ -563,6 +556,28 @@ app.post("/api/instance", async (req, res) => {
res.end();
return;
}
// setup action by adding non resource values
let action = {
vmid: req.body.vmid,
cores: Number(req.body.cores),
memory: Number(req.body.memory),
pool: user.instances.pool
};
for (let key of Object.keys(user.instances.templates[req.body.type])) {
action[key] = user.instances.templates[req.body.type][key].value;
}
if (req.body.type === "lxc") {
action.hostname = req.body.name;
action.unprivileged = 1;
action.features = "nesting=1";
action.password = req.body.password;
action.ostemplate = req.body.ostemplate;
action.rootfs = `${req.body.rootfslocation}:${req.body.rootfssize}`;
}
else {
action.name = req.body.name;
}
console.log(action)
action = JSON.stringify(action);
// commit action
let result = await requestPVE(`/nodes/${req.body.node}/${req.body.type}`, "POST", req.cookies, action, pveAPIToken);