2023-09-15 22:13:21 +00:00
|
|
|
import { requestPVE, requestAPI, goToPage, setTitleAndHeader } from "./utils.js";
|
2023-06-29 22:20:15 +00:00
|
|
|
import { alert, dialog } from "./dialog.js";
|
2023-07-28 18:32:04 +00:00
|
|
|
import { setupClientSync } from "./clientsync.js";
|
2024-06-05 22:11:53 +00:00
|
|
|
import wf_align from "../modules/wfa.js";
|
2023-06-29 22:20:15 +00:00
|
|
|
|
|
|
|
window.addEventListener("DOMContentLoaded", init);
|
|
|
|
|
2023-09-18 19:37:07 +00:00
|
|
|
let instances = [];
|
|
|
|
|
2023-06-29 22:20:15 +00:00
|
|
|
async function init () {
|
|
|
|
setTitleAndHeader();
|
|
|
|
const cookie = document.cookie;
|
|
|
|
if (cookie === "") {
|
|
|
|
goToPage("login.html");
|
|
|
|
}
|
2023-07-28 18:32:04 +00:00
|
|
|
|
2023-09-15 22:13:21 +00:00
|
|
|
document.querySelector("#instance-add").addEventListener("click", handleInstanceAdd);
|
2023-09-18 19:37:07 +00:00
|
|
|
document.querySelector("#vm-search").addEventListener("input", populateInstances);
|
2023-07-11 21:14:54 +00:00
|
|
|
|
2023-09-18 19:37:07 +00:00
|
|
|
setupClientSync(refreshInstances);
|
|
|
|
}
|
2023-09-15 22:13:21 +00:00
|
|
|
|
2023-09-18 19:37:07 +00:00
|
|
|
async function refreshInstances () {
|
|
|
|
await getInstances();
|
|
|
|
await populateInstances();
|
2023-06-29 22:20:15 +00:00
|
|
|
}
|
|
|
|
|
2023-09-18 19:37:07 +00:00
|
|
|
async function getInstances () {
|
2023-06-29 22:20:15 +00:00
|
|
|
const resources = await requestPVE("/cluster/resources", "GET");
|
2023-09-18 19:37:07 +00:00
|
|
|
instances = [];
|
2023-06-29 22:20:15 +00:00
|
|
|
resources.data.forEach((element) => {
|
|
|
|
if (element.type === "lxc" || element.type === "qemu") {
|
|
|
|
const nodeName = element.node;
|
|
|
|
const nodeStatus = resources.data.find(item => item.node === nodeName && item.type === "node").status;
|
|
|
|
element.node = { name: nodeName, status: nodeStatus };
|
|
|
|
instances.push(element);
|
|
|
|
}
|
|
|
|
});
|
2023-09-18 19:37:07 +00:00
|
|
|
}
|
2023-06-29 22:20:15 +00:00
|
|
|
|
2023-09-18 19:37:07 +00:00
|
|
|
async function populateInstances () {
|
2024-06-05 22:11:53 +00:00
|
|
|
let searchCriteria = localStorage.getItem("search-criteria");
|
|
|
|
if (!searchCriteria) {
|
|
|
|
searchCriteria = "fuzzy";
|
|
|
|
localStorage.setItem("search-criteria", "fuzzy");
|
|
|
|
}
|
2023-09-20 22:17:06 +00:00
|
|
|
const searchQuery = document.querySelector("#search").value || null;
|
2023-09-18 19:37:07 +00:00
|
|
|
let criteria;
|
2023-09-20 22:17:06 +00:00
|
|
|
if (!searchQuery) {
|
2023-09-18 19:37:07 +00:00
|
|
|
criteria = (a, b) => {
|
|
|
|
return (a.vmid > b.vmid) ? 1 : -1;
|
|
|
|
};
|
|
|
|
}
|
2024-06-05 22:11:53 +00:00
|
|
|
else if (searchCriteria === "exact") {
|
2023-09-18 19:37:07 +00:00
|
|
|
criteria = (a, b) => {
|
2023-09-28 01:38:19 +00:00
|
|
|
const aInc = a.name.toLowerCase().includes(searchQuery.toLowerCase());
|
|
|
|
const bInc = b.name.toLowerCase().includes(searchQuery.toLowerCase());
|
2023-09-19 20:59:28 +00:00
|
|
|
if (aInc && bInc) {
|
2023-09-18 19:37:07 +00:00
|
|
|
return a.vmid > b.vmid ? 1 : -1;
|
|
|
|
}
|
2023-09-19 20:59:28 +00:00
|
|
|
else if (aInc && !bInc) {
|
2023-09-18 19:37:07 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2023-09-19 20:59:28 +00:00
|
|
|
else if (!aInc && bInc) {
|
2023-09-18 19:37:07 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return a.vmid > b.vmid ? 1 : -1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2024-06-05 22:11:53 +00:00
|
|
|
else if (searchCriteria === "fuzzy") {
|
|
|
|
const penalties = {
|
|
|
|
m: 0,
|
|
|
|
x: 1,
|
|
|
|
o: 1,
|
|
|
|
e: 1
|
|
|
|
};
|
|
|
|
criteria = (a, b) => {
|
|
|
|
// lower is better
|
|
|
|
const aAlign = wf_align(a.name.toLowerCase(), searchQuery.toLowerCase(), penalties);
|
|
|
|
const aScore = aAlign.score / a.name.length;
|
|
|
|
const bAlign = wf_align(b.name.toLowerCase(), searchQuery.toLowerCase(), penalties);
|
|
|
|
const bScore = bAlign.score / b.name.length;
|
|
|
|
if (aScore === bScore) {
|
|
|
|
return a.vmid > b.vmid ? 1 : -1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return aScore - bScore;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2023-09-18 19:37:07 +00:00
|
|
|
instances.sort(criteria);
|
|
|
|
const instanceContainer = document.querySelector("#instance-container");
|
2023-09-19 20:59:28 +00:00
|
|
|
instanceContainer.innerHTML = "";
|
2023-06-29 22:20:15 +00:00
|
|
|
for (let i = 0; i < instances.length; i++) {
|
2023-08-15 20:27:45 +00:00
|
|
|
const newInstance = document.createElement("instance-card");
|
2023-09-20 22:17:06 +00:00
|
|
|
instances[i].searchQuery = searchQuery;
|
2023-06-29 22:20:15 +00:00
|
|
|
newInstance.data = instances[i];
|
2023-08-15 20:27:45 +00:00
|
|
|
instanceContainer.append(newInstance);
|
2023-06-29 22:20:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function handleInstanceAdd () {
|
|
|
|
const header = "Create New Instance";
|
|
|
|
|
|
|
|
const body = `
|
2023-11-14 00:09:41 +00:00
|
|
|
<form method="dialog" class="input-grid" style="grid-template-columns: auto 1fr;" id="form">
|
|
|
|
<label for="type">Instance Type</label>
|
|
|
|
<select class="w3-select w3-border" name="type" id="type" required>
|
|
|
|
<option value="lxc">Container</option>
|
|
|
|
<option value="qemu">Virtual Machine</option>
|
|
|
|
</select>
|
|
|
|
<label for="node">Node</label>
|
|
|
|
<select class="w3-select w3-border" name="node" id="node" required></select>
|
|
|
|
<label for="name">Name</label>
|
|
|
|
<input class="w3-input w3-border" name="name" id="name" required></input>
|
|
|
|
<label for="vmid">ID</label>
|
|
|
|
<input class="w3-input w3-border" name="vmid" id="vmid" type="number" required></input>
|
2024-04-16 21:38:25 +00:00
|
|
|
<label for="pool">Pool</label>
|
|
|
|
<select class="w3-select w3-border" name="pool" id="pool" required></select>
|
2023-11-14 00:09:41 +00:00
|
|
|
<label for="cores">Cores (Threads)</label>
|
|
|
|
<input class="w3-input w3-border" name="cores" id="cores" type="number" min="1" max="8192" required></input>
|
|
|
|
<label for="memory">Memory (MiB)</label>
|
|
|
|
<input class="w3-input w3-border" name="memory" id="memory" type="number" min="16", step="1" required></input>
|
|
|
|
<p class="container-specific none" style="grid-column: 1 / span 2; text-align: center;">Container Options</p>
|
|
|
|
<label class="container-specific none" for="swap">Swap (MiB)</label>
|
|
|
|
<input class="w3-input w3-border container-specific none" name="swap" id="swap" type="number" min="0" step="1" required disabled></input>
|
|
|
|
<label class="container-specific none" for="template-image">Template Image</label>
|
|
|
|
<select class="w3-select w3-border container-specific none" name="template-image" id="template-image" required disabled></select>
|
|
|
|
<label class="container-specific none" for="rootfs-storage">ROOTFS Storage</label>
|
|
|
|
<select class="w3-select w3-border container-specific none" name="rootfs-storage" id="rootfs-storage" required disabled></select>
|
|
|
|
<label class="container-specific none" for="rootfs-size">ROOTFS Size (GiB)</label>
|
|
|
|
<input class="w3-input w3-border container-specific none" name="rootfs-size" id="rootfs-size" type="number" min="0" max="131072" required disabled></input>
|
|
|
|
<label class="container-specific none" for="password">Password</label>
|
|
|
|
<input class="w3-input w3-border container-specific none" name="password" id="password" type="password" required disabled></input>
|
|
|
|
</form>
|
2023-06-29 22:20:15 +00:00
|
|
|
`;
|
|
|
|
|
2024-04-16 21:38:25 +00:00
|
|
|
const templates = await requestAPI("/user/ct-templates", "GET");
|
|
|
|
|
2023-06-29 22:20:15 +00:00
|
|
|
const d = dialog(header, body, async (result, form) => {
|
|
|
|
if (result === "confirm") {
|
|
|
|
const body = {
|
|
|
|
name: form.get("name"),
|
|
|
|
cores: form.get("cores"),
|
2024-04-16 21:38:25 +00:00
|
|
|
memory: form.get("memory"),
|
|
|
|
pool: form.get("pool")
|
2023-06-29 22:20:15 +00:00
|
|
|
};
|
|
|
|
if (form.get("type") === "lxc") {
|
|
|
|
body.swap = form.get("swap");
|
|
|
|
body.password = form.get("password");
|
|
|
|
body.ostemplate = form.get("template-image");
|
|
|
|
body.rootfslocation = form.get("rootfs-storage");
|
|
|
|
body.rootfssize = form.get("rootfs-size");
|
|
|
|
}
|
2023-07-04 04:41:39 +00:00
|
|
|
const node = form.get("node");
|
|
|
|
const type = form.get("type");
|
|
|
|
const vmid = form.get("vmid");
|
2023-08-03 00:35:56 +00:00
|
|
|
const result = await requestAPI(`/cluster/${node}/${type}/${vmid}/create`, "POST", body);
|
2023-06-29 22:20:15 +00:00
|
|
|
if (result.status === 200) {
|
|
|
|
populateInstances();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
alert(result.error);
|
|
|
|
populateInstances();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const typeSelect = d.querySelector("#type");
|
|
|
|
typeSelect.selectedIndex = -1;
|
|
|
|
typeSelect.addEventListener("change", () => {
|
|
|
|
if (typeSelect.value === "qemu") {
|
|
|
|
d.querySelectorAll(".container-specific").forEach((element) => {
|
|
|
|
element.classList.add("none");
|
|
|
|
element.disabled = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
d.querySelectorAll(".container-specific").forEach((element) => {
|
|
|
|
element.classList.remove("none");
|
|
|
|
element.disabled = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const rootfsContent = "rootdir";
|
|
|
|
const rootfsStorage = d.querySelector("#rootfs-storage");
|
|
|
|
rootfsStorage.selectedIndex = -1;
|
|
|
|
|
2023-11-15 20:18:01 +00:00
|
|
|
const userResources = await requestAPI("/user/dynamic/resources", "GET");
|
|
|
|
const userCluster = await requestAPI("/user/config/cluster", "GET");
|
|
|
|
|
2023-06-29 22:20:15 +00:00
|
|
|
const nodeSelect = d.querySelector("#node");
|
|
|
|
const clusterNodes = await requestPVE("/nodes", "GET");
|
2024-04-27 03:50:29 +00:00
|
|
|
const allowedNodes = Object.keys(userCluster.nodes);
|
2023-06-29 22:20:15 +00:00
|
|
|
clusterNodes.data.forEach((element) => {
|
|
|
|
if (element.status === "online" && allowedNodes.includes(element.node)) {
|
|
|
|
nodeSelect.add(new Option(element.node));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
nodeSelect.selectedIndex = -1;
|
2024-04-16 21:38:25 +00:00
|
|
|
nodeSelect.addEventListener("change", async () => { // change rootfs storage based on node
|
2023-06-29 22:20:15 +00:00
|
|
|
const node = nodeSelect.value;
|
|
|
|
const storage = await requestPVE(`/nodes/${node}/storage`, "GET");
|
|
|
|
storage.data.forEach((element) => {
|
|
|
|
if (element.content.includes(rootfsContent)) {
|
|
|
|
rootfsStorage.add(new Option(element.storage));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
rootfsStorage.selectedIndex = -1;
|
2023-11-15 20:18:01 +00:00
|
|
|
|
2024-04-16 21:38:25 +00:00
|
|
|
// set core and memory min/max depending on node selected
|
2023-11-15 20:18:01 +00:00
|
|
|
if (node in userResources.cores.nodes) {
|
|
|
|
d.querySelector("#cores").max = userResources.cores.nodes[node].avail;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
d.querySelector("#cores").max = userResources.cores.global.avail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node in userResources.memory.nodes) {
|
|
|
|
d.querySelector("#memory").max = userResources.memory.nodes[node].avail;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
d.querySelector("#memory").max = userResources.memory.global.avail;
|
|
|
|
}
|
2024-04-16 21:38:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// set vmid min/max
|
|
|
|
d.querySelector("#vmid").min = userCluster.vmid.min;
|
|
|
|
d.querySelector("#vmid").max = userCluster.vmid.max;
|
2023-11-15 20:18:01 +00:00
|
|
|
|
2024-04-16 21:38:25 +00:00
|
|
|
// add user pools to selector
|
|
|
|
const poolSelect = d.querySelector("#pool");
|
|
|
|
const userPools = Object.keys(userCluster.pools);
|
|
|
|
userPools.forEach((element) => {
|
|
|
|
poolSelect.add(new Option(element));
|
2023-06-29 22:20:15 +00:00
|
|
|
});
|
2024-04-16 21:38:25 +00:00
|
|
|
poolSelect.selectedIndex = -1;
|
2023-06-29 22:20:15 +00:00
|
|
|
|
2024-04-16 21:38:25 +00:00
|
|
|
// add template images to selector
|
2023-06-29 22:20:15 +00:00
|
|
|
const templateImage = d.querySelector("#template-image"); // populate templateImage depending on selected image storage
|
2024-04-16 21:38:25 +00:00
|
|
|
for (const template of templates) {
|
2024-04-27 03:50:29 +00:00
|
|
|
templateImage.append(new Option(template.name, template.volid));
|
2024-04-16 21:38:25 +00:00
|
|
|
}
|
|
|
|
templateImage.selectedIndex = -1;
|
2023-06-29 22:20:15 +00:00
|
|
|
}
|