From 81d2841b79e0aa24e1f21f62702b1061b091b511 Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Thu, 29 Jun 2023 02:07:57 +0000 Subject: [PATCH] improve db format for templates, add additional per user customization for instance network interfaces, update tempalte localdb --- config/localdb.json.template | 52 +++++++++++++++++++++++++----------- src/main.js | 26 +++++++++--------- 2 files changed, 49 insertions(+), 29 deletions(-) diff --git a/config/localdb.json.template b/config/localdb.json.template index 1a38689..fbf2d0c 100644 --- a/config/localdb.json.template +++ b/config/localdb.json.template @@ -88,31 +88,32 @@ } }, "users": { - "exampleuser@authrealm": { + "exampleuser@examplepool": { "resources": { "max": { "cpu": ["kvm64", "host"], - "cores": 0, - "memory": 0, - "swap": 0, - "local": 0, - "cephpl": 0, - "network": 0, - "pci": ["[GeForce GTX 1070]", "[GeForce GTX 1080 Ti]"] + "cores": 128, + "memory": 131072, + "swap": 131072, + "local": 1099511627776, + "cephpl": 1099511627776, + "network": 100000, + "pci": ["Device Name Matcher 1", "Device Name Matcher 2"] } }, "nodes": [ - "node1", - "node2" + "examplenode1", + "examplenode2" ], - "instances": { + "cluster": { "vmid": { - "min": 100, - "max": 199 + "min": 200, + "max": 299 }, - "pool": "exampleuserpool", - "vlan": "10", - "templates": { + "pool": "examplepool" + }, + "templates": { + "instances": { "lxc": { "net0": { "value": "name=eth0,bridge=vmbr0,ip=dhcp,ip6=dhcp,tag=10,type=veth,rate=1000", @@ -123,6 +124,10 @@ } }, "qemu": { + "cpu": { + "value": "host", + "resource": null + }, "net0": { "value": "virtio,bridge=vmbr0,tag=10,rate=1000", "resource": { @@ -131,6 +136,21 @@ } } } + }, + "network": { + "lxc": { + "type": "veth", + "bridge": "vmbr0", + "vlan": 10, + "ip": "dhcp", + "ip6": "dhcp" + }, + "qemu": { + "type": "virtio", + "bridge": "vmbr0", + "vlan": 10 + } + } } } diff --git a/src/main.js b/src/main.js index d571d8c..054f682 100644 --- a/src/main.js +++ b/src/main.js @@ -148,17 +148,17 @@ app.get("/api/user/config/resources", async (req, res) => { }); /** - * GET - get db user instance configuration + * GET - get db user cluster configuration * responses: * - 200: {pool: String, templates: {lxc: Object, qemu: Object}, vmid: {min: Number, max: Number}} * - 401: {auth: false, path: String} */ -app.get("/api/user/config/instances", async (req, res) => { +app.get("/api/user/config/cluster", async (req, res) => { // check auth let auth = await checkAuth(req.cookies, res); if (!auth) { return; } let config = db.getUserConfig(req.cookies.username); - res.status(200).send(config.instances) + res.status(200).send(config.cluster) }); /** @@ -510,13 +510,13 @@ app.post("/api/instance/network/create", async (req, res) => { return; } // setup action - let vlan = db.getUserConfig(req.cookies.username).instances.vlan; + let nc = db.getUserConfig(req.cookies.username).templates.network[req.body.type]; let action = {}; if (req.body.type === "lxc") { - action[`net${req.body.netid}`] = `name=${req.body.name},bridge=vmbr0,ip=dhcp,ip6=dhcp,tag=${vlan},type=veth,rate=${req.body.rate}`; + action[`net${req.body.netid}`] = `name=${req.body.name},bridge=${nc.bridge},ip=${nc.ip},ip6=${nc.ip6},tag=${nc.vlan},type=${nc.type},rate=${req.body.rate}`; } else { - action[`net${req.body.netid}`] = `virtio,bridge=vmbr0,tag=${vlan},rate=${req.body.rate}`; + action[`net${req.body.netid}`] = `${nc.type},bridge=${nc.bridge},tag=${nc.vlan},rate=${req.body.rate}`; } action = JSON.stringify(action); let method = req.body.type === "qemu" ? "POST" : "PUT"; @@ -943,8 +943,8 @@ app.post("/api/instance", async (req, res) => { // get user db config let user = await db.getUserConfig(req.cookies.username); let vmid = Number.parseInt(req.body.vmid); - let vmid_min = user.instances.vmid.min; - let vmid_max = user.instances.vmid.max; + let vmid_min = user.cluster.vmid.min; + let vmid_max = user.cluster.vmid.max; // check vmid is within allowed range if (vmid < vmid_min || vmid > vmid_max) { res.status(500).send({ error: `Requested vmid ${vmid} is out of allowed range [${vmid_min},${vmid_max}].` }); @@ -966,8 +966,8 @@ app.post("/api/instance", async (req, res) => { 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]; + for (let key of Object.keys(user.templates.instances[req.body.type])) { + let item = user.templates.instances[req.body.type][key]; if (item.resource) { if (request[item.resource.name]) { request[item.resource.name] += item.resource.amount; @@ -988,10 +988,10 @@ app.post("/api/instance", async (req, res) => { vmid: req.body.vmid, cores: Number(req.body.cores), memory: Number(req.body.memory), - pool: user.instances.pool + pool: user.cluster.pool }; - for (let key of Object.keys(user.instances.templates[req.body.type])) { - action[key] = user.instances.templates[req.body.type][key].value; + for (let key of Object.keys(user.templates.instances[req.body.type])) { + action[key] = user.templates.instances[req.body.type][key].value; } if (req.body.type === "lxc") { action.hostname = req.body.name;