package Ġcom . github . ker ub istan . ker ub . utils . j unix . storage manager . g vin um Ċ Ċ import Ġcom . github . ker ub istan . ker ub . host . bash Monitor Ċ import Ġcom . github . ker ub istan . ker ub . host . execute Or Die Ċ import Ġcom . github . ker ub istan . ker ub . utils . KB Ċ import Ġcom . github . ker ub istan . ker ub . utils . getLogger Ċ import Ġcom . github . ker ub istan . ker ub . utils . to Size Ċ import Ġio . github . ker ub istan . k ro ki . strings . substring Between Ċ import Ġorg . apache . ss hd . client . session . Client Session Ċ import Ġjava . io . OutputStream Ċ import Ġjava . math . BigInteger Ċ Ċ object ĠG V in um Ġ{ ĊĊ ĉ private Ġval Ġlogger Ġ= Ġget Logger ( GV in um :: class ) ĊĊ ĉ fun Ġlist D rives ( session : ĠClient Session ) Ġ= Ġparse Drive List ( g vin um (" ld ", Ġsession )) Ċ ĉ fun Ġlist Sub Dis ks ( session : ĠClient Session ): ĠList < G vin um Sub Disk > Ġ= Ġparse Sub Disk List ( g vin um (" ls ", Ġsession )) Ċ ĉ fun Ġlist Volumes ( session : ĠClient Session ): ĠList < G vin um Volume > Ġ= Ġparse Volume List ( g vin um (" lv ", Ġsession )) Ċ ĉ fun Ġlist P lex es ( session : ĠClient Session ) Ġ= Ġparse P lex es List ( g vin um (" lp ", Ġsession )) ĊĊ ĉ private Ġconst Ġval Ġseparator Ġ= Ġ"-- sep " ĊĊ ĉ private Ġval Ġheader Ġ= ĠRegex . from Literal ("\\ n + \\ s \\ S + :$ ") Ċ ĉ private Ġval Ġdrive Header Ġ= ĠRegex . from Literal (" Drive Ġ") Ċ ĉ private Ġval Ġsub disk Header Ġ= ĠRegex . from Literal (" Sub disk ") Ċ ĉ private Ġval Ġp lex Header Ġ= ĠRegex . from Literal (" P lex Ġ") Ċ ĉ private Ġval Ġvolume Header Ġ= ĠRegex . from Literal (" Volume Ġ") Ċ ĉ private Ġconst Ġval Ġup Ġ= Ġ" up " ĊĊ ĉ private Ġfun Ġg vin um ( command : ĠString , Ġsession : ĠClient Session ): ĠString Ċĉĉ ĉ = Ġsession . execute Or Die (" g vin um Ġ$ command Ġ- v ") ĊĊ ĉ internal Ġfun Ġparse Drive List ( output : ĠString ): ĠList < G vin um Drive > Ġ= Ċĉĉ ĉ output . substring After (" d rives :\ n ") Ċĉĉĉĉ ĉ . substring After (" 1 Ġdrive : "). split ( drive Header ). filter Ġ{ Ġit . isNotBlank () Ġ}. map Ġ{ Ċĉĉĉ ĉ str Ġ-> Ċĉĉĉ ĉ G vin um Drive ( Ċĉĉĉĉĉ ĉ name Ġ= Ġstr . substring Before (" :" ), Ċĉĉĉĉĉ ĉ device Ġ= Ġstr . substring Between (" Device Ġ", Ġ"\ n "), Ċĉĉĉĉĉ ĉ available Ġ= Ġstr . substring Between (" Available :", Ġ" Ġ( "). to Size (), Ċĉĉĉĉĉ ĉ size Ġ= Ġstr . substring Between (" Size :", Ġ" Ġ( "). to Size (), Ċĉĉĉĉĉ ĉ up Ġ= Ġis Up ( str ), Ċĉĉĉĉĉ ĉ used Ġ= Ġstr . substring Between (" Used :", Ġ" Ġ( "). to Size () Ċĉĉĉ ĉ ) Ċĉĉ ĉ } ĊĊ ĉ private Ġfun Ġis Up ( str : ĠString ) Ġ= Ġstr . substring Between (" State : Ġ", Ġ"\ n ") Ġ== Ġup ĊĊ ĉ internal Ġfun Ġparse Sub Disk List ( output : ĠString ): ĠList < G vin um Sub Disk > Ġ= Ċĉĉ ĉ output . substring After (" sub disks : "). split ( sub disk Header ). filter Ġ{ Ġit . isNotBlank () Ġ}. map Ġ{ Ċĉĉĉ ĉ str Ġ-> Ċĉĉĉ ĉ G vin um Sub Disk ( Ċĉĉĉĉĉ ĉ name Ġ= Ġstr . substring Before (": "). trim (), Ċĉĉĉĉĉ ĉ drive Ġ= Ġstr . substring Between (" Drive Ġ", Ġ" Ġ( "), Ċĉĉĉĉĉ ĉ up Ġ= Ġis Up ( str ), Ċĉĉĉĉĉ ĉ size Ġ= Ġstr . substring Between (" Size :", Ġ" Ġ( "). to Size (), Ċĉĉĉĉĉ ĉ offset Ġ= ĠBigInteger ( str . substring Between (" ) Ġat Ġoffset Ġ", Ġ" Ġ( ")), Ċĉĉĉĉĉ ĉ plex Ġ= Ġstr . substring Between (" P lex Ġ", Ġ" Ġat Ġoffset Ġ") Ċĉĉĉ ĉ ) Ċĉĉ ĉ } ĊĊ ĉ internal Ġfun Ġparse P lex es List ( output : ĠString ): ĠList < G vin um P lex > Ġ= Ċĉĉ ĉ output . substring After (" plex es : "). split ( plex Header ). filter Ġ{ Ġit . isNotBlank () Ġ}. map Ġ{ Ċĉĉĉ ĉ str Ġ-> Ċĉĉĉ ĉ G vin um P lex ( Ċĉĉĉĉĉ ĉ name Ġ= Ġstr . substring Before (": "). trim (), Ċĉĉĉĉĉ ĉ sub disks Ġ= Ġstr . substring Between (" Sub disks :", Ġ"\ n "). trim (). toInt (), Ċĉĉĉĉĉ ĉ up Ġ= Ġis Up ( str ), Ċĉĉĉĉĉ ĉ volume Ġ= Ġstr . substring After (" Part Ġof Ġvolume Ġ"). trim () Ċĉĉĉ ĉ ) Ċĉĉ ĉ } ĊĊ ĉ internal Ġfun Ġparse Volume List ( output : ĠString ): ĠList < G vin um Volume > Ċĉĉ ĉ = Ġoutput . substring After (" volumes : "). split ( volume Header ). filter Ġ{ Ġit . isNotBlank () Ġ}. map Ġ{ Ċĉ ĉ str Ġ-> Ċĉ ĉ G vin um Volume ( Ċĉĉĉ ĉ name Ġ= Ġstr . substring Before (": "). trim (), Ċĉĉĉ ĉ size Ġ= Ġstr . substring Between (" Size : Ġ", Ġ" Ġ( "). to Size (), Ċĉĉĉ ĉ up Ġ= Ġis Up ( str ) Ċĉ ĉ ) Ċ ĉ } ĊĊ ĉ private Ġfun Ġround Up Kb ( size : ĠBigInteger ): ĠBigInteger Ġ{ Ċĉ ĉ val Ġk bs Ġ= Ġsize Ġ/ ĠKB . to BigInteger () Ċĉ ĉ return Ġ( k bs Ġ+ ĠBigInteger . ONE ) Ċ ĉ } ĊĊ ĉ fun Ġcreate Simple Volume ( session : ĠClient Session , Ġvol Name : ĠString , Ġdisk : ĠString , Ġsize : ĠBigInteger ) Ġ{ Ċĉ ĉ create Volume ( config Ġ= Ċĉ ĉ """ Ċĉ ĉ volume Ġ$ vol Name ĊĉĉĉĠ Ġp lex Ġorg Ġconcat Ċĉĉĉ ĉ sd Ġlength Ġ${ round Up Kb ( size )} kb Ġdrive Ġ$ disk Ċĉ ĉ """, Ġsession Ġ= Ġsession , Ġvol Name Ġ= Ġvol Name ) Ċ ĉ } ĊĊ ĉ fun Ġcreate Concat en ated Volume ( session : ĠClient Session , Ġvol Name : ĠString , Ġdisks : ĠMap < String , ĠBigInteger >) Ġ{ Ċĉ ĉ create Volume ( config Ġ= Ċĉ ĉ build String ( 1 2 8 Ġ+ Ġ( disks . size Ġ* Ġ 6 4 )) Ġ{ Ċĉĉ ĉ append ( Ċĉĉĉĉ ĉ """ Ċĉĉ ĉ volume Ġ$ vol Name ĊĉĉĉĠ Ġp lex Ġorg Ġconcat Ċĉĉĉĉ ĉ """) Ċĉĉ ĉ disks . forEach Ġ{ Ċĉĉĉ ĉ disk Ġ-> Ċĉĉĉ ĉ append (" sd Ġlength Ġ"). append ( round Up Kb ( disk . value )). append (" Ġdrive Ġ"). append ( disk . key ). append ("\ n ") Ċĉĉ ĉ } Ċĉ ĉ }, Ġsession Ġ= Ġsession , Ġvol Name Ġ= Ġvol Name ) Ċ ĉ } ĊĊ ĉ private Ġfun Ġcreate Volume ( config : ĠString , Ġsession : ĠClient Session , Ġvol Name : ĠString ) Ġ{ Ċĉ ĉ logger . debug (" creating Ġg vin um Ġvolume Ġ$ vol Name Ġwith Ġconfig Ġ$ config ") Ċĉ ĉ session . create S ftp Client (). use Ġ{ Ċĉĉ ĉ sf tp Ġ-> Ċĉĉ ĉ val Ġtemp ConfigFile Ġ= Ġ"/ tmp /$ vol Name " Ċĉĉ ĉ sf tp . write ( temp ConfigFile ). writer ( charset (" ASCII ")). use Ġ{ Ċĉĉĉ ĉ writer Ġ-> Ċĉĉĉ ĉ writer . write ( config ) Ċĉĉ ĉ } Ċĉĉ ĉ session . execute Or Die (" g vin um Ġcreate Ġ$ temp ConfigFile ") Ċĉĉ ĉ sf tp . remove ( temp ConfigFile ) Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ fun Ġremove Volume ( session : ĠClient Session , Ġvol Name : ĠString ) Ġ{ Ċĉ ĉ session . execute Or Die (" g vin um Ġrm Ġ$ vol Name ") Ċ ĉ } ĊĊ ĉ class ĠG vin um Drive Monitor OutputStream ( private Ġval Ġcallback : Ġ( List < G vin um Drive >) Ġ-> ĠUnit ) Ġ: ĠOutputStream () Ġ{ Ċĉ ĉ private Ġval Ġbuffer Ġ= ĠStringBuilder ( 1 2 8 ) Ċĉ ĉ override Ġfun Ġwrite ( input : ĠInt ) Ġ{ Ċĉĉ ĉ buffer . append ( input . to Char ()) Ċĉĉ ĉ if Ġ( buffer . endsWith ( separator )) Ġ{ Ċĉĉĉ ĉ callback ( parse Drive List ( buffer . substring ( 0 , Ġbuffer . length Ġ- Ġseparator . length ))) Ċĉĉĉ ĉ buffer . clear () Ċĉĉ ĉ } Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ fun Ġmonitor D rives ( session : ĠClient Session , Ġcallback : Ġ( List < G vin um Drive >) Ġ-> ĠUnit ) Ġ{ Ċĉ ĉ session . bash Monitor (" g vin um Ġld Ġ- v ", Ġ 6 0 , Ġseparator , ĠG vin um Drive Monitor OutputStream ( callback )) Ċ ĉ } Ċ Ċ }