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

61
main.js
View File

@ -513,12 +513,7 @@ app.post("/api/instance", async (req, res) => {
// check auth // check auth
let auth = await checkAuth(req.cookies, res); let auth = await checkAuth(req.cookies, res);
if (!auth) { return; } if (!auth) { return; }
// setup request // get user db config
let request = {
cores: Number(req.body.cores),
memory: Number(req.body.memory)
};
// setup action
let user = await getUserConfig(req.cookies.username); let user = await getUserConfig(req.cookies.username);
let vmid = Number.parseInt(req.body.vmid); let vmid = Number.parseInt(req.body.vmid);
let vmid_min = user.instances.vmid.min; let vmid_min = user.instances.vmid.min;
@ -535,27 +530,25 @@ app.post("/api/instance", async (req, res) => {
res.end(); res.end();
return; return;
} }
let action = { // setup request
vmid: req.body.vmid, let request = {
cores: req.body.cores, cores: Number(req.body.cores),
memory: req.body.memory, 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];
}
if (req.body.type === "lxc") { if (req.body.type === "lxc") {
action.swap = req.body.swap; request.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[req.body.rootfslocation] = req.body.rootfssize; request[req.body.rootfslocation] = req.body.rootfssize;
} }
else { for (let key of Object.keys(user.instances.templates[req.body.type])) {
action.name = req.body.name; 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 {
request[item.resource.name] = item.resource.amount;
}
}
} }
// check resource approval // check resource approval
if (!await approveResources(req, req.cookies.username, request)) { // 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(); res.end();
return; 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); action = JSON.stringify(action);
// commit action // commit 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);