prio for startup and new overview to sort vms

This commit is contained in:
nativemad 2018-10-23 17:39:54 +02:00
parent ed5d37572e
commit f10d0e51bb
5 changed files with 88 additions and 20 deletions

20
nlvmi
View File

@ -81,7 +81,8 @@ function createdb {
brdev3 VARCHAR(50),
vncport INTEGER,
websocket INTEGER,
vncpassword VARCHAR(50)
vncpassword VARCHAR(50),
prio INTEGER
);"
sqlite3 $SQLITEFILE "CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
@ -147,8 +148,9 @@ function vmstart {
sqlite3 $SQLITEFILE "SELECT * FROM vms LEFT JOIN servers on vms.server = servers.hostname WHERE vms.id=$1" | while read line; do
IFS='|' read -r -a array <<< "$line"
#prepare tmpfile
TMPF=/home/nlvmi/start${array[1]}.tmp
rm $TMPF &>/dev/null
TMPF=/home/nlvmi/start${array[32]}-${array[1]}.tmp
rm /home/nlvmi/start*-${array[1]}.tmp &>/dev/null
touch $TMPF && chmod 660 $TMPF
#add user if necessary
echo "if ! \`id -u ${array[2]} &>/dev/null\`; then useradd ${array[2]} -d /run/nlvmi -g nlvmi -M -s /bin/false -G kvm; fi" >>$TMPF
#chown mon and pid files
@ -183,14 +185,18 @@ function vmstart {
echo "${COMMAND[@]}" >>$TMPF
#Set VNC Password
echo "echo \"change vnc password ${array[31]}\" | socat - unix-connect:$RUNDIRECTORY/${array[1]}.mon >/dev/null" >>$TMPF
if [ ! -z "${array[34]}" ]; then
if [ ! -z "${array[35]}" ]; then
echo "chmod g+w $RUNDIRECTORY/${array[1]}.mon" >>$TMPF
echo "chmod g+r $RUNDIRECTORY/${array[1]}.pid" >>$TMPF
openssl dgst -sha512 -sign "/etc/nlvmi/nlvmi_priv.key" -out /home/nlvmi/${array[1]}.tmp.sha512 $TMPF
su nlvmi -c "scp $TMPF* ${array[34]}:/home/nlvmi/"
su nlvmi -c "ssh ${array[34]} \"/usr/bin/nlvmi remote $TMPF run\""
TMPFR="/home/nlvmi/rstart{array[32]}-${array[1]}.tmp"
mv $TMPF $TMPFR
su nlvmi -c "ssh ${array[35]} \"rm /home/nlvmi/start*-${array[1]}.tmp\""
mv $TMPFR $TMPF
su nlvmi -c "scp $TMPF* ${array[35]}:/home/nlvmi/"
su nlvmi -c "ssh ${array[35]} \"/usr/bin/nlvmi remote $TMPF run\""
if [ ${array[12]} == "0" ]; then
su nlvmi -c "ssh ${array[34]} \"rm $TMPF $TMPF.sha512\""
su nlvmi -c "ssh ${array[35]} \"rm $TMPF $TMPF.sha512\""
fi
rm $TMPF $TMPF.sha512
else

BIN
web/arrowdown.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.0 KiB

BIN
web/arrowup.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.0 KiB

View File

@ -141,7 +141,7 @@ if (isset($_SESSION['username'])){
} else {
$vmname = clean($_POST['deletename']);
$vmid = clean($_POST['deleteid']);
$sql="SELECT id FROM vms WHERE vmname='$vmname' AND id='$vmid'";
$sql="SELECT id,prio 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'";
@ -150,7 +150,9 @@ if (isset($_SESSION['username'])){
}
}
}
} elseif ((isset($_POST['deleteid']))&&(isset($_POST['deletename']))&&($_POST['delwhat']=="user")){
} elseif
//delete user
((isset($_POST['deleteid']))&&(isset($_POST['deletename']))&&($_POST['delwhat']=="user")){
if($_SESSION['isadmin']!=1){
echo "you are not allowed to delete users!";
} else {
@ -165,7 +167,9 @@ if (isset($_SESSION['username'])){
}
}
}
} elseif ((isset($_POST['deleteid']))&&(isset($_POST['deletename']))&&($_POST['delwhat']=="server")){
} elseif
//delete server
((isset($_POST['deleteid']))&&(isset($_POST['deletename']))&&($_POST['delwhat']=="server")){
if($_SESSION['isadmin']!=1){
echo "you are not allowed to delete vms!";
} else {
@ -274,6 +278,11 @@ if (isset($_SESSION['username'])){
//insert or update vm in db
if(isset($_REQUEST['mode'])){
if($_REQUEST['mode']=="newvm"){
$psql = "SELECT MAX(prio) AS prio FROM vms";
$pres = $db_handle->query($psql);
$prioa = $pres->fetchArray(SQLITE3_ASSOC);
$prio = $prioa['prio'] + 1;
$mode="newvm";
$sqls="INSERT INTO vms ";
$sqlc="(";
@ -304,10 +313,10 @@ if (isset($_SESSION['username'])){
$sqlv .= "'$rvalue',";
}
}
$sqlc = rtrim($sqlc, ',');
$sqlc.=")";
$sqlv = rtrim($sqlv, ',');
$sqlv.=")";
// $sqlc = rtrim($sqlc, ',');
$sqlc.=" 'prio')";
// $sqlv = rtrim($sqlv, ',');
$sqlv.=" '$prio')";
$sql = "$sqls$sqlc$sqlv";
//echo $sql;
$res = $db_handle->exec($sql);
@ -354,6 +363,37 @@ if (isset($_SESSION['username'])){
exit;
}
}
if (isset($_REQUEST['neworder'])){
if (isset($_REQUEST['vid'])){
$vid = clean($_REQUEST['vid']);
$sql="SELECT prio FROM vms WHERE id='$vid'";
$oldprio = $db_handle->query($sql)->fetchArray(SQLITE3_ASSOC)['prio'];
//print_r($res);
//echo "buu";
if ($_REQUEST['neworder']=="up"){
$newprio = $oldprio - 1;
$sqlu="SELECT id FROM vms WHERE prio='$newprio'";
$oldid = $db_handle->query($sqlu)->fetchArray(SQLITE3_ASSOC)['id'];
$sqlm="UPDATE vms SET prio='$newprio' WHERE id='$vid'";
$sqln="UPDATE vms SET prio='$oldprio' WHERE id='$oldid'";
if(($db_handle->exec($sqlm))&&($db_handle->exec($sqln))){
echo "success";
exit;
}
}elseif ($_REQUEST['neworder']=="down"){
$newprio = $oldprio + 1;
$sqlu="SELECT id FROM vms WHERE prio='$newprio'";
$oldid = $db_handle->query($sqlu)->fetchArray(SQLITE3_ASSOC)['id'];
$sqlm="UPDATE vms SET prio='$newprio' WHERE id='$vid'";
$sqln="UPDATE vms SET prio='$oldprio' WHERE id='$oldid'";
if(($db_handle->exec($sqlm))&&($db_handle->exec($sqln))){
echo "success";
exit;
}
}
}
}
if (isset($_POST['serverbuttoni'])){
serverdepropdown($_POST['serverbuttoni'], "no drive1", 'drive1');
formatbuttons('format1', '');
@ -598,10 +638,11 @@ if (isset($_SESSION['username'])){
}
//getting vms from DB
$sql="SELECT * FROM vms $sqllimit";
$sql="SELECT * FROM vms $sqllimit ORDER BY prio";
$res = $db_handle->query($sql);
echo "<div class=row>";
while ($row = $res->fetchArray(SQLITE3_ASSOC)){
echo "<div class=row>";
if (checkvm($row['vmname'])){
$button = "start";
$buttonc = "btn-success";
@ -610,8 +651,12 @@ if (isset($_SESSION['username'])){
$buttonc = "btn-warning";
}
$vncport = sprintf("%02d", $row['vncport']);
echo "<div class=col style=\"margin:5px;padding:5px;border:solid 1px;max-width:295px;\"><h3>$row[vmname]</h3>$row[server]</br>$row[cpus] CPUs, $row[memory] MB RAM</br>$row[drive1]</br>";
echo "VNC port: 59$vncport</br><a class=\"btn btn-primary\" href=# onclick=\"post('?', {edit: '$row[id]'});\">edit</a> ";
echo "<div >";
//style=\"margin:5px;padding:5px;border:solid 1px;max-width:295px;\">
echo "<button type=button onclick=\"arrow('up', '$row[id]');\" class=\"btn btn-default\" aria-label=\"Left Align\"><span><img src=arrowup.png width=6px ></span></button><br/>";
echo "<button type=button onclick=\"arrow('down', '$row[id]');\" class=\"btn btn-default\" aria-label=\"Left Align\"><span><img src=arrowdown.png width=6px></span></button></div>";
echo "<div class=col-sm><b>$row[vmname]</b></div><div class=col-sm>$row[server]</div><div class=col-sm>$row[cpus] CPUs, $row[memory] MB RAM</div><div class=col-sm>$row[drive1]</div>";
echo "<div class=col-sm>VNC port: 59$vncport</div><div class-col-sm><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-id=\"$row[id]\" data-delete-name=\"$row[vmname]\" data-toggle=\"modal\" data-target=\"#confirm-delete\" data-delete-what=vm>Delete VM</button> ";
if ($row['websocket']!=""){
@ -620,13 +665,14 @@ if (isset($_SESSION['username'])){
echo "<a target=_blank href=novnc/vnc.html?path=pussyvm&host=$server class=\"btn btn-success\">VNC</a>";
}
}
echo "</div>";
echo "</div></div><hr>";
}
}
echo "</div></div></body></html>";
echo "</div></body></html>";
} elseif (!isset($_SESSION['username'])){
echo "<h1>Welcome to nlvmi!</h1><h2>You need to log in</h2></br>";
echo "<form action=? method=post>Username: <input type=text name=username> Password: <input type=password name=password> <input name=login type=submit></form>";
exit;
}
?>

View File

@ -73,3 +73,19 @@ function submitbutton(formid){
},
});
}
function arrow(dir, id){
data = "&neworder="+ dir +"&vid="+ id;
$.ajax({
type: 'POST',
url: "?rand="+ makeid(),
data: data,
success: function(response) {
if (response != "success"){
window.alert('Failure!\n' + response);
} else {
window.location.href = "";
}
},
});
}