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" ] ,
2023-01-17 00:14:33 +00:00
mp : { name : "MP" , limit : 255 , used : { } , icon : "images/resources/drive.svg" , storageContent : "rootdir" , resizable : true , hasPath : true , hasDiskImage : false }
2023-01-11 10:57:37 +00:00
} ,
qemu : {
prefixOrder : [ "sata" , "ide" ] ,
2023-01-17 00:14:33 +00:00
ide : { name : "IDE" , limit : 3 , used : { } , icon : "images/resources/disk.svg" , storageContent : "iso" , reziable : false , hasPath : false , hasDiskImage : true } ,
sata : { name : "SATA" , limit : 5 , used : { } , icon : "images/resources/drive.svg" , storageContent : "images" , resizable : true , hasPath : false , hasDiskImage : false }
2023-01-11 10:57:37 +00:00
}
}
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 ;
2023-01-12 23:08:02 +00:00
await populateResources ( ) ;
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
}
2023-01-12 23:08:02 +00:00
async function populateResources ( ) {
2022-12-20 00:21:24 +00:00
let config = await request ( ` /nodes/ ${ node } / ${ type } / ${ vmid } /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-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
let storageOptions = await request ( ` /nodes/ ${ node } /storage ` ) ;
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-16 23:05:58 +00:00
let rootfs = parseDisk ( config . data . rootfs ) ;
2023-01-17 17:52:20 +00:00
addDiskLine ( "disks" , "mp" , "Root FS" , null , rootfs , storageOptions ) ;
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-16 23:05:58 +00:00
let busName = 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-16 23:05:58 +00:00
let disk = parseDisk ( entry . used [ element ] ) ;
2023-01-17 17:52:20 +00:00
addDiskLine ( "disks" , prefix , busName , element , disk , storageOptions ) ;
2023-01-10 07:50:11 +00:00
} ) ;
2022-12-21 23:06:26 +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-17 18:48:09 +00:00
async function addDiskLine ( fieldset , busPrefix , busName , device , diskDataParsed , storageOptions ) {
2023-01-09 04:26:20 +00:00
let field = document . querySelector ( ` # ${ fieldset } ` ) ;
2023-01-17 17:52:20 +00:00
2023-01-09 04:26:20 +00:00
let icon = document . createElement ( "img" ) ;
2023-01-16 23:12:46 +00:00
icon . src = diskConfig [ 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-16 23:05:58 +00:00
let busLabel = document . createElement ( "label" ) ;
2023-01-16 23:12:46 +00:00
busLabel . innerHTML = busName ;
2023-01-16 23:05:58 +00:00
field . append ( busLabel ) ;
2023-01-17 18:01:06 +00:00
let deviceInput = document . createElement ( "input" ) ;
deviceInput . type = "number" ;
deviceInput . min = 0 ;
deviceInput . max = diskConfig [ type ] [ busPrefix ] . limit ;
deviceInput . value = device ;
if ( ! device ) {
deviceInput . disabled = true ;
deviceInput . classList . add ( "hidden" ) ;
2023-01-16 23:05:58 +00:00
}
2023-01-17 18:18:22 +00:00
// for now, disable disk device reassignment
deviceInput . disabled = true ;
//
deviceInput . addEventListener ( "change" , handleDeviceChange ) ;
2023-01-17 18:01:06 +00:00
field . append ( deviceInput ) ;
2023-01-16 23:05:58 +00:00
2023-01-17 17:52:20 +00:00
let storage = diskDataParsed . storage ;
2023-01-16 23:05:58 +00:00
let storageSelect = document . createElement ( "select" ) ;
2023-01-16 23:14:34 +00:00
storageOptions . data . forEach ( ( element ) => {
2023-01-17 00:14:33 +00:00
if ( element . content . includes ( diskConfig [ type ] [ busPrefix ] . storageContent ) ) { // check if the storage contains rootdir or images content
2023-01-16 23:05:58 +00:00
storageSelect . add ( new Option ( element . storage ) ) ;
2023-01-12 23:05:22 +00:00
}
} ) ;
2023-01-16 23:05:58 +00:00
storageSelect . value = storage ;
2023-01-17 18:31:26 +00:00
storageSelect . id = ` ${ busPrefix } _ ${ device } _storage ` ;
2023-01-17 19:39:28 +00:00
storageSelect . addEventListener ( "change" , handleStorageChange ) ;
2023-01-16 23:16:02 +00:00
field . append ( storageSelect ) ;
2023-01-16 23:05:58 +00:00
2023-01-17 18:48:09 +00:00
if ( diskConfig [ type ] [ busPrefix ] . resizable ) {
let size = diskDataParsed . size ;
let sizeInput = document . createElement ( "input" ) ;
sizeInput . type = "number" ;
sizeInput . min = size ;
sizeInput . minSize = size ;
sizeInput . max = 131072 ; // 128 TiB, everything should just use GiB
sizeInput . value = size ;
sizeInput . id = ` ${ busPrefix } _ ${ device } _size ` ;
field . append ( sizeInput ) ;
let sizeUnit = document . createElement ( "p" ) ;
sizeUnit . innerText = "GiB" ;
field . append ( sizeUnit ) ;
2023-01-12 22:43:17 +00:00
}
2023-01-17 18:48:09 +00:00
else if ( diskConfig [ type ] [ busPrefix ] . hasPath ) {
let pathInput = document . createElement ( "input" ) ;
pathInput . value = diskDataParsed . mp ;
pathInput . id = ` ${ busPrefix } _ ${ device } _path ` ;
2023-01-17 19:15:04 +00:00
pathInput . style . gridColumn = "auto / span 2" ;
2023-01-17 18:48:09 +00:00
field . append ( pathInput ) ;
let blank = document . createElement ( "div" ) ;
field . append ( blank ) ;
}
else if ( diskConfig [ type ] [ busPrefix ] . hasDiskImage ) {
let diskImageSelect = document . createElement ( "select" ) ;
let diskImageOptions = await request ( ` /nodes/ ${ node } /storage/ ${ storage } /content?content=iso ` ) ;
diskImageOptions . data . forEach ( ( element ) => {
2023-01-17 19:04:28 +00:00
diskImageSelect . add ( new Option ( element . volid . replace ( ` ${ storage } :iso/ ` , "" ) , element . volid ) ) ;
2023-01-17 18:48:09 +00:00
} ) ;
2023-01-17 18:59:06 +00:00
diskImageSelect . value = diskDataParsed . filename ;
2023-01-17 19:43:50 +00:00
diskImageSelect . id = ` ${ busPrefix } _ ${ device } _img ` ;
2023-01-17 19:15:04 +00:00
diskImageSelect . style . gridColumn = "auto / span 2" ;
2023-01-17 18:48:09 +00:00
field . append ( diskImageSelect ) ;
}
else {
2023-01-17 19:15:04 +00:00
let blank = document . createElement ( "div" ) ;
blank . style . gridColumn = "auto / span 2" ;
field . append ( blank ) ;
2023-01-16 23:50:32 +00:00
}
2023-01-16 23:45:51 +00:00
2023-01-16 23:22:39 +00:00
let deleteDiv = document . createElement ( "div" ) ;
deleteDiv . classList . add ( "last-item" ) ;
let deleteBtn = document . createElement ( "img" ) ;
deleteBtn . src = "images/actions/delete.svg" ;
2023-01-16 23:24:24 +00:00
deleteBtn . alt = ` Delete disk ${ busName } ${ device } ` ;
2023-01-16 23:22:39 +00:00
deleteBtn . classList . add ( "clickable" ) ;
deleteDiv . append ( deleteBtn ) ;
field . append ( deleteDiv ) ;
2023-01-12 22:43:17 +00:00
}
2023-01-17 18:18:22 +00:00
function handleDeviceChange ( ) {
}
2023-01-17 19:39:28 +00:00
async function handleStorageChange ( ) {
2023-01-17 19:45:29 +00:00
let storage = this . value ;
2023-01-17 19:39:28 +00:00
let id = this . id ;
let busPrefix = id . split ( "_" ) [ 0 ]
if ( diskConfig [ type ] [ busPrefix ] . hasDiskImage ) { // if the bus has diskImage = true
let diskImageSelect = document . querySelector ( ` # ${ id . replace ( "storage" , "img" ) } ` ) ;
2023-01-17 19:44:34 +00:00
for ( let i = diskImageSelect . options . length - 1 ; i >= 0 ; i -- ) {
2023-01-17 19:39:28 +00:00
diskImageSelect . remove ( i ) ;
}
let diskImageOptions = await request ( ` /nodes/ ${ node } /storage/ ${ storage } /content?content=iso ` ) ;
diskImageOptions . data . forEach ( ( element ) => {
diskImageSelect . add ( new Option ( element . volid . replace ( ` ${ storage } :iso/ ` , "" ) , element . volid ) ) ;
} ) ;
}
}
2023-01-12 22:43:17 +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 ;
}
2023-01-16 23:05:58 +00:00
function parseDisk ( disk ) { // disk in format: STORAGE: FILENAME, ARG1=..., ARG2 = ..., ...
let parsed = { } ;
let kvpairs = disk . split ( "," ) ;
parsed . storage = kvpairs [ 0 ] . split ( ":" ) [ 0 ] ;
2023-01-17 19:50:59 +00:00
parsed . filename = kvpairs [ 0 ] ;
2023-01-16 23:05:58 +00:00
kvpairs . shift ( ) ;
kvpairs . forEach ( ( element ) => {
let key = element . split ( "=" ) [ 0 ] ;
let val = element . split ( "=" ) [ 1 ] ;
parsed [ key ] = val ;
2023-01-12 22:43:17 +00:00
} ) ;
2023-01-17 17:52:20 +00:00
if ( parsed . size . includes ( "G" ) ) {
parsed . size = parseInt ( parsed . size . replace ( "G" , "" ) ) ;
2023-01-16 23:45:51 +00:00
}
else if ( parsed . size . includes ( "T" ) ) {
2023-01-17 17:52:20 +00:00
parsed . size = parseInt ( parsed . size . replace ( "G" , "" ) ) * 1024 ;
2023-01-16 23:45:51 +00:00
}
2023-01-17 17:52:20 +00:00
else {
2023-01-17 18:26:11 +00:00
parsed . size = 0 ;
2023-01-17 17:52:20 +00:00
}
2023-01-16 23:05:58 +00:00
return parsed ;
2022-12-19 06:14:57 +00:00
}