2023-01-25 02:06:31 +00:00
import { requestPVE , requestAPI , goToPage , getURIData , reload , resources } from "./utils.js" ;
2023-01-30 22:21:05 +00:00
import { Dialog } from "./elements.js" ;
2022-12-19 06:14:57 +00:00
window . addEventListener ( "DOMContentLoaded" , init ) ;
2023-01-19 20:33:05 +00:00
let diskMetaData = resources . disk ;
2023-01-11 10:57:37 +00:00
let node ;
let type ;
let vmid ;
2023-01-26 19:48:41 +00:00
let config ;
2023-01-11 10:57:37 +00:00
2022-12-19 06:14:57 +00:00
async function init ( ) {
2022-12-20 23:15:08 +00:00
let cookie = document . cookie ;
if ( cookie === "" ) {
goToPage ( "login.html" ) ;
}
2022-12-19 06:14:57 +00:00
let uriData = getURIData ( ) ;
2023-01-11 10:57:37 +00:00
node = uriData . node ;
type = uriData . type ;
vmid = uriData . vmid ;
2023-01-26 19:48:41 +00:00
2023-01-26 19:55:17 +00:00
await getConfig ( ) ;
2023-01-26 19:48:41 +00:00
populateResources ( ) ;
populateDisk ( ) ;
2022-12-20 23:42:13 +00:00
let cancelButton = document . querySelector ( "#cancel" ) ;
cancelButton . addEventListener ( "click" , ( ) => {
goToPage ( "index.html" ) ;
} ) ;
2022-12-20 00:13:43 +00:00
}
2023-01-26 19:55:17 +00:00
async function getConfig ( ) {
config = await requestPVE ( ` /nodes/ ${ node } / ${ type } / ${ vmid } /config ` , "GET" ) ;
}
function populateResources ( ) {
2022-12-21 23:10:13 +00:00
let name = type === "qemu" ? "name" : "hostname" ;
2023-01-16 22:25:31 +00:00
document . querySelector ( "#name" ) . innerText = config . data [ name ] ;
2023-01-09 04:26:20 +00:00
addResourceLine ( "resources" , "images/resources/cpu.svg" , "Cores" , { type : "number" , value : config . data . cores , min : 1 , max : 8192 } , "Threads" ) ; // TODO add max from quota API
2023-01-09 05:32:22 +00:00
addResourceLine ( "resources" , "images/resources/ram.svg" , "Memory" , { type : "number" , value : config . data . memory , min : 16 , step : 1 } , "MiB" ) ; // TODO add max from quota API
2023-01-16 23:05:58 +00:00
2022-12-21 23:39:09 +00:00
if ( type === "lxc" ) {
2023-01-17 18:26:11 +00:00
addResourceLine ( "resources" , "images/resources/swap.svg" , "Swap" , { type : "number" , value : config . data . swap , min : 0 , step : 1 } , "MiB" ) ; // TODO add max from quota API
2023-01-10 00:02:25 +00:00
}
2023-01-11 10:43:56 +00:00
}
2023-01-09 04:26:20 +00:00
function addResourceLine ( fieldset , iconHref , labelText , inputAttr , 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 ;
2023-01-09 05:23:08 +00:00
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" ) ;
2022-12-21 09:31:58 +00:00
label . innerHTML = labelText ;
2023-01-09 05:26:44 +00:00
label . htmlFor = labelText ;
2022-12-21 08:49:57 +00:00
field . append ( label ) ;
2022-12-20 01:59:34 +00:00
let input = document . createElement ( "input" ) ;
2022-12-20 02:08:16 +00:00
for ( let k in inputAttr ) {
2022-12-20 02:12:39 +00:00
input . setAttribute ( k , inputAttr [ k ] )
2022-12-20 01:59:34 +00:00
}
2023-01-09 05:23:08 +00:00
input . id = labelText ;
2022-12-21 08:49:57 +00:00
field . append ( input ) ;
2022-12-21 09:31:58 +00:00
if ( unitText ) {
let unit = document . createElement ( "p" ) ;
unit . innerText = unitText ;
field . append ( unit ) ;
}
2023-01-09 04:26:20 +00:00
}
2023-01-26 19:55:17 +00:00
function populateDisk ( ) {
2023-01-26 19:48:41 +00:00
document . querySelector ( "#disks" ) . innerHTML = "" ;
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 = getOrderedUsed ( disks ) ;
ordered _keys . forEach ( element => {
let disk = disks [ element ] ;
addDiskLine ( "disks" , prefix , busName , element , disk ) ;
} ) ;
}
}
2023-01-26 19:55:17 +00:00
function addDiskLine ( fieldset , busPrefix , busName , device , disk ) {
2023-01-09 04:26:20 +00:00
let field = document . querySelector ( ` # ${ fieldset } ` ) ;
2023-01-17 17:52:20 +00:00
2023-01-18 23:43:57 +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" ) ;
2023-01-19 19:58:44 +00:00
icon . src = diskMetaData [ type ] [ busPrefix ] . icon ;
2023-01-16 23:13:17 +00:00
icon . alt = ` ${ busName } ${ device } ` ;
2023-01-09 04:26:20 +00:00
field . append ( icon ) ;
2023-01-19 00:35:52 +00:00
// Add a label for the disk bus and device number
let diskLabel = document . createElement ( "label" ) ;
diskLabel . innerHTML = ` ${ busName } ${ device } ` ;
field . append ( diskLabel ) ;
2023-01-18 23:43:57 +00:00
2023-01-19 00:35:52 +00:00
// Add text of the disk configuration
let diskDesc = document . createElement ( "p" ) ;
diskDesc . innerText = disk ;
field . append ( diskDesc ) ;
2023-01-16 23:45:51 +00:00
2023-01-19 00:35:52 +00:00
let actionDiv = document . createElement ( "div" ) ;
actionDiv . classList . add ( "last-item" ) ;
2023-01-19 19:58:44 +00:00
diskMetaData . actionBarOrder . forEach ( ( element ) => {
2023-01-19 02:13:07 +00:00
let action = document . createElement ( "img" ) ;
action . classList . add ( "clickable" ) ;
2023-01-30 22:21:05 +00:00
if ( element === "delete_detach_attach" && diskMetaData [ type ] [ busPrefix ] . actions . includes ( "attach" ) ) { // attach
2023-01-19 02:13:07 +00:00
action . src = "images/actions/attach.svg" ;
action . title = "Attach Disk" ;
}
2023-01-30 22:21:05 +00:00
else if ( element === "delete_detach_attach" && diskMetaData [ type ] [ busPrefix ] . actions . includes ( "detach" ) ) { // detach
2023-01-19 02:13:07 +00:00
action . src = "images/actions/detach.svg" ;
action . title = "Detach Disk" ;
2023-01-25 02:06:31 +00:00
action . addEventListener ( "click" , handleDiskDetach ) ;
2023-01-19 02:13:07 +00:00
}
else if ( element === "delete_detach_attach" ) {
2023-01-30 22:21:05 +00:00
let active = diskMetaData [ type ] [ busPrefix ] . actions . includes ( "delete" ) ? "active" : "inactive" ; // delete
2023-01-19 02:13:07 +00:00
action . src = ` images/actions/delete- ${ active } .svg ` ;
action . title = ` Delete Disk ` ;
}
else {
2023-01-30 22:21:05 +00:00
let active = diskMetaData [ type ] [ busPrefix ] . actions . includes ( element ) ? "active" : "inactive" ; // resize
2023-01-19 02:13:07 +00:00
action . src = ` images/actions/ ${ element } - ${ active } .svg ` ;
action . title = ` ${ element . charAt ( 0 ) . toUpperCase ( ) } ${ element . slice ( 1 ) } Disk ` ;
2023-01-30 22:21:05 +00:00
if ( element === "config" ) {
}
else if ( element === "move" ) {
}
else if ( element === "resize" ) {
action . addEventListener ( "click" , handleDiskResize ) ;
}
else {
}
2023-01-19 02:13:07 +00:00
}
2023-01-19 18:08:00 +00:00
action . id = ` ${ busPrefix } ${ device } `
2023-01-19 02:13:07 +00:00
actionDiv . append ( action ) ;
} ) ;
2023-01-19 00:35:52 +00:00
field . append ( actionDiv ) ;
2023-01-17 19:39:28 +00:00
}
2023-01-25 02:06:31 +00:00
async function handleDiskDetach ( ) {
2023-01-30 22:21:05 +00:00
let dialog = document . createElement ( "dialog-form" ) ;
document . body . append ( dialog ) ;
dialog . body = `
< p > Detach $ { this . id } < / p >
< hr >
` ;
dialog . callback = async ( result , form ) => {
if ( result === "confirm" ) {
let body = {
node : node ,
type : type ,
vmid : vmid ,
2023-01-30 23:35:26 +00:00
action : JSON . stringify ( { delete : this . id } )
2023-01-30 22:21:05 +00:00
} ;
let result = await requestAPI ( "/disk/detach" , "POST" , body ) ;
if ( result . status === 200 ) {
await getConfig ( ) ;
populateDisk ( ) ;
}
else {
console . error ( result ) ;
}
}
document . querySelector ( "dialog-form" ) . remove ( ) ;
2023-01-25 02:06:31 +00:00
} ;
2023-01-30 22:21:05 +00:00
dialog . show ( ) ;
}
async function handleDiskResize ( ) {
let dialog = document . createElement ( "dialog-form" ) ;
document . body . append ( dialog ) ;
dialog . body = `
< p > Resize $ { this . id } < / p >
< hr >
< label for = "size-increment" > Size Increment ( GiB ) < / l a b e l >
< input name = "size-increment" type = "number" min = "0" max = "131072" value = "0" >
< hr >
` ;
dialog . callback = async ( result , form ) => {
if ( result === "confirm" ) {
let body = {
node : node ,
type : type ,
vmid : vmid ,
2023-01-30 23:35:26 +00:00
action : JSON . stringify ( { disk : this . id , size : ` + ${ form . get ( "size-increment" ) } G ` } )
2023-01-30 22:21:05 +00:00
}
let result = await requestAPI ( "/disk/resize" , "POST" , body ) ;
if ( result . status === 200 ) {
await getConfig ( ) ;
populateDisk ( ) ;
}
else {
console . error ( result ) ;
}
}
document . querySelector ( "dialog-form" ) . remove ( ) ;
} ;
dialog . show ( ) ;
2023-01-25 02:06:31 +00:00
}
2023-01-18 23:43:57 +00:00
function getOrderedUsed ( disks ) {
let ordered _keys = Object . keys ( disks ) . sort ( ( a , b ) => { parseInt ( a ) - parseInt ( b ) } ) ; // ordered integer list
2023-01-12 22:43:17 +00:00
return ordered _keys ;
2022-12-19 06:14:57 +00:00
}