server management

This commit is contained in:
mad 2018-08-20 20:19:52 +02:00
parent 40b341c626
commit 96edf459f1
1 changed files with 83 additions and 45 deletions

View File

@ -17,34 +17,32 @@ $mode="";
function checkvm($vmname) {
$shellout = shell_exec("/usr/bin/nlvmi checkvm $vmname bla");//){
// echo $shellout;
if (preg_match_all('/not running/', $shellout)){
return 1;
}
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>";
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
@ -96,9 +94,6 @@ if ($usersfound=="no"){
$_SESSION['isadmin']="1";
if (!isset($_POST['newusername'])){
echo "<h1>You have no users in the datbase!</h1></br>";
// echo "Aborting....";
// exit;
$mode="usermgmt";
}
} elseif ((isset($_POST['login']))&&(isset($_POST['username']))&&(isset($_POST['password']))){
@ -198,7 +193,41 @@ if (isset($_SESSION['username'])){
}
}
}
//insert new server in db
if(isset($_POST['newservername'])){
if($_SESSION['isadmin']!=1){
echo "you are not allowed to add servers!";
} else {
$newservername = clean($_POST['newhostname']);
$connectstring = clean($_POST['connectstring']);
$vmdirectory = clean($_POST['vmdirectory']);
$isodirectory = clean($_POST['isodirectory']);
$sqlu="INSERT INTO servers ('hostname','connectstring','vmdirectory','isodirectory') VALUES('$newservername',$connectstring','$vmdirectory','$isodirectory')";
$res = $db_handle->query($sqlu);
echo "success";
exit;
}
}
//update server in db
if(isset($_POST['changehostname'])){
if($_SESSION['isadmin']!=1){
echo "you are not allowed to change servers!";
} else {
$newservername = clean($_POST['changehostname']);
$connectstring = clean($_POST['connectstring']);
$vmdirectory = clean($_POST['vmdirectory']);
$isodirectory = clean($_POST['isodirectory']);
$changeid = clean($_POST['changeid']);
$sql="UPDATE servers SET hostname='$newservername', connectstring='$connectstring', vmdirectory='$vmdirectory', isodirectory='$isodirectory' where id='$changeid'";
if($db_handle->query($sql)){
echo "success";
exit;
} else {
echo "$sql";
exit;
}
}
}
//insert or update vm in db
if(isset($_REQUEST['mode'])){
if($_REQUEST['mode']=="newvm"){
@ -232,10 +261,7 @@ if (isset($_SESSION['username'])){
$res = $db_handle->exec($sql);
echo "success";
exit;
// $mode="";
} elseif ($_REQUEST['mode']=="editvm"){
// $mode="";
$sql="UPDATE vms SET ";
foreach(array_keys($_REQUEST) as $rkey){
if (($rkey!="mode")&&($rkey!="editid")&&($rkey!="rand")){
@ -256,18 +282,16 @@ if (isset($_SESSION['username'])){
$sql = rtrim($sql, ',');
$eid=clean($_REQUEST['editid']);
$sql .= " WHERE id='$eid'";
// if($db_handle->exec($sql)){
$res = $db_handle->exec($sql);
echo "success";
//echo $res;
exit;
//}
}
}
if (isset($_POST['serverbuttoni'])){
serverdepropdown($_POST['serverbuttoni'], "no cdrom", 'cdrom');
exit;
} elseif (isset($_POST['serverbuttona'])){
//not nice, i know... Will do a function later...
} 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>";
@ -298,6 +322,7 @@ if (isset($_SESSION['username'])){
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
include('header.php');
@ -337,6 +362,31 @@ if (isset($_SESSION['username'])){
}
//servermanagement
if (isset($_POST['servermgmt'])){
$mode="servermgmt";
if ($_SESSION['isadmin']=="1"){
$sql="SELECT * FROM servers";
$res = $db_handle->query($sql);
echo "</br>";
//echo "<div class=row><div class=col>Username</div><div class=col>Is Admin</div><div class=col>New Password</div></div>";
echo "<h2>Existing servers</h2>";
echo "<div class=row><div class=col>hostname</div><div class=col>connectstring</div><div class=col>VM directory</div><div class=col>ISO directory</div><div class=col></div></div>";
while ($row = $res->fetchArray(SQLITE3_ASSOC)){
echo "<form id=sform$row[id] action=? method=post><div class=row><div class=col style=text-align:right><input class=form-control name=changehostname value=$row[hostname]></div>";
echo "<div class=col><input name=connectstring id=\"connectstring\" type=\"text\" value=$row[connectstring]></div>";
echo "<div class=col><input name=vmdirectory id=\"connectstring\" type=\"text\" value=$row[vmdirectory]></div>";
echo "<div class=col><input name=isodirectory id=\"connectstring\" type=\"text\" value=$row[isodirectory]></div>";
echo "<div class=col><input type=hidden name=changeid value=$row[id]><a href=# class=\"btn btn-success\" onclick=\"javascript:submitbutton('sform$row[id]');\">save server</a></div></div></form>";
}
//newserverform
echo "<h2>Create a new server</h2><form id=newserverform action=? method=post><div class=row><div class=col><input type=text class=form-control name=newhostname placeholder=hostname></div>";
echo "<div class=col><input class=form-control name=connectstring type=\"text\" placeholder=\"nlvmi@192.168.0.X\"></div>";
echo "<div class=col><input class=form-control type=test name=vmdirectory placeholder=\"multiple directories like /directorya;/directoryb\"></div>";
echo "<div class=col><input class=form-control type=test name=isoirectory placeholder=\"multiple directories like /directorya;/directoryb\"></div>";
echo "<div class=col><a href=# class=\"btn btn-success\" onclick=\"javascript:submitbutton('newserverform');\">Create server</a></div></div></form>";
}
}
//start vm
@ -423,7 +473,6 @@ if (isset($_SESSION['username'])){
$val = "";
}
$colname=$arrColnames[$col];
//$placeh=$placehold[$arrColnames[$col]];
if ($arrColnames[$col] == "bootoption"){
if($val=="") {
$val="c";
@ -433,7 +482,6 @@ if (isset($_SESSION['username'])){
echo "<a class=dropdown-item href=\"javascript:dropdown('bootoptionbutton','c')\">c</a>";
echo "<a class=dropdown-item href=\"javascript:dropdown('bootoptionbutton','d')\">d</a>";
echo "</div></div></div></div><input type=hidden id=bootoptionbuttonh name=bootoption value=$val>";
// echo "$arrColnames[$col] <select name=bootoption><option value=c>C</option><option value=d>D</option></select></br>";
} elseif ($arrColnames[$col] == "user"){
if ($val=="") {
$val=$_SESSION['username'];
@ -470,8 +518,6 @@ if (isset($_SESSION['username'])){
echo "<div id=cdromdiv>";
serverdepropdown($server, $val, "cdrom");
echo "</div>";
// } 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></div>";
} 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>";
} elseif (preg_match('/macaddr/', $arrColnames[$col])) {
@ -491,14 +537,6 @@ if (isset($_SESSION['username'])){
echo "<a class=dropdown-item href=\"javascript:dropdown('$arrColnames[$col]button','$rows[hostname]')\">$rows[hostname]</a>";
}
// $vmdirs = explode('|', $config['VMDIRECTORY']);
// 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 {