2023-02-14 21:08:48 +00:00
import { requestPVE , requestAPI , goToPage , getURIData , resources } from "./utils.js" ;
import { Dialog } from "./dialog.js" ;
2022-12-19 06:14:57 +00:00
2023-01-30 23:43:00 +00:00
window . addEventListener ( "DOMContentLoaded" , init ) ; // do the dumb thing where the disk config refreshes every second
2022-12-19 06:14:57 +00:00
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-02-14 21:03:53 +00:00
document . querySelector ( "#disk-add" ) . addEventListener ( "click" , handleDiskAdd ) ;
2023-01-26 19:48:41 +00:00
}
2023-02-13 22:51:22 +00:00
function addDiskLine ( fieldset , busPrefix , busName , device , diskDetails ) {
2023-01-09 04:26:20 +00:00
let field = document . querySelector ( ` # ${ fieldset } ` ) ;
2023-02-13 22:51:22 +00:00
let diskName = ` ${ busName } ${ device } ` ;
let diskID = ` ${ busPrefix } ${ device } ` ;
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-02-13 22:51:22 +00:00
icon . alt = diskName ;
icon . dataset . disk = diskID ;
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" ) ;
2023-02-13 22:51:22 +00:00
diskLabel . innerHTML = diskName ;
diskLabel . dataset . disk = diskID ;
2023-01-19 00:35:52 +00:00
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" ) ;
2023-02-13 22:51:22 +00:00
diskDesc . innerText = diskDetails ;
diskDesc . dataset . disk = diskID ;
2023-01-19 00:35:52 +00:00
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-02-10 21:36:47 +00:00
if ( element === "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-02-10 21:15:00 +00:00
action . addEventListener ( "click" , handleDiskAttach ) ;
2023-01-19 02:13:07 +00:00
}
2023-02-10 21:36:47 +00:00
else if ( element === "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 {
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-02-14 20:05:56 +00:00
if ( active === "active" ) {
if ( element === "move" ) {
action . addEventListener ( "click" , handleDiskMove ) ;
}
else if ( element === "resize" ) {
action . addEventListener ( "click" , handleDiskResize ) ;
}
else if ( element === "delete" ) {
action . addEventListener ( "click" , handleDiskDelete ) ;
}
2023-01-30 22:21:05 +00:00
}
2023-01-19 02:13:07 +00:00
}
2023-02-13 22:51:22 +00:00
action . dataset . disk = diskID ;
2023-02-10 21:36:47 +00:00
action . alt = action . title ;
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 ) ;
2023-02-01 23:42:30 +00:00
2023-02-13 22:51:22 +00:00
dialog . header = ` Detach ${ this . dataset . disk } ` ;
2023-02-14 21:03:53 +00:00
dialog . formBody = ` <p>Are you sure you want to detach disk</p><p> ${ this . dataset . disk } </p> ` ;
2023-02-02 00:12:36 +00:00
2023-01-30 22:21:05 +00:00
dialog . callback = async ( result , form ) => {
2023-02-10 21:15:00 +00:00
if ( result === "confirm" ) {
2023-02-14 09:23:37 +00:00
document . querySelector ( ` img[data-disk=" ${ this . dataset . disk } "] ` ) . src = "images/actions/loading.svg" ;
2023-01-30 22:21:05 +00:00
let body = {
node : node ,
type : type ,
vmid : vmid ,
2023-02-13 22:51:22 +00:00
action : JSON . stringify ( { delete : this . dataset . disk } )
2023-01-30 22:21:05 +00:00
} ;
let result = await requestAPI ( "/disk/detach" , "POST" , body ) ;
if ( result . status === 200 ) {
await getConfig ( ) ;
populateDisk ( ) ;
}
2023-02-10 21:15:00 +00:00
else {
console . error ( result ) ;
}
}
} ;
dialog . show ( ) ;
}
async function handleDiskAttach ( ) {
let dialog = document . createElement ( "dialog-form" ) ;
document . body . append ( dialog ) ;
2023-02-13 22:51:22 +00:00
let diskImage = config . data [ this . dataset . disk ] ;
2023-02-10 21:15:00 +00:00
dialog . header = ` Attach ${ diskImage } ` ;
2023-02-14 21:56:27 +00:00
dialog . formBody = ` <label for="device"> ${ type === "qemu" ? "SATA" : "MP" } </label><input name="device" id="device" type="number" min="0" max=" ${ type === "qemu" ? "5" : "255" } " value="0"></input> ` ;
2023-02-10 21:15:00 +00:00
dialog . callback = async ( result , form ) => {
if ( result === "confirm" ) {
2023-02-14 21:03:53 +00:00
let device = form . get ( "device" ) ;
2023-02-14 09:23:37 +00:00
document . querySelector ( ` img[data-disk=" ${ this . dataset . disk } "] ` ) . src = "images/actions/loading.svg" ;
2023-02-10 21:15:00 +00:00
let action = { } ;
2023-02-10 22:17:23 +00:00
let bus = type === "qemu" ? "sata" : "mp" ;
let details = diskImage ;
if ( type === "lxc" ) {
2023-02-14 21:03:53 +00:00
details += ` ,mp=/mp ${ device } / ` ;
2023-02-10 22:17:23 +00:00
}
2023-02-14 21:03:53 +00:00
action [ ` ${ bus } ${ device } ` ] = details ;
2023-02-10 21:15:00 +00:00
let body = {
node : node ,
type : type ,
vmid : vmid ,
action : JSON . stringify ( action )
}
let result = await requestAPI ( "/disk/attach" , "POST" , body ) ;
if ( result . status === 200 ) {
await getConfig ( ) ;
populateDisk ( ) ;
}
else {
2023-01-30 22:21:05 +00:00
console . error ( result ) ;
}
}
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 ) ;
2023-02-01 23:42:30 +00:00
2023-02-14 21:03:53 +00:00
dialog . header = ` Resize ${ this . dataset . disk } ` ;
dialog . formBody = ` <label for="size-increment">Size Increment (GiB)</label><input name="size-increment" id="size-increment" type="number" min="0" max="131072" value="0"></input> ` ;
2023-02-01 23:42:30 +00:00
2023-01-30 22:21:05 +00:00
dialog . callback = async ( result , form ) => {
2023-02-10 21:15:00 +00:00
if ( result === "confirm" ) {
2023-02-14 09:23:37 +00:00
document . querySelector ( ` img[data-disk=" ${ this . dataset . disk } "] ` ) . src = "images/actions/loading.svg" ;
2023-01-30 22:21:05 +00:00
let body = {
node : node ,
type : type ,
vmid : vmid ,
2023-02-13 22:51:22 +00:00
action : JSON . stringify ( { disk : this . dataset . disk , 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 ) ;
}
}
} ;
dialog . show ( ) ;
2023-01-25 02:06:31 +00:00
}
2023-02-01 23:42:30 +00:00
async function handleDiskMove ( ) {
2023-02-02 00:12:36 +00:00
let content = type === "qemu" ? "images" : "rootdir"
let storage = await requestPVE ( ` /nodes/ ${ node } /storage ` , "GET" , null ) ;
let dialog = document . createElement ( "dialog-form" ) ;
document . body . append ( dialog ) ;
2023-02-13 22:51:22 +00:00
dialog . header = ` Move ${ this . dataset . disk } ` ;
2023-02-02 18:26:44 +00:00
2023-02-14 22:55:09 +00:00
let options = "" ;
2023-02-02 00:12:36 +00:00
storage . data . forEach ( ( element ) => {
2023-02-10 21:15:00 +00:00
if ( element . content . includes ( content ) ) {
2023-02-14 22:55:09 +00:00
options += ` <option value=" ${ element . storage } "> ${ element . storage } </option>" ` ;
2023-02-02 00:12:36 +00:00
}
} ) ;
2023-02-14 22:55:09 +00:00
let select = ` <label for="storage-select">Storage</label><select name="storage-select" id="storage-select"> ${ options } </select> ` ;
2023-02-02 00:12:36 +00:00
2023-02-14 22:55:09 +00:00
dialog . formBody = `
$ { select }
< label for = "delete-check" > Delete Source < / l a b e l > < i n p u t n a m e = " d e l e t e - c h e c k " i d = " d e l e t e - c h e c k " t y p e = " c h e c k b o x " c h e c k e d >
` ;
2023-02-02 19:35:14 +00:00
2023-02-02 18:26:44 +00:00
dialog . callback = async ( result , form ) => {
2023-02-10 21:15:00 +00:00
if ( result === "confirm" ) {
2023-02-14 09:23:37 +00:00
document . querySelector ( ` img[data-disk=" ${ this . dataset . disk } "] ` ) . src = "images/actions/loading.svg" ;
2023-02-14 22:55:09 +00:00
let action = { storage : form . get ( "storage-select" ) , delete : form . get ( "delete-check" ) === "on" ? "1" : "0" }
2023-02-10 21:36:47 +00:00
if ( type === "qemu" ) { // if vm, move disk
2023-02-13 22:51:22 +00:00
action . disk = this . dataset . disk ;
2023-02-10 21:36:47 +00:00
}
else { // type is lxc, move volume
2023-02-13 22:51:22 +00:00
action . volume = this . dataset . disk ;
2023-02-10 21:36:47 +00:00
}
2023-02-02 18:26:44 +00:00
let body = {
node : node ,
type : type ,
vmid : vmid ,
2023-02-10 21:36:47 +00:00
action : JSON . stringify ( action )
2023-02-02 18:26:44 +00:00
}
let result = await requestAPI ( "/disk/move" , "POST" , body ) ;
if ( result . status === 200 ) {
await getConfig ( ) ;
populateDisk ( ) ;
}
2023-02-10 21:15:00 +00:00
else {
2023-02-02 18:26:44 +00:00
console . error ( result ) ;
}
}
2023-02-02 00:12:36 +00:00
} ;
dialog . show ( ) ;
2023-02-01 23:42:30 +00:00
}
2023-02-10 21:36:47 +00:00
async function handleDiskDelete ( ) {
let dialog = document . createElement ( "dialog-form" ) ;
document . body . append ( dialog ) ;
2023-02-13 22:51:22 +00:00
dialog . header = ` Delete ${ this . dataset . disk } ` ;
2023-02-14 21:03:53 +00:00
dialog . formBody = ` <p style="color: #FF0000;">Are you sure you want to <strong>delete</strong> disk</p><p style="color: #FF0000;"> ${ this . dataset . disk } </p> ` ;
2023-02-10 21:36:47 +00:00
dialog . callback = async ( result , form ) => {
if ( result === "confirm" ) {
2023-02-14 09:23:37 +00:00
document . querySelector ( ` img[data-disk=" ${ this . dataset . disk } "] ` ) . src = "images/actions/loading.svg" ;
2023-02-10 21:36:47 +00:00
let body = {
node : node ,
type : type ,
vmid : vmid ,
2023-02-13 22:51:22 +00:00
action : JSON . stringify ( { delete : this . dataset . disk } )
2023-02-10 21:36:47 +00:00
} ;
let result = await requestAPI ( "/disk/delete" , "POST" , body ) ;
if ( result . status === 200 ) {
await getConfig ( ) ;
populateDisk ( ) ;
}
else {
console . error ( result ) ;
}
}
} ;
dialog . show ( ) ;
}
2023-02-14 21:03:53 +00:00
async function handleDiskAdd ( ) {
2023-02-14 21:56:27 +00:00
let content = type === "qemu" ? "images" : "rootdir"
let storage = await requestPVE ( ` /nodes/ ${ node } /storage ` , "GET" , null ) ;
let dialog = document . createElement ( "dialog-form" ) ;
document . body . append ( dialog ) ;
let diskImage = config . data [ this . dataset . disk ] ;
dialog . header = ` Create A New Disk ` ;
2023-02-14 22:55:09 +00:00
let options = "" ;
2023-02-14 21:56:27 +00:00
storage . data . forEach ( ( element ) => {
if ( element . content . includes ( content ) ) {
2023-02-14 22:55:09 +00:00
options += ` <option value=" ${ element . storage } "> ${ element . storage } </option>" ` ;
2023-02-14 21:56:27 +00:00
}
} ) ;
2023-02-14 22:55:09 +00:00
let select = ` <label for="storage-select">Storage</label><select name="storage-select" id="storage-select"> ${ options } </select> ` ;
dialog . formBody = `
< label for = "device" > $ { type === "qemu" ? "SATA" : "MP" } < / l a b e l > < i n p u t n a m e = " d e v i c e " i d = " d e v i c e " t y p e = " n u m b e r " m i n = " 0 " m a x = " $ { t y p e = = = " q e m u " ? " 5 " : " 2 5 5 " } " v a l u e = " 0 " > < / i n p u t >
$ { select }
< label for = "size" > Size ( GiB ) < / l a b e l > < i n p u t n a m e = " s i z e " i d = " s i z e " t y p e = " n u m b e r " m i n = " 0 " m a x = " 1 3 1 0 7 2 " v a l u e = " 0 " > < / i n p u t >
` ;
2023-02-14 21:56:27 +00:00
dialog . callback = ( ) => {
} ;
dialog . show ( ) ;
2023-02-14 21:03:53 +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
}