2023-01-19 19:58:44 +00:00
|
|
|
import {requestPVE, goToPage, deleteAllCookies} from "./utils.js";
|
2023-02-23 21:11:43 +00:00
|
|
|
import { Dialog } from "./dialog.js";
|
2022-12-11 06:49:10 +00:00
|
|
|
|
2022-12-11 02:02:29 +00:00
|
|
|
window.addEventListener("DOMContentLoaded", init);
|
|
|
|
|
|
|
|
async function init () {
|
2022-12-18 00:07:18 +00:00
|
|
|
await populateInstances();
|
2023-01-09 23:08:45 +00:00
|
|
|
|
|
|
|
let instances = document.querySelector("nav #instances");
|
|
|
|
instances.addEventListener("click", () => {
|
|
|
|
goToPage("index.html");
|
|
|
|
});
|
|
|
|
|
|
|
|
let logout = document.querySelector("nav #logout");
|
|
|
|
logout.addEventListener("click", () => {
|
|
|
|
deleteAllCookies();
|
|
|
|
goToPage("login.html");
|
|
|
|
});
|
2023-02-23 21:11:43 +00:00
|
|
|
|
|
|
|
let addInstanceBtn = document.querySelector("#instance-add");
|
|
|
|
addInstanceBtn.addEventListener("click", handleInstanceAdd);
|
2022-12-18 00:07:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function populateInstances () {
|
2022-12-11 02:02:29 +00:00
|
|
|
let cookie = document.cookie;
|
2022-12-18 00:07:18 +00:00
|
|
|
if (cookie === "") {
|
2022-12-19 05:58:46 +00:00
|
|
|
goToPage("login.html");
|
2022-12-11 02:02:29 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 19:58:44 +00:00
|
|
|
let resources = await requestPVE("/cluster/resources", "GET", null);
|
2022-12-11 23:24:19 +00:00
|
|
|
|
2022-12-14 21:37:30 +00:00
|
|
|
let instanceContainer = document.getElementById("instance-container")
|
|
|
|
|
2023-01-17 20:26:19 +00:00
|
|
|
let instances = [];
|
2023-01-17 20:24:51 +00:00
|
|
|
|
|
|
|
resources.data.forEach((element) => {
|
|
|
|
if (element.type === "lxc" || element.type === "qemu") {
|
|
|
|
let nodeName = element.node;
|
2023-01-17 20:29:10 +00:00
|
|
|
let nodeStatus = resources.data.find(item => item.node === nodeName && item.type === "node").status;
|
2023-01-17 20:24:51 +00:00
|
|
|
element.node = {name: nodeName, status: nodeStatus};
|
2023-01-17 20:26:19 +00:00
|
|
|
instances.push(element);
|
2023-01-17 20:24:51 +00:00
|
|
|
}
|
|
|
|
});
|
2022-12-14 21:37:30 +00:00
|
|
|
|
|
|
|
instances.sort((a, b) => (a.vmid > b.vmid) ? 1 : -1);
|
2022-12-11 23:06:10 +00:00
|
|
|
|
2022-12-18 00:07:18 +00:00
|
|
|
instanceContainer.innerText = "";
|
2022-12-14 21:37:30 +00:00
|
|
|
for(let i = 0; i < instances.length; i++) {
|
|
|
|
let newInstance = document.createElement("instance-article");
|
|
|
|
newInstance.data = instances[i];
|
|
|
|
instanceContainer.append(newInstance);
|
2022-12-11 02:02:29 +00:00
|
|
|
}
|
2023-02-23 21:11:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function handleInstanceAdd () {
|
|
|
|
let dialog = document.createElement("dialog-form");
|
|
|
|
document.body.append(dialog);
|
|
|
|
|
|
|
|
dialog.header = "Create New Instance";
|
|
|
|
|
|
|
|
dialog.formBody = `
|
|
|
|
<label for="type">Instance Type</label>
|
|
|
|
<select name="type" id="type" required>
|
|
|
|
<option value="lxc">Container</option>
|
|
|
|
<option value="qemu">Virtual Machine</option>
|
|
|
|
</select>
|
|
|
|
<label for="cpu">Cores</label>
|
|
|
|
<input name="cpu" id="cpu" type="number" min="1" max="8192" required></input>
|
|
|
|
<label for="ram">Memory</label>
|
|
|
|
<input name="ram" id="ram" type="number" min="16", step="1" required></input>
|
|
|
|
<label for="swap" disabled>Swap</label>
|
|
|
|
<input name="swap" id="swap" type="number" min="0" step="1" required disabled></input>
|
|
|
|
`;
|
|
|
|
|
|
|
|
let typeSelect = dialog.shadowRoot.querySelector("#type");
|
|
|
|
typeSelect.selectedIndex = -1;
|
|
|
|
typeSelect.addEventListener("change", () => {
|
|
|
|
if(typeSelect.value === "qemu") {
|
|
|
|
dialog.shadowRoot.querySelector(`label[for="swap"]`).disabled = true;
|
|
|
|
dialog.shadowRoot.querySelector("#swap").disabled = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dialog.shadowRoot.querySelector(`label[for="swap"]`).disabled = false;
|
|
|
|
dialog.shadowRoot.querySelector("#swap").disabled = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
dialog.callback = async (result, form) => {
|
|
|
|
if (result === "confirm") {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dialog.show();
|
2022-12-11 02:02:29 +00:00
|
|
|
}
|