import { requestPVE, requestAPI, goToPage, goToURL, instances_config, nodes_config, setTitleAndHeader } from "./utils.js"; import { alert, dialog } from "./dialog.js"; import { PVE } from "../vars.js" window.addEventListener("DOMContentLoaded", init); async function init() { setTitleAndHeader(); let cookie = document.cookie; if (cookie === "") { goToPage("login.html"); } await populateInstances(); let addInstanceBtn = document.querySelector("#instance-add"); addInstanceBtn.addEventListener("click", handleInstanceAdd); } async function populateInstances() { let resources = await requestPVE("/cluster/resources", "GET"); let instanceContainer = document.getElementById("instance-container"); let instances = []; resources.data.forEach((element) => { if (element.type === "lxc" || element.type === "qemu") { let nodeName = element.node; let nodeStatus = resources.data.find(item => item.node === nodeName && item.type === "node").status; element.node = { name: nodeName, status: nodeStatus }; instances.push(element); } }); instances.sort((a, b) => (a.vmid > b.vmid) ? 1 : -1); instanceContainer.innerHTML = `

VM ID

VM Name

VM Type

VM Status

Host Name

Host Status

Actions

`; for (let i = 0; i < instances.length; i++) { let newInstance = new Instance(); newInstance.data = instances[i]; instanceContainer.append(newInstance.shadowElement); } } async function handleInstanceAdd() { let header = "Create New Instance"; let body = `

Container Options

`; let d = dialog(header, body, async (result, form) => { if (result === "confirm") { 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 { alert(result.error); populateInstances(); } } }); let 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; }); } }); let templateContent = "iso"; let templateStorage = d.querySelector("#template-storage"); templateStorage.selectedIndex = -1; let rootfsContent = "rootdir"; let rootfsStorage = d.querySelector("#rootfs-storage"); rootfsStorage.selectedIndex = -1; let nodeSelect = d.querySelector("#node"); let clusterNodes = await requestPVE("/nodes", "GET"); let allowedNodes = await requestAPI("/user/nodes", "GET"); clusterNodes.data.forEach((element) => { if (element.status === "online" && allowedNodes.nodes.includes(element.node)) { 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 = d.querySelector("#template-image"); // populate templateImage depending on selected image storage templateStorage.addEventListener("change", async () => { templateImage.innerHTML = ``; 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; }); let userResources = await requestAPI("/user/resources", "GET"); let userInstances = await requestAPI("/user/instances", "GET"); d.querySelector("#cores").max = userResources.avail.cores; d.querySelector("#memory").max = userResources.avail.memory; d.querySelector("#vmid").min = userInstances.vmid.min; d.querySelector("#vmid").max = userInstances.vmid.max; } class Instance { constructor() { let shadowRoot = document.createElement("div"); shadowRoot.classList.add("w3-row"); shadowRoot.innerHTML = `

`; this.shadowElement = shadowRoot; this.actionLock = false; } set data(data) { if (data.status === "unknown") { data.status = "stopped"; } this.type = data.type; this.status = data.status; this.vmid = data.vmid; this.name = data.name; this.node = data.node; this.update(); } update() { let vmidParagraph = this.shadowElement.querySelector("#instance-id"); vmidParagraph.innerText = this.vmid; let nameParagraph = this.shadowElement.querySelector("#instance-name"); nameParagraph.innerText = this.name ? this.name : ""; let typeParagraph = this.shadowElement.querySelector("#instance-type"); typeParagraph.innerText = this.type; let statusParagraph = this.shadowElement.querySelector("#instance-status"); statusParagraph.innerText = this.status; let statusIcon = this.shadowElement.querySelector("#instance-status-icon"); statusIcon.src = instances_config[this.status].statusSrc; statusIcon.alt = instances_config[this.status].statusAlt; let nodeNameParagraph = this.shadowElement.querySelector("#node-name"); nodeNameParagraph.innerText = this.node.name; let nodeStatusParagraph = this.shadowElement.querySelector("#node-status"); nodeStatusParagraph.innerText = this.node.status; let nodeStatusIcon = this.shadowElement.querySelector("#node-status-icon"); nodeStatusIcon.src = nodes_config[this.node.status].statusSrc; nodeStatusIcon.alt = nodes_config[this.node.status].statusAlt; let powerButton = this.shadowElement.querySelector("#power-btn"); powerButton.src = instances_config[this.status].powerButtonSrc; powerButton.alt = instances_config[this.status].powerButtonAlt; powerButton.title = instances_config[this.status].powerButtonAlt; powerButton.onclick = this.handlePowerButton.bind(this) let configButton = this.shadowElement.querySelector("#configure-btn"); configButton.src = instances_config[this.status].configButtonSrc; configButton.alt = instances_config[this.status].configButtonAlt; configButton.title = instances_config[this.status].configButtonAlt; configButton.onclick = this.handleConfigButton.bind(this); let consoleButton = this.shadowElement.querySelector("#console-btn"); consoleButton.src = instances_config[this.status].consoleButtonSrc; consoleButton.alt = instances_config[this.status].consoleButtonAlt; consoleButton.title = instances_config[this.status].consoleButtonAlt; consoleButton.onclick = this.handleConsoleButton.bind(this); let deleteButton = this.shadowElement.querySelector("#delete-btn"); deleteButton.src = instances_config[this.status].deleteButtonSrc; deleteButton.alt = instances_config[this.status].deleteButtonAlt; deleteButton.title = instances_config[this.status].deleteButtonAlt; deleteButton.onclick = this.handleDeleteButton.bind(this); if (this.node.status !== "online") { powerButton.classList.add("hidden"); configButton.classList.add("hidden"); consoleButton.classList.add("hidden"); deleteButton.classList.add("hidden"); } } async handlePowerButton() { if (!this.actionLock) { let header = `${this.status === "running" ? "Stop" : "Start"} VM ${this.vmid}`; let body = `

Are you sure you want to ${this.status === "running" ? "stop" : "start"} VM

${this.vmid}

` dialog(header, body, async (result, form) => { if (result === "confirm") { this.actionLock = true; let targetAction = this.status === "running" ? "stop" : "start"; let targetStatus = this.status === "running" ? "stopped" : "running"; let prevStatus = this.status; this.status = "loading"; this.update(); let result = await requestPVE(`/nodes/${this.node.name}/${this.type}/${this.vmid}/status/${targetAction}`, "POST", { node: this.node.name, vmid: this.vmid }); const waitFor = delay => new Promise(resolve => setTimeout(resolve, delay)); while (true) { let taskStatus = await requestPVE(`/nodes/${this.node.name}/tasks/${result.data}/status`, "GET"); if (taskStatus.data.status === "stopped" && taskStatus.data.exitstatus === "OK") { // task stopped and was successful this.status = targetStatus; this.update(); this.actionLock = false; break; } else if (taskStatus.data.status === "stopped") { // task stopped but was not successful this.status = prevStatus; alert(`attempted to ${targetAction} ${this.vmid} but process returned stopped:${result.data.exitstatus}`); this.update(); this.actionLock = false; break; } else { // task has not stopped await waitFor(1000); } } } }); } } handleConfigButton() { if (!this.actionLock && this.status === "stopped") { // if the action lock is false, and the node is stopped, then navigate to the conig page with the node infor in the search query goToPage("config.html", { node: this.node.name, type: this.type, vmid: this.vmid }); } } handleConsoleButton() { if (!this.actionLock && this.status === "running") { let data = { console: `${this.type === "qemu" ? "kvm" : "lxc"}`, vmid: this.vmid, vmname: this.name, node: this.node.name, resize: "off", cmd: "" }; data[`${this.type === "qemu" ? "novnc" : "xtermjs"}`] = 1; goToURL(PVE, data, true); } } handleDeleteButton() { if (!this.actionLock && this.status === "stopped") { let header = `Delete VM ${this.vmid}`; let body = `

Are you sure you want to delete VM

${this.vmid}

` dialog(header, body, async (result, form) => { if (result === "confirm") { this.actionLock = true; let prevStatus = this.status; this.status = "loading"; this.update(); let action = {}; action.purge = 1; action["destroy-unreferenced-disks"] = 1; let body = { node: this.node.name, type: this.type, vmid: this.vmid, action: JSON.stringify(action) }; let result = await requestAPI("/instance", "DELETE", body); if (result.status === 200) { this.shadowElement.parentElement.removeChild(this.shadowElement); } else { alert(result.error); this.status = this.prevStatus; this.update(); this.actionLock = false; } } }); } } }