2023-04-03 21:57:03 +00:00
|
|
|
import {requestPVE, requestAPI, goToPage} from "./utils.js";
|
2023-04-06 21:26:32 +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
|
|
|
|
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
|
|
|
|
2023-04-06 04:21:03 +00:00
|
|
|
instanceContainer.innerHTML = `
|
|
|
|
<div style="display: contents;" id="instance-container-header">
|
|
|
|
<p>VM ID</p>
|
|
|
|
<p>VM Name</p>
|
|
|
|
<p>VM Type</p>
|
|
|
|
<p>VM Status</p>
|
|
|
|
<p>Host Name</p>
|
|
|
|
<p>Host Status</p>
|
|
|
|
<div class="hidden"></div>
|
|
|
|
<p>Actions</p>
|
|
|
|
</div>
|
|
|
|
`;
|
2022-12-14 21:37:30 +00:00
|
|
|
for(let i = 0; i < instances.length; i++) {
|
2023-04-03 22:49:24 +00:00
|
|
|
let newInstance = document.createElement("instance-obj");
|
2022-12-14 21:37:30 +00:00
|
|
|
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>
|
2023-02-23 22:14:06 +00:00
|
|
|
<label for="node">Node</label>
|
|
|
|
<select name="node" id="node" required></select>
|
2023-02-23 21:58:42 +00:00
|
|
|
<label for="name">Name</label>
|
|
|
|
<input name="name" id="name" required></input>
|
|
|
|
<label for="vmid">ID</label>
|
|
|
|
<input name="vmid" id="vmid" type="number" required></input>
|
2023-02-27 23:03:19 +00:00
|
|
|
<label for="cores">Cores (Threads)</label>
|
|
|
|
<input name="cores" id="cores" type="number" min="1" max="8192" required></input>
|
|
|
|
<label for="memory">Memory (MiB)</label>
|
|
|
|
<input name="memory" id="memory" type="number" min="16", step="1" required></input>
|
2023-02-24 23:27:03 +00:00
|
|
|
<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="container-specific none" name="swap" id="swap" type="number" min="0" step="1" required disabled></input>
|
|
|
|
<label class="container-specific none" for="template-storage">Template Storage</label>
|
|
|
|
<select class="container-specific none" name="template-storage" id="template-storage" required disabled></select>
|
|
|
|
<label class="container-specific none" for="template-image">Template Image</label>
|
|
|
|
<select class="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="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="container-specific none" name="rootfs-size" id="rootfs-size" type="number" min="0" max="131072" required disabled></input>
|
2023-02-27 23:03:19 +00:00
|
|
|
<label class="container-specific none" for="password">Password</label>
|
|
|
|
<input class="container-specific none" name="password" id="password" type="password" required disabled></input>
|
2023-02-23 21:11:43 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
let typeSelect = dialog.shadowRoot.querySelector("#type");
|
|
|
|
typeSelect.selectedIndex = -1;
|
|
|
|
typeSelect.addEventListener("change", () => {
|
|
|
|
if(typeSelect.value === "qemu") {
|
2023-02-24 23:27:03 +00:00
|
|
|
dialog.shadowRoot.querySelectorAll(".container-specific").forEach((element) => {
|
|
|
|
element.classList.add("none");
|
|
|
|
element.disabled = true;
|
|
|
|
});
|
2023-02-23 21:11:43 +00:00
|
|
|
}
|
|
|
|
else {
|
2023-02-24 23:27:03 +00:00
|
|
|
dialog.shadowRoot.querySelectorAll(".container-specific").forEach((element) => {
|
|
|
|
element.classList.remove("none");
|
|
|
|
element.disabled = false;
|
|
|
|
});
|
2023-02-23 21:11:43 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-02-23 22:14:06 +00:00
|
|
|
let templateContent = "iso";
|
|
|
|
let templateStorage = dialog.shadowRoot.querySelector("#template-storage");
|
2023-02-24 23:27:03 +00:00
|
|
|
templateStorage.selectedIndex = -1;
|
2023-02-23 22:14:06 +00:00
|
|
|
|
|
|
|
let rootfsContent = "rootdir";
|
|
|
|
let rootfsStorage = dialog.shadowRoot.querySelector("#rootfs-storage");
|
2023-02-24 23:27:03 +00:00
|
|
|
rootfsStorage.selectedIndex = -1;
|
2023-02-23 22:14:06 +00:00
|
|
|
|
2023-02-24 23:27:03 +00:00
|
|
|
let nodeSelect = dialog.shadowRoot.querySelector("#node");
|
|
|
|
let nodes = await requestPVE("/nodes", "GET");
|
|
|
|
nodes.data.forEach((element) => {
|
|
|
|
if (element.status === "online") {
|
|
|
|
nodeSelect.add(new Option(element.node));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
nodeSelect.selectedIndex = -1;
|
|
|
|
nodeSelect.addEventListener("change", async () => { // change template and rootfs storage based on node
|
|
|
|
let node = nodeSelect.value;
|
|
|
|
let storage = await requestPVE(`/nodes/${node}/storage`, "GET");
|
|
|
|
storage.data.forEach((element) => {
|
|
|
|
if (element.content.includes(templateContent)) {
|
|
|
|
templateStorage.add(new Option(element.storage));
|
|
|
|
}
|
|
|
|
if (element.content.includes(rootfsContent)) {
|
|
|
|
rootfsStorage.add(new Option(element.storage));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
templateStorage.selectedIndex = -1;
|
|
|
|
rootfsStorage.selectedIndex = -1;
|
|
|
|
});
|
|
|
|
|
|
|
|
let templateImage = dialog.shadowRoot.querySelector("#template-image"); // populate templateImage by
|
2023-02-27 23:03:19 +00:00
|
|
|
templateStorage.addEventListener("change", async () => {
|
|
|
|
let content = "vztmpl";
|
|
|
|
let images = await requestPVE(`/nodes/${nodeSelect.value}/storage/${templateStorage.value}/content`, "GET");
|
|
|
|
images.data.forEach((element) => {
|
|
|
|
if (element.content.includes(content)) {
|
|
|
|
templateImage.append(new Option(element.volid.replace(`${templateStorage.value}:${content}/`, ""), element.volid));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
templateImage.selectedIndex = -1;
|
|
|
|
});
|
2023-02-24 23:27:03 +00:00
|
|
|
|
2023-02-23 21:11:43 +00:00
|
|
|
dialog.callback = async (result, form) => {
|
|
|
|
if (result === "confirm") {
|
2023-02-27 23:03:19 +00:00
|
|
|
let body = {
|
|
|
|
node: form.get("node"),
|
|
|
|
type: form.get("type"),
|
|
|
|
name: form.get("name"),
|
|
|
|
vmid: form.get("vmid"),
|
|
|
|
cores: form.get("cores"),
|
|
|
|
memory: form.get("memory")
|
|
|
|
};
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
let result = await requestAPI("/instance", "POST", body);
|
|
|
|
if (result.status === 200) {
|
|
|
|
populateInstances();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.error(result);
|
2023-02-28 23:56:39 +00:00
|
|
|
populateInstances();
|
2023-02-27 23:03:19 +00:00
|
|
|
}
|
2023-02-23 21:11:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dialog.show();
|
2022-12-11 02:02:29 +00:00
|
|
|
}
|