start stop in web

This commit is contained in:
mad 2018-08-12 20:21:33 +02:00
parent b6d400555e
commit e418137c2e
2 changed files with 39 additions and 2 deletions

3
nlvmi
View File

@ -176,6 +176,9 @@ if [ $# -gt 0 ]; then
vmstop $2 vmstop $2
done done
if [[ $VMFOUND == "no" ]]; then echo "no VM with that name found!"; exit 1; fi) if [[ $VMFOUND == "no" ]]; then echo "no VM with that name found!"; exit 1; fi)
elif [ $1 == "checkvm" ]; then
if [ -z $2 ]; then echo "checkvm needs a vmname!"; exit 1; fi
if [ -e $RUNDIRECTORY/$2.pid ]; then echo $2 running; else echo $2 stopped; fi
fi fi
else else
checkdb checkdb

View File

@ -6,6 +6,15 @@ function clean($str) {
$str = str_replace($search, $replace, $str); $str = str_replace($search, $replace, $str);
return $str; return $str;
} }
function checkvm($vmname) {
$shellout = shell_exec("/usr/bin/wrap-nlvmi checkvm $vmname bla");//){
if (preg_match_all('/running/', $shellout)){
return 0;
}
return 1;
}
//read config file //read config file
$configfile = new SplFileObject("/etc/nlvmi/nlvmi.conf"); $configfile = new SplFileObject("/etc/nlvmi/nlvmi.conf");
while (!$configfile->eof()) { while (!$configfile->eof()) {
@ -37,6 +46,26 @@ if ($config['DATABASETYPE']=="sqlite"){
} }
include('header.php'); include('header.php');
$mode=""; $mode="";
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;
}
}
}
//form for edit and new vms //form for edit and new vms
if (isset($_REQUEST['edit'])){ if (isset($_REQUEST['edit'])){
@ -173,7 +202,12 @@ $sql="SELECT * FROM vms";
$res = $db_handle->query($sql); $res = $db_handle->query($sql);
echo "<div class=row>"; echo "<div class=row>";
while ($row = $res->fetchArray(SQLITE3_ASSOC)){ while ($row = $res->fetchArray(SQLITE3_ASSOC)){
echo "<div class=col style=\"border:solid 1px;\">$row[vmname]</br><a href=?edit=$row[id]>edit</a></div>"; if (checkvm($row['vmname'])){
$button = "start";
} else {
$button = "stop";
}
echo "<div class=col style=\"border:solid 1px;\">$row[vmname]</br><a class=\"btn btn-primary\" href=?edit=$row[id]>edit</a> <a href=?$button=$row[vmname] class=\"btn btn-primary\">$button</a></div>";
} }
echo "</div></div></body></html>"; echo "</div></div></body></html>";
?> ?>