From fd7dec6af367ce7afbfe5d9a19c1956401ccfcb1 Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Fri, 19 May 2023 20:50:07 +0000 Subject: [PATCH] update localdb.json.template, fix tempalte resource checking for post /instance Signed-off-by: Arthur Lu --- localdb.json.template | 40 ++++++++++++++++++++-------- main.js | 61 +++++++++++++++++++++++++++---------------- 2 files changed, 67 insertions(+), 34 deletions(-) diff --git a/localdb.json.template b/localdb.json.template index 67f05a9..aaf2c82 100644 --- a/localdb.json.template +++ b/localdb.json.template @@ -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 + } + } } } } diff --git a/main.js b/main.js index ac6a947..ba7af24 100644 --- a/main.js +++ b/main.js @@ -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; } - else { - action.name = req.body.name; + 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 { + 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);