get files for dropdowns from/on selected servers

This commit is contained in:
mad 2018-08-20 18:56:48 +02:00
parent b224a88e7d
commit 40b341c626
3 changed files with 125 additions and 30 deletions

View File

@ -27,7 +27,7 @@
</div> </div>
</div> </div>
</div> </div>
<script src="nlvmi.js?3"></script> <script src="nlvmi.js?4"></script>
<div class=container-fluid><div class="row" style="background-color:lightgreen;"><div class="col-sm"><h1><a href="?" style=color:white;>nlvmi</a><a href="?" style="color:black;font-size:10px;">@<?php echo gethostname();?></a></h1></div><div class=col-sm style=text-align:right> <div class=container-fluid><div class="row" style="background-color:lightgreen;"><div class="col-sm"><h1><a href="?" style=color:white;>nlvmi</a><a href="?" style="color:black;font-size:10px;">@<?php echo gethostname();?></a></h1></div><div class=col-sm style=text-align:right>
<?php if($_SESSION['isadmin']==1){ echo "<a style=color:black href=# onclick=\"javascript:post('?', {'usermgmt': 'show'});\">usermanagement</a></br><a style=color:black href=# onclick=\"javascript:post('?', {'servermgmt': 'show'});\">servers</a></br>";} ?> <?php if($_SESSION['isadmin']==1){ echo "<a style=color:black href=# onclick=\"javascript:post('?', {'usermgmt': 'show'});\">usermanagement</a></br><a style=color:black href=# onclick=\"javascript:post('?', {'servermgmt': 'show'});\">servers</a></br>";} ?>
<a style=color:black href=# onclick="javascript:post('?', {'logout': 'logout'});">logout</a></div></div> <a style=color:black href=# onclick="javascript:post('?', {'logout': 'logout'});">logout</a></div></div>

View File

