add js linting, fix js linting issues

This commit is contained in:
Arthur Lu 2023-06-29 22:20:15 +00:00
parent 6ce1cc332a
commit e66d3ceb33
9 changed files with 1643 additions and 1568 deletions

42
.eslintrc.json Normal file
View File

@ -0,0 +1,42 @@
{
"env": {
"browser": true,
"es2021": true
},
"extends": "standard",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"rules": {
"no-tabs": [
"error",
{
"allowIndentationTabs": true
}
],
"indent": [
"error",
"tab"
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"double"
],
"semi": [
"error",
"always"
],
"brace-style": [
"error",
"stroustrup",
{
"allowSingleLine": false
}
]
}
}

4
.gitignore vendored
View File

@ -1 +1,3 @@
vars.js vars.js
**/package-lock.json
**/node_modules

16
package.json Normal file
View File

@ -0,0 +1,16 @@
{
"name": "proxmoxaas-client",
"version": "0.0.1",
"description": "Front-end for ProxmoxAAS",
"type": "module",
"scripts": {
"lint": "eslint --fix ."
},
"devDependencies": {
"eslint": "^8.43.0",
"eslint-config-standard": "^17.1.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-n": "^16.0.1",
"eslint-plugin-promise": "^6.1.1"
}
}

View File

