2022-12-16 07:05:32 +00:00
|
|
|
import { request } from "./utils.js";
|
2022-12-16 07:03:50 +00:00
|
|
|
|
2022-12-18 00:34:40 +00:00
|
|
|
const waitFor = delay => new Promise(resolve => setTimeout(resolve, delay));
|
|
|
|
|
2022-12-12 04:20:32 +00:00
|
|
|
class Instance extends HTMLElement {
|
|
|
|
constructor () {
|
|
|
|
super();
|
|
|
|
let shadowRoot = this.attachShadow({mode: "open"});
|
|
|
|
|
2022-12-15 00:05:15 +00:00
|
|
|
let instanceTemplate = document.querySelector("#instance-template");
|
2022-12-15 00:07:25 +00:00
|
|
|
let instanceTemplateContent = instanceTemplate.content;
|
2022-12-15 00:05:15 +00:00
|
|
|
shadowRoot.append(instanceTemplateContent.cloneNode(true));
|
2022-12-12 04:20:32 +00:00
|
|
|
|
|
|
|
let styleLink = document.createElement("link");
|
|
|
|
styleLink.rel = "stylesheet";
|
2022-12-13 05:43:19 +00:00
|
|
|
styleLink.href = "css/style.css";
|
2022-12-12 04:20:32 +00:00
|
|
|
styleLink.type = "text/css";
|
|
|
|
shadowRoot.append(styleLink);
|
|
|
|
|
2022-12-14 22:30:46 +00:00
|
|
|
let instanceLink = document.createElement("link");
|
|
|
|
instanceLink.rel = "stylesheet";
|
|
|
|
instanceLink.href = "css/instance.css";
|
|
|
|
instanceLink.type = "text/css";
|
|
|
|
shadowRoot.append(instanceLink);
|
|
|
|
|
2022-12-12 04:20:32 +00:00
|
|
|
this.shadowElement = shadowRoot;
|
|
|
|
}
|
|
|
|
|
|
|
|
set data (data) {
|
2022-12-15 00:10:28 +00:00
|
|
|
let typeImg = this.shadowElement.querySelector("#instance-type");
|
2022-12-14 23:28:22 +00:00
|
|
|
typeImg.src = `images/instances/${data.type}/${data.status}.svg`;
|
2022-12-16 07:03:50 +00:00
|
|
|
this.type = data.type;
|
|
|
|
this.status = data.status;
|
2022-12-14 23:28:22 +00:00
|
|
|
|
2022-12-15 00:10:28 +00:00
|
|
|
let vmidParagraph = this.shadowElement.querySelector("#instance-id");
|
2022-12-14 23:28:22 +00:00
|
|
|
vmidParagraph.innerText = data.vmid;
|
2022-12-16 07:03:50 +00:00
|
|
|
this.vmid = data.vmid;
|
2022-12-14 23:28:22 +00:00
|
|
|
|
2022-12-15 00:10:28 +00:00
|
|
|
let nameParagraph = this.shadowElement.querySelector("#instance-name");
|
2022-12-14 23:28:22 +00:00
|
|
|
nameParagraph.innerText = data.name;
|
2022-12-14 23:38:13 +00:00
|
|
|
|
2022-12-15 00:10:28 +00:00
|
|
|
let nodeImg = this.shadowElement.querySelector("#node-status");
|
2022-12-14 23:29:08 +00:00
|
|
|
nodeImg.src = `images/nodes/${data.node.status}.svg`;
|
2022-12-14 23:28:22 +00:00
|
|
|
|
2022-12-15 00:10:28 +00:00
|
|
|
let nodeParagraph = this.shadowElement.querySelector("#node-name");
|
2022-12-14 23:28:22 +00:00
|
|
|
nodeParagraph.innerText = data.node.name;
|
2022-12-16 07:03:50 +00:00
|
|
|
this.node = data.node.name;
|
2022-12-15 01:20:31 +00:00
|
|
|
|
|
|
|
let resourceCPU = this.shadowElement.querySelector("#resource-cpu");
|
|
|
|
resourceCPU.innerText = data.cpus;
|
|
|
|
|
|
|
|
let resourceRAM = this.shadowElement.querySelector("#resource-ram");
|
|
|
|
resourceRAM.innerText = (data.maxmem / 1073741824).toFixed(3);
|
|
|
|
|
|
|
|
let resourceSWAP = this.shadowElement.querySelector("#resource-swap");
|
|
|
|
resourceSWAP.innerText = (data.maxswap / 1073741824).toFixed(3);
|
|
|
|
|
|
|
|
let resourceDISK = this.shadowElement.querySelector("#resource-disk");
|
2022-12-15 01:28:48 +00:00
|
|
|
resourceDISK.innerText = (data.maxdisk / 1073741824).toFixed(3);
|
2022-12-16 02:04:20 +00:00
|
|
|
|
|
|
|
let powerButton = this.shadowElement.querySelector("#power-btn");
|
2022-12-16 02:28:12 +00:00
|
|
|
powerButton.src = data.status === "running" ? "images/actions/stop.svg" : "images/actions/start.svg";
|
2022-12-18 00:34:40 +00:00
|
|
|
powerButton.addEventListener("click", async () => {
|
2022-12-18 00:07:18 +00:00
|
|
|
let targetAction = this.status === "running" ? "shutdown" : "start";
|
2022-12-18 00:34:40 +00:00
|
|
|
let targetStatus = this.status === "running" ? "stopped" : "running";
|
2022-12-18 00:37:26 +00:00
|
|
|
|
2022-12-18 00:34:40 +00:00
|
|
|
await request(`/nodes/${this.node}/${this.type}/${this.vmid}/status/${targetAction}`, "POST", {node: this.node, vmid: this.vmid});
|
2022-12-18 00:37:26 +00:00
|
|
|
|
2022-12-18 00:34:40 +00:00
|
|
|
while (true) {
|
|
|
|
let data = await request(`/nodes/${this.node}/${this.type}/${this.vmid}/status/current`);
|
|
|
|
if(data.data.status === targetStatus) {
|
|
|
|
break;
|
|
|
|
}
|
2022-12-18 00:35:51 +00:00
|
|
|
await waitFor(1000);
|
2022-12-18 00:34:40 +00:00
|
|
|
}
|
2022-12-18 00:37:26 +00:00
|
|
|
|
2022-12-18 00:34:40 +00:00
|
|
|
this.status = targetStatus;
|
2022-12-18 00:37:26 +00:00
|
|
|
|
2022-12-18 00:34:40 +00:00
|
|
|
let typeImg = this.shadowElement.querySelector("#instance-type");
|
|
|
|
typeImg.src = `images/instances/${this.type}/${this.status}.svg`;
|
2022-12-18 00:37:26 +00:00
|
|
|
|
|
|
|
let powerButton = this.shadowElement.querySelector("#power-btn");
|
|
|
|
powerButton.src = this.status === "running" ? "images/actions/stop.svg" : "images/actions/start.svg";
|
2022-12-16 07:03:50 +00:00
|
|
|
});
|
2022-12-16 02:28:12 +00:00
|
|
|
|
|
|
|
let configButton = this.shadowElement.querySelector("#configure-btn");
|
|
|
|
configButton.src = data.status === "running" ? "images/actions/config-inactive.svg" : "images/actions/config-active.svg";
|
2022-12-12 04:20:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-14 21:37:30 +00:00
|
|
|
customElements.define("instance-article", Instance);
|