@ -23,6 +23,29 @@ function checkvm($vmname) {
} }
return 0; return 0;
} }
function serverdepropdown($server, $val, $what){
if ($what == "cdrom"){
$dir = "isodirectory";
$enddiv = "</div>";
} elseif (preg_match('/drive/', $what)) {
$dir = "vmdirectory";
$enddiv="";
}
echo "<div class=row><div class=col-md-1 style=text-align:right>$what</div><div class=col-md-2><div class=dropdown><button class=\"btn btn-secondary dropdown-toggle\" type=button id=".$what."button data-toggle=dropdown aria-haspopup=true aria-expanded=false>$val</button>";
echo "<div class=dropdown-menu aria-labelledby=".$what."button>";
echo "<a class=dropdown-item href=\"javascript:dropdown('".$what."button','')\">no $what</a><div class=dropdown-divider></div>";
$shellout=shell_exec("/usr/bin/nlvmi listdir $server $dir");
$files = explode("\n", $shellout);
for ($f=0; $f<count($files); $f++){
$file = explode(" ", $files[$f]);
$lastpart = count($file) -1;
if((preg_match('/\//', $file[$lastpart]))&&(!preg_match('/^d/', $file[0]))){
echo "<a class=dropdown-item href=\"javascript:dropdown('".$what."button','$file[$lastpart]')\">$file[$lastpart]</a>";
}
}
echo "$enddiv</div></div></div><input type=hidden id=".$what."buttonh name=$what value=$val>";
}
//read config file //read config file
$configfile = new SplFileObject("/etc/nlvmi/nlvmi.conf"); $configfile = new SplFileObject("/etc/nlvmi/nlvmi.conf");
@ -241,7 +264,40 @@ if (isset($_SESSION['username'])){
//} //}
} }
} }
if (isset($_POST['serverbuttoni'])){
serverdepropdown($_POST['serverbuttoni'], "no cdrom", 'cdrom');
exit;
} elseif (isset($_POST['serverbuttona'])){
serverdepropdown($_POST['serverbuttona'], "no drive1", 'drive1');
echo "<div class=row><div class=col>format1</div><div class=col><div class=dropdown><button class=\"btn btn-secondary dropdown-toggle\" type=button id=format1buttonformat1 data-toggle=dropdown aria-haspopup=true aria-expanded=false></button>";
echo "<div class=dropdown-menu aria-labelledby=formatbuttonformat1>";
echo "<a class=dropdown-item href=\"javascript:dropdown('format1buttonformat1','')\">&nbsp</a>";
echo "<div class=dropdown-divider></div>";
echo "<a class=dropdown-item href=\"javascript:dropdown('format1buttonformat1','raw')\">raw</a>";
echo "<a class=dropdown-item href=\"javascript:dropdown('format1buttonformat1','qcow2')\">qcow2</a>";
echo "</div></div></div></div></div></div></div><input type=hidden id=format1buttonformat1h name=format1 value=\"no drive1\">";
exit;
} elseif (isset($_POST['serverbuttonb'])){
serverdepropdown($_POST['serverbuttonb'], "no drive2", 'drive2');
echo "<div class=row><div class=col>format2</div><div class=col><div class=dropdown><button class=\"btn btn-secondary dropdown-toggle\" type=button id=format2buttonformat2 data-toggle=dropdown aria-haspopup=true aria-expanded=false></button>";
echo "<div class=dropdown-menu aria-labelledby=formatbuttonformat2>";
echo "<a class=dropdown-item href=\"javascript:dropdown('format2buttonformat2','')\">&nbsp</a>";
echo "<div class=dropdown-divider></div>";
echo "<a class=dropdown-item href=\"javascript:dropdown('format2buttonformat2','raw')\">raw</a>";
echo "<a class=dropdown-item href=\"javascript:dropdown('format2buttonformat2','qcow2')\">qcow2</a>";
echo "</div></div></div></div></div><input type=hidden id=format2buttonformat2h name=format2 value=\"no drive1\">";
exit;
} elseif (isset($_POST['serverbuttonc'])){
serverdepropdown($_POST['serverbuttonc'], "no drive3", 'drive3');
echo "<div class=row><div class=col>format3</div><div class=col><div class=dropdown><button class=\"btn btn-secondary dropdown-toggle\" type=button id=format3buttonformat3 data-toggle=dropdown aria-haspopup=true aria-expanded=false></button>";
echo "<div class=dropdown-menu aria-labelledby=formatbuttonformat3>";
echo "<a class=dropdown-item href=\"javascript:dropdown('format3buttonformat3','')\">&nbsp</a>";
echo "<div class=dropdown-divider></div>";
echo "<a class=dropdown-item href=\"javascript:dropdown('format3buttonformat3','raw')\">raw</a>";
echo "<a class=dropdown-item href=\"javascript:dropdown('format3buttonformat3','qcow2')\">qcow2</a>";
echo "</div></div></div></div></div><input type=hidden id=format3buttonformat3h name=format3 value=\"no drive1\">";
exit;
}
//ajax queries are all done, time for the header //ajax queries are all done, time for the header
include('header.php'); include('header.php');
@ -358,6 +414,7 @@ if (isset($_SESSION['username'])){
while ($col = $res->fetchArray(SQLITE3_ASSOC)) { while ($col = $res->fetchArray(SQLITE3_ASSOC)) {
$arrColnames[]=$col['name']; $arrColnames[]=$col['name'];
} }
$server = $e['server'];
for ($col=0; $col <count($arrColnames); $col++){ for ($col=0; $col <count($arrColnames); $col++){
if ($arrColnames[$col]!="id"){ if ($arrColnames[$col]!="id"){
if (isset($e[$arrColnames[$col]])){ if (isset($e[$arrColnames[$col]])){
@ -402,7 +459,7 @@ if (isset($_SESSION['username'])){
echo "<div class=dropdown-divider></div>"; echo "<div class=dropdown-divider></div>";
echo "<a class=dropdown-item href=\"javascript:dropdown('$arrColnames[$col]button$arrColnames[$col]','raw')\">raw</a>"; echo "<a class=dropdown-item href=\"javascript:dropdown('$arrColnames[$col]button$arrColnames[$col]','raw')\">raw</a>";
echo "<a class=dropdown-item href=\"javascript:dropdown('$arrColnames[$col]button$arrColnames[$col]','qcow2')\">qcow2</a>"; echo "<a class=dropdown-item href=\"javascript:dropdown('$arrColnames[$col]button$arrColnames[$col]','qcow2')\">qcow2</a>";
echo "</div></div></div></div></div><input type=hidden id=$arrColnames[$col]button$arrColnames[$col]h name=$arrColnames[$col] value=\"$val\">"; echo "</div></div></div></div></div></div><input type=hidden id=$arrColnames[$col]button$arrColnames[$col]h name=$arrColnames[$col] value=\"$val\">";
} elseif ($arrColnames[$col] == "autostart"){ } elseif ($arrColnames[$col] == "autostart"){
$checked=""; $checked="";
if ($val=="1"){ if ($val=="1"){
@ -410,22 +467,11 @@ if (isset($_SESSION['username'])){
} }
echo "<div class=row><div class=col-md-1 style=text-align:right>autostart</div><div class=col-md-2><input data-onstyle=\"success\" data-offstyle=danger name=autostart id=\"autostart\" type=\"checkbox\" data-toggle=\"toggle\" data-on=\"boot with host\" data-off=\"no autostart\" $checked></div></div>"; echo "<div class=row><div class=col-md-1 style=text-align:right>autostart</div><div class=col-md-2><input data-onstyle=\"success\" data-offstyle=danger name=autostart id=\"autostart\" type=\"checkbox\" data-toggle=\"toggle\" data-on=\"boot with host\" data-off=\"no autostart\" $checked></div></div>";
} elseif ($arrColnames[$col] == "cdrom"){ } elseif ($arrColnames[$col] == "cdrom"){
echo "<div class=row><div class=col-md-1 style=text-align:right>cdrom</div><div class=col-md-2><div class=dropdown><button class=\"btn btn-secondary dropdown-toggle\" type=button id=cdrombutton data-toggle=dropdown aria-haspopup=true aria-expanded=false>$val</button>"; echo "<div id=cdromdiv>";
echo "<div class=dropdown-menu aria-labelledby=cdrombutton>"; serverdepropdown($server, $val, "cdrom");
echo "<a class=dropdown-item href=\"javascript:dropdown('cdrombutton','')\">no cdrom</a><div class=dropdown-divider></div>"; echo "</div>";
$isodirs = explode('|', $config['ISODIRECTORY']); // } elseif (preg_match('/format/', $arrColnames[$col])) {
foreach($isodirs as $isodir){ // echo "<div class=col-md-1 style=text-align:right>$arrColnames[$col]</div><div class=col-md-2><input class=form-control type=text name=$arrColnames[$col] value=$val></div></div></div>";
$files = array_diff(scandir($isodir), array('.', '..'));
$files = array_values(array_filter($files));
for ($f=0; $f<count($files); $f++){
if ((preg_match('/iso/', $files[$f]))||(preg_match('/ISO/', $files[$f]))){
echo "<a class=dropdown-item href=\"javascript:dropdown('cdrombutton','$isodir/$files[$f]')\">$isodir/$files[$f]</a>";
}
}
}
echo "</div></div></div></div><input type=hidden id=cdrombuttonh name=cdrom value=$val>";
} elseif (preg_match('/format/', $arrColnames[$col])) {
echo "<div class=col-md-1 style=text-align:right>$arrColnames[$col]</div><div class=col-md-2><input class=form-control type=text name=$arrColnames[$col] value=$val></div></div>";
} elseif (preg_match('/tapdev/', $arrColnames[$col])) { } elseif (preg_match('/tapdev/', $arrColnames[$col])) {
echo "<div class=row><div class=col-md-1 style=text-align:right>$arrColnames[$col]</div><div class=col-md-2><input class=form-control type=text name=$arrColnames[$col] value=\"$val\" placeholder=\"$placehold[$colname]\"></div>"; echo "<div class=row><div class=col-md-1 style=text-align:right>$arrColnames[$col]</div><div class=col-md-2><input class=form-control type=text name=$arrColnames[$col] value=\"$val\" placeholder=\"$placehold[$colname]\"></div>";
} elseif (preg_match('/macaddr/', $arrColnames[$col])) { } elseif (preg_match('/macaddr/', $arrColnames[$col])) {
@ -433,18 +479,27 @@ if (isset($_SESSION['username'])){
} elseif (preg_match('/brdev/', $arrColnames[$col])) { } elseif (preg_match('/brdev/', $arrColnames[$col])) {
echo "<div class=col-md-1 style=text-align:right>$arrColnames[$col]</div><div class=col-md-2><input class=form-control type=text name=$arrColnames[$col] value=\"$val\" placeholder=\"$placehold[$colname]\"></div></div>"; echo "<div class=col-md-1 style=text-align:right>$arrColnames[$col]</div><div class=col-md-2><input class=form-control type=text name=$arrColnames[$col] value=\"$val\" placeholder=\"$placehold[$colname]\"></div></div>";
} elseif (preg_match('/drive/', $arrColnames[$col])){ } elseif (preg_match('/drive/', $arrColnames[$col])){
echo "<div id=$arrColnames[$col]div>";
serverdepropdown($server, $val, $arrColnames[$col]);
} elseif ($arrColnames[$col] == "server"){
echo "<div class=row><div class=col-md-1 style=text-align:right>$arrColnames[$col]</div><div class=col-md-2><div class=dropdown><button class=\"btn btn-secondary dropdown-toggle\" type=button id=$arrColnames[$col]button data-toggle=dropdown aria-haspopup=true aria-expanded=false>$val</button>"; echo "<div class=row><div class=col-md-1 style=text-align:right>$arrColnames[$col]</div><div class=col-md-2><div class=dropdown><button class=\"btn btn-secondary dropdown-toggle\" type=button id=$arrColnames[$col]button data-toggle=dropdown aria-haspopup=true aria-expanded=false>$val</button>";
echo "<div class=dropdown-menu aria-labelledby=$arrColnames[$col]button>"; echo "<div class=dropdown-menu aria-labelledby=$arrColnames[$col]button>";
echo "<a class=dropdown-item href=\"javascript:dropdown('$arrColnames[$col]button','')\">no disk</a><div class=dropdown-divider></div>"; $sqls="SELECT hostname FROM servers";
$vmdirs = explode('|', $config['VMDIRECTORY']); $ress = $db_handle->query($sqls);
foreach($vmdirs as $vmdir){ while ($rows = $ress->fetchArray(SQLITE3_ASSOC)){
$files = array_diff(scandir($vmdir), array('.', '..'));
$files = array_values(array_filter($files)); echo "<a class=dropdown-item href=\"javascript:dropdown('$arrColnames[$col]button','$rows[hostname]')\">$rows[hostname]</a>";
for ($f=0; $f<count($files); $f++){
echo "<a class=dropdown-item href=\"javascript:dropdown('$arrColnames[$col]button','$vmdir/$files[$f]')\">$vmdir/$files[$f]</a>";
} }
} // $vmdirs = explode('|', $config['VMDIRECTORY']);
echo "</div></div></div><input type=hidden id=$arrColnames[$col]buttonh name=$arrColnames[$col] value=$val>"; // foreach($vmdirs as $vmdir){
// $files = array_diff(scandir($vmdir), array('.', '..'));
// $files = array_values(array_filter($files));
// for ($f=0; $f<count($files); $f++){
// echo "<a class=dropdown-item href=\"javascript:dropdown('$arrColnames[$col]button','$vmdir/$files[$f]')\">$vmdir/$files[$f]</a>";
// }
// }
echo "</div></div></div></div><input type=hidden id=$arrColnames[$col]buttonh name=$arrColnames[$col] value=$val>";
} else { } else {
echo "<div class=row><div class=col-md-1 style=text-align:right>$arrColnames[$col]</div><div class=col-md-2><input class=form-control type=text name=$arrColnames[$col] value=\"$val\" placeholder=\"$placehold[$colname]\"></div></div>"; echo "<div class=row><div class=col-md-1 style=text-align:right>$arrColnames[$col]</div><div class=col-md-2><input class=form-control type=text name=$arrColnames[$col] value=\"$val\" placeholder=\"$placehold[$colname]\"></div></div>";

View File

@ -11,6 +11,48 @@ function makeid() {
function dropdown(i,e) { function dropdown(i,e) {
document.getElementById(i).innerHTML = e; document.getElementById(i).innerHTML = e;
document.getElementById(i +'h').value = e; document.getElementById(i +'h').value = e;
if (i == "serverbutton") {
console.log("here");
$.ajax({
type: 'POST',
url: "?rand="+ makeid(),
data: "serverbuttoni="+e,
success: function(response) {
document.getElementById('cdromdiv').innerHTML = response;
},
});
console.log("a");
$.ajax({
type: 'POST',
url: "?rand="+ makeid(),
data: "serverbuttona="+e,
success: function(response) {
document.getElementById('drive1div').innerHTML = response;
},
});
console.log("b");
$.ajax({
type: 'POST',
url: "?rand="+ makeid(),
data: "serverbuttonb="+e,
success: function(response) {
document.getElementById('drive2div').innerHTML = response;
},
});
console.log("c");
$.ajax({
type: 'POST',
url: "?rand="+ makeid(),
data: "serverbuttonc="+e,
success: function(response) {
document.getElementById('drive3div').innerHTML = response;
},
});
}
} }
function post(path, params, method) { function post(path, params, method) {
@ -48,9 +90,7 @@ $('#confirm-delete').on('show.bs.modal', function(e) {
}); });
function submitbutton(formid){ function submitbutton(formid){
console.log(formid);
data = $('#'+ formid).serialize(); data = $('#'+ formid).serialize();
console.log(data);
$.ajax({ $.ajax({
type: 'POST', type: 'POST',
url: "?rand="+ makeid(), url: "?rand="+ makeid(),