import {requestPVE, requestAPI, goToPage, goToURL, instances} from "./utils.js";
import { Dialog } from "./dialog.js";
export class Instance extends HTMLElement {
constructor () {
super();
let shadowRoot = this.attachShadow({mode: "open"});
shadowRoot.innerHTML = `
Are you sure you want to ${this.status === "running" ? "stop" : "start"} VM
${this.vmid}
` dialog.callback = 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`); 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; console.error(`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); } } } } dialog.show(); } } 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("https://pve.tronnet.net", data, true); } } handleDeleteButton () { if (!this.actionLock && this.status === "stopped") { let dialog = document.createElement("dialog-form"); document.body.append(dialog); dialog.header = `Delete VM ${this.vmid}`; dialog.formBody = `Are you sure you want to delete VM
${this.vmid}
` dialog.callback = async (result, form) => { if (result === "confirm") { this.actionLock = true; 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.parentNode.removeChild(this); } else { console.error(result); } } } dialog.show(); } } } customElements.define("instance-article", Instance);