improve db format for templates,

add additional per user customization for instance network interfaces,
update tempalte localdb
This commit is contained in:
Arthur Lu 2023-06-29 02:07:57 +00:00
parent 7719911d5a
commit 81d2841b79
2 changed files with 49 additions and 29 deletions

View File

@ -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
}
}
}
}

View File

@ -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;