2018-08-11 20:07:23 +02:00
|
|
|
<?php
|
|
|
|
function clean($str) {
|
|
|
|
$search = array('&' , '"' , "'" , '<' , '>' );
|
|
|
|
$replace = array('&', '"', ''', '<', '>' );
|
|
|
|
|
|
|
|
$str = str_replace($search, $replace, $str);
|
|
|
|
return $str;
|
|
|
|
}
|
2018-08-12 20:21:33 +02:00
|
|
|
|
|
|
|
function checkvm($vmname) {
|
|
|
|
$shellout = shell_exec("/usr/bin/wrap-nlvmi checkvm $vmname bla");//){
|
|
|
|
if (preg_match_all('/running/', $shellout)){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-08-11 20:07:23 +02:00
|
|
|
//read config file
|
|
|
|
$configfile = new SplFileObject("/etc/nlvmi/nlvmi.conf");
|
|
|
|
while (!$configfile->eof()) {
|
|
|
|
$line = $configfile->fgets();
|
|
|
|
if (!preg_match('/^#/', $line)){
|
|
|
|
if (!preg_match('/^$/', $line)){
|
|
|
|
if (preg_match('/=/', $line)){
|
|
|
|
$configparam = explode('=', $line);
|
|
|
|
$p = rtrim($configparam[1]);
|
|
|
|
//echo "B$p";
|
|
|
|
$config[$configparam[0]]=str_replace('"', '', $p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$file = null;
|
|
|
|
|
|
|
|
//check db connection
|
|
|
|
if ($config['DATABASETYPE']=="sqlite"){
|
|
|
|
$db_handle = new SQLite3($config['SQLITEFILE']);
|
|
|
|
$query = "SELECT name FROM sqlite_master WHERE type='table' AND name='vms';";
|
|
|
|
$db_handle->exec($query);
|
|
|
|
$result = $db_handle->query($query);
|
|
|
|
$row = $result->fetchArray();
|
|
|
|
if (!$row['name']=="vms"){
|
|
|
|
echo "DB connection failed!";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
2018-08-13 16:49:49 +02:00
|
|
|
if ((isset($_POST['deletevm']))&&(isset($_POST['deletename']))){
|
|
|
|
$vmname = clean($_POST['deletename']);
|
|
|
|
$vmid = clean($_POST['deletevm']);
|
|
|
|
$sql="SELECT id FROM vms WHERE vmname='$vmname' AND id='$vmid'";
|
|
|
|
$res = $db_handle->query($sql);
|
|
|
|
while ($row = $res->fetchArray(SQLITE3_ASSOC)){
|
|
|
|
$sqld="DELETE FROM vms WHERE id='$vmid'";
|
|
|
|
if ($db_handle->query($sqld)){
|
|
|
|
echo "VM $vmname deleted!";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-12 15:09:47 +02:00
|
|
|
include('header.php');
|
|
|
|
$mode="";
|
2018-08-12 20:21:33 +02:00
|
|
|
if (isset($_REQUEST['start'])){
|
|
|
|
$vmname = clean($_REQUEST['start']);
|
|
|
|
$sql="SELECT * FROM vms WHERE vmname='$vmname'";
|
|
|
|
$res = $db_handle->query($sql);
|
|
|
|
while ($row = $res->fetchArray(SQLITE3_ASSOC)){
|
|
|
|
$shellout = shell_exec("/usr/bin/wrap-nlvmi start $vmname bla");
|
|
|
|
echo $shellout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isset($_REQUEST['stop'])){
|
|
|
|
$vmname = clean($_REQUEST['stop']);
|
|
|
|
$sql="SELECT * FROM vms WHERE vmname='$vmname'";
|
|
|
|
$res = $db_handle->query($sql);
|
|
|
|
while ($row = $res->fetchArray(SQLITE3_ASSOC)){
|
|
|
|
if(checkvm($vmname)){
|
|
|
|
$shellout = shell_exec("/usr/bin/wrap-nlvmi stop $vmname bla");
|
|
|
|
echo $shellout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-11 20:07:23 +02:00
|
|
|
|
|
|
|
//form for edit and new vms
|
|
|
|
if (isset($_REQUEST['edit'])){
|
2018-08-12 15:09:47 +02:00
|
|
|
$mode="editvm";
|
2018-08-11 20:07:23 +02:00
|
|
|
$formname="editvm";
|
|
|
|
$eid=clean($_REQUEST['edit']);
|
|
|
|
$sql="SELECT * FROM vms WHERE id='$eid'";
|
|
|
|
$res = $db_handle->query($sql);
|
|
|
|
$editid = "<input type=hidden name=editid value=$eid>";
|
|
|
|
while ($row = $res->fetchArray(SQLITE3_ASSOC)){
|
|
|
|
$e = $row;
|
|
|
|
}
|
|
|
|
} elseif(isset($_REQUEST['newvm'])) {
|
|
|
|
$formname="newvm";
|
|
|
|
$editid="";
|
2018-08-12 15:09:47 +02:00
|
|
|
$mode="newvm";
|
2018-08-11 20:07:23 +02:00
|
|
|
}
|
2018-08-12 15:09:47 +02:00
|
|
|
|
2018-08-11 20:07:23 +02:00
|
|
|
if (isset($formname)){
|
|
|
|
echo "<h2>$formname</h2></br>";
|
|
|
|
echo "<form name=$formname action=? method=post>";
|
|
|
|
$res = $db_handle->query('PRAGMA table_info(vms)');
|
|
|
|
while ($col = $res->fetchArray(SQLITE3_ASSOC)) {
|
|
|
|
$arrColnames[]=$col['name'];
|
|
|
|
}
|
|
|
|
for ($col=0; $col <count($arrColnames); $col++){
|
|
|
|
if ($arrColnames[$col]!="id"){
|
2018-08-12 16:32:25 +02:00
|
|
|
if (isset($e[$arrColnames[$col]])){
|
|
|
|
$val = $e[$arrColnames[$col]];
|
|
|
|
} else {
|
|
|
|
$val = "";
|
|
|
|
}
|
2018-08-11 20:07:23 +02:00
|
|
|
if ($arrColnames[$col] == "bootoption"){
|
2018-08-12 15:09:47 +02:00
|
|
|
echo "<div class=row><div class=col-md-1 style=text-align:right>bootoption</div><div class=col-md-2><div class=dropdown><button class=\"btn btn-secondary dropdown-toggle\" type=button id=bootoptionbutton data-toggle=dropdown aria-haspopup=true aria-expanded=false>C</button>";
|
|
|
|
echo "<div class=dropdown-menu aria-labelledby=bootoptionbutton>";
|
2018-08-12 16:23:02 +02:00
|
|
|
echo "<a class=dropdown-item href=\"javascript:dropdown('bootoptionbutton','c')\">c</a>";
|
|
|
|
echo "<a class=dropdown-item href=\"javascript:dropdown('bootoptionbutton','d')\">d</a>";
|
2018-08-12 15:09:47 +02:00
|
|
|
echo "</div></div></div></div><input type=hidden id=bootoptionbuttonh name=bootoption value=c>";
|
|
|
|
// echo "$arrColnames[$col] <select name=bootoption><option value=c>C</option><option value=d>D</option></select></br>";
|
2018-08-11 20:07:23 +02:00
|
|
|
} elseif ($arrColnames[$col] == "arch"){
|
2018-08-12 16:23:02 +02:00
|
|
|
echo "<div class=row><div class=col-md-1 style=text-align:right>arch</div><div class=col-md-2><div class=dropdown><button class=\"btn btn-secondary dropdown-toggle\" type=button id=archbutton data-toggle=dropdown aria-haspopup=true aria-expanded=false>x86_64</button>";
|
2018-08-12 15:09:47 +02:00
|
|
|
echo "<div class=dropdown-menu aria-labelledby=archbutton>";
|
|
|
|
echo "<a class=dropdown-item href=\"javascript:dropdown('archbutton','x86_64')\">x86_64</a>";
|
|
|
|
echo "</div></div></div></div><input type=hidden id=archbuttonh name=arch value=x86_64>";
|
|
|
|
// echo "$arrColnames[$col] <select name=arch><option value=qemu-system-x86_64>x86_64</option></select></br>";
|
2018-08-12 15:51:15 +02:00
|
|
|
} 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 class=dropdown-menu aria-labelledby=cdrombutton>";
|
2018-08-12 17:22:18 +02:00
|
|
|
$isodirs = explode('|', $config['ISODIRECTORY']);
|
|
|
|
foreach($isodirs as $isodir){
|
|
|
|
$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>";
|
|
|
|
}
|
2018-08-12 15:51:15 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-12 16:32:25 +02:00
|
|
|
echo "</div></div></div></div><input type=hidden id=cdrombuttonh name=cdrom value=$val>";
|
2018-08-13 19:21:12 +02:00
|
|
|
} 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])) {
|
|
|
|
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></div>";
|
|
|
|
} elseif (preg_match('/macaddr/', $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>";
|
|
|
|
} 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></div></div>";
|
2018-08-12 17:22:18 +02:00
|
|
|
} elseif (preg_match('/drive/', $arrColnames[$col])){
|
|
|
|
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>";
|
|
|
|
$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>";
|
|
|
|
}
|
|
|
|
}
|
2018-08-13 19:21:12 +02:00
|
|
|
echo "</div></div></div><input type=hidden id=$arrColnames[$col]buttonh name=$arrColnames[$col] value=$val>";
|
2018-08-11 20:07:23 +02:00
|
|
|
} else {
|
2018-08-12 15:09:47 +02:00
|
|
|
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></div></div>";
|
2018-08-11 20:07:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-12 15:09:47 +02:00
|
|
|
echo "$editid<input type=hidden name=mode value=$formname><input class=\"btn btn-primary\" type=submit></form>";
|
2018-08-11 20:07:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-08-12 15:09:47 +02:00
|
|
|
|
|
|
|
|
2018-08-11 20:07:23 +02:00
|
|
|
|
|
|
|
//insert in db
|
|
|
|
if(isset($_REQUEST['mode'])){
|
2018-08-12 15:09:47 +02:00
|
|
|
if($_REQUEST['mode']=="newvm"){
|
|
|
|
$mode="newvm";
|
|
|
|
$sqls="INSERT INTO vms ";
|
|
|
|
$sqlc="(";
|
|
|
|
$sqlv=" VALUES (";
|
|
|
|
foreach(array_keys($_REQUEST) as $rkey){
|
|
|
|
if ($rkey!="mode"){
|
|
|
|
$rvalue = clean($_REQUEST[$rkey]);
|
2018-08-12 16:05:37 +02:00
|
|
|
if ($rvalue == "x86_64"){
|
|
|
|
$rvalue = "qemu-system-x86_64";
|
|
|
|
}
|
2018-08-12 15:09:47 +02:00
|
|
|
$sqlc .= "$rkey,";
|
|
|
|
$sqlv .= "'$rvalue',";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$sqlc = rtrim($sqlc, ',');
|
|
|
|
$sqlc.=")";
|
|
|
|
$sqlv = rtrim($sqlv, ',');
|
|
|
|
$sqlv.=")";
|
|
|
|
$sql = "$sqls$sqlc$sqlv";
|
|
|
|
echo $sql;
|
|
|
|
$result = $db_handle->exec($sql);
|
|
|
|
} elseif ($_REQUEST['mode']=="editvm"){
|
|
|
|
$mode="editvm";
|
|
|
|
$sql="UPDATE vms SET ";
|
|
|
|
foreach(array_keys($_REQUEST) as $rkey){
|
|
|
|
if (($rkey!="mode")&&($rkey!="editid")){
|
|
|
|
$rvalue = clean($_REQUEST[$rkey]);
|
2018-08-12 16:05:37 +02:00
|
|
|
if ($rvalue == "x86_64"){
|
|
|
|
$rvalue = "qemu-system-x86_64";
|
|
|
|
}
|
2018-08-12 15:09:47 +02:00
|
|
|
$sql .= " $rkey='$rvalue',";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$sql = rtrim($sql, ',');
|
|
|
|
$eid=clean($_REQUEST['editid']);
|
|
|
|
$sql .= " WHERE id='$eid'";
|
|
|
|
$result = $db_handle->exec($sql);
|
2018-08-11 20:07:23 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-12 15:09:47 +02:00
|
|
|
|
2018-08-13 19:21:12 +02:00
|
|
|
if(($mode!="newvm")&&($mode!="editvm")){
|
|
|
|
echo "</br><a class=\"btn btn-primary\" href=# onclick=\"post('?', {newvm: 'newvm'});\">Create a new VM</a></br></br>";
|
|
|
|
|
2018-08-12 15:09:47 +02:00
|
|
|
//getting vms from DB
|
|
|
|
$sql="SELECT * FROM vms";
|
|
|
|
$res = $db_handle->query($sql);
|
|
|
|
echo "<div class=row>";
|
|
|
|
while ($row = $res->fetchArray(SQLITE3_ASSOC)){
|
2018-08-12 20:21:33 +02:00
|
|
|
if (checkvm($row['vmname'])){
|
|
|
|
$button = "start";
|
2018-08-13 19:21:12 +02:00
|
|
|
$buttonc = "btn-success";
|
2018-08-12 20:21:33 +02:00
|
|
|
} else {
|
2018-08-13 19:21:12 +02:00
|
|
|
$button = "stop";
|
|
|
|
$buttonc = "btn-warning";
|
2018-08-12 20:21:33 +02:00
|
|
|
}
|
2018-08-13 19:21:12 +02:00
|
|
|
echo "<div class=col style=\"border:solid 1px;\">$row[vmname]</br><a class=\"btn btn-primary\" href=# onclick=\"post('?', {edit: '$row[id]'});\">edit</a> ";
|
|
|
|
echo "<a href=# onclick=\"post('?', {'$button': '$row[vmname]'});\"class=\"btn $buttonc\">$button</a> ";
|
|
|
|
echo "<button class=\"btn btn-danger\" data-delete-text=\"Delete VM $row[vmname]!\" data-delete-vm=\"$row[id]\" data-delete-name=\"$row[vmname]\" data-toggle=\"modal\" data-target=\"#confirm-delete\">Delete VM</button> ";
|
2018-08-13 18:25:05 +02:00
|
|
|
$server = gethostname();
|
2018-08-13 19:21:12 +02:00
|
|
|
if (preg_match('/stop/', $button)) {
|
|
|
|
echo "<a target=_blank href=novnc/vnc.html?path&host=$server class=\"btn btn-success\">VNC</a></div>";
|
2018-08-13 18:25:05 +02:00
|
|
|
}
|
2018-08-11 20:07:23 +02:00
|
|
|
}
|
2018-08-13 19:21:12 +02:00
|
|
|
}
|
2018-08-12 15:09:47 +02:00
|
|
|
echo "</div></div></body></html>";
|
2018-08-11 20:07:23 +02:00
|
|
|
?>
|