2022-12-19 06:14:57 +00:00
import { request , goToPage , getURIData } from "./utils.js" ;
window . addEventListener ( "DOMContentLoaded" , init ) ;
2023-01-11 10:57:37 +00:00
let diskConfig = {
lxc : {
prefixOrder : [ "mp" ] ,
mp : { name : "MP" , limit : 255 , used : { } }
} ,
qemu : {
prefixOrder : [ "sata" , "ide" ] ,
ide : { name : "IDE" , limit : 3 , used : { } } ,
sata : { name : "SATA" , limit : 5 , used : { } }
}
}
let node ;
let type ;
let vmid ;
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 ;
2022-12-20 00:13:43 +00:00
await populateForm ( node , type , vmid ) ;
2022-12-20 23:42:13 +00:00
let cancelButton = document . querySelector ( "#cancel" ) ;
cancelButton . addEventListener ( "click" , ( ) => {
goToPage ( "index.html" ) ;
} ) ;
2023-01-10 07:50:11 +00:00
/ * *
* Add disk qemu : POST config with sata4 : "cephpl:4"
* Add disk lxc POST config with mp2 : "cephpl:8,mp=/test/,backup=1"
* /
2022-12-20 00:13:43 +00:00
}
async function populateForm ( node , type , vmid ) {
2022-12-20 00:21:24 +00:00
let config = await request ( ` /nodes/ ${ node } / ${ type } / ${ vmid } /config ` ) ;
2022-12-20 00:13:43 +00:00
console . log ( config ) ;
2022-12-20 01:59:34 +00:00
2022-12-21 23:10:13 +00:00
let name = type === "qemu" ? "name" : "hostname" ;
2023-01-09 04:32:15 +00:00
addMetaLine ( "name" , "Name" , { type : "text" , value : 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
2022-12-21 23:39:09 +00:00
if ( type === "lxc" ) {
2023-01-09 04:26:20 +00:00
addResourceLine ( "resources" , "images/resources/swap.svg" , "Swap" , { type : "number" , value : config . data . swap , min : 0 , step : 1 } , "GiB" ) ; // TODO add max from quota API
2023-01-10 06:24:04 +00:00
addDiskLine ( "disks" , "rootfs" , "images/resources/drive.svg" , "Root FS" , config . data . rootfs ) ;
2023-01-10 00:02:25 +00:00
}
2023-01-11 01:04:20 +00:00
for ( let i = 0 ; i < diskConfig [ type ] . prefixOrder . length ; i ++ ) {
let prefix = diskConfig [ type ] . prefixOrder [ i ] ;
2023-01-11 01:05:41 +00:00
let entry = diskConfig [ type ] [ prefix ] ;
2023-01-11 01:12:24 +00:00
let name = entry . name ;
2023-01-10 00:21:45 +00:00
Object . keys ( config . data ) . forEach ( element => {
if ( element . startsWith ( prefix ) ) {
2023-01-11 01:05:41 +00:00
entry . used [ element . replace ( prefix , "" ) ] = config . data [ element ] ;
2023-01-10 00:21:45 +00:00
}
2023-01-10 07:50:11 +00:00
} ) ;
2023-01-11 10:01:34 +00:00
let ordered _keys = getOrderedUsed ( entry ) ;
2023-01-10 07:50:11 +00:00
ordered _keys . forEach ( element => {
2023-01-11 01:12:24 +00:00
addDiskLine ( "disks" , ` ${ prefix } ${ element } ` , entry . used [ element ] . includes ( "media=cdrom" ) ? "images/resources/disk.svg" : "images/resources/drive.svg" , ` ${ name } ${ element } ` , entry . used [ element ] ) ;
2023-01-10 07:50:11 +00:00
} ) ;
2022-12-21 23:06:26 +00:00
}
2023-01-11 00:49:57 +00:00
let addDiskBus = document . querySelector ( "#add-disk #bus" ) ;
2023-01-11 01:16:05 +00:00
diskConfig [ type ] . prefixOrder . forEach ( element => {
2023-01-11 01:04:20 +00:00
addDiskBus . add ( new Option ( diskConfig [ type ] [ element ] . name , element ) ) ;
2023-01-11 00:49:57 +00:00
} ) ;
2023-01-11 11:00:01 +00:00
addDiskBus . value = diskConfig [ type ] . prefixOrder [ 0 ] ;
2023-01-11 10:46:25 +00:00
handleDiskBusChange ( diskConfig ) ;
2023-01-11 11:02:17 +00:00
addDiskBus . addEventListener ( "change" , handleDiskBusChange ) ;
2023-01-11 10:47:36 +00:00
2023-01-11 11:00:01 +00:00
let addDiskDevice = document . querySelector ( "#add-disk #device" ) ;
2023-01-11 11:02:17 +00:00
addDiskDevice . addEventListener ( "input" , handleDiskDeviceChange ) ;
addDiskDevice . addEventListener ( "focus" , handleDiskDeviceChange ) ;
2023-01-11 10:15:58 +00:00
2023-01-11 00:49:57 +00:00
let addDiskStorage = document . querySelector ( "#add-disk #storage" ) ;
let addDiskSize = document . querySelector ( "#add-disk #size" ) ;
2023-01-11 01:04:20 +00:00
console . log ( diskConfig ) ;
2022-12-20 01:59:34 +00:00
}
2023-01-11 10:01:34 +00:00
function getOrderedUsed ( entry ) {
let ordered _keys = Object . keys ( entry . used ) . sort ( ( a , b ) => { parseInt ( a ) - parseInt ( b ) } ) ; // ordered integer list
return ordered _keys ;
}
function getNextAvaliable ( entry ) {
let ordered _keys = getOrderedUsed ( entry ) ;
let nextAvaliable = 0 ;
ordered _keys . forEach ( element => {
if ( parseInt ( element ) === nextAvaliable ) {
nextAvaliable ++ ;
}
} ) ;
return nextAvaliable ;
}
2023-01-11 10:57:37 +00:00
function handleDiskBusChange ( ) {
2023-01-11 10:43:56 +00:00
let bus = document . querySelector ( "#add-disk #bus" ) . value ;
let entry = diskConfig [ type ] [ bus ] ;
let limit = entry . limit ;
let addDiskDevice = document . querySelector ( "#add-disk #device" ) ;
addDiskDevice . max = limit ;
let nextAvaliable = getNextAvaliable ( entry ) ;
if ( nextAvaliable > limit ) {
addDiskDevice . value = 0 ;
}
else {
addDiskDevice . value = nextAvaliable ;
}
handleDiskDeviceChange ( ) ;
}
2023-01-11 10:57:37 +00:00
function handleDiskDeviceChange ( ) {
2023-01-11 10:43:56 +00:00
let value = document . querySelector ( "#add-disk #device" ) . value ;
let bus = document . querySelector ( "#add-disk #bus" ) . value ;
let entry = diskConfig [ type ] [ bus ] ;
2023-01-11 10:58:22 +00:00
let addDiskDevice = document . querySelector ( "#add-disk #device" ) ;
2023-01-11 10:43:56 +00:00
if ( value in entry . used ) {
addDiskDevice . style . border = "solid red 1px" ;
}
else {
addDiskDevice . style . border = "solid white 1px" ;
}
}
2023-01-09 04:32:15 +00:00
function addMetaLine ( fieldset , labelText , inputAttr ) {
let field = document . querySelector ( ` # ${ fieldset } ` ) ;
let label = document . createElement ( "label" ) ;
label . innerHTML = labelText ;
2023-01-09 05:28:35 +00:00
label . htmlFor = labelText ;
2023-01-09 04:32:15 +00:00
field . append ( label ) ;
let input = document . createElement ( "input" ) ;
for ( let k in inputAttr ) {
input . setAttribute ( k , inputAttr [ k ] )
}
2023-01-09 05:28:35 +00:00
input . id = labelText ;
2023-01-09 04:32:15 +00:00
field . append ( input ) ;
}
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-09 04:28:50 +00:00
function addDiskLine ( fieldset , id , iconHref , labelText , valueText ) {
2023-01-09 04:26:20 +00:00
let field = document . querySelector ( ` # ${ fieldset } ` ) ;
let icon = document . createElement ( "img" ) ;
icon . src = iconHref ;
2023-01-09 05:26:44 +00:00
icon . alt = labelText ;
2023-01-09 04:26:20 +00:00
field . append ( icon ) ;
let label = document . createElement ( "label" ) ;
label . innerHTML = labelText ;
field . append ( label ) ;
let value = document . createElement ( "p" ) ;
2023-01-09 04:28:50 +00:00
value . innerText = valueText ;
2023-01-09 04:26:20 +00:00
field . append ( value ) ;
2023-01-09 04:48:58 +00:00
let config = document . createElement ( "img" ) ;
config . src = "images/actions/config-active.svg" ;
2023-01-09 05:23:08 +00:00
config . alt = ` Config disk ${ labelText } ` ;
2023-01-11 09:32:51 +00:00
config . classList . add ( "clickable" ) ;
2023-01-09 04:49:57 +00:00
field . append ( config ) ;
2022-12-19 06:14:57 +00:00
}