Compare commits

..

7 Commits

Author SHA1 Message Date
nativemad
e4c3b52cd5 introduce mac generator 2018-12-01 18:51:36 +01:00
nativemad
d57893514d introduce image management 2018-12-01 17:52:19 +01:00
nativemad
a8353787ed some fixed paths for centos 2018-11-13 18:23:44 +01:00
nativemad
7d43ab1428 centos has no tunctl, using ip instead to create tap devices 2018-11-13 12:53:59 +01:00
nativemad
9e31a8d419 loglevel errors should get shown always, startupcommand is shown with debug level >=2 2018-11-12 17:21:05 +01:00
nativemad
8498ada9bf Merge branch 'master' of https://git.nativenet.ch/nativenet.ch/nlvmi 2018-11-12 16:19:35 +01:00
nativemad
31f872e6f9 tabs 2018-11-12 16:19:19 +01:00
5 changed files with 580 additions and 122 deletions

301
nlvmi
View File

@@ -2,17 +2,16 @@
#getting the config #getting the config
source /etc/nlvmi/nlvmi.conf source /etc/nlvmi/nlvmi.conf
if [ $LOGLEVEL -gt "1" ]; then echo "configuration loaded"; fi if [ $LOGLEVEL -gt "2" ]; then echo "configuration loaded"; fi
#check if we are root and wrap if we are not #check if we are root and wrap if we are not
U=`/usr/bin/whoami` U=`/usr/bin/whoami`
if [ $U != "root" ]; then if [ $U != "root" ]; then
if [ ! -z $1 ]; then A=$1; else A="bla"; fi if [ ! -z $1 ]; then A="$1"; else A="bla"; fi
if [ ! -z $2 ]; then B=$2; else B="bla"; fi if [ ! -z $2 ]; then B="$2"; else B="bla"; fi
if [ ! -z $3 ]; then C=$3; else C="bla"; fi if [ ! -z $3 ]; then C="$3"; else C="bla"; fi
/usr/bin/wrap-nlvmi $A $B $C && exit /usr/bin/wrap-nlvmi \"$A\" \"$B\" \"$C\" && exit
fi fi
if [ ! -d $RUNDIRECTORY ]; then if [ $LOGLEVEL -gt "1" ]; then echo "creating $RUNDIRECTORY"; fi; mkdir -p $RUNDIRECTORY; chown root:nlvmi -R $RUNDIRECTORY; chmod g+w $RUNDIRECTORY; fi if [ ! -d $RUNDIRECTORY ]; then if [ $LOGLEVEL -gt "1" ]; then echo "creating $RUNDIRECTORY"; fi; mkdir -p $RUNDIRECTORY; chown root:nlvmi -R $RUNDIRECTORY; chmod g+w $RUNDIRECTORY; fi
#checking database #checking database
@@ -44,43 +43,43 @@ function createdb {
if [ $DATABASETYPE == "sqlite" ]; then if [ $DATABASETYPE == "sqlite" ]; then
sqlite3 $SQLITEFILE "CREATE TABLE vms ( sqlite3 $SQLITEFILE "CREATE TABLE vms (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
vmname VARCHAR(50) NOT NULL, vmname VARCHAR(50) NOT NULL,
user VARCHAR(50) NOT NULL, user VARCHAR(50) NOT NULL,
server VARCHAR(50), server VARCHAR(50),
arch VARCHAR(50) NOT NULL, arch VARCHAR(50) NOT NULL,
cputype VARCHAR(50), cputype VARCHAR(50),
cpus INTEGER, cpus INTEGER,
memory INTEGER, memory INTEGER,
usbdev VARCHAR(50), usbdev VARCHAR(50),
kblang VARCHAR(50), kblang VARCHAR(50),
custom VARCHAR(50), custom VARCHAR(50),
bootoption VARCHAR(2) NOT NULL, bootoption VARCHAR(2) NOT NULL,
autostart INTEGER, autostart INTEGER,
drive1 VARCHAR(50), drive1 VARCHAR(50),
format1 VARCHAR(50), format1 VARCHAR(50),
drive2 VARCHAR(50), drive2 VARCHAR(50),
format2 VARCHAR(50), format2 VARCHAR(50),
drive3 VARCHAR(50), drive3 VARCHAR(50),
format3 VARCHAR(50), format3 VARCHAR(50),
cdrom VARCHAR(50), cdrom VARCHAR(50),
tapdev1 VARCHAR(50), tapdev1 VARCHAR(50),
macaddr1 VARCHAR(50), macaddr1 VARCHAR(50),
brdev1 VARCHAR(50), brdev1 VARCHAR(50),
tapdev2 VARCHAR(50), tapdev2 VARCHAR(50),
macaddr2 VARCHAR(50), macaddr2 VARCHAR(50),
brdev2 VARCHAR(50), brdev2 VARCHAR(50),
tapdev3 VARCHAR(50), tapdev3 VARCHAR(50),
macaddr3 VARCHAR(50), macaddr3 VARCHAR(50),
brdev3 VARCHAR(50), brdev3 VARCHAR(50),
vncport INTEGER, vncport INTEGER,
websocket INTEGER, websocket INTEGER,
vncpassword VARCHAR(50), vncpassword VARCHAR(50),
prio INTEGER prio INTEGER
);" );"
sqlite3 $SQLITEFILE "CREATE TABLE users ( sqlite3 $SQLITEFILE "CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
username VARCHAR(50) NOT NULL, username VARCHAR(50) NOT NULL,
password VARCHAR(255), password VARCHAR(255),
admin INTEGER admin INTEGER
);" );"
sqlite3 $SQLITEFILE "CREATE TABLE servers ( sqlite3 $SQLITEFILE "CREATE TABLE servers (
@@ -137,7 +136,7 @@ function vmautostart {
#start single VM #start single VM
function vmstart { function vmstart {
if [ -z $1 ]; then echo "function start needs an id!"; exit 1; fi if [ -z $1 ]; then echo "function start needs an id!"; exit 1; fi
if [ $LOGLEVEL -gt "1" ]; then echo "startvm function entered for vmid $1"; fi if [ $LOGLEVEL -gt "2" ]; then echo "startvm function entered for vmid $1"; fi
sqlite3 $SQLITEFILE "SELECT * FROM vms LEFT JOIN servers on vms.server = servers.hostname WHERE vms.id=$1" | while read line; do 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" IFS='|' read -r -a array <<< "$line"
#prepare tmpfile #prepare tmpfile
@@ -148,32 +147,33 @@ function vmstart {
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 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 #chown mon and pid files
echo "if [ -e $RUNDIRECTORY/${array[1]}.mon ] || [ -e $RUNDIRECTORY/${array[1]}.pid ]; then chown ${array[2]}:nlvmi $RUNDIRECTORY/${array[1]}.*; fi" >>$TMPF echo "if [ -e $RUNDIRECTORY/${array[1]}.mon ] || [ -e $RUNDIRECTORY/${array[1]}.pid ]; then chown ${array[2]}:nlvmi $RUNDIRECTORY/${array[1]}.*; fi" >>$TMPF
if [ ! -z ${array[5]} ]; then CPUTYPE="-cpu ${array[5]}"; fi if [ ! -z ${array[5]} ]; then CPUTYPE="-cpu ${array[5]}"; fi
if [ ! -z ${array[6]} ]; then SMP="-smp ${array[6]}"; fi if [ ! -z ${array[6]} ]; then SMP="-smp ${array[6]}"; fi
if [ ! -z ${array[7]} ]; then MEMORY="-m ${array[7]}"; fi if [ ! -z ${array[7]} ]; then MEMORY="-m ${array[7]}"; fi
if [ ! -z ${array[8]} ]; then USBDEV="-usb -device usb-ehci,id=ehci -device usb-${array[8]},bus=usb-bus.0"; fi if [ ! -z ${array[8]} ]; then USBDEV="-usb -device usb-ehci,id=ehci -device usb-${array[8]},bus=usb-bus.0"; fi
if [ ! -z ${array[9]} ]; then KB="-k ${array[9]}"; fi if [ ! -z ${array[9]} ]; then KB="-k ${array[9]}"; fi
if [ ! -z ${array[10]} ]; then CUSTOM="${array[10]}"; fi if [ ! -z ${array[10]} ]; then CUSTOM="${array[10]}"; fi
if [ ! -z ${array[13]} ]; then DRIVE1="-drive file=${array[13]}"; echo "chown ${array[2]}:nlvmi ${array[13]}" >>$TMPF; fi if [ ! -z ${array[13]} ]; then DRIVE1="-drive file=${array[13]}"; echo "chown ${array[2]}:nlvmi ${array[13]}" >>$TMPF; fi
if [ ! -z ${array[14]} ]; then FORMAT1=",format=${array[14]},if=virtio"; fi if [ ! -z ${array[14]} ]; then FORMAT1=",format=${array[14]},if=virtio"; fi
if [ ! -z ${array[15]} ]; then DRIVE2="-drive file=${array[15]}"; echo "chown ${array[2]}:nlvmi ${array[15]}" >>$TMPF; fi if [ ! -z ${array[15]} ]; then DRIVE2="-drive file=${array[15]}"; echo "chown ${array[2]}:nlvmi ${array[15]}" >>$TMPF; fi
if [ ! -z ${array[16]} ]; then FORMAT2=",format=${array[16]},if=virtio"; fi if [ ! -z ${array[16]} ]; then FORMAT2=",format=${array[16]},if=virtio"; fi
if [ ! -z ${array[17]} ]; then DRIVE3="-drive file=${array[17]}"; echo "chown ${array[2]}:nlvmi ${array[17]}" >>$TMPF; fi if [ ! -z ${array[17]} ]; then DRIVE3="-drive file=${array[17]}"; echo "chown ${array[2]}:nlvmi ${array[17]}" >>$TMPF; fi
if [ ! -z ${array[18]} ]; then FORMAT3=",format=${array[18]},if=virtio"; fi if [ ! -z ${array[18]} ]; then FORMAT3=",format=${array[18]},if=virtio"; fi
if [ ! -z ${array[19]} ]; then CDROM="-cdrom ${array[19]}"; fi if [ ! -z ${array[19]} ]; then CDROM="-cdrom ${array[19]}"; fi
if [ ! -z ${array[20]} ]; then TAPDEV1="-netdev tap,ifname=${array[20]},script=no,id=net0"; echo "tunctl -t ${array[20]} -u ${array[2]} && ifconfig ${array[20]} up" >>$TMPF; fi if [ ! -z ${array[20]} ]; then TAPDEV1="-netdev tap,ifname=${array[20]},script=no,id=net0"; echo "which tunctl && tunctl -t ${array[20]} -u ${array[2]} >/dev/null || ip tuntap add ${array[20]} mode tap user ${array[2]}; ifconfig ${array[20]} up" >>$TMPF; fi
if [ ! -z ${array[21]} ]; then MACADDR1="-net nic,macaddr=${array[21]},model=virtio,netdev=net0"; fi if [ ! -z ${array[21]} ]; then MACADDR1="-net nic,macaddr=${array[21]},model=virtio,netdev=net0"; fi
if [ ! -z ${array[22]} ]; then BRDEV1="${array[22]}"; echo "/sbin/brctl addif ${array[22]} ${array[20]}" >>$TMPF; fi if [ ! -z ${array[22]} ]; then BRDEV1="${array[22]}"; TAPDS1=`echo ${array[20]} | cut -c 1-15`; echo "if [ -L /sys/devices/virtual/net/${TAPDS1}/master ]; then OLDBR=\`readlink -f /sys/devices/virtual/net/${TAPDS1}/master | rev | cut -d "/" -f1 | rev\`; brctl delif \$OLDBR ${array[20]}; fi; /sbin/brctl addif ${array[22]} ${array[20]} >/dev/null" >>$TMPF; fi
if [ ! -z ${array[23]} ]; then TAPDEV2="-netdev tap,ifname=${array[23]},script=no,id=net1"; echo "tunctl -t ${array[23]} -u ${array[2]} && ifconfig ${array[23]} up" >>$TMPF; fi if [ ! -z ${array[23]} ]; then TAPDEV2="-netdev tap,ifname=${array[23]},script=no,id=net1"; echo "which tunctl && tunctl -t ${array[23]} -u ${array[2]} >/dev/null || ip tuntap add ${array[23]} mode tap user ${array[2]}; ifconfig ${array[23]} up" >>$TMPF; fi
if [ ! -z ${array[24]} ]; then MACADDR2="-net nic,macaddr=${array[24]},model=virtio,netdev=net1"; fi if [ ! -z ${array[24]} ]; then MACADDR2="-net nic,macaddr=${array[24]},model=virtio,netdev=net1"; fi
if [ ! -z ${array[25]} ]; then BRDEV2="${array[25]}"; echo "/sbin/brctl addif ${array[25]} ${array[23]}" >>$TMPF; fi if [ ! -z ${array[25]} ]; then BRDEV2="${array[25]}"; echo "if [ -L /sys/devices/virtual/net/${array[23]}/master ]; then OLDBR=\`readlink -f /sys/devices/virtual/net/${array[23]}/master | rev | cut -d "/" -f1 | rev\`; brctl delif \$OLDBR ${array[23]}; fi; /sbin/brctl addif ${array[25]} ${array[23]} >/dev/null" >>$TMPF; fi
if [ ! -z ${array[26]} ]; then TAPDEV3="-netdev tap,ifname=${array[26]},script=no,id=net2"; echo "tunctl -t ${array[26]} -u ${array[2]} && ifconfig ${array[26]} up" >>$TMPF; fi if [ ! -z ${array[26]} ]; then TAPDEV3="-netdev tap,ifname=${array[26]},script=no,id=net2"; echo "which tunctl && tunctl -t ${array[26]} -u ${array[2]} >/dev/null || ip tuntap add ${array[26]} mode tap user ${array[2]}; ifconfig ${array[26]} up" >>$TMPF; fi
if [ ! -z ${array[27]} ]; then MACADDR3="-net nic,macaddr=${array[27]},model=virtio,netdev=net2"; fi if [ ! -z ${array[27]} ]; then MACADDR3="-net nic,macaddr=${array[27]},model=virtio,netdev=net2"; fi
if [ ! -z ${array[28]} ]; then BRDEV3="${array[28]}"; echo "/sbin/brctl addif ${array[28]} ${array[26]}" >>$TMPF; fi if [ ! -z ${array[28]} ]; then BRDEV3="${array[28]}"; echo "if [ -L /sys/devices/virtual/net/${array[26]}/master ]; then OLDBR=\`readlink -f /sys/devices/virtual/net/${array[26]}/master | rev | cut -d "/" -f1 | rev\`; brctl delif \$OLDBR ${array[26]}; fi; /sbin/brctl addif ${array[28]} ${array[26]} >/dev/null" >>$TMPF; fi
if [ ! -z ${array[30]} ]; then WPORT=$((${array[30]} + 5700)); WEBSOCK=",websocket=$WPORT"; else WEBSOCK=""; fi if [ ! -z ${array[30]} ]; then WPORT=$((${array[30]} + 5700)); WEBSOCK=",websocket=$WPORT"; else WEBSOCK=""; fi
if [ ! -z ${array[31]} ]; then VNCPASS=",password"; fi if [ ! -z ${array[31]} ]; then VNCPASS=",password"; fi
if [ ! -z ${array[29]} ]; then VNCPORT="-vnc :${array[29]}$WEBSOCK$VNCPASS"; fi if [ ! -z ${array[29]} ]; then VNCPORT="-vnc :${array[29]}$WEBSOCK$VNCPASS"; fi
COMMAND=(su - ${array[2]} -s /bin/bash -c \"${array[4]} -name ${array[1]} -enable-kvm ${array[10]} $CPUTYPE $MEMORY $SMP -device virtio-rng-pci -boot ${array[11]} $USBDEV $KB -daemonize $DRIVE1$FORMAT1 $DRIVE2$FORMAT2 $DRIVE3$FORMAT3 $CDROM $MACADDR1 $TAPDEV1 $MACADDR2 $TAPDEV2 $MACADDR3 $TAPDEV3 $VNCPORT $CUSTOM -pidfile $RUNDIRECTORY/${array[1]}.pid -monitor unix:$RUNDIRECTORY/${array[1]}.mon,server,nowait\") # echo "which ${array[4]} && QEMUCOMMAND=/usr/bin/${array[4]} || QEMUCOMMAND=/usr/libexec/qemu-kvm" >>$TMPF
COMMAND=(su - ${array[2]} -s /bin/bash -c \"which ${array[4]} \&\& QEMUCOMMAND=/usr/bin/${array[4]} \|\| QEMUCOMMAND=/usr/libexec/qemu-kvm\; \\\$QEMUCOMMAND -name ${array[1]} -enable-kvm ${array[10]} $CPUTYPE $MEMORY $SMP -device virtio-rng-pci -boot ${array[11]} $USBDEV $KB -daemonize $DRIVE1$FORMAT1 $DRIVE2$FORMAT2 $DRIVE3$FORMAT3 $CDROM $MACADDR1 $TAPDEV1 $MACADDR2 $TAPDEV2 $MACADDR3 $TAPDEV3 $VNCPORT $CUSTOM -pidfile $RUNDIRECTORY/${array[1]}.pid -monitor unix:$RUNDIRECTORY/${array[1]}.mon,server,nowait\")
if [ $LOGLEVEL -gt "1" ]; then echo ${COMMAND[@]}; fi if [ $LOGLEVEL -gt "1" ]; then echo ${COMMAND[@]}; fi
echo "${COMMAND[@]}" >>$TMPF echo "${COMMAND[@]}" >>$TMPF
#Set VNC Password #Set VNC Password
@@ -187,13 +187,16 @@ function vmstart {
su nlvmi -c "ssh ${array[35]} \"rm /home/nlvmi/start*-${array[1]}.tmp*\"" su nlvmi -c "ssh ${array[35]} \"rm /home/nlvmi/start*-${array[1]}.tmp*\""
mv $TMPFR $TMPF mv $TMPFR $TMPF
su nlvmi -c "scp $TMPF* ${array[35]}:/home/nlvmi/" su nlvmi -c "scp $TMPF* ${array[35]}:/home/nlvmi/"
su nlvmi -c "ssh ${array[35]} \"/usr/bin/nlvmi remote $TMPF run\"" OUT=`su nlvmi -c "ssh ${array[35]} \"/usr/bin/nlvmi remote $TMPF run\"" 2>&1`
echo $OUT
if [ ${array[12]} == "0" ]; then if [ ${array[12]} == "0" ]; then
su nlvmi -c "ssh ${array[35]} \"rm $TMPF $TMPF.sha512\"" su nlvmi -c "ssh ${array[35]} \"rm $TMPF $TMPF.sha512\""
echo $OUT
fi fi
rm $TMPF $TMPF.sha512 rm $TMPF $TMPF.sha512
else else
/bin/bash $TMPF OUT=`/bin/bash $TMPF 2>&1`
echo $OUT
if [ ${array[12]} == "0" ]; then if [ ${array[12]} == "0" ]; then
rm $TMPF rm $TMPF
fi fi
@@ -215,19 +218,19 @@ function vmstart {
#stop VM #stop VM
function vmstop { function vmstop {
if [ $LOGLEVEL -gt "1" ]; then echo "stopvm function entered for VM $1"; fi if [ $LOGLEVEL -gt "2" ]; then echo "stopvm function entered for VM $1"; fi
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 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" IFS='|' read -r -a array <<< "$line"
if [ -z ${array[2]} ]; then if [ -z ${array[2]} ]; then
if [ ! -e $RUNDIRECTORY/$1.pid ]; then echo "pidfile $RUNDIRECTORY/$1.pid does not exist"; exit 1; fi 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 echo "system_powerdown" | socat - unix-connect:$RUNDIRECTORY/${array[1]}.mon >/dev/null
else else
su nlvmi -c "ssh ${array[2]} \"echo \\\"system_powerdown\\\" | socat - unix-connect:$RUNDIRECTORY/${array[1]}.mon >/dev/null\"" su nlvmi -c "ssh ${array[2]} \"echo \\\"system_powerdown\\\" | socat - unix-connect:$RUNDIRECTORY/${array[1]}.mon >/dev/null\""
fi fi
sleep 5s sleep 5s
STOPPED="no" STOPPED="no"
COUNTER=0 COUNTER=0
while [ $STOPPED = "no" ]; do while [ $STOPPED = "no" ]; do
if [ $COUNTER == 10 ]; then if [ $COUNTER == 10 ]; then
if [ -z ${array[2]} ]; then if [ -z ${array[2]} ]; then
kill `cat $RUNDIRECTORY/${array[1]}.pid` kill `cat $RUNDIRECTORY/${array[1]}.pid`
@@ -248,19 +251,19 @@ function vmstop {
((COUNTER++)) ((COUNTER++))
checkvm ${array[1]} | grep "is not running" >/dev/null && STOPPED="yes" checkvm ${array[1]} | grep "is not running" >/dev/null && STOPPED="yes"
sleep 1s sleep 1s
done done
done done
} }
#stop local vm #stop local vm
function vmlocalstop { function vmlocalstop {
if [ ! -e $RUNDIRECTORY/$1.pid ]; then echo "pidfile $RUNDIRECTORY/$1.pid does not exist"; exit 1; fi if [ ! -e $RUNDIRECTORY/$1.pid ]; then echo "pidfile $RUNDIRECTORY/$1.pid does not exist"; exit 1; fi
echo "system_powerdown" | socat - unix-connect:$RUNDIRECTORY/$1.mon >/dev/null echo "system_powerdown" | socat - unix-connect:$RUNDIRECTORY/$1.mon >/dev/null
sleep 5s sleep 5s
STOPPED="no" STOPPED="no"
COUNTER=0 COUNTER=0
while [ $STOPPED = "no" ]; do while [ $STOPPED = "no" ]; do
if [ $COUNTER == 10 ]; then if [ $COUNTER == 10 ]; then
kill `cat $RUNDIRECTORY/$1.pid` kill `cat $RUNDIRECTORY/$1.pid`
echo "$1 forcefully killed!"; echo "$1 forcefully killed!";
@@ -280,26 +283,136 @@ function stopall {
done done
} }
function listdir { function listdirs {
#echo $1 $2 if [ $2 == "isodirectory" ] || [ $2 == "vmdirectory" ]; then
sqlite3 $SQLITEFILE "SELECT $2,connectstring FROM servers WHERE hostname='$1'" | while read line; do COLNAMES=$2
IFS='|' read -r -a array <<< "$line" MODE="vm"
IFS=';' read -r -a dirs <<< "${array[0]}" else
for di in ${dirs[@]}; do COLNAMES="isodirectory,vmdirectory"
if [ -z ${array[1]} ]; then MODE="img"
ls -lrt -d -1 $di/{*,.*} fi
else sqlite3 $SQLITEFILE "SELECT $COLNAMES,connectstring FROM servers WHERE hostname='$1'" | while read line; do
su nlvmi -c "ssh ${array[1]} \"ls -lrt -d -1 $di/{*,.*}\"" IFS='|' read -r -a array <<< "$line"
fi if [ $MODE == "vm" ]; then
done IFS=';' read -r -a dirs <<< "${array[0]}"
for di in ${dirs[@]}; do
if [ -z ${array[1]} ]; then
ls -lrt -d -1 $di/{*,.*}
else
su nlvmi -c "ssh ${array[1]} \"ls -lrt -d -1 $di/{*,.*}\""
fi
done
elif [ $MODE == "img" ]; then
IFS=';' read -r -a dirs <<< "${array[0]};${array[1]}"
for di in ${dirs[@]}; do
if [ $di == $2 ]; then
if [ -z ${array[2]} ]; then
ls -lrt -d -1 $di/{*,.*}
else
su nlvmi -c "ssh ${array[2]} \"ls -lrt -d -1 $di/{*,.*}\""
fi
fi
done
fi
done done
} }
function delimg {
DDIR=`echo $2 | rev | cut -d "/" -f 2- | rev`
sqlite3 $SQLITEFILE "SELECT vmdirectory,isodirectory,connectstring FROM servers WHERE hostname='$1'" | while read line; do
IFS="|" read -r -a array <<< "$line"
IFS=";" read -r -a dirs <<< "${array[0]};${array[1]}"
for dir in ${dirs[@]}; do
if [ $dir == $DDIR ]; then
if [ -z ${array[2]} ]; then
if [ -e $2 ]; then rm $2 && echo "delete completed"; fi
else
su nlvmi -c "ssh ${array[2]} \"if [ -e $2 ]; then rm $2 && echo delete completed; fi\""
fi
fi
done
done
}
function createimg {
re='^[0-9]+$'
if ! [[ $2 =~ $re ]] ; then
echo "size argument is not numeric" >&2; exit 1
fi
IFS=';' read -r -a T <<< "$1"
TDIR=`echo ${T[1]} | rev | cut -d "/" -f 2- | rev`
TFOUND="no"
declare -a A=`sqlite3 $SQLITEFILE "SELECT vmdirectory,connectstring FROM servers WHERE hostname='${T[0]}'" | while read line; do
IFS="|" read -r -a array <<< "$line"
IFS=";" read -r -a dirs <<< "${array[0]}"
for di in ${dirs[@]}; do
if [ $di == $TDIR ]; then
TFOUND="yes"
TCONN=${array[1]}
fi
done
echo "($TFOUND $TCONN)"
done`
if [ ${A[0]} == "yes" ]; then
if [ -z ${A[1]} ]; then
qemu-img create -f qcow2 ${T[1]} ${2}G && echo "creatimg completed"
else
su nlvmi -c "ssh ${A[1]} \"qemu-img create -f qcow2 ${T[1]} ${2}G\"" && echo "createimg completed"
fi
fi
}
function copyimg {
IFS=';' read -r -a S <<< "$1"
IFS=';' read -r -a T <<< "$2"
SDIR=`echo ${S[1]} | rev | cut -d "/" -f 2- | rev`
TDIR=`echo ${T[1]} | rev | cut -d "/" -f 2- | rev`
SFOUND="no"
TFOUND="no"
for m in S T; do
A="$m"DIR
declare -a B=`sqlite3 $SQLITEFILE "SELECT vmdirectory,isodirectory,connectstring FROM servers WHERE hostname='${!m[0]}'" | while read line; do
IFS='|' read -r -a array <<< "$line"
IFS=';' read -r -a dirs <<< "${array[0]};${array[1]}"
for di in ${dirs[@]}; do
if [ $di == ${!A} ]; then
if [ $m == "S" ]; then
if [ -z ${array[2]} ]; then
if [ -e ${S[1]} ]; then FOUND="yes"; CONN="empty"; fi
else
su nlvmi -c "ssh ${array[2]} \"if [ ! -e ${S[1]} ]; then exit 1; fi\"" && FOUND="yes"; CONN=${array[2]}
fi
elif [ $m == "T" ]; then FOUND="yes"; CONN=${array[2]}; if [ -z $CONN ]; then CONN="empty"; fi; fi
fi
done
echo "($FOUND $CONN)"
done`
export ${m}FOUND=${B[0]}
export ${m}CONN=${B[1]}
done
if [ $SFOUND == "yes" ] && [ $TFOUND == "yes" ]; then
if [ $SCONN == "empty" ] && [ $TCONN == "empty" ]; then
if [ -z $FID ]; then
FID="nldebugger"
fi
rsync --progress ${S[1]} ${T[1]} >/tmp/$FID && echo done >/tmp/$FID || echo fail >/tmp/$FID
elif [ $SCONN == "empty" ]; then
su nlvmi -c "rsync --progress ${S[1]} $TCONN:${T[1]} >/tmp/$FID && echo done >/tmp/$FID || echo fail >/tmp/$FID"
elif [ $SCONN == $TCONN ]; then
su nlvmi -c "ssh $TCONN \"rsync --progress ${S[1]} ${T[1]}\"" >/tmp/$FID && echo done >/tmp/$FID || echo fail >/tmp/$FID
elif [ $TCONN == "empty" ]; then
su nlvmi -c "rsync --progress $SCONN:${S[1]} ${T[1]} >/tmp/$FID && echo done >/tmp/$FID || echo fail >/tmp/$FID"
else
su nlvmi -c "echo "noprogressavailable" >/tmp/$FID && scp -3 $SCONN:${S[1]} $TCONN:${T[1]}" && echo done >/tmp/$FID || echo fail >/tmp/$FID
fi
fi
}
#mainloop #mainloop
if [ $# -gt 0 ]; then if [ $# -gt 0 ]; then
if [ $1 == "createdb" ]; then if [ $1 == "createdb" ]; then
createdb createdb
elif [ $1 == "autostart" ]; then elif [ $1 == "autostart" ]; then
vmautostart vmautostart
elif [ $1 == "checkvm" ]; then elif [ $1 == "checkvm" ]; then
if [ -z $2 ]; then echo "checkvm needs two arguments"; fi if [ -z $2 ]; then echo "checkvm needs two arguments"; fi
@@ -319,7 +432,7 @@ if [ $# -gt 0 ]; then
elif [ $1 == "start" ]; then elif [ $1 == "start" ]; then
if [ -z $2 ]; then echo "start needs a vmname!"; exit 1; fi if [ -z $2 ]; then echo "start needs a vmname!"; exit 1; fi
VMFOUND="no" VMFOUND="no"
if [ $LOGLEVEL -gt "1" ]; then echo "searching for vmname $2"; fi if [ $LOGLEVEL -gt "2" ]; then echo "searching for vmname $2"; fi
sqlite3 $SQLITEFILE "SELECT id FROM vms WHERE vmname='$2'" | (while read line; do sqlite3 $SQLITEFILE "SELECT id FROM vms WHERE vmname='$2'" | (while read line; do
IFS='|' read -r -a arrays <<< "$line" IFS='|' read -r -a arrays <<< "$line"
declare VMFOUND="yes" declare VMFOUND="yes"
@@ -330,7 +443,7 @@ if [ $# -gt 0 ]; then
elif [ $1 == "stop" ]; then elif [ $1 == "stop" ]; then
if [ -z $2 ]; then echo "stop needs a vmname!"; exit 1; fi if [ -z $2 ]; then echo "stop needs a vmname!"; exit 1; fi
VMFOUND="no" VMFOUND="no"
if [ $LOGLEVEL -gt "1" ]; then echo "searching for vmname $2"; fi if [ $LOGLEVEL -gt "2" ]; then echo "searching for vmname $2"; fi
sqlite3 $SQLITEFILE "SELECT id FROM vms WHERE vmname='$2'" | (while read line; do sqlite3 $SQLITEFILE "SELECT id FROM vms WHERE vmname='$2'" | (while read line; do
IFS='|' read -r -a arrays <<< "$line" IFS='|' read -r -a arrays <<< "$line"
declare VMFOUND="yes" declare VMFOUND="yes"
@@ -340,13 +453,23 @@ if [ $# -gt 0 ]; then
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 == "stopall" ]; then elif [ $1 == "stopall" ]; then
stopall stopall
elif [ $1 == "listdir" ]; then elif [ $1 == "listdirs" ]; then
if [ -z $2 ] || [ -z $3 ]; then echo listdir needs two more arguments; exit 1; fi if [ -z $2 ] || [ -z $3 ]; then echo listdirs needs two arguments; exit 1; fi
listdir $2 $3 listdirs $2 $3
elif [ $1 == "createimg" ]; then
if [ -z $2 ] || [ -z $3 ]; then echo createimg needs two arguments; exit 1; fi
createimg $2 $3
elif [ $1 == "copyimg" ]; then
if [ -z $2 ] || [ -z $3 ]; then echo copyimg needs two arguments; exit 1; fi
copyimg $2 $3
elif [ $1 == "delimg" ]; then
if [ -z $2 ] || [ -z $3 ]; then echo delimg needs two arguments; exit 1; fi
delimg $2 $3
elif [ $1 == "remote" ]; then elif [ $1 == "remote" ]; then
if [ ! -z $MASTERSERVER ]; then if [ ! -z $MASTERSERVER ]; then
if `echo $SSH_CLIENT | grep "$MASTERSERVER " &>/dev/null`; then if `echo $SSH_CLIENT | grep "$MASTERSERVER " &>/dev/null`; then
if [ -e $2 ]; then if [ -e $2 ]; then
source /etc/profile
openssl dgst -sha512 -verify <(openssl x509 -in "/etc/nlvmi/nlvmi_sign.crt" -pubkey -noout) -signature $2.sha512 $2 >/dev/null && /bin/bash $2 || echo "signature failed!!" openssl dgst -sha512 -verify <(openssl x509 -in "/etc/nlvmi/nlvmi_sign.crt" -pubkey -noout) -signature $2.sha512 $2 >/dev/null && /bin/bash $2 || echo "signature failed!!"
fi fi
else else

Binary file not shown.

View File

@@ -12,6 +12,23 @@
<title>nlvmi</title> <title>nlvmi</title>
</head> </head>
<body> <body>
<div class="modal fade" id="imagemgmt" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header" id="imagemodaltext1">
Imagemanagement
</div>
<div class="modal-body" id="imagemodaltext2">
...
</div>
<div id="imgprogr" class="d-none row"><div id="imgprogd" style="text-align:center;" class="col"></div></div>
<div id="imgprog" class="d-none row"><div class="col" style="text-align:center;"><progress style="max-width:80%;" max="100" value="0" id=imgprogbar></progress></div></div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<div class="modal fade" id="confirm-delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal fade" id="confirm-delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog"> <div class="modal-dialog">
<div class="modal-content"> <div class="modal-content">
@@ -27,11 +44,11 @@
</div> </div>
</div> </div>
</div> </div>
</div> </div>
<script src="nlvmi.js?4"></script> <script src="nlvmi.js?5"></script>
<div class=container-fluid><div class="row" style="background-color:lightgreen;"><div class="col-sm"><h1><a href="?" style=color:white;>nlvmi</a><a href="?" style="color:black;font-size:10px;">@<?php echo gethostname();?></a></h1></div><div class=col-sm style=text-align:right> <div class=container-fluid><div class="row" style="background-color:lightgreen;"><div class="col-sm"><h1><a href="?" style=color:white;>nlvmi</a><a href="?" style="color:black;font-size:10px;">@<?php echo gethostname();?></a></h1></div><div class=col-sm style=text-align:right>
<?php if($_SESSION['isadmin']==1){ echo "<a style=color:black href=# onclick=\"javascript:post('?', {'usermgmt': 'show'});\">usermanagement</a></br><a style=color:black href=# onclick=\"javascript:post('?', {'servermgmt': 'show'});\">servers</a></br>";} ?> <?php if($_SESSION['isadmin']==1){ echo "<a style=padding-right:2em;color:black href=#imagemgmt onclick=\"javascript:imagemgmt('show');\"> images </a> <a style=padding-right:2em;color:black href=# onclick=\"javascript:post('?', {'servermgmt': 'show'});\"> servers </a> <a style=padding-right:2em;color:black href=# onclick=\"javascript:post('?', {'usermgmt': 'show'});\"> usermanagement </a> ";} ?>
<a style=color:black href=# onclick="javascript:post('?', {'logout': 'logout'});">logout</a></div></div> <a style=padding-right:2em;color:black href=# onclick="javascript:post('?', {'logout': 'logout'});"> logout </a></div></div>

View File

@@ -36,7 +36,7 @@ function serverdepropdown($server, $val, $what){
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=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 "<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>"; 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"); $shellout=shell_exec("/usr/bin/nlvmi listdirs $server $dir");
$files = explode("\n", $shellout); $files = explode("\n", $shellout);
for ($f=0; $f<count($files); $f++){ for ($f=0; $f<count($files); $f++){
$file = explode(" ", $files[$f]); $file = explode(" ", $files[$f]);
@@ -57,6 +57,24 @@ function formatbuttons($label, $val){
echo "<a class=dropdown-item href=\"javascript:dropdown('".$label."button$label','qcow2')\">qcow2</a>"; echo "<a class=dropdown-item href=\"javascript:dropdown('".$label."button$label','qcow2')\">qcow2</a>";
echo "</div></div></div></div></div><input type=hidden id=".$label."button".$label."h name=$label value=\"$val\">"; echo "</div></div></div></div></div><input type=hidden id=".$label."button".$label."h name=$label value=\"$val\">";
} }
function listimg($server,$dir){
$shellout=shell_exec("/usr/bin/nlvmi listdirs $server $dir");
$files = explode("\n", $shellout);
echo "<div id=listimgd class=form-group><label for=ld><select class=form-control id=ld>";
for ($f=0; $f<count($files); $f++){
$file = explode(" ", $files[$f]);
$lastpart = count($file) -1;
$filenames = explode('/', $file[$lastpart]);
$filenamep = count($filenames) -1;
$filename = $filenames[$filenamep];
if((preg_match('/\//', $file[$lastpart]))&&(!preg_match('/^d/', $file[0]))){
echo "<option>$filename</option>";
}
}
echo "</select></label></div>";
}
//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()) {
@@ -137,8 +155,18 @@ function checkmacfree($vmname,$mac,$db_handle){
return 0; return 0;
} }
if (isset($_POST['checkmac'])){
$mac = clean($_POST['checkmac']);
$mf = checkmacfree('novm', $mac, $db_handle);
if ($mf == 0){
echo "mac is free";
} else {
echo "mac not free";
}
exit;
//delete vms //delete vms
if (isset($_SESSION['username'])){ } elseif (isset($_SESSION['username'])){
if ((isset($_POST['deleteid']))&&(isset($_POST['deletename']))&&($_POST['delwhat']=="vm")){ if ((isset($_POST['deleteid']))&&(isset($_POST['deletename']))&&($_POST['delwhat']=="vm")){
if($_SESSION['isadmin']!=1){ if($_SESSION['isadmin']!=1){
echo "you are not allowed to delete vms!"; echo "you are not allowed to delete vms!";
@@ -217,9 +245,39 @@ if (isset($_SESSION['username'])){
} }
} }
} }
} elseif
//delete image
((isset($_POST['deleteid']))&&(isset($_POST['deletename']))&&($_POST['delwhat']=="image")){
if($_SESSION['isadmin']!=1){
echo "you are not allowed to delete images!";
} else {
$server = clean($_POST['deletename']);
$filec = explode(';', clean($_POST['deleteid']));
$sql="SELECT id FROM servers WHERE hostname='$server' AND (vmdirectory LIKE '%$filec[0]%' OR isodirectory LIKE '%$filec[0]%')";
$res = $db_handle->query($sql);
while ($row = $res->fetchArray(SQLITE3_ASSOC)){
$shellout = shell_exec("/usr/bin/nlvmi delimg $server $filec[0]/$filec[1]");
echo $shellout;
}
}
} elseif
//create new image
(isset($_POST['createimg'])){
$img = clean($_POST['createimg']);
$server = clean($_POST['createimgs']);
$dir = clean($_POST['createimgd']);
$size = clean($_POST['createimgsize']);
$sql="SELECT id FROM servers WHERE hostname='$server' AND (vmdirectory LIKE '%$dir%')";
$res = $db_handle->query($sql);
while ($row = $res->fetchArray(SQLITE3_ASSOC)){
$shellout = shell_exec("/usr/bin/nlvmi createimg \"$server;$dir/$img\" $size");
echo $shellout;
exit;
}
} }
//insert new user in db //insert new user in db
if(isset($_POST['newusername'])){ elseif(isset($_POST['newusername'])){
if($_SESSION['isadmin']!=1){ if($_SESSION['isadmin']!=1){
echo "you are not allowed to add usernames!"; echo "you are not allowed to add usernames!";
} else { } else {
@@ -397,8 +455,6 @@ if (isset($_SESSION['username'])){
$vid = clean($_REQUEST['vid']); $vid = clean($_REQUEST['vid']);
$sql="SELECT prio FROM vms WHERE id='$vid'"; $sql="SELECT prio FROM vms WHERE id='$vid'";
$oldprio = $db_handle->query($sql)->fetchArray(SQLITE3_ASSOC)['prio']; $oldprio = $db_handle->query($sql)->fetchArray(SQLITE3_ASSOC)['prio'];
//print_r($res);
//echo "buu";
if ($_REQUEST['neworder']=="up"){ if ($_REQUEST['neworder']=="up"){
$newprio = $oldprio - 1; $newprio = $oldprio - 1;
$sqlu="SELECT id FROM vms WHERE prio='$newprio'"; $sqlu="SELECT id FROM vms WHERE prio='$newprio'";
@@ -409,7 +465,7 @@ if (isset($_SESSION['username'])){
echo "success"; echo "success";
exit; exit;
} }
}elseif ($_REQUEST['neworder']=="down"){ } elseif ($_REQUEST['neworder']=="down"){
$newprio = $oldprio + 1; $newprio = $oldprio + 1;
$sqlu="SELECT id FROM vms WHERE prio='$newprio'"; $sqlu="SELECT id FROM vms WHERE prio='$newprio'";
$oldid = $db_handle->query($sqlu)->fetchArray(SQLITE3_ASSOC)['id']; $oldid = $db_handle->query($sqlu)->fetchArray(SQLITE3_ASSOC)['id'];
@@ -434,6 +490,113 @@ if (isset($_SESSION['username'])){
exit; exit;
} }
if (isset($_REQUEST['imagemgmt'])){
$server = clean($_REQUEST['imagemgmt']);
if ($server == "undefined"){
$serverq = "";
} else {
$serverq = $server;
}
function imgdropdowns($db_handle, $server, $serverq, $dmode){
$sqli="SELECT vmdirectory,isodirectory,hostname FROM servers";
$resi = $db_handle->query($sqli);
$sdropdown = "";
$firstserver = "";
while ($rowi = $resi->fetchArray(SQLITE3_ASSOC)){
$hostname = $rowi['hostname'];
if ($firstserver == ""){
$firstserver = $hostname;
}
$vmdirs = explode(';', $rowi['vmdirectory']);
$isodirs = explode(';', $rowi['isodirectory']);
$imgdirs[$hostname] = array_merge($vmdirs, $isodirs);
$sdropdown .= "<a class=dropdown-item href=\"javascript:imgdropdown('server','$hostname', '$vmdirs[0]', '$dmode')\">$hostname</a>";
}
if ($serverq == ""){
$serverq = $firstserver;
}
$dropdowns = "<div class=row><div class=col-sm><div class=dropdown><button class=\"btn btn-secondary dropdown-toggle\" type=button id=imageserverbutton$dmode data-toggle=dropdown aria-haspopup=true aria-expanded=false>$serverq</button>";
$dropdowns .= "<div class=dropdown-menu aria-labelledby=imageserverbutton>$sdropdown</div></div></div>";
foreach(array_keys($imgdirs) as $iserver){
$firstdir = $imgdirs[$iserver][0];
$hiddend = "d-none";
if(($server == "undefined")&&($imgdirs[$firstserver][0] == $firstdir)&&($firstserver == $iserver)){
$hiddend = "";
}
$dropdowns .= "<div class=\"col-sm $hiddend\" id=imagedirdropd$iserver$dmode><div class=dropdown ><button class=\"btn btn-secondary dropdown-toggle\" type=button id=imagedirbutton$iserver$dmode data-toggle=dropdown aria-haspopup=true aria-expanded=false>$firstdir</button>";
$dropdowns .= "<div class=dropdown-menu aria-labelledby=imagedirbutton$iserver>";
for ($di=0; $di<count($imgdirs[$iserver]); $di++){
$dir = $imgdirs[$iserver][$di];
$dropdowns .= "<a class=dropdown-item href=\"javascript:imgdropdown('dir','$iserver','$dir', '$dmode')\">$dir</a>";
}
$dropdowns .= "</div></div></div>";
}
$dropdowns .= "</div><br/>";
echo $dropdowns;
if ($dmode == "source"){
listimg($firstserver, $firstdir);
}
}
imgdropdowns($db_handle, $server, $serverq, 'source');
echo "<div class=d-none id=copytargetd>Target";
imgdropdowns($db_handle, $server, $serverq, 'target');
echo "<div class=row><div class=col-sm><input id=targetfilename name=targetfilenme type=text class=form-control placeholder=\"new filename\"></div></div><br/></div>";
echo "<div class=row><div class=col-sm><a class=\"btn btn-primary\" id=copybtn onclick=imgcopy();>copy</a></div><div class=col-sm id=delimgbutton><button class=\"btn btn-danger\" data-delete-text=\"Delete image \" data-delete-what=image data-delete-id=\"\" data-delete-name=\"\" data-toggle=\"modal\" data-target=\"#confirm-delete\">Delete</button></div></div>";
echo "<div id=newimgdiv><hr><div class=row ><div class=col-sm><input type=text class=form-control id=newimg placeholder=\"new image name\"></div><div class=col-md-3><input type=text class=form-control id=newimgsize placeholder=\"size in GB\"></div><div class=col-md-3><a class=\"btn btn-primary\" onclick=createimg();>create</a></div></div></div>";
exit;
}
if ((isset($_REQUEST['listdir']))&&(isset($_REQUEST['server']))){
$listdir = clean($_REQUEST['listdir']);
$server = clean($_REQUEST['server']);
$sqld="SELECT id FROM servers WHERE hostname = '$server' AND (vmdirectory LIKE '%$listdir%' OR isodirectory LIKE '%$listdir%')";
$resd = $db_handle->query($sqld);
while ($rowd = $resd->fetchArray(SQLITE3_ASSOC)){
listimg($server, $listdir);
exit;
}
}
if (isset($_REQUEST['copyss'])){
$sserver = clean($_REQUEST['copyss']);
$sfile = clean($_REQUEST['copysf']);
$tserver = clean($_REQUEST['copyts']);
$tfile = clean($_REQUEST['copytf']);
$shellout = shell_exec("export FID=\$\$; printf \"\$FID\"; /usr/bin/nlvmi copyimg \"$sserver;$sfile\" \"$tserver;$tfile\" 2&>/dev/null &");
if (preg_match_all('/completed/', $shellout)){
echo "success";
} else {
echo $shellout;
}
exit;
}
if (isset($_REQUEST['progress'])){
$txt_file = file_get_contents("/tmp/$_REQUEST[progress]");
if (preg_match('/noprogressavailable/', $txt_file)){
echo "sorry, no progress available";
} elseif(preg_match('/fail/', $txt_file)){
echo "fail";
} elseif(!preg_match('/^done/', $txt_file)){
$row = explode(" ", $txt_file);
$c = count($row) - 1;
$pfound="no";
while ($pfound == "no"){
$field = $row[$c];
if (preg_match('/%/', $field)){
$perc = explode('%', $field);
echo $perc[0];
$pfound="yes";
}
$c = $c -1;
}
} else {
echo "done";
}
exit;
}
//ajax queries are all done, time for the header //ajax queries are all done, time for the header
include('header.php'); include('header.php');
@@ -624,7 +787,7 @@ if (isset($_SESSION['username'])){
} elseif (preg_match('/tapdev/', $arrColnames[$col])) { } 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>"; 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])) { } 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\" placeholder=\"$placehold[$colname]\"></div>"; 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] id=$arrColnames[$col] value=\"$val\" placeholder=\"$placehold[$colname]\"></div><div onclick=genmac('$arrColnames[$col]') class=\"btn btn-primary\">gen</div>";
} elseif (preg_match('/brdev/', $arrColnames[$col])) { } 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\" placeholder=\"$placehold[$colname]\"></div></div>"; 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\" placeholder=\"$placehold[$colname]\"></div></div>";
} elseif (preg_match('/drive/', $arrColnames[$col])){ } elseif (preg_match('/drive/', $arrColnames[$col])){
@@ -652,8 +815,6 @@ if (isset($_SESSION['username'])){
} }
if(($mode!="newvm")&&($mode!="editvm")&&($mode!='usermgmt')){ if(($mode!="newvm")&&($mode!="editvm")&&($mode!='usermgmt')){
if ($_SESSION['isadmin']=="1"){ if ($_SESSION['isadmin']=="1"){
$sqllimit=""; $sqllimit="";
@@ -683,7 +844,7 @@ if (isset($_SESSION['username'])){
echo "<a href=# onclick=\"post('?', {'$button': '$row[vmname]'});\"class=\"btn $buttonc\">$button</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> "; 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']!=""){ if ($row['websocket']!=""){
$server = $_SERVER['HTTP_HOST']; $server = $row['server'];
$websocketport = $row['websocket'] + 5700; $websocketport = $row['websocket'] + 5700;
$wpath=""; $wpath="";
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])){ if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])){

View File

@@ -1,11 +1,9 @@
function makeid() { function makeid() {
var text = ""; var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 5; i++)
for (var i = 0; i < 5; i++) text += possible.charAt(Math.floor(Math.random() * possible.length));
text += possible.charAt(Math.floor(Math.random() * possible.length)); return text;
return text;
} }
function dropdown(i,e) { function dropdown(i,e) {
@@ -28,34 +26,40 @@ function post(path, params, method) {
var form = document.createElement("form"); var form = document.createElement("form");
form.setAttribute("method", method); form.setAttribute("method", method);
form.setAttribute("action", path); form.setAttribute("action", path);
for(var key in params) { for(var key in params) {
if(params.hasOwnProperty(key)) { if(params.hasOwnProperty(key)) {
var hiddenField = document.createElement("input"); var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden"); hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", key); hiddenField.setAttribute("name", key);
hiddenField.setAttribute("value", params[key]); hiddenField.setAttribute("value", params[key]);
form.appendChild(hiddenField); form.appendChild(hiddenField);
} }
} }
document.body.appendChild(form); document.body.appendChild(form);
form.submit(); form.submit();
} }
$('#confirm-delete').on('click', '.btn-ok', function(e) { $('#confirm-delete').on('click', '.btn-ok', function(e) {
var $modalDiv = $('#confirm-detele'.delegateTarget); var $modalDiv = $('#confirm-detele'.delegateTarget);
var dname = $(this).data('deleteName'); var dname = $(this).data('deleteName');
var did = $(this).data('deleteId'); var did = $(this).data('deleteId');
var delwhat = $(this).data('deleteWhat'); var delwhat = $(this).data('deleteWhat');
post('?', {delwhat: delwhat,deleteid: did,deletename: dname}); post('?', {delwhat: delwhat,deleteid: did,deletename: dname});
}); });
$('#confirm-delete').on('show.bs.modal', function(e) { $('#confirm-delete').on('show.bs.modal', function(e) {
var data = $(e.relatedTarget).data(); var data = $(e.relatedTarget).data();
$('.title', this).text(data.deleteVm); $('.title', this).text(data.deleteVm);
document.getElementById('modaltext2').innerHTML = data.deleteText; if (data.deleteWhat == "image"){
var server = $('#imageserverbuttonsource').html();
var dir = $('#imagedirbutton'+ server +'source').html();
var img = $('#ld').find(":selected").text();
document.getElementById('modaltext2').innerHTML = data.deleteText + img;
$('.btn-ok', this).data({'deleteWhat': data.deleteWhat,'deleteName': server,'deleteId': dir +";"+ img});
} else {
document.getElementById('modaltext2').innerHTML = data.deleteText;
$('.btn-ok', this).data({'deleteWhat': data.deleteWhat,'deleteName': data.deleteName,'deleteId': data.deleteId}); $('.btn-ok', this).data({'deleteWhat': data.deleteWhat,'deleteName': data.deleteName,'deleteId': data.deleteId});
}
}); });
function submitbutton(formid){ function submitbutton(formid){
@@ -64,7 +68,7 @@ function submitbutton(formid){
type: 'POST', type: 'POST',
url: "?rand="+ makeid(), url: "?rand="+ makeid(),
data: data, data: data,
success: function(response) { success: function(response) {
if (response != "success"){ if (response != "success"){
window.alert('Failure!\n'+ response); window.alert('Failure!\n'+ response);
} else { } else {
@@ -89,3 +93,156 @@ function arrow(dir, id){
}, },
}); });
} }
function imagemgmt(mode, server){
if(mode == "show") {
$('#imagemgmt').modal('show');
$.ajax({
type: 'POST',
url: "?rand="+ makeid(),
data: "&imagemgmt="+ server,
success: function(response) {
$('#imagemodaltext2').html(response);
var server = $('#imageserverbuttonsource').html();
imgdropdown('dir', server, $('#imagedirbutton'+ server +'source').html(), 'source')
},
});
}
}
function imgdropdown(mode,server,dir,dmode){
$('#imagedirbutton'+ server +""+ dmode).html(dir);
if(mode == "server"){
$('#imageserverbutton'+ dmode).html(server);
$("[id^=imagedirdropd][id$="+ dmode +"]").addClass('d-none');
$('#imagedirdropd'+ server +''+ dmode).removeClass('d-none');
}
if(dmode == "source"){
$.ajax({
type: 'POST',
url: "?rand="+ makeid(),
data: "&listdir="+ dir +"&server="+ server,
success: function(response) {
$('#listimgd').html(response);
},
});
}
}
function actioncheck(){
$.ajax({
type: 'POST',
url: "?rand="+ makeid(),
data: "&progress="+ jresponse,
success: function(response) {
if (response == "done") {
cdone = "yes";
} else {
if (response == "sorry, no progress available"){
$('#imgprogd').html(response);
}
if (response == "fail"){
cdone = "yes";
window.alert("Failed to copy!");
} else {
$('#imgprogbar').val(response);
$('#imgprogd').html(response +"%");
}
}
}
});
if (cdone == "yes") {
$('#imgprog').addClass('d-none');
$('#imgprogr').addClass('d-none');
$('#imagemodaltext2').removeClass('d-none');
$('#imgprogbar').val("0");
clearInterval(interval);
}
}
function imgcopy(){
if ($('#copybtn').html() == "copy"){
$('#copytargetd').removeClass('d-none');
$('#copybtn').html("start copy");
$('#copybtn').removeClass('btn-primary');
$('#copybtn').addClass('btn-warning');
$('#newimgdiv').addClass('d-none');
$('#delimgbutton').addClass('d-none');
} else {
var sserver = $('#imageserverbuttonsource').html();
var sdir = $('#imagedirbutton'+ sserver +"source").html();
var sfile = $('#ld').val();
var tserver = $('#imageserverbuttontarget').html();
var tdir = $('#imagedirbutton'+ tserver +"target").html();
var tfile = $('#targetfilename').val();
$.ajax({
type: 'POST',
url: "?rand="+ makeid(),
data: "&copyss="+ sserver +"&copysf="+ sdir +"/"+ sfile +"&copyts="+ tserver +"&copytf="+ tdir +"/"+ tfile,
success: function(response) {
jresponse = response;
if (Number.isInteger(+jresponse)){
$('#imgprogr').removeClass('d-none');
$('#imgprog').removeClass('d-none');
$('#imagemodaltext2').addClass('d-none');
interval= setInterval(actioncheck,3000);
cdone = "no";
} else {
cdone = "fail";
window.alert("FAIL! nana"+ jresponse +"nana");
}
},
});
}
}
function createimg(){
var server = $('#imageserverbuttonsource').html();
var dir = $('#imagedirbutton'+ server +'source').html();
var imgname = $('#newimg').val();
var imgsize = $('#newimgsize').val();
$.ajax({
type: 'POST',
url: "?rand="+ makeid(),
data: "createimgs="+ server +"&createimgd="+ dir +"&createimg="+ imgname +"&createimgsize="+ imgsize,
success: function(response){
var imgsuc = new RegExp("createimg completed")
if (imgsuc.test(response)){
window.location.href = "";
} else {
window.alert("Failure\r "+ response);
}
}
});
}
function genmac(field){
freemacfound = "no";
while (freemacfound == "no"){
var text = "";
var possible = "abcdef0123456789";
for (j=0; j < 4; j++){
text += ":";
for (var i = 0; i < 2; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
var newmac = "52:54:00"+ text;
freemacfound = checkmac(newmac, field);
}
}
function checkmac(newmac,field){
$.ajax({
type: 'POST',
url: "?rand="+ makeid(),
data: "checkmac="+ newmac,
success: function(response){
if (response == "mac is free"){
$('#'+ field).val(newmac);
return newmac;
} else {
return "no";
}
}
});
}