2023-01-19 19:58:44 +00:00
|
|
|
import {requestPVE, goToPage} 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"});
|
|
|
|
|
2023-01-26 19:33:39 +00:00
|
|
|
shadowRoot.innerHTML = `
|
|
|
|
<article>
|
|
|
|
<div>
|
|
|
|
<div>
|
|
|
|
<img id="instance-type">
|
|
|
|
<p id="instance-id"></p>
|
|
|
|
<p id="instance-name"></p>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<img id="node-status" alt="instance node">
|
|
|
|
<p id="node-name"></p>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<hr>
|
|
|
|
<div class="btn-group">
|
|
|
|
<img id="power-btn">
|
|
|
|
<img id="configure-btn" alt="change instance configuration">
|
|
|
|
</div>
|
|
|
|
</article>
|
|
|
|
`;
|
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;
|
2022-12-19 00:53:07 +00:00
|
|
|
this.actionLock = false;
|
2022-12-12 04:20:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
set data (data) {
|
2023-01-17 20:36:34 +00:00
|
|
|
if (data.status === "unknown") {
|
|
|
|
data.status = "stopped";
|
|
|
|
}
|
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-18 01:36:45 +00:00
|
|
|
typeImg.alt = `${data.status} instance`;
|
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");
|
2023-01-17 20:33:21 +00:00
|
|
|
if (data.name) {
|
|
|
|
nameParagraph.innerText = data.name;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
nameParagraph.innerText = "";
|
|
|
|
}
|
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
|
|
|
|
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 01:36:45 +00:00
|
|
|
powerButton.alt = data.status === "running" ? "shutdown instance" : "start instance";
|
2022-12-18 00:34:40 +00:00
|
|
|
powerButton.addEventListener("click", async () => {
|
2022-12-19 00:56:02 +00:00
|
|
|
if (!this.actionLock) {
|
2022-12-19 00:53:07 +00:00
|
|
|
this.actionLock = true;
|
2022-12-19 01:11:35 +00:00
|
|
|
|
2022-12-19 00:53:07 +00:00
|
|
|
let targetAction = this.status === "running" ? "shutdown" : "start";
|
2022-12-19 01:11:35 +00:00
|
|
|
let targetActionDesc = targetAction === "start" ? "starting" : "shutting down";
|
2022-12-19 00:53:07 +00:00
|
|
|
let targetStatus = this.status === "running" ? "stopped" : "running";
|
|
|
|
|
2022-12-19 01:06:05 +00:00
|
|
|
let typeImg = this.shadowElement.querySelector("#instance-type");
|
|
|
|
typeImg.src = "images/actions/loading.svg";
|
2022-12-19 01:11:35 +00:00
|
|
|
typeImg.alt = `instance is ${targetActionDesc}`;
|
|
|
|
let powerButton = this.shadowElement.querySelector("#power-btn");
|
|
|
|
powerButton.src = "images/actions/loading.svg";
|
|
|
|
powerButton.alt = `instance is ${targetActionDesc}`;
|
2022-12-19 05:00:58 +00:00
|
|
|
let configButton = this.shadowElement.querySelector("#configure-btn");
|
|
|
|
configButton.src = "images/actions/config-inactive.svg";
|
2022-12-19 01:06:05 +00:00
|
|
|
|
2023-01-17 20:41:52 +00:00
|
|
|
try {
|
2023-01-19 19:58:44 +00:00
|
|
|
let task = await requestPVE(`/nodes/${this.node}/${this.type}/${this.vmid}/status/${targetAction}`, "POST", {node: this.node, vmid: this.vmid});
|
2023-01-17 20:41:52 +00:00
|
|
|
}
|
|
|
|
catch {
|
|
|
|
typeImg.src = `images/instances/${this.type}/${this.status}.svg`;
|
|
|
|
typeImg.alt = `${this.status} instance`;
|
|
|
|
|
|
|
|
powerButton.src = this.status === "running" ? "images/actions/stop.svg" : "images/actions/start.svg";
|
|
|
|
powerButton.alt = this.status === "running" ? "shutdown instance" : "start instance";
|
|
|
|
|
|
|
|
configButton.src = this.status === "running" ? "images/actions/config-inactive.svg" : "images/actions/config-active.svg";
|
|
|
|
|
|
|
|
this.actionLock = false;
|
|
|
|
|
|
|
|
console.error(`attempted to ${targetAction} ${this.vmid} but process returned stopped:${taskStatus.data.exitstatus}`);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2022-12-19 00:53:07 +00:00
|
|
|
|
|
|
|
while (true) {
|
2023-01-19 19:58:44 +00:00
|
|
|
let taskStatus = await requestPVE(`/nodes/${this.node}/tasks/${task.data}/status`);
|
2022-12-19 04:32:05 +00:00
|
|
|
if(taskStatus.data.status === "stopped" && taskStatus.data.exitstatus === "OK") {
|
|
|
|
this.status = targetStatus;
|
|
|
|
|
|
|
|
typeImg.src = `images/instances/${this.type}/${this.status}.svg`;
|
|
|
|
typeImg.alt = `${this.status} instance`;
|
|
|
|
|
|
|
|
powerButton.src = this.status === "running" ? "images/actions/stop.svg" : "images/actions/start.svg";
|
|
|
|
powerButton.alt = this.status === "running" ? "shutdown instance" : "start instance";
|
|
|
|
|
2022-12-19 05:05:05 +00:00
|
|
|
configButton.src = this.status === "running" ? "images/actions/config-inactive.svg" : "images/actions/config-active.svg";
|
2022-12-19 05:00:58 +00:00
|
|
|
|
2022-12-19 04:32:05 +00:00
|
|
|
this.actionLock = false;
|
|
|
|
|
2022-12-19 00:53:07 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-12-19 04:32:05 +00:00
|
|
|
else if (taskStatus.data.status === "stopped") { // stopped but not OK -> instance did not change state
|
|
|
|
typeImg.src = `images/instances/${this.type}/${this.status}.svg`;
|
|
|
|
typeImg.alt = `${this.status} instance`;
|
2022-12-18 00:37:26 +00:00
|
|
|
|
2022-12-19 04:32:05 +00:00
|
|
|
powerButton.src = this.status === "running" ? "images/actions/stop.svg" : "images/actions/start.svg";
|
|
|
|
powerButton.alt = this.status === "running" ? "shutdown instance" : "start instance";
|
2022-12-18 00:37:26 +00:00
|
|
|
|
2022-12-19 05:05:05 +00:00
|
|
|
configButton.src = this.status === "running" ? "images/actions/config-inactive.svg" : "images/actions/config-active.svg";
|
2022-12-19 05:00:58 +00:00
|
|
|
|
2022-12-19 04:32:05 +00:00
|
|
|
this.actionLock = false;
|
2022-12-18 00:37:26 +00:00
|
|
|
|
2022-12-19 04:32:05 +00:00
|
|
|
console.error(`attempted to ${targetAction} ${this.vmid} but process returned stopped:${taskStatus.data.exitstatus}`);
|
2022-12-19 01:11:35 +00:00
|
|
|
|
2022-12-19 04:32:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
await waitFor(1000);
|
|
|
|
}
|
|
|
|
}
|
2022-12-19 00:53:07 +00:00
|
|
|
}
|
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-19 05:52:39 +00:00
|
|
|
configButton.addEventListener("click", () => {
|
|
|
|
if (!this.actionLock && this.status !== "running") {
|
2022-12-20 00:13:43 +00:00
|
|
|
goToPage("config.html", {node: this.node, type: this.type, vmid: this.vmid});
|
2022-12-19 05:52:39 +00:00
|
|
|
}
|
|
|
|
})
|
2023-01-17 20:45:16 +00:00
|
|
|
|
|
|
|
if (data.node.status !== "online") {
|
|
|
|
powerButton.classList.add("hidden");
|
|
|
|
configButton.classList.add("hidden");
|
|
|
|
}
|
2022-12-12 04:20:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-14 21:37:30 +00:00
|
|
|
customElements.define("instance-article", Instance);
|