ProxmoxAAS-Dashboard/scripts/config.js

824 lines
26 KiB
JavaScript
Raw Normal View History

2023-05-17 21:40:37 +00:00
import { requestPVE, requestAPI, goToPage, getURIData, resources_config, setTitleAndHeader } from "./utils.js";
import { alert, dialog } from "./dialog.js";
window.addEventListener("DOMContentLoaded", init); // do the dumb thing where the disk config refreshes every second
let diskMetaData = resources_config.disk;
let networkMetaData = resources_config.network;
let pcieMetaData = resources_config.pcie;
2023-01-11 10:57:37 +00:00
let node;
let type;
let vmid;
let config;
2023-01-11 10:57:37 +00:00
2023-05-17 21:40:37 +00:00
async function init() {
setTitleAndHeader();
2022-12-20 23:15:08 +00:00
let cookie = document.cookie;
if (cookie === "") {
goToPage("login.html");
}
2023-05-17 21:40:37 +00:00
let uriData = getURIData();
2023-01-11 10:57:37 +00:00
node = uriData.node;
type = uriData.type;
vmid = uriData.vmid;
await getConfig();
populateResources();
populateDisk();
populateNetworks();
populateDevices();
2022-12-20 23:42:13 +00:00
document.querySelector("#exit").addEventListener("click", handleFormExit);
}
2023-05-17 21:40:37 +00:00
function getOrdered(keys) {
let ordered_keys = Object.keys(keys).sort((a, b) => { parseInt(a) - parseInt(b) }); // ordered integer list
return ordered_keys;
}
2023-05-17 21:40:37 +00:00
async function getConfig() {
config = await requestPVE(`/nodes/${node}/${type}/${vmid}/config`, "GET");
}
async function populateResources() {
2022-12-21 23:10:13 +00:00
let name = type === "qemu" ? "name" : "hostname";
document.querySelector("#name").innerHTML = document.querySelector("#name").innerHTML.replace("%{vmname}", config.data[name]);
2023-06-14 01:13:06 +00:00
if (type === "qemu") {
let global = await requestAPI("/global/config/resources");
let user = await requestAPI("/user/config/resources");
let options = [];
if (global.cpu.whitelist) {
options = user.max.cpu.sort((a, b) => { return a.localeCompare(b) });
}
else {
let supported = await requestPVE(`/nodes/${node}/capabilities/qemu/cpu`);
supported.data.forEach((element) => {
if (!user.max.cpu.includes(element.name)) {
options.push(element.name);
}
});
options = options.sort((a, b) => { return a.localeCompare(b) })
console.log(options);
console.log("blacklist not yet supported")
}
addResourceLine("resources", "images/resources/cpu.svg", "select", "CPU Type", "proctype", { value: config.data.cpu, options: options });
2023-06-14 01:13:06 +00:00
}
addResourceLine("resources", "images/resources/cpu.svg", "input", "CPU Amount", "cores", { type: "number", value: config.data.cores, min: 1, max: 8192 }, "Cores");
2023-06-14 01:13:06 +00:00
addResourceLine("resources", "images/resources/ram.svg", "input", "Memory", "ram", { type: "number", value: config.data.memory, min: 16, step: 1 }, "MiB");
2022-12-21 23:39:09 +00:00
if (type === "lxc") {
addResourceLine("resources", "images/resources/swap.svg", "input", "Swap", "swap", { type: "number", value: config.data.swap, min: 0, step: 1 }, "MiB");
}
2023-01-11 10:43:56 +00:00
}
2023-06-14 01:13:06 +00:00
function addResourceLine(fieldset, iconHref, type, labelText, id, attributes, unitText = null) {
2022-12-21 08:49:57 +00:00
let field = document.querySelector(`#${fieldset}`);
2022-12-20 02:46:28 +00:00
2023-01-09 04:26:20 +00:00
let icon = document.createElement("img");
icon.src = iconHref;
icon.alt = labelText;
2023-01-09 04:26:20 +00:00
field.append(icon);
2022-12-21 23:56:52 +00:00
2022-12-20 01:59:34 +00:00
let label = document.createElement("label");
label.innerText = labelText;
label.htmlFor = labelText;
2022-12-21 08:49:57 +00:00
field.append(label);
2022-12-20 01:59:34 +00:00
2023-06-14 01:13:06 +00:00
if (type === "input") {
let input = document.createElement("input");
for (let k in attributes) {
input.setAttribute(k, attributes[k])
}
input.id = id;
input.name = id;
input.required = true;
input.classList.add("w3-input");
input.classList.add("w3-border");
field.append(input);
}
else if (type === "select") {
let select = document.createElement("select");
for (let option of attributes.options) {
select.append(new Option(option));
}
select.value = attributes.value;
2023-06-14 01:13:06 +00:00
select.id = id;
select.name = id;
select.required = true;
select.classList.add("w3-select");
select.classList.add("w3-border");
field.append(select);
2022-12-20 01:59:34 +00:00
}
2022-12-21 08:49:57 +00:00
2022-12-21 09:31:58 +00:00
if (unitText) {
let unit = document.createElement("p");
unit.innerText = unitText;
field.append(unit);
}
2023-06-14 01:13:06 +00:00
else {
let unit = document.createElement("div");
unit.classList.add("hidden");
field.append(unit);
}
2023-01-09 04:26:20 +00:00
}
async function populateDisk() {
document.querySelector("#disks").innerHTML = "";
2023-05-17 21:40:37 +00:00
for (let i = 0; i < diskMetaData[type].prefixOrder.length; i++) {
let prefix = diskMetaData[type].prefixOrder[i];
let busName = diskMetaData[type][prefix].name;
let disks = {};
Object.keys(config.data).forEach((element) => {
if (element.startsWith(prefix)) {
disks[element.replace(prefix, "")] = config.data[element];
}
});
let ordered_keys = getOrdered(disks);
ordered_keys.forEach((element) => {
let disk = disks[element];
addDiskLine("disks", prefix, busName, element, disk);
});
}
document.querySelector("#disk-add").addEventListener("click", handleDiskAdd);
if (type === "qemu") {
document.querySelector("#cd-add").classList.remove("none");
document.querySelector("#cd-add").addEventListener("click", handleCDAdd);
}
}
2023-05-17 21:40:37 +00:00
function addDiskLine(fieldset, busPrefix, busName, device, diskDetails) {
2023-01-09 04:26:20 +00:00
let field = document.querySelector(`#${fieldset}`);
let diskName = `${busName} ${device}`;
let diskID = `${busPrefix}${device}`;
2023-05-17 21:40:37 +00:00
// Set the disk icon, either drive.svg or disk.svg
2023-01-09 04:26:20 +00:00
let icon = document.createElement("img");
icon.src = diskMetaData[type][busPrefix].icon;
icon.alt = diskName;
icon.dataset.disk = diskID;
2023-01-09 04:26:20 +00:00
field.append(icon);
// Add a label for the disk bus and device number
let diskLabel = document.createElement("label");
diskLabel.innerText = diskName;
diskLabel.dataset.disk = diskID;
field.append(diskLabel);
// Add text of the disk configuration
let diskDesc = document.createElement("p");
diskDesc.innerText = diskDetails;
diskDesc.dataset.disk = diskID;
2023-06-09 06:55:14 +00:00
diskDesc.style.overflowX = "hidden";
diskDesc.style.whiteSpace = "nowrap";
field.append(diskDesc);
let actionDiv = document.createElement("div");
diskMetaData.actionBarOrder.forEach((element) => {
let action = document.createElement("img");
2023-05-17 21:40:37 +00:00
if (element === "detach_attach" && diskMetaData[type][busPrefix].actions.includes("attach")) { // attach
action.src = "images/actions/disk/attach.svg";
action.title = "Attach Disk";
action.addEventListener("click", handleDiskAttach);
action.classList.add("clickable");
}
2023-05-17 21:40:37 +00:00
else if (element === "detach_attach" && diskMetaData[type][busPrefix].actions.includes("detach")) { // detach
action.src = "images/actions/disk/detach.svg";
action.title = "Detach Disk";
2023-01-25 02:06:31 +00:00
action.addEventListener("click", handleDiskDetach);
action.classList.add("clickable");
}
else if (element === "delete") {
let active = diskMetaData[type][busPrefix].actions.includes(element) ? "active" : "inactive"; // resize
action.src = `images/actions/delete-${active}.svg`;
action.title = "Delete Disk";
if (active === "active") {
action.addEventListener("click", handleDiskDelete);
action.classList.add("clickable");
2023-05-17 21:40:37 +00:00
}
}
else {
let active = diskMetaData[type][busPrefix].actions.includes(element) ? "active" : "inactive"; // resize
action.src = `images/actions/disk/${element}-${active}.svg`;
if (active === "active") {
action.title = `${element.charAt(0).toUpperCase()}${element.slice(1)} Disk`;
if (element === "move") {
action.addEventListener("click", handleDiskMove);
}
else if (element === "resize") {
action.addEventListener("click", handleDiskResize);
}
action.classList.add("clickable");
}
}
action.dataset.disk = diskID;
action.alt = action.title;
actionDiv.append(action);
});
field.append(actionDiv);
}
2023-05-17 21:40:37 +00:00
async function handleDiskDetach() {
let header = `Detach ${this.dataset.disk}`;
let body = `<p>Are you sure you want to detach disk</p><p>${this.dataset.disk}</p>`;
dialog(header, body, async (result, form) => {
if (result === "confirm") {
document.querySelector(`img[data-disk="${this.dataset.disk}"]`).src = "images/status/loading.svg";
let body = {
node: node,
type: type,
vmid: vmid,
2023-02-27 02:29:55 +00:00
disk: this.dataset.disk
};
let result = await requestAPI("/instance/disk/detach", "POST", body);
if (result.status === 200) {
await getConfig();
populateDisk();
}
else {
alert(result.error);
await getConfig();
populateDisk();
}
}
});
}
2023-05-17 21:40:37 +00:00
async function handleDiskAttach() {
2023-05-18 08:09:23 +00:00
let header = `Attach ${this.dataset.disk}`;
let body = `<label for="device">${type === "qemu" ? "SATA" : "MP"}</label><input class="w3-input w3-border" name="device" id="device" type="number" min="0" max="${type === "qemu" ? "5" : "255"}" required></input>`;
dialog(header, body, async (result, form) => {
if (result === "confirm") {
let device = form.get("device");
2023-05-17 21:40:37 +00:00
document.querySelector(`img[data-disk="${this.dataset.disk}"]`).src = "images/status/loading.svg";
let body = {
node: node,
type: type,
vmid: vmid,
2023-02-27 02:29:55 +00:00
disk: `${type === "qemu" ? "sata" : "mp"}${device}`,
2023-05-18 08:09:23 +00:00
source: this.dataset.disk.replace("unused", "")
}
let result = await requestAPI("/instance/disk/attach", "POST", body);
if (result.status === 200) {
await getConfig();
populateDisk();
}
else {
alert(result.error);
await getConfig();
populateDisk();
}
}
});
}
2023-05-17 21:40:37 +00:00
async function handleDiskResize() {
let header = `Resize ${this.dataset.disk}`;
let body = `<label for="size-increment">Size Increment (GiB)</label><input class="w3-input w3-border" name="size-increment" id="size-increment" type="number" min="0" max="131072"></input>`;
dialog(header, body, async (result, form) => {
if (result === "confirm") {
document.querySelector(`img[data-disk="${this.dataset.disk}"]`).src = "images/status/loading.svg";
let body = {
node: node,
type: type,
vmid: vmid,
2023-02-27 02:29:55 +00:00
disk: this.dataset.disk,
size: form.get("size-increment")
}
let result = await requestAPI("/instance/disk/resize", "POST", body);
if (result.status === 200) {
await getConfig();
populateDisk();
}
2023-05-17 21:40:37 +00:00
else {
alert(result.error);
await getConfig();
populateDisk();
}
}
});
2023-01-25 02:06:31 +00:00
}
2023-05-17 21:40:37 +00:00
async function handleDiskMove() {
let content = type === "qemu" ? "images" : "rootdir";
2023-05-11 07:13:41 +00:00
let storage = await requestPVE(`/nodes/${node}/storage`, "GET");
let header = `Move ${this.dataset.disk}`;
let options = "";
storage.data.forEach((element) => {
2023-05-17 21:40:37 +00:00
if (element.content.includes(content)) {
options += `<option value="${element.storage}">${element.storage}</option>"`;
}
});
let select = `<label for="storage-select">Storage</label><select class="w3-select w3-border" name="storage-select" id="storage-select"><option hidden disabled selected value></option>${options}</select>`;
let body = `
${select}
<label for="delete-check">Delete Source</label><input class="w3-input w3-border" name="delete-check" id="delete-check" type="checkbox" checked required>
`;
dialog(header, body, async (result, form) => {
if (result === "confirm") {
document.querySelector(`img[data-disk="${this.dataset.disk}"]`).src = "images/status/loading.svg";
let body = {
node: node,
type: type,
vmid: vmid,
2023-02-27 02:29:55 +00:00
disk: this.dataset.disk,
storage: form.get("storage-select"),
2023-05-17 21:40:37 +00:00
delete: form.get("delete-check") === "on" ? "1" : "0"
}
let result = await requestAPI("/instance/disk/move", "POST", body);
if (result.status === 200) {
await getConfig();
populateDisk();
}
else {
alert(result.error);
await getConfig();
populateDisk();
}
}
});
}
2023-05-17 21:40:37 +00:00
async function handleDiskDelete() {
let header = `Delete ${this.dataset.disk}`;
let body = `<p>Are you sure you want to <strong>delete</strong> disk</p><p>${this.dataset.disk}</p>`;
dialog(header, body, async (result, form) => {
if (result === "confirm") {
document.querySelector(`img[data-disk="${this.dataset.disk}"]`).src = "images/status/loading.svg";
let body = {
node: node,
type: type,
vmid: vmid,
2023-02-27 02:29:55 +00:00
disk: this.dataset.disk
};
let result = await requestAPI("/instance/disk/delete", "DELETE", body);
if (result.status === 200) {
await getConfig();
populateDisk();
}
else {
alert(result.error);
await getConfig();
populateDisk();
}
}
});
}
2023-05-17 21:40:37 +00:00
async function handleDiskAdd() {
let content = type === "qemu" ? "images" : "rootdir";
2023-05-11 07:13:41 +00:00
let storage = await requestPVE(`/nodes/${node}/storage`, "GET");
2023-05-17 21:40:37 +00:00
let header = "Create New Disk";
let options = "";
storage.data.forEach((element) => {
2023-05-17 21:40:37 +00:00
if (element.content.includes(content)) {
options += `<option value="${element.storage}">${element.storage}</option>"`;
}
});
let select = `<label for="storage-select">Storage</label><select class="w3-select w3-border" name="storage-select" id="storage-select" required><option hidden disabled selected value></option>${options}</select>`;
let body = `
<label for="device">${type === "qemu" ? "SATA" : "MP"}</label><input class="w3-input w3-border" name="device" id="device" type="number" min="0" max="${type === "qemu" ? "5" : "255"}" value="0" required></input>
${select}
<label for="size">Size (GiB)</label><input class="w3-input w3-border" name="size" id="size" type="number" min="0" max="131072" required></input>
`;
dialog(header, body, async (result, form) => {
if (result === "confirm") {
let body = {
node: node,
type: type,
vmid: vmid,
2023-02-27 02:29:55 +00:00
disk: `${type === "qemu" ? "sata" : "mp"}${form.get("device")}`,
storage: form.get("storage-select"),
size: form.get("size")
};
let result = await requestAPI("/instance/disk/create", "POST", body);
if (result.status === 200) {
await getConfig();
populateDisk();
}
else {
alert(result.error);
await getConfig();
populateDisk();
}
}
});
}
2023-05-17 21:40:37 +00:00
async function handleCDAdd() {
2023-02-16 05:19:22 +00:00
let content = "iso";
2023-05-11 07:13:41 +00:00
let storage = await requestPVE(`/nodes/${node}/storage`, "GET");
2023-05-17 21:40:37 +00:00
let header = `Add a CDROM`;
2023-02-16 05:19:22 +00:00
let storageOptions = "";
storage.data.forEach((element) => {
2023-05-17 21:40:37 +00:00
if (element.content.includes(content)) {
2023-02-16 05:19:22 +00:00
storageOptions += `<option value="${element.storage}">${element.storage}</option>"`;
}
});
let storageSelect = `<label for="storage-select">Storage</label><select class="w3-select w3-border" name="storage-select" id="storage-select" required><option hidden disabled selected value></option>${storageOptions}</select>`;
2023-02-16 05:19:22 +00:00
let body = `
<label for="device">IDE</label><input class="w3-input w3-border" name="device" id="device" type="number" min="0" max="3" required></input>
2023-02-16 05:19:22 +00:00
${storageSelect}
<label for="iso-select">Image</label><select class="w3-select w3-border" name="iso-select" id="iso-select" required></select>
2023-02-16 05:19:22 +00:00
`;
let d = dialog(header, body, async (result, form) => {
2023-02-16 05:19:22 +00:00
if (result === "confirm") {
let body = {
node: node,
type: type,
vmid: vmid,
2023-02-27 02:29:55 +00:00
disk: `ide${form.get("device")}`,
iso: form.get("iso-select")
2023-02-16 05:19:22 +00:00
};
let result = await requestAPI("/instance/disk/create", "POST", body);
2023-02-16 05:19:22 +00:00
if (result.status === 200) {
await getConfig();
populateDisk();
}
else {
alert(result.error);
await getConfig();
populateDisk();
2023-02-16 05:19:22 +00:00
}
}
});
2023-02-16 05:19:22 +00:00
d.querySelector("#storage-select").addEventListener("change", async () => {
let storage = document.querySelector("#storage-select").value;
let ISOSelect = document.querySelector("#iso-select");
ISOSelect.innerHTML = `<option hidden disabled selected value></option>`;
2023-05-17 21:40:37 +00:00
let isos = await requestPVE(`/nodes/${node}/storage/${storage}/content`, "GET", { content: content });
isos.data.forEach((element) => {
if (element.content.includes(content)) {
ISOSelect.append(new Option(element.volid.replace(`${storage}:${content}/`, ""), element.volid));
}
});
});
}
async function populateNetworks() {
document.querySelector("#networks").innerHTML = "";
let networks = {};
let prefix = networkMetaData.prefix;
Object.keys(config.data).forEach((element) => {
if (element.startsWith(prefix)) {
networks[element.replace(prefix, "")] = config.data[element];
}
});
let ordered_keys = getOrdered(networks);
ordered_keys.forEach((element) => {
addNetworkLine("networks", prefix, element, networks[element]);
});
2023-06-09 04:25:53 +00:00
document.querySelector("#network-add").addEventListener("click", handleNetworkAdd)
}
2023-05-17 21:40:37 +00:00
function addNetworkLine(fieldset, prefix, netID, netDetails) {
let field = document.querySelector(`#${fieldset}`);
let icon = document.createElement("img");
icon.src = "images/resources/network.svg";
icon.alt = `${prefix}${netID}`;
icon.dataset.network = netID;
icon.dataset.values = netDetails;
field.appendChild(icon);
let netLabel = document.createElement("label");
netLabel.innerText = `${prefix}${netID}`;
netLabel.dataset.network = netID;
netLabel.dataset.values = netDetails;
field.append(netLabel);
let netDesc = document.createElement("p");
netDesc.innerText = netDetails;
netDesc.dataset.network = netID;
netDesc.dataset.values = netDetails;
2023-06-09 06:55:14 +00:00
netDesc.style.overflowX = "hidden";
netDesc.style.whiteSpace = "nowrap";
field.append(netDesc);
let actionDiv = document.createElement("div");
2023-06-09 04:25:53 +00:00
let configBtn = document.createElement("img");
configBtn.classList.add("clickable");
configBtn.src = `images/actions/network/config.svg`;
configBtn.title = "Config Interface";
configBtn.addEventListener("click", handleNetworkConfig);
configBtn.dataset.network = netID;
configBtn.dataset.values = netDetails;
actionDiv.appendChild(configBtn);
let deleteBtn = document.createElement("img");
deleteBtn.classList.add("clickable");
deleteBtn.src = `images/actions/delete-active.svg`;
deleteBtn.title = "Delete Interface";
deleteBtn.addEventListener("click", handleNetworkDelete);
deleteBtn.dataset.network = netID;
deleteBtn.dataset.values = netDetails;
actionDiv.appendChild(deleteBtn);
field.append(actionDiv);
}
2023-05-17 21:40:37 +00:00
async function handleNetworkConfig() {
let netID = this.dataset.network;
let netDetails = this.dataset.values;
2023-06-09 04:25:53 +00:00
let header = `Edit net${netID}`;
let body = `<label for="rate">Rate Limit (MB/s)</label><input type="number" id="rate" name="rate" class="w3-input w3-border">`;
2023-05-17 21:40:37 +00:00
let d = dialog(header, body, async (result, form) => {
if (result === "confirm") {
document.querySelector(`img[data-network="${netID}"]`).src = "images/status/loading.svg";
let body = {
node: node,
type: type,
vmid: vmid,
netid: netID,
rate: form.get("rate")
}
let result = await requestAPI("/instance/network/modify", "POST", body);
if (result.status === 200) {
await getConfig();
populateNetworks();
}
2023-05-17 21:40:37 +00:00
else {
alert(result.error);
await getConfig();
populateNetworks();
}
}
});
d.querySelector("#rate").value = netDetails.split("rate=")[1].split(",")[0];
}
2023-06-09 04:25:53 +00:00
async function handleNetworkDelete() {
let netID = this.dataset.network;
let header = `Delete net${netID}`;
let body = ``;
2023-06-09 04:25:53 +00:00
let d = dialog(header, body, async (result, form) => {
if (result === "confirm") {
document.querySelector(`img[data-network="${netID}"]`).src = "images/status/loading.svg";
let body = {
node: node,
type: type,
vmid: vmid,
netid: netID
}
let result = await requestAPI("/instance/network/delete", "DELETE", body);
if (result.status === 200) {
await getConfig();
populateNetworks();
}
else {
alert(result.error);
await getConfig();
populateNetworks();
}
}
});
}
async function handleNetworkAdd() {
let header = `Create Network Interface`;
let body = `<label for="netid">Interface ID</label><input type="number" id="netid" name="netid" class="w3-input w3-border"><label for="rate">Rate Limit (MB/s)</label><input type="number" id="rate" name="rate" class="w3-input w3-border">`;
if (type === "lxc") {
body += `<label for="name">Interface Name</label><input type="text" id="name" name="name" class="w3-input w3-border"></input>`;
}
let d = dialog(header, body, async (result, form) => {
if (result === "confirm") {
let body = {
node: node,
type: type,
vmid: vmid,
netid: form.get("netid"),
rate: form.get("rate")
}
if (type === "lxc") {
body.name = form.get("name")
}
let result = await requestAPI("/instance/network/create", "POST", body);
if (result.status === 200) {
await getConfig();
populateNetworks();
}
else {
alert(result.error);
await getConfig();
populateNetworks();
}
}
});
}
async function populateDevices() {
if (type === "qemu") {
document.querySelector("#devices-card").classList.remove("none");
document.querySelector("#devices").innerHTML = "";
let devices = {};
let prefix = pcieMetaData.prefix;
Object.keys(config.data).forEach((element) => {
if (element.startsWith(prefix)) {
devices[element.replace(prefix, "")] = config.data[element];
}
});
let ordered_keys = getOrdered(devices);
ordered_keys.forEach(async (element) => {
let deviceData = await requestAPI(`/instance/pci?node=${node}&type=${type}&vmid=${vmid}&hostpci=${element}`, "GET");
2023-06-22 00:33:53 +00:00
addDeviceLine("devices", prefix, element, devices[element], deviceData.device_name);
});
2023-06-09 04:25:53 +00:00
document.querySelector("#device-add").addEventListener("click", handleDeviceAdd)
}
}
2023-06-22 00:33:53 +00:00
function addDeviceLine(fieldset, prefix, deviceID, deviceDetails, deviceName) {
let field = document.querySelector(`#${fieldset}`);
let icon = document.createElement("img");
icon.src = "images/resources/device.svg";
icon.alt = `${prefix}${deviceID}`;
icon.dataset.device = deviceID;
icon.dataset.values = deviceDetails;
2023-06-22 00:33:53 +00:00
icon.dataset.name = deviceName;
field.appendChild(icon);
let deviceLabel = document.createElement("p");
2023-06-22 00:33:53 +00:00
deviceLabel.innerText = deviceName;
deviceLabel.dataset.device = deviceID;
deviceLabel.dataset.values = deviceDetails;
2023-06-22 00:33:53 +00:00
deviceLabel.dataset.name = deviceName;
2023-06-09 06:55:14 +00:00
deviceLabel.style.overflowX = "hidden";
deviceLabel.style.whiteSpace = "nowrap";
field.append(deviceLabel);
let actionDiv = document.createElement("div");
let configBtn = document.createElement("img");
configBtn.classList.add("clickable");
configBtn.src = `images/actions/device/config.svg`;
configBtn.title = "Config Device";
configBtn.addEventListener("click", handleDeviceConfig);
configBtn.dataset.device = deviceID;
configBtn.dataset.values = deviceDetails;
2023-06-22 00:33:53 +00:00
configBtn.dataset.name = deviceName;
actionDiv.appendChild(configBtn);
let deleteBtn = document.createElement("img");
deleteBtn.classList.add("clickable");
deleteBtn.src = `images/actions/delete-active.svg`;
deleteBtn.title = "Delete Device";
deleteBtn.addEventListener("click", handleDeviceDelete);
2023-06-22 00:33:53 +00:00
deleteBtn.dataset.device = deviceID;
deleteBtn.dataset.values = deviceDetails;
deleteBtn.dataset.name = deviceName;
actionDiv.appendChild(deleteBtn);
field.append(actionDiv);
}
async function handleDeviceConfig() {
let deviceID = this.dataset.device;
let deviceDetails = this.dataset.values;
2023-06-22 00:33:53 +00:00
let deviceName = this.dataset.name;
let header = `Edit Expansion Card ${deviceID}`;
let body = `<label for="device">Device</label><select id="device" name="device" required></select><label for="pcie">PCI-Express</label><input type="checkbox" id="pcie" name="pcie" class="w3-input w3-border">`;
let d = dialog(header, body, async (result, form) => {
if (result === "confirm") {
document.querySelector(`img[data-device="${deviceID}"]`).src = "images/status/loading.svg";
let body = {
node: node,
type: type,
vmid: vmid,
2023-06-22 00:33:53 +00:00
hostpci: deviceID,
device: form.get("device"),
pcie: form.get("pcie")
}
let result = await requestAPI("/instance/pci/modify", "POST", body);
if (result.status === 200) {
await getConfig();
populateDevices();
}
else {
alert(result.error);
await getConfig();
populateDevices();
}
}
});
let availDevices = await requestAPI(`/nodes/pci?node=${node}`, "GET");
d.querySelector("#device").append(new Option(deviceName, deviceDetails.split(",")[0]));
for (let availDevice of availDevices) {
d.querySelector("#device").append(new Option(availDevice.device_name, availDevice.id));
}
d.querySelector("#pcie").checked = deviceDetails.includes("pcie=1");
}
async function handleDeviceDelete() {
let deviceID = this.dataset.device;
2023-06-22 00:33:53 +00:00
let header = `Remove Expansion Card ${deviceID}`;
let body = ``;
let d = dialog(header, body, async (result, form) => {
if (result === "confirm") {
document.querySelector(`img[data-device="${deviceID}"]`).src = "images/status/loading.svg";
let body = {
node: node,
type: type,
vmid: vmid,
2023-06-22 00:33:53 +00:00
hostpci: deviceID
}
let result = await requestAPI("/instance/pci/delete", "DELETE", body);
if (result.status === 200) {
await getConfig();
populateDevices();
}
else {
alert(result.error);
await getConfig();
populateDevices();
}
}
});
}
async function handleDeviceAdd() {
2023-06-22 00:33:53 +00:00
let header = `Add Expansion Card`;
let body = `<label for="device">Device</label><select id="device" name="device" required></select><label for="pcie">PCI-Express</label><input type="checkbox" id="pcie" name="pcie" class="w3-input w3-border">`;
let d = dialog(header, body, async (result, form) => {
if (result === "confirm") {
let body = {
node: node,
type: type,
vmid: vmid,
device: form.get("device"),
2023-06-22 00:33:53 +00:00
pcie: form.get("pcie") ? 1 : 0
}
let result = await requestAPI("/instance/pci/create", "POST", body);
if (result.status === 200) {
await getConfig();
populateDevices();
}
else {
alert(result.error);
await getConfig();
populateDevices();
}
}
});
2023-06-22 00:33:53 +00:00
let availDevices = await requestAPI(`/nodes/pci?node=${node}`, "GET");
for (let availDevice of availDevices) {
d.querySelector("#device").append(new Option(availDevice.device_name, availDevice.id));
}
2023-06-22 00:33:53 +00:00
d.querySelector("#pcie").checked = true;
}
2023-05-17 21:40:37 +00:00
async function handleFormExit() {
let body = {
node: node,
type: type,
vmid: vmid,
2023-06-14 01:13:06 +00:00
cores: document.querySelector("#cores").value,
memory: document.querySelector("#ram").value
}
if (type === "lxc") {
body.swap = document.querySelector("#swap").value;
}
else if (type === "qemu") {
body.proctype = document.querySelector("#proctype").value;
}
let result = await requestAPI("/instance/resources", "POST", body);
if (result.status === 200) {
await getConfig();
populateDisk();
goToPage("index.html");
}
else {
alert(result.error);
}
2023-02-16 05:19:22 +00:00
}