From edc5dddb8c4c8b99bb57c366ee4d03af4311046f Mon Sep 17 00:00:00 2001 From: mad Date: Tue, 21 Aug 2018 16:34:49 +0200 Subject: [PATCH] remote ssh checkvm, stopvm --- nlvmi | 49 +++++++++++++++++++++++++++++++++++-------------- nlvmi.conf | 3 +++ 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/nlvmi b/nlvmi index 20a4f4f..c0a0887 100755 --- a/nlvmi +++ b/nlvmi @@ -156,7 +156,11 @@ function vmstart { #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 + echo "chmod g+w $RUNDIRECTORY/${array[1]}.mon" >>$TMPF su nlvmi -c "scp $TMPF ${array[34]}:/home/nlvmi/" + su nlvmi -c "ssh ${array[34]} \"/usr/bin/nlvmi remote $TMPF run\"" + su nlvmi -c "ssh ${array[34]} \"rm $TMPF\"" + rm $TMPF else /bin/bash $TMPF rm $TMPF @@ -167,22 +171,34 @@ function vmstart { #stop VM function vmstop { - if [ ! -e $RUNDIRECTORY/$1.pid ]; then echo "pidfile $RUNDIRECTORY/$1.pid does not exist"; exit 1; fi if [ $LOGLEVEL -gt "1" ]; then echo "stopvm function entered for VM $1"; fi - sqlite3 $SQLITEFILE "SELECT id,vmname FROM vms WHERE vmname='$1'" | while read line; do + sqlite3 $SQLITEFILE "SELECT vms.id AS vid,vmname,connectstring FROM vms LEFT JOIN servers ON vms.server = servers.hostname WHERE vmname='$1'" | while read line; do IFS='|' read -r -a array <<< "$line" - echo "system_powerdown" | socat - unix-connect:$RUNDIRECTORY/${array[1]}.mon >/dev/null + if [ -z ${array[2]} ]; then + if [ ! -e $RUNDIRECTORY/$1.pid ]; then echo "pidfile $RUNDIRECTORY/$1.pid does not exist"; exit 1; fi + echo "system_powerdown" | socat - unix-connect:$RUNDIRECTORY/${array[1]}.mon >/dev/null + else + su nlvmi -c "ssh ${array[2]} \"echo \\\"system_powerdown\\\" | socat - unix-connect:$RUNDIRECTORY/${array[1]}.mon >/dev/null\"" + fi sleep 5s STOPPED="no" COUNTER=0 while [ $STOPPED = "no" ]; do if [ $COUNTER == 200 ]; then - kill `cat $RUNDIRECTORY/${array[1]}.pid` - echo "${array[1]} forcefully killed!"; - STOPPED="yes" + if [ -z ${array[2]} ]; then + kill `cat $RUNDIRECTORY/${array[1]}.pid` + echo "${array[1]} forcefully killed!"; + STOPPED="yes" + else + su nlvmi -c "ssh ${array[2]} \"echo \\\"kill \\\$(<\\\"$RUNDIRECTORY/${array[1]}.pid\\\")\\\" >/home/nlvmi/${array[1]}.tmp\"" + su nlvmi -c "ssh ${array[2]} \"/usr/bin/nlvmi remote /home/nlvmi/${array[1]}.tmp stop\"" + su nlvmi -c "ssh ${array[2]} \"rm /home/nlvmi/${array[1]}.tmp\"" + echo "remotekill" + STOPPED="yes" + fi fi ((COUNTER++)) - ps -ef | grep `cat $RUNDIRECTORY/${array[1]}.pid` | grep -v grep >/dev/null || STOPPED="yes" + checkvm ${array[1]} | grep "is not running" >/dev/null && STOPPED="yes" sleep 1s done done @@ -202,7 +218,7 @@ function checkvm { sqlite3 $SQLITEFILE "SELECT connectstring from vms LEFT JOIN servers ON vms.server = servers.hostname WHERE vmname='$1'" | while read line; do CONN=$line if [ ! -z $CONN ]; then - su nlvmi -c "ssh 192.168.0.199 \"RPID=\\\$(<\\\"$RUNDIRECTORY/$1.pid\\\"); ps -ef | grep -v grep | grep \\\$RPID\"" &>/dev/null && echo "VM is running" || echo "VM is not running" + su nlvmi -c "ssh $CONN \"RPID=\\\$(<\\\"$RUNDIRECTORY/$1.pid\\\"); ps -ef | grep -v grep | grep \\\$RPID | grep qemu\"" &>/dev/null && echo "VM is running" || echo "VM is not running" else ps -ef | grep `cat $RUNDIRECTORY/$1.pid` | grep -v grep >/dev/null && echo "VM is running" || echo "VM is not running" @@ -215,11 +231,7 @@ function listdir { sqlite3 $SQLITEFILE "SELECT $2,connectstring FROM servers WHERE hostname='$1'" | while read line; do IFS='|' read -r -a array <<< "$line" IFS=';' read -r -a dirs <<< "${array[0]}" -# echo ${dirs[1]} -# echo ${array[0]} -#echo $line for di in ${dirs[@]}; do -# echo $di if [ -z ${array[1]} ]; then ls -lrt -d -1 $di/{*,.*} else @@ -231,7 +243,7 @@ function listdir { #mainloop if [ $# -gt 0 ]; then -echo $1 $2 $3 +#echo $1 $2 $3 if [ $1 == "createdb" ]; then createdb elif [ $1 == "autostart" ]; then @@ -269,7 +281,16 @@ echo $1 $2 $3 elif [ $1 == "listdir" ]; then if [ -z $2 ] || [ -z $3 ]; then echo listdir needs two more arguments; exit 1; fi listdir $2 $3 - + elif [ $1 == "remote" ]; then + if [ ! -z $MASTERSERVER ]; then + if `echo $SSH_CLIENT | grep "$MASTERSERVER " &>/dev/null`; then + if [ -e $2 ]; then + /bin/bash $2; + fi + else + echo "not allowed"; + fi + fi fi else checkdb diff --git a/nlvmi.conf b/nlvmi.conf index ea70f21..f72b755 100644 --- a/nlvmi.conf +++ b/nlvmi.conf @@ -21,9 +21,12 @@ DATABASETYPE="sqlite" #only sqlite settings SQLITEFILE="/etc/nlvmi/nlvmi.sqlite" + #The unix user that runs php, to grand permissions on the sqlite file PHPUSER="nobody" +#Masterserver only used for remote connections +MASTERSERVER="192.168.0.3" #only mariadb settings