add pool selection to instance create,

allow multiple pools per user
This commit is contained in:
Arthur Lu 2024-04-08 21:19:57 +00:00
parent 3281788089
commit 98479205d9
3 changed files with 28 additions and 17 deletions

View File

@ -145,6 +145,7 @@ router.post(`${basePath}/create`, async (req, res) => {
vmid: req.params.vmid, vmid: req.params.vmid,
hostname: req.body.hostname, hostname: req.body.hostname,
name: req.body.name, name: req.body.name,
pool: req.body.pool,
cores: req.body.cores, cores: req.body.cores,
memory: req.body.memory, memory: req.body.memory,
swap: req.body.swap, swap: req.body.swap,
@ -206,7 +207,7 @@ router.post(`${basePath}/create`, async (req, res) => {
vmid: params.vmid, vmid: params.vmid,
cores: Number(params.cores), cores: Number(params.cores),
memory: Number(params.memory), memory: Number(params.memory),
pool: user.cluster.pool pool: params.pool // TODO allow user to select pool to assign VM
}; };
for (const key of Object.keys(user.templates.instances[params.type])) { for (const key of Object.keys(user.templates.instances[params.type])) {
action[key] = user.templates.instances[params.type][key].value; action[key] = user.templates.instances[params.type][key].value;

View File

@ -63,7 +63,7 @@ if (schemes.hash.enabled) {
// setup interupt scheme // setup interupt scheme
if (schemes.interrupt.enabled) { if (schemes.interrupt.enabled) {
const wsServer = new WebSocketServer({ noServer: true, path: "/api/sync/interrupt" }); const wsServer = new WebSocketServer({ noServer: true, path: "/api/sync/interrupt" });
wsServer.on("connection", (socket, username, pool) => { wsServer.on("connection", (socket, username, pools) => {
// add new socket to userSocketmap // add new socket to userSocketmap
if (userSocketMap[username]) { if (userSocketMap[username]) {
const index = Object.keys(userSocketMap[username]).length; const index = Object.keys(userSocketMap[username]).length;
@ -74,7 +74,8 @@ if (schemes.interrupt.enabled) {
userSocketMap[username] = { 0: socket }; userSocketMap[username] = { 0: socket };
socket.userIndex = 0; socket.userIndex = 0;
} }
// add user to associated pool in poolUserMap // for each pool, add user to associated pool in poolUserMap
for (const pool of pools) {
if (poolUserMap[pool]) { if (poolUserMap[pool]) {
poolUserMap[pool][username] = true; poolUserMap[pool][username] = true;
} }
@ -82,6 +83,7 @@ if (schemes.interrupt.enabled) {
poolUserMap[pool] = {}; poolUserMap[pool] = {};
poolUserMap[pool][username] = true; poolUserMap[pool][username] = true;
} }
}
// add socket entry into requestedRates // add socket entry into requestedRates
const index = Object.keys(requestedRates).length; const index = Object.keys(requestedRates).length;
requestedRates[index] = Infinity; requestedRates[index] = Infinity;
@ -96,13 +98,15 @@ if (schemes.interrupt.enabled) {
if (Object.keys(userSocketMap[username]).length === 0) { if (Object.keys(userSocketMap[username]).length === 0) {
// delete the user entry // delete the user entry
delete userSocketMap[username]; delete userSocketMap[username];
// remove user from poolUserMap pool entry // remove user from poolUserMap entry for each pool
for (const pool of pools) {
delete poolUserMap[pool][username]; delete poolUserMap[pool][username];
// if the poolUserMap pool entry is empty, delete the entry // if the poolUserMap pool entry is empty, delete the entry
if (Object.keys(poolUserMap[pool]).length === 0) { if (Object.keys(poolUserMap[pool]).length === 0) {
delete poolUserMap[pool]; delete poolUserMap[pool];
} }
} }
}
// remove socket entry from requestedRates // remove socket entry from requestedRates
delete requestedRates[socket.rateIndex]; delete requestedRates[socket.rateIndex];
if (Object.keys(requestedRates).length === 0) { // if there are no requested rates left, clear the timer if (Object.keys(requestedRates).length === 0) { // if there are no requested rates left, clear the timer
@ -154,6 +158,7 @@ if (schemes.interrupt.enabled) {
}); });
// handle the wss upgrade request // handle the wss upgrade request
global.server.on("upgrade", async (req, socket, head) => { global.server.on("upgrade", async (req, socket, head) => {
// basic auth validation
const cookies = cookie.parse(req.headers.cookie || ""); const cookies = cookie.parse(req.headers.cookie || "");
const auth = (await global.pve.requestPVE("/version", "GET", { cookies })).status === 200; const auth = (await global.pve.requestPVE("/version", "GET", { cookies })).status === 200;
if (!auth) { if (!auth) {
@ -161,8 +166,10 @@ if (schemes.interrupt.enabled) {
} }
else { else {
wsServer.handleUpgrade(req, socket, head, (socket) => { wsServer.handleUpgrade(req, socket, head, (socket) => {
const pool = global.db.getUser(cookies.username).cluster.pool; // get the user pools
wsServer.emit("connection", socket, cookies.username, pool); const pools = global.db.getUser(cookies.username).cluster.pools;
// emit the connection to initialize socket
wsServer.emit("connection", socket, cookies.username, pools);
}); });
} }
}); });

View File

@ -81,7 +81,10 @@
"min": 100, "min": 100,
"max": 199 "max": 199
}, },
"pool": "examplepool" "pools": [
"examplepool1",
"examplepool2"
]
}, },
"templates": { "templates": {
"instances": { "instances": {