@ -2,7 +2,7 @@ import { requestAPI, goToPage, getCookie, setTitleAndHeader } from "./utils.js";
window.addEventListener("DOMContentLoaded", init); window.addEventListener("DOMContentLoaded", init);
let prefixes = { const prefixes = {
1024: [ 1024: [
"", "",
"Ki", "Ki",
@ -17,17 +17,17 @@ let prefixes = {
"G", "G",
"T" "T"
] ]
} };
async function init() { async function init () {
setTitleAndHeader(); setTitleAndHeader();
let cookie = document.cookie; const cookie = document.cookie;
if (cookie === "") { if (cookie === "") {
goToPage("login.html"); goToPage("login.html");
} }
let resources = await requestAPI("/user/resources"); const resources = await requestAPI("/user/resources");
let instances = await requestAPI("/user/config/cluster"); const instances = await requestAPI("/user/config/cluster");
let nodes = await requestAPI("/user/config/nodes"); const nodes = await requestAPI("/user/config/nodes");
document.querySelector("#username").innerText = `Username: ${getCookie("username")}`; document.querySelector("#username").innerText = `Username: ${getCookie("username")}`;
document.querySelector("#pool").innerText = `Pool: ${instances.pool}`; document.querySelector("#pool").innerText = `Pool: ${instances.pool}`;
document.querySelector("#vmid").innerText = `VMID Range: ${instances.vmid.min} - ${instances.vmid.max}`; document.querySelector("#vmid").innerText = `VMID Range: ${instances.vmid.min} - ${instances.vmid.max}`;
@ -35,24 +35,25 @@ async function init() {
buildResourceTable(resources, "#resource-table"); buildResourceTable(resources, "#resource-table");
} }
function buildResourceTable(resources, tableid) { function buildResourceTable (resources, tableid) {
if (resources instanceof Object) { if (resources instanceof Object) {
let table = document.querySelector(tableid); const table = document.querySelector(tableid);
let tbody = table.querySelector("tbody"); const tbody = table.querySelector("tbody");
Object.keys(resources.resources).forEach((element) => { Object.keys(resources.resources).forEach((element) => {
if (resources.resources[element].display) { if (resources.resources[element].display) {
if (resources.resources[element].type === "list") { if (resources.resources[element].type === "list") {
// TODO
console.error("Unimplemented");
} }
else { else {
let row = tbody.insertRow(); const row = tbody.insertRow();
let key = row.insertCell(); const key = row.insertCell();
key.innerText = `${element}`; key.innerText = `${element}`;
let used = row.insertCell(); const used = row.insertCell();
used.innerText = `${parseNumber(resources.used[element], resources.resources[element])}`; used.innerText = `${parseNumber(resources.used[element], resources.resources[element])}`;
let val = row.insertCell(); const val = row.insertCell();
val.innerText = `${parseNumber(resources.avail[element], resources.resources[element])}`; val.innerText = `${parseNumber(resources.avail[element], resources.resources[element])}`;
let total = row.insertCell(); const total = row.insertCell();
total.innerText = `${parseNumber(resources.max[element], resources.resources[element])}`; total.innerText = `${parseNumber(resources.max[element], resources.resources[element])}`;
} }
} }
@ -60,22 +61,22 @@ function buildResourceTable(resources, tableid) {
} }
} }
function parseNumber(value, unitData) { function parseNumber (value, unitData) {
let compact = unitData.compact; const compact = unitData.compact;
let multiplier = unitData.multiplier; const multiplier = unitData.multiplier;
let base = unitData.base; const base = unitData.base;
let unit = unitData.unit; const unit = unitData.unit;
value = multiplier * value; value = multiplier * value;
if (value <= 0) { if (value <= 0) {
return `0 ${unit}`; return `0 ${unit}`;
} }
else if (compact) { else if (compact) {
let exponent = Math.floor(Math.log(value) / Math.log(base)); const exponent = Math.floor(Math.log(value) / Math.log(base));
value = value / base ** exponent; value = value / base ** exponent;
let unitPrefix = prefixes[base][exponent]; const unitPrefix = prefixes[base][exponent];
return `${value} ${unitPrefix}${unit}` return `${value} ${unitPrefix}${unit}`;
} }
else { else {
return `${value} ${unit}`; return `${value} ${unit}`;
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,5 @@
export function dialog(header, body, callback = async (result, form) => { }) { export function dialog (header, body, callback = async (result, form) => { }) {
let dialog = document.createElement("dialog"); const dialog = document.createElement("dialog");
dialog.innerHTML = ` dialog.innerHTML = `
<p class="w3-large" id="prompt" style="text-align: center;"></p> <p class="w3-large" id="prompt" style="text-align: center;"></p>
<form method="dialog" class="input-grid" style="grid-template-columns: auto 1fr;" id="form"></form> <form method="dialog" class="input-grid" style="grid-template-columns: auto 1fr;" id="form"></form>
@ -23,8 +23,8 @@ export function dialog(header, body, callback = async (result, form) => { }) {
return dialog; return dialog;
} }
export function alert(message) { export function alert (message) {
let dialog = document.createElement("dialog"); const dialog = document.createElement("dialog");
dialog.innerHTML = ` dialog.innerHTML = `
<form method="dialog"> <form method="dialog">
<p class="w3-center" style="margin-bottom: 0px;">${message}</p> <p class="w3-center" style="margin-bottom: 0px;">${message}</p>
@ -40,7 +40,7 @@ export function alert(message) {
dialog.addEventListener("close", () => { dialog.addEventListener("close", () => {
dialog.parentElement.removeChild(dialog); dialog.parentElement.removeChild(dialog);
}) });
return dialog; return dialog;
} }

View File

@ -1,416 +1,424 @@
import { requestPVE, requestAPI, goToPage, goToURL, instances_config, nodes_config, setTitleAndHeader } from "./utils.js"; import { requestPVE, requestAPI, goToPage, goToURL, instancesConfig, nodesConfig, setTitleAndHeader } from "./utils.js";
import { alert, dialog } from "./dialog.js"; import { alert, dialog } from "./dialog.js";
import { PVE } from "../vars.js" import { PVE } from "../vars.js";
window.addEventListener("DOMContentLoaded", init); window.addEventListener("DOMContentLoaded", init);
async function init() { async function init () {
setTitleAndHeader(); setTitleAndHeader();
let cookie = document.cookie; const cookie = document.cookie;
if (cookie === "") { if (cookie === "") {
goToPage("login.html"); goToPage("login.html");
} }
await populateInstances(); await populateInstances();
let addInstanceBtn = document.querySelector("#instance-add"); const addInstanceBtn = document.querySelector("#instance-add");
addInstanceBtn.addEventListener("click", handleInstanceAdd); addInstanceBtn.addEventListener("click", handleInstanceAdd);
} }
async function populateInstances() { async function populateInstances () {
let resources = await requestPVE("/cluster/resources", "GET"); const resources = await requestPVE("/cluster/resources", "GET");
let instanceContainer = document.getElementById("instance-container"); const instanceContainer = document.getElementById("instance-container");
let instances = []; const instances = [];
resources.data.forEach((element) => { resources.data.forEach((element) => {
if (element.type === "lxc" || element.type === "qemu") { if (element.type === "lxc" || element.type === "qemu") {
let nodeName = element.node; const nodeName = element.node;
let nodeStatus = resources.data.find(item => item.node === nodeName && item.type === "node").status; const nodeStatus = resources.data.find(item => item.node === nodeName && item.type === "node").status;
element.node = { name: nodeName, status: nodeStatus }; element.node = { name: nodeName, status: nodeStatus };
instances.push(element); instances.push(element);
} }
}); });
instances.sort((a, b) => (a.vmid > b.vmid) ? 1 : -1); instances.sort((a, b) => (a.vmid > b.vmid) ? 1 : -1);
instanceContainer.innerHTML = ` instanceContainer.innerHTML = `
<div class="w3-row w3-hide-small" style="border-bottom: 1px solid;"> <div class="w3-row w3-hide-small" style="border-bottom: 1px solid;">
<div class="w3-col l1 m2"> <div class="w3-col l1 m2">
<p>VM ID</p> <p>VM ID</p>
</div> </div>
<div class="w3-col l2 m3"> <div class="w3-col l2 m3">
<p>VM Name</p> <p>VM Name</p>
</div> </div>
<div class="w3-col l1 m2"> <div class="w3-col l1 m2">
<p>VM Type</p> <p>VM Type</p>
</div> </div>
<div class="w3-col l2 m3"> <div class="w3-col l2 m3">
<p>VM Status</p> <p>VM Status</p>
</div> </div>
<div class="w3-col l2 w3-hide-medium"> <div class="w3-col l2 w3-hide-medium">
<p>Host Name</p> <p>Host Name</p>
</div> </div>
<div class="w3-col l2 w3-hide-medium"> <div class="w3-col l2 w3-hide-medium">
<p>Host Status</p> <p>Host Status</p>
</div> </div>
<div class="w3-col l2 m2"> <div class="w3-col l2 m2">
<p>Actions</p> <p>Actions</p>
</div> </div>
</div> </div>
`; `;
for (let i = 0; i < instances.length; i++) { for (let i = 0; i < instances.length; i++) {
let newInstance = new Instance(); const newInstance = new Instance();
newInstance.data = instances[i]; newInstance.data = instances[i];
instanceContainer.append(newInstance.shadowElement); instanceContainer.append(newInstance.shadowElement);
} }
} }
async function handleInstanceAdd() { async function handleInstanceAdd () {
let header = "Create New Instance"; const header = "Create New Instance";
let body = ` const body = `
<label for="type">Instance Type</label> <label for="type">Instance Type</label>
<select class="w3-select w3-border" name="type" id="type" required> <select class="w3-select w3-border" name="type" id="type" required>
<option value="lxc">Container</option> <option value="lxc">Container</option>
<option value="qemu">Virtual Machine</option> <option value="qemu">Virtual Machine</option>
</select> </select>
<label for="node">Node</label> <label for="node">Node</label>
<select class="w3-select w3-border" name="node" id="node" required></select> <select class="w3-select w3-border" name="node" id="node" required></select>
<label for="name">Name</label> <label for="name">Name</label>
<input class="w3-input w3-border" name="name" id="name" required></input> <input class="w3-input w3-border" name="name" id="name" required></input>
<label for="vmid">ID</label> <label for="vmid">ID</label>
<input class="w3-input w3-border" name="vmid" id="vmid" type="number" required></input> <input class="w3-input w3-border" name="vmid" id="vmid" type="number" required></input>
<label for="cores">Cores (Threads)</label> <label for="cores">Cores (Threads)</label>
<input class="w3-input w3-border" name="cores" id="cores" type="number" min="1" max="8192" required></input> <input class="w3-input w3-border" name="cores" id="cores" type="number" min="1" max="8192" required></input>
<label for="memory">Memory (MiB)</label> <label for="memory">Memory (MiB)</label>
<input class="w3-input w3-border" name="memory" id="memory" type="number" min="16", step="1" required></input> <input class="w3-input w3-border" name="memory" id="memory" type="number" min="16", step="1" required></input>
<p class="container-specific none" style="grid-column: 1 / span 2; text-align: center;">Container Options</p> <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> <label class="container-specific none" for="swap">Swap (MiB)</label>
<input class="w3-input w3-border container-specific none" name="swap" id="swap" type="number" min="0" step="1" required disabled></input> <input class="w3-input w3-border 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> <label class="container-specific none" for="template-storage">Template Storage</label>
<select class="w3-select w3-border container-specific none" name="template-storage" id="template-storage" required disabled></select> <select class="w3-select w3-border container-specific none" name="template-storage" id="template-storage" required disabled></select>
<label class="container-specific none" for="template-image">Template Image</label> <label class="container-specific none" for="template-image">Template Image</label>
<select class="w3-select w3-border container-specific none" name="template-image" id="template-image" required disabled></select> <select class="w3-select w3-border container-specific none" name="template-image" id="template-image" required disabled></select>
<label class="container-specific none" for="rootfs-storage">ROOTFS Storage</label> <label class="container-specific none" for="rootfs-storage">ROOTFS Storage</label>
<select class="w3-select w3-border container-specific none" name="rootfs-storage" id="rootfs-storage" required disabled></select> <select class="w3-select w3-border container-specific none" name="rootfs-storage" id="rootfs-storage" required disabled></select>
<label class="container-specific none" for="rootfs-size">ROOTFS Size (GiB)</label> <label class="container-specific none" for="rootfs-size">ROOTFS Size (GiB)</label>
<input class="w3-input w3-border container-specific none" name="rootfs-size" id="rootfs-size" type="number" min="0" max="131072" required disabled></input> <input class="w3-input w3-border container-specific none" name="rootfs-size" id="rootfs-size" type="number" min="0" max="131072" required disabled></input>
<label class="container-specific none" for="password">Password</label> <label class="container-specific none" for="password">Password</label>
<input class="w3-input w3-border container-specific none" name="password" id="password" type="password" required disabled></input> <input class="w3-input w3-border container-specific none" name="password" id="password" type="password" required disabled></input>
`; `;
let d = dialog(header, body, async (result, form) => { const d = dialog(header, body, async (result, form) => {
if (result === "confirm") { if (result === "confirm") {
let body = { const body = {
node: form.get("node"), node: form.get("node"),
type: form.get("type"), type: form.get("type"),
name: form.get("name"), name: form.get("name"),
vmid: form.get("vmid"), vmid: form.get("vmid"),
cores: form.get("cores"), cores: form.get("cores"),
memory: form.get("memory") memory: form.get("memory")
}; };
if (form.get("type") === "lxc") { if (form.get("type") === "lxc") {
body.swap = form.get("swap"); body.swap = form.get("swap");
body.password = form.get("password"); body.password = form.get("password");
body.ostemplate = form.get("template-image"); body.ostemplate = form.get("template-image");
body.rootfslocation = form.get("rootfs-storage"); body.rootfslocation = form.get("rootfs-storage");
body.rootfssize = form.get("rootfs-size"); body.rootfssize = form.get("rootfs-size");
} }
let result = await requestAPI("/instance", "POST", body); const result = await requestAPI("/instance", "POST", body);
if (result.status === 200) { if (result.status === 200) {
populateInstances(); populateInstances();
} }
else { else {
alert(result.error); alert(result.error);
populateInstances(); populateInstances();
} }
} }
}); });
let typeSelect = d.querySelector("#type"); const typeSelect = d.querySelector("#type");
typeSelect.selectedIndex = -1; typeSelect.selectedIndex = -1;
typeSelect.addEventListener("change", () => { typeSelect.addEventListener("change", () => {
if (typeSelect.value === "qemu") { if (typeSelect.value === "qemu") {
d.querySelectorAll(".container-specific").forEach((element) => { d.querySelectorAll(".container-specific").forEach((element) => {
element.classList.add("none"); element.classList.add("none");
element.disabled = true; element.disabled = true;
}); });
} }
else { else {
d.querySelectorAll(".container-specific").forEach((element) => { d.querySelectorAll(".container-specific").forEach((element) => {
element.classList.remove("none"); element.classList.remove("none");
element.disabled = false; element.disabled = false;
}); });
} }
}); });
let templateContent = "iso"; const templateContent = "iso";
let templateStorage = d.querySelector("#template-storage"); const templateStorage = d.querySelector("#template-storage");
templateStorage.selectedIndex = -1; templateStorage.selectedIndex = -1;
let rootfsContent = "rootdir"; const rootfsContent = "rootdir";
let rootfsStorage = d.querySelector("#rootfs-storage"); const rootfsStorage = d.querySelector("#rootfs-storage");
rootfsStorage.selectedIndex = -1; rootfsStorage.selectedIndex = -1;
let nodeSelect = d.querySelector("#node"); const nodeSelect = d.querySelector("#node");
let clusterNodes = await requestPVE("/nodes", "GET"); const clusterNodes = await requestPVE("/nodes", "GET");
let allowedNodes = await requestAPI("/user/config/nodes", "GET"); const allowedNodes = await requestAPI("/user/config/nodes", "GET");
clusterNodes.data.forEach((element) => { clusterNodes.data.forEach((element) => {
if (element.status === "online" && allowedNodes.includes(element.node)) { if (element.status === "online" && allowedNodes.includes(element.node)) {
nodeSelect.add(new Option(element.node)); nodeSelect.add(new Option(element.node));
} }
}); });
nodeSelect.selectedIndex = -1; nodeSelect.selectedIndex = -1;
nodeSelect.addEventListener("change", async () => { // change template and rootfs storage based on node nodeSelect.addEventListener("change", async () => { // change template and rootfs storage based on node
let node = nodeSelect.value; const node = nodeSelect.value;
let storage = await requestPVE(`/nodes/${node}/storage`, "GET"); const storage = await requestPVE(`/nodes/${node}/storage`, "GET");
storage.data.forEach((element) => { storage.data.forEach((element) => {
if (element.content.includes(templateContent)) { if (element.content.includes(templateContent)) {
templateStorage.add(new Option(element.storage)); templateStorage.add(new Option(element.storage));
} }
if (element.content.includes(rootfsContent)) { if (element.content.includes(rootfsContent)) {
rootfsStorage.add(new Option(element.storage)); rootfsStorage.add(new Option(element.storage));
} }
}); });
templateStorage.selectedIndex = -1; templateStorage.selectedIndex = -1;
rootfsStorage.selectedIndex = -1; rootfsStorage.selectedIndex = -1;
}); });
let templateImage = d.querySelector("#template-image"); // populate templateImage depending on selected image storage const templateImage = d.querySelector("#template-image"); // populate templateImage depending on selected image storage
templateStorage.addEventListener("change", async () => { templateStorage.addEventListener("change", async () => {
templateImage.innerHTML = ``; templateImage.innerHTML = "";
let content = "vztmpl"; const content = "vztmpl";
let images = await requestPVE(`/nodes/${nodeSelect.value}/storage/${templateStorage.value}/content`, "GET"); const images = await requestPVE(`/nodes/${nodeSelect.value}/storage/${templateStorage.value}/content`, "GET");
images.data.forEach((element) => { images.data.forEach((element) => {
if (element.content.includes(content)) { if (element.content.includes(content)) {
templateImage.append(new Option(element.volid.replace(`${templateStorage.value}:${content}/`, ""), element.volid)); templateImage.append(new Option(element.volid.replace(`${templateStorage.value}:${content}/`, ""), element.volid));
} }
}); });
templateImage.selectedIndex = -1; templateImage.selectedIndex = -1;
}); });
let userResources = await requestAPI("/user/resources", "GET"); const userResources = await requestAPI("/user/resources", "GET");
let userInstances = await requestAPI("/user/config/instances", "GET"); const userInstances = await requestAPI("/user/config/instances", "GET");
d.querySelector("#cores").max = userResources.avail.cores; d.querySelector("#cores").max = userResources.avail.cores;
d.querySelector("#memory").max = userResources.avail.memory; d.querySelector("#memory").max = userResources.avail.memory;
d.querySelector("#vmid").min = userInstances.vmid.min; d.querySelector("#vmid").min = userInstances.vmid.min;
d.querySelector("#vmid").max = userInstances.vmid.max; d.querySelector("#vmid").max = userInstances.vmid.max;
} }
class Instance { class Instance {
constructor() { constructor () {
let shadowRoot = document.createElement("div"); const shadowRoot = document.createElement("div");
shadowRoot.classList.add("w3-row"); shadowRoot.classList.add("w3-row");
shadowRoot.innerHTML = ` shadowRoot.innerHTML = `
<div class="w3-col l1 m2 s6"> <div class="w3-col l1 m2 s6">
<p id="instance-id"></p> <p id="instance-id"></p>
</div> </div>
<div class="w3-col l2 m3 s6"> <div class="w3-col l2 m3 s6">
<p id="instance-name"></p> <p id="instance-name"></p>
</div> </div>
<div class="w3-col l1 m2 w3-hide-small"> <div class="w3-col l1 m2 w3-hide-small">
<p id="instance-type"></p> <p id="instance-type"></p>
</div> </div>
<div class="w3-col l2 m3 s6 flex row nowrap"> <div class="w3-col l2 m3 s6 flex row nowrap">
<img id="instance-status-icon"> <img id="instance-status-icon">
<p id="instance-status"></p> <p id="instance-status"></p>
</div> </div>
<div class="w3-col l2 w3-hide-medium w3-hide-small"> <div class="w3-col l2 w3-hide-medium w3-hide-small">
<p id="node-name"></p> <p id="node-name"></p>
</div> </div>
<div class="w3-col l2 w3-hide-medium w3-hide-small flex row nowrap"> <div class="w3-col l2 w3-hide-medium w3-hide-small flex row nowrap">
<img id="node-status-icon"> <img id="node-status-icon">
<p id="node-status"></p> <p id="node-status"></p>
</div> </div>
<div class="w3-col l2 m2 s6 flex row nowrap" style="height: 1lh; margin-top: 15px; margin-bottom: 15px;"> <div class="w3-col l2 m2 s6 flex row nowrap" style="height: 1lh; margin-top: 15px; margin-bottom: 15px;">
<img id="power-btn"> <img id="power-btn">
<img id="console-btn"> <img id="console-btn">
<img id="configure-btn"> <img id="configure-btn">
<img id="delete-btn"> <img id="delete-btn">
</div> </div>
`; `;
this.shadowElement = shadowRoot; this.shadowElement = shadowRoot;
this.actionLock = false; this.actionLock = false;
} }
set data(data) { get data () {
if (data.status === "unknown") { return {
data.status = "stopped"; type: this.type,
} status: this.status,
this.type = data.type; vmid: this.status,
this.status = data.status; name: this.name,
this.vmid = data.vmid; node: this.node
this.name = data.name; };
this.node = data.node; }
this.update();
} set data (data) {
if (data.status === "unknown") {
update() { data.status = "stopped";
let vmidParagraph = this.shadowElement.querySelector("#instance-id"); }
vmidParagraph.innerText = this.vmid; this.type = data.type;
this.status = data.status;
let nameParagraph = this.shadowElement.querySelector("#instance-name"); this.vmid = data.vmid;
nameParagraph.innerText = this.name ? this.name : ""; this.name = data.name;
this.node = data.node;
let typeParagraph = this.shadowElement.querySelector("#instance-type"); this.update();
typeParagraph.innerText = this.type; }
let statusParagraph = this.shadowElement.querySelector("#instance-status"); update () {
statusParagraph.innerText = this.status; const vmidParagraph = this.shadowElement.querySelector("#instance-id");
vmidParagraph.innerText = this.vmid;
let statusIcon = this.shadowElement.querySelector("#instance-status-icon");
statusIcon.src = instances_config[this.status].status.src; const nameParagraph = this.shadowElement.querySelector("#instance-name");
statusIcon.alt = instances_config[this.status].status.alt; nameParagraph.innerText = this.name ? this.name : "";
let nodeNameParagraph = this.shadowElement.querySelector("#node-name"); const typeParagraph = this.shadowElement.querySelector("#instance-type");
nodeNameParagraph.innerText = this.node.name; typeParagraph.innerText = this.type;
let nodeStatusParagraph = this.shadowElement.querySelector("#node-status"); const statusParagraph = this.shadowElement.querySelector("#instance-status");
nodeStatusParagraph.innerText = this.node.status; statusParagraph.innerText = this.status;
let nodeStatusIcon = this.shadowElement.querySelector("#node-status-icon"); const statusIcon = this.shadowElement.querySelector("#instance-status-icon");
nodeStatusIcon.src = nodes_config[this.node.status].status.src; statusIcon.src = instancesConfig[this.status].status.src;
nodeStatusIcon.alt = nodes_config[this.node.status].status.src; statusIcon.alt = instancesConfig[this.status].status.alt;
let powerButton = this.shadowElement.querySelector("#power-btn"); const nodeNameParagraph = this.shadowElement.querySelector("#node-name");
powerButton.src = instances_config[this.status].power.src; nodeNameParagraph.innerText = this.node.name;
powerButton.alt = instances_config[this.status].power.alt;
powerButton.title = instances_config[this.status].power.alt; const nodeStatusParagraph = this.shadowElement.querySelector("#node-status");
if (instances_config[this.status].power.clickable) { nodeStatusParagraph.innerText = this.node.status;
powerButton.classList.add("clickable");
powerButton.onclick = this.handlePowerButton.bind(this) const nodeStatusIcon = this.shadowElement.querySelector("#node-status-icon");
} nodeStatusIcon.src = nodesConfig[this.node.status].status.src;
nodeStatusIcon.alt = nodesConfig[this.node.status].status.src;
let configButton = this.shadowElement.querySelector("#configure-btn");
configButton.src = instances_config[this.status].config.src; const powerButton = this.shadowElement.querySelector("#power-btn");
configButton.alt = instances_config[this.status].config.alt; powerButton.src = instancesConfig[this.status].power.src;
configButton.title = instances_config[this.status].config.alt; powerButton.alt = instancesConfig[this.status].power.alt;
if (instances_config[this.status].config.clickable) { powerButton.title = instancesConfig[this.status].power.alt;
configButton.classList.add("clickable"); if (instancesConfig[this.status].power.clickable) {
configButton.onclick = this.handleConfigButton.bind(this); powerButton.classList.add("clickable");
} powerButton.onclick = this.handlePowerButton.bind(this);
}
let consoleButton = this.shadowElement.querySelector("#console-btn");
consoleButton.src = instances_config[this.status].console.src; const configButton = this.shadowElement.querySelector("#configure-btn");
consoleButton.alt = instances_config[this.status].console.alt; configButton.src = instancesConfig[this.status].config.src;
consoleButton.title = instances_config[this.status].console.alt; configButton.alt = instancesConfig[this.status].config.alt;
if (instances_config[this.status].console.clickable) { configButton.title = instancesConfig[this.status].config.alt;
consoleButton.classList.add("clickable"); if (instancesConfig[this.status].config.clickable) {
consoleButton.onclick = this.handleConsoleButton.bind(this); configButton.classList.add("clickable");
} configButton.onclick = this.handleConfigButton.bind(this);
}
let deleteButton = this.shadowElement.querySelector("#delete-btn");
deleteButton.src = instances_config[this.status].delete.src; const consoleButton = this.shadowElement.querySelector("#console-btn");
deleteButton.alt = instances_config[this.status].delete.alt; consoleButton.src = instancesConfig[this.status].console.src;
deleteButton.title = instances_config[this.status].delete.alt; consoleButton.alt = instancesConfig[this.status].console.alt;
if (instances_config[this.status].delete.clickable) { consoleButton.title = instancesConfig[this.status].console.alt;
deleteButton.classList.add("clickable"); if (instancesConfig[this.status].console.clickable) {
deleteButton.onclick = this.handleDeleteButton.bind(this); consoleButton.classList.add("clickable");
} consoleButton.onclick = this.handleConsoleButton.bind(this);
}
if (this.node.status !== "online") {
powerButton.classList.add("hidden"); const deleteButton = this.shadowElement.querySelector("#delete-btn");
configButton.classList.add("hidden"); deleteButton.src = instancesConfig[this.status].delete.src;
consoleButton.classList.add("hidden"); deleteButton.alt = instancesConfig[this.status].delete.alt;
deleteButton.classList.add("hidden"); deleteButton.title = instancesConfig[this.status].delete.alt;
} if (instancesConfig[this.status].delete.clickable) {
} deleteButton.classList.add("clickable");
deleteButton.onclick = this.handleDeleteButton.bind(this);
async handlePowerButton() { }
if (!this.actionLock) {
let header = `${this.status === "running" ? "Stop" : "Start"} VM ${this.vmid}`; if (this.node.status !== "online") {
let body = `<p>Are you sure you want to ${this.status === "running" ? "stop" : "start"} VM</p><p>${this.vmid}</p>` powerButton.classList.add("hidden");
configButton.classList.add("hidden");
dialog(header, body, async (result, form) => { consoleButton.classList.add("hidden");
if (result === "confirm") { deleteButton.classList.add("hidden");
this.actionLock = true; }
let targetAction = this.status === "running" ? "stop" : "start"; }
let targetStatus = this.status === "running" ? "stopped" : "running";
let prevStatus = this.status; async handlePowerButton () {
this.status = "loading"; if (!this.actionLock) {
const header = `${this.status === "running" ? "Stop" : "Start"} VM ${this.vmid}`;
this.update(); const body = `<p>Are you sure you want to ${this.status === "running" ? "stop" : "start"} VM</p><p>${this.vmid}</p>`;
let result = await requestPVE(`/nodes/${this.node.name}/${this.type}/${this.vmid}/status/${targetAction}`, "POST", { node: this.node.name, vmid: this.vmid }); dialog(header, body, async (result, form) => {
if (result === "confirm") {
const waitFor = delay => new Promise(resolve => setTimeout(resolve, delay)); this.actionLock = true;
const targetAction = this.status === "running" ? "stop" : "start";
while (true) { const targetStatus = this.status === "running" ? "stopped" : "running";
let taskStatus = await requestPVE(`/nodes/${this.node.name}/tasks/${result.data}/status`, "GET"); const prevStatus = this.status;
if (taskStatus.data.status === "stopped" && taskStatus.data.exitstatus === "OK") { // task stopped and was successful this.status = "loading";
this.status = targetStatus;
this.update(); this.update();
this.actionLock = false;
break; const result = await requestPVE(`/nodes/${this.node.name}/${this.type}/${this.vmid}/status/${targetAction}`, "POST", { node: this.node.name, vmid: this.vmid });
}
else if (taskStatus.data.status === "stopped") { // task stopped but was not successful const waitFor = delay => new Promise(resolve => setTimeout(resolve, delay));
this.status = prevStatus;
alert(`attempted to ${targetAction} ${this.vmid} but process returned stopped:${result.data.exitstatus}`); while (true) {
this.update(); const taskStatus = await requestPVE(`/nodes/${this.node.name}/tasks/${result.data}/status`, "GET");
this.actionLock = false; if (taskStatus.data.status === "stopped" && taskStatus.data.exitstatus === "OK") { // task stopped and was successful
break; this.status = targetStatus;
} this.update();
else { // task has not stopped this.actionLock = false;
await waitFor(1000); 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;
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 else { // task has not stopped
goToPage("config.html", { node: this.node.name, type: this.type, vmid: this.vmid }); await waitFor(1000);
} }
} }
}
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); 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 });
}
handleDeleteButton() { }
if (!this.actionLock && this.status === "stopped") {
handleConsoleButton () {
let header = `Delete VM ${this.vmid}`; if (!this.actionLock && this.status === "running") {
let body = `<p>Are you sure you want to <strong>delete</strong> VM </p><p>${this.vmid}</p>` const 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;
dialog(header, body, async (result, form) => { goToURL(PVE, data, true);
if (result === "confirm") { }
this.actionLock = true; }
let prevStatus = this.status;
this.status = "loading"; handleDeleteButton () {
this.update(); if (!this.actionLock && this.status === "stopped") {
const header = `Delete VM ${this.vmid}`;
let action = {}; const body = `<p>Are you sure you want to <strong>delete</strong> VM </p><p>${this.vmid}</p>`;
action.purge = 1;
action["destroy-unreferenced-disks"] = 1; dialog(header, body, async (result, form) => {
if (result === "confirm") {
let body = { this.actionLock = true;
node: this.node.name, this.status = "loading";
type: this.type, this.update();
vmid: this.vmid,
action: JSON.stringify(action) const action = {};
}; action.purge = 1;
action["destroy-unreferenced-disks"] = 1;
let result = await requestAPI("/instance", "DELETE", body);
if (result.status === 200) { const body = {
this.shadowElement.parentElement.removeChild(this.shadowElement); node: this.node.name,
} type: this.type,
else { vmid: this.vmid,
alert(result.error); action: JSON.stringify(action)
this.status = this.prevStatus; };
this.update();
this.actionLock = false; const 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;
}
}
});
}
}
}

View File

@ -1,43 +1,43 @@
import { requestTicket, goToPage, deleteAllCookies, requestPVE, setTitleAndHeader } from "./utils.js"; import { requestTicket, goToPage, deleteAllCookies, requestPVE, setTitleAndHeader } from "./utils.js";
import { alert } from "./dialog.js"; import { alert } from "./dialog.js";
window.addEventListener("DOMContentLoaded", init); window.addEventListener("DOMContentLoaded", init);
async function init() { async function init () {
setTitleAndHeader(); setTitleAndHeader();
await deleteAllCookies(); await deleteAllCookies();
let formSubmitButton = document.querySelector("#submit"); const formSubmitButton = document.querySelector("#submit");
let realms = await requestPVE("/access/domains", "GET"); const realms = await requestPVE("/access/domains", "GET");
let realmSelect = document.querySelector("#realm"); const realmSelect = document.querySelector("#realm");
realms.data.forEach((element) => { realms.data.forEach((element) => {
realmSelect.add(new Option(element.comment, element.realm)); realmSelect.add(new Option(element.comment, element.realm));
if ("default" in element && element.default === 1) { if ("default" in element && element.default === 1) {
realmSelect.value = element.realm; realmSelect.value = element.realm;
} }
}); });
formSubmitButton.addEventListener("click", async (e) => { formSubmitButton.addEventListener("click", async (e) => {
e.preventDefault(); e.preventDefault();
let form = document.querySelector("form"); const form = document.querySelector("form");
let formData = new FormData(form); const formData = new FormData(form);
formSubmitButton.innerText = "Authenticating..."; formSubmitButton.innerText = "Authenticating...";
let ticket = await requestTicket(formData.get("username"), formData.get("password"), formData.get("realm")); const ticket = await requestTicket(formData.get("username"), formData.get("password"), formData.get("realm"));
if (ticket.status === 200) { if (ticket.status === 200) {
formSubmitButton.innerText = "LOGIN"; formSubmitButton.innerText = "LOGIN";
goToPage("index.html"); goToPage("index.html");
} }
else if (ticket.status === 401) { else if (ticket.status === 401) {
alert("Authenticaton failed."); alert("Authenticaton failed.");
formSubmitButton.innerText = "LOGIN"; formSubmitButton.innerText = "LOGIN";
} }
else if (ticket.status === 408) { else if (ticket.status === 408) {
alert("Network error."); alert("Network error.");
formSubmitButton.innerText = "LOGIN"; formSubmitButton.innerText = "LOGIN";
} }
else { else {
alert("An error occured."); alert("An error occured.");
formSubmitButton.innerText = "LOGIN"; formSubmitButton.innerText = "LOGIN";
console.error(ticket.error); console.error(ticket.error);
} }
}); });
} }

View File

@ -1,253 +1,253 @@
import { API, organization } from "/vars.js"; import { API, organization } from "../../../../../vars.js";
export const resources_config = { export const resourcesConfig = {
disk: { disk: {
actionBarOrder: ["move", "resize", "detach_attach", "delete"], actionBarOrder: ["move", "resize", "detach_attach", "delete"],
lxc: { lxc: {
prefixOrder: ["rootfs", "mp", "unused"], prefixOrder: ["rootfs", "mp", "unused"],
rootfs: { name: "ROOTFS", icon: "images/resources/drive.svg", actions: ["move", "resize"] }, rootfs: { name: "ROOTFS", icon: "images/resources/drive.svg", actions: ["move", "resize"] },
mp: { name: "MP", icon: "images/resources/drive.svg", actions: ["detach", "move", "reassign", "resize"] }, mp: { name: "MP", icon: "images/resources/drive.svg", actions: ["detach", "move", "reassign", "resize"] },
unused: { name: "UNUSED", icon: "images/resources/drive.svg", actions: ["attach", "delete", "reassign"] } unused: { name: "UNUSED", icon: "images/resources/drive.svg", actions: ["attach", "delete", "reassign"] }
}, },
qemu: { qemu: {
prefixOrder: ["ide", "sata", "unused"], prefixOrder: ["ide", "sata", "unused"],
ide: { name: "IDE", icon: "images/resources/disk.svg", actions: ["delete"] }, ide: { name: "IDE", icon: "images/resources/disk.svg", actions: ["delete"] },
sata: { name: "SATA", icon: "images/resources/drive.svg", actions: ["detach", "move", "reassign", "resize"] }, sata: { name: "SATA", icon: "images/resources/drive.svg", actions: ["detach", "move", "reassign", "resize"] },
unused: { name: "UNUSED", icon: "images/resources/drive.svg", actions: ["attach", "delete", "reassign"] } unused: { name: "UNUSED", icon: "images/resources/drive.svg", actions: ["attach", "delete", "reassign"] }
} }
}, },
network: { network: {
prefix: "net" prefix: "net"
}, },
pcie: { pcie: {
prefix: "hostpci" prefix: "hostpci"
} }
} };
export const instances_config = { export const instancesConfig = {
running: { running: {
status: { status: {
src: "images/status/active.svg", src: "images/status/active.svg",
alt: "Instance is running", alt: "Instance is running",
clickable: false clickable: false
}, },
power: { power: {
src: "images/actions/instance/stop.svg", src: "images/actions/instance/stop.svg",
alt: "Shutdown Instance", alt: "Shutdown Instance",
clickable: true, clickable: true
}, },
config: { config: {
src: "images/actions/instance/config-inactive.svg", src: "images/actions/instance/config-inactive.svg",
alt: "Change Configuration (Inactive)", alt: "Change Configuration (Inactive)",
clickable: false, clickable: false
}, },
console: { console: {
src: "images/actions/instance/console-active.svg", src: "images/actions/instance/console-active.svg",
alt: "Open Console", alt: "Open Console",
clickable: true, clickable: true
}, },
delete: { delete: {
src: "images/actions/delete-inactive.svg", src: "images/actions/delete-inactive.svg",
alt: "Delete Instance (Inactive)", alt: "Delete Instance (Inactive)",
clickable: false, clickable: false
} }
}, },
stopped: { stopped: {
status: { status: {
src: "images/status/inactive.svg", src: "images/status/inactive.svg",
alt: "Instance is stopped", alt: "Instance is stopped",
clickable: false clickable: false
}, },
power: { power: {
src: "images/actions/instance/start.svg", src: "images/actions/instance/start.svg",
alt: "Start Instance", alt: "Start Instance",
clickable: true, clickable: true
}, },
config: { config: {
src: "images/actions/instance/config-active.svg", src: "images/actions/instance/config-active.svg",
alt: "Change Configuration", alt: "Change Configuration",
clickable: true, clickable: true
}, },
console: { console: {
src: "images/actions/instance/console-inactive.svg", src: "images/actions/instance/console-inactive.svg",
alt: "Open Console (Inactive)", alt: "Open Console (Inactive)",
clickable: false, clickable: false
}, },
delete: { delete: {
src: "images/actions/delete-active.svg", src: "images/actions/delete-active.svg",
alt: "Delete Instance", alt: "Delete Instance",
clickable: true, clickable: true
} }
}, },
loading: { loading: {
status: { status: {
src: "images/status/loading.svg", src: "images/status/loading.svg",
alt: "Instance is loading", alt: "Instance is loading",
clickable: false clickable: false
}, },
power: { power: {
src: "images/status/loading.svg", src: "images/status/loading.svg",
alt: "Loading Instance", alt: "Loading Instance",
clickable: false, clickable: false
}, },
config: { config: {
src: "images/actions/instance/config-inactive.svg", src: "images/actions/instance/config-inactive.svg",
alt: "Change Configuration (Inactive)", alt: "Change Configuration (Inactive)",
clickable: false, clickable: false
}, },
console: { console: {
src: "images/actions/instance/console-inactive.svg", src: "images/actions/instance/console-inactive.svg",
alt: "Open Console (Inactive)", alt: "Open Console (Inactive)",
clickable: false, clickable: false
}, },
delete: { delete: {
src: "images/actions/delete-inactive.svg", src: "images/actions/delete-inactive.svg",
alt: "Delete Instance (Inactive)", alt: "Delete Instance (Inactive)",
clickable: false, clickable: false
} }
} }
} };
export const nodes_config = { export const nodesConfig = {
online: { online: {
status: { status: {
src: "images/status/active.svg", src: "images/status/active.svg",
alt: "Node is online" alt: "Node is online"
} }
}, },
offline: { offline: {
status: { status: {
src: "images/status/inactive.svg", src: "images/status/inactive.svg",
alt: "Node is offline" alt: "Node is offline"
} }
}, },
uknown: { uknown: {
status: { status: {
src: "images/status/inactive.svg", src: "images/status/inactive.svg",
alt: "Node is offline" alt: "Node is offline"
} }
} }
} };
export function getCookie(cname) { export function getCookie (cname) {
let name = cname + "="; const name = cname + "=";
let decodedCookie = decodeURIComponent(document.cookie); const decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(";"); const ca = decodedCookie.split(";");
for (let i = 0; i < ca.length; i++) { for (let i = 0; i < ca.length; i++) {
let c = ca[i]; let c = ca[i];
while (c.charAt(0) === " ") { while (c.charAt(0) === " ") {
c = c.substring(1); c = c.substring(1);
} }
if (c.indexOf(name) === 0) { if (c.indexOf(name) === 0) {
return c.substring(name.length, c.length); return c.substring(name.length, c.length);
} }
} }
return ""; return "";
} }
export async function requestTicket(username, password, realm) { export async function requestTicket (username, password, realm) {
let response = await requestAPI("/ticket", "POST", { username: `${username}@${realm}`, password: password }, false); const response = await requestAPI("/ticket", "POST", { username: `${username}@${realm}`, password }, false);
return response; return response;
} }
export async function requestPVE(path, method, body = null) { export async function requestPVE (path, method, body = null) {
let prms = new URLSearchParams(body); const prms = new URLSearchParams(body);
let content = { const content = {
method: method, method,
mode: "cors", mode: "cors",
credentials: "include", credentials: "include",
headers: { headers: {
"Content-Type": "application/x-www-form-urlencoded" "Content-Type": "application/x-www-form-urlencoded"
} }
} };
if (method === "POST") { if (method === "POST") {
content.body = prms.toString(); content.body = prms.toString();
content.headers.CSRFPreventionToken = getCookie("CSRFPreventionToken"); content.headers.CSRFPreventionToken = getCookie("CSRFPreventionToken");
} }
let response = await request(`${API}/proxmox${path}`, content); const response = await request(`${API}/proxmox${path}`, content);
return response; return response;
} }
export async function requestAPI(path, method, body = null) { export async function requestAPI (path, method, body = null) {
let prms = new URLSearchParams(body); const prms = new URLSearchParams(body);
let content = { const content = {
method: method, method,
mode: "cors", mode: "cors",
credentials: "include", credentials: "include",
headers: { headers: {
"Content-Type": "application/x-www-form-urlencoded" "Content-Type": "application/x-www-form-urlencoded"
} }
} };
if (method === "POST" || method === "DELETE") { if (method === "POST" || method === "DELETE") {
content.headers.CSRFPreventionToken = getCookie("CSRFPreventionToken"); content.headers.CSRFPreventionToken = getCookie("CSRFPreventionToken");
} }
if (body) { if (body) {
content.body = prms.toString(); content.body = prms.toString();
} }
let response = await request(`${API}${path}`, content); const response = await request(`${API}${path}`, content);
return response; return response;
} }
async function request(url, content) { async function request (url, content) {
let response = await fetch(url, content); const response = await fetch(url, content);
let data = null; let data = null;
try { try {
data = await response.json(); data = await response.json();
data.status = response.status; data.status = response.status;
} }
catch { catch {
data = null; data = null;
} }
if (!response.ok) { if (!response.ok) {
return { status: response.status, error: data ? data.error : response.status }; return { status: response.status, error: data ? data.error : response.status };
} }
else { else {
data.status = response.status; data.status = response.status;
return data ? data : response; return data || response;
} }
} }
export function goToPage(page, data = {}, newwindow = false) { export function goToPage (page, data = {}, newwindow = false) {
let url = new URL(`https://${window.location.host}/${page}`); const url = new URL(`https://${window.location.host}/${page}`);
for (let k in data) { for (const k in data) {
url.searchParams.append(k, data[k]); url.searchParams.append(k, data[k]);
} }
if (newwindow) { if (newwindow) {
window.open(url, `${organization} - client`, "height=480,width=848"); window.open(url, `${organization} - client`, "height=480,width=848");
} }
else { else {
window.location.assign(url.toString()); window.location.assign(url.toString());
} }
} }
export function goToURL(href, data = {}, newwindow = false) { export function goToURL (href, data = {}, newwindow = false) {
let url = new URL(href); const url = new URL(href);
for (let k in data) { for (const k in data) {
url.searchParams.append(k, data[k]); url.searchParams.append(k, data[k]);
} }
if (newwindow) { if (newwindow) {
window.open(url, `${organization} - client`, "height=480,width=848"); window.open(url, `${organization} - client`, "height=480,width=848");
} }
else { else {
window.location.assign(url.toString()); window.location.assign(url.toString());
} }
} }
export function getURIData() { export function getURIData () {
let url = new URL(window.location.href); const url = new URL(window.location.href);
return Object.fromEntries(url.searchParams); return Object.fromEntries(url.searchParams);
} }
export async function deleteAllCookies() { export async function deleteAllCookies () {
await requestAPI("/ticket", "DELETE"); await requestAPI("/ticket", "DELETE");
} }
export function setTitleAndHeader() { export function setTitleAndHeader () {
document.title = `${organization} - client`; document.title = `${organization} - client`;
document.querySelector("h1").innerText = organization; document.querySelector("h1").innerText = organization;
} }