Commit de97fe0c authored by Maria Matejka's avatar Maria Matejka

reworked config parser

parent ae07c9e8
......@@ -4,34 +4,23 @@
declare -A seen
seen[dhcpd]=1
for S in `find $ROOT/run -name '*.sock'`; do
S=$(basename $S)
gethost ${S%%.sock}
if ping -nc1 $HOSTIP4 >/dev/null; then
PING="(ping ok)"
for H in $(hostlist); do
if [ -S $ROOT/run/$H.sock ]; then
if ping -nc1 ${CONF[$H;IP4]} >/dev/null; then
PING="(ping ok)"
else
PING="(ping FAIL)"
fi
echo "$H running, IPv4 ${CONF[$H;IP4]} $PING, monitor $ROOT/run/$H.sock"
elif [ -f $ROOT/run/$H.pid ]; then
if ps -p $(<$ROOT/run/$H.pid) >/dev/null; then
echo "$H probably running at PID $(<$ROOT/run/$H.pid), no socket, IPv4 ${CONF[$H;IP4]}"
else
echo "$H stopped, $ROOT/run/$H.pid stale pidfile found, removing"
rm $ROOT/run/$H.pid
fi
else
PING="(ping FAIL)"
echo "$H stopped"
fi
echo "${S%%.sock} running, IPv4 $HOSTIP4 $PING, monitor $S"
seen["${S%%.sock}"]=1
done
for P in `find $ROOT/run -name '*.pid'`; do
P=$(basename $P)
if [ "${seen[${P%%.pid}]}" = "1" ]; then continue; fi
gethost ${P%%.pid}
if ps -p $(<$P) >/dev/null; then
echo "${P%%.pid} probably running at PID $(<$P), no socket, IPv4 $HOSTIP4"
else
echo "${P%%.pid} stale pidfile found, removing"
rm $ROOT/run/$P
fi
seen["${P%%.pid}"]=1
done
for H in `grep -i '^Host' $VIRTUALS| sed 's/^[Hh][Oo][Ss][Tt] //'`; do
if [ "${seen[$H]}" = "1" ]; then continue; fi
echo "$H not running"
done
......@@ -32,77 +32,78 @@ function wait_pidfile() {
return 0
}
function gethost() {
HOSTIMAGE=
HOSTNETWORK=
HOSTCDROM=
HOSTARCH=
HOSTMEMORY=
HOSTCOW=
HOSTVNCID=
HOSTTAP=
HOSTIP4=
HOSTMAC=
HOSTNIC=()
HOSTNICCMD=
HOSTTYPE=
SOCAT=$ROOT/bin/socat
declare -A HOSTS
declare -A CONF
NAME=$1
_HOST_READING=false
while read item value; do
if $_HOST_READING; then
case ${item^^} in
#) ;;
HOST) hostdefaults
return $? ;;
IMAGE) HOSTIMAGE="$value" ;;
NETWORK)HOSTNETWORK="$value" ;;
CDROM) HOSTCDROM="$value" ;;
ARCH) HOSTARCH="$value" ;;
MEMORY) HOSTMEMORY="$value" ;;
COW) HOSTCOW="$value" ;;
VNC) HOSTVNCID="$value" ;;
NIC) HOSTNIC+=("$value") ;;
TYPE) HOSTTYPE="$value" ;;
esac
else
if [ "${item^^}" = "HOST" ] \
&& [ "$value" = "$NAME" ]; then
_HOST_READING=true
HOSTNAME=$NAME
fi
fi
done <$VIRTUALS
function hostdefaults() {
if [ -z "${CONF[$HOSTNAME;IMAGE]}" ]; then echo "Error: Image not set"; return 1; fi
if [ -z "${CONF[$HOSTNAME;ARCH]}" ]; then CONF[$HOSTNAME;ARCH]=x86_64; fi
if [ -z "${CONF[$HOSTNAME;MEMORY]}" ]; then CONF[$HOSTNAME;MEMORY]=64M; fi
if [ "${CONF[$HOSTNAME;COW]^^}" = "YES" ]; then CONF[$HOSTNAME;COW]=-snapshot; else CONF[$HOSTNAME;COW]=; fi
if $_HOST_READING; then
hostdefaults
return $?
if [ -z "${CONF[$HOSTNAME;NETWORK]}" ]; then
local SHA=$(sha1sum <<<"$HOSTNAME" | head -c 12)
CONF[$HOSTNAME;TAP]=vc-$SHA
CONF[$HOSTNAME;MAC]=02:$(sed -r 's/^.*(..)(..)(..)(..)(..)$/\1:\2:\3:\4:\5/'<<<"$SHA")
CONF[$HOSTNAME;IP4]=192.168.$((193+((0x$SHA / 254) % 63))).$((0x$SHA % 254))
else
return 1
read CONF[$HOSTNAME;TAP] CONF[$HOSTNAME;MAC] CONF[$HOSTNAME;IP4] <<<"${CONF[$HOSTNAME;NETWORK]}"
fi
HOSTS[$HOSTNAME]=true
HOSTNAME=
}
function hostdefaults() {
if [ -z "$HOSTIMAGE" ]; then echo "Error: Image not set"; return 1; fi
if [ -z "$HOSTARCH" ]; then HOSTARCH=x86_64; fi
if [ -z "$HOSTMEMORY" ]; then HOSTMEMORY=64M; fi
if [ "${HOSTCOW^^}" = "YES" ]; then HOSTCOW=-snapshot; else HOSTCOW= ; fi
HOSTNAME=
while read item value; do
if [ -z "$HOSTNAME" ]; then
if [ -z "$item" ]; then
continue
elif [ "${item^^}" = "HOST" ]; then
HOSTNAME=$value
continue
else
echo "Config must begin by host definition."
exit 2
fi
fi
if [ -z "$item" ]; then continue; fi
for NIC in "${HOSTNIC[@]}"; do
NICNAME=${NIC%% *}
NICMAC=${NIC##* }
done
case ${item^^} in
\#*) continue ;;
HOST) hostdefaults
HOSTNAME=$value
;;
IMAGE) CONF[$HOSTNAME;IMAGE]="$value" ;;
NETWORK)CONF[$HOSTNAME;NETWORK]="$value" ;;
CDROM) CONF[$HOSTNAME;CDROM]="$value" ;;
ARCH) CONF[$HOSTNAME;ARCH]="$value" ;;
MEMORY) CONF[$HOSTNAME;MEMORY]="$value" ;;
COW) CONF[$HOSTNAME;COW]="$value" ;;
VNC) CONF[$HOSTNAME;VNCID]="$value" ;;
TYPE) CONF[$HOSTNAME;TYPE]="$value" ;;
*) echo "Unknown directive '$item' in config"
exit 2 ;;
esac
done <$VIRTUALS
if [ -z "$HOSTNETWORK" ]; then
local SHA=$(sha1sum <<<"$HOSTNAME" | head -c 12)
HOSTTAP=vc-$SHA
HOSTMAC=02:$(sed -r 's/^.*(..)(..)(..)(..)(..)$/\1:\2:\3:\4:\5/'<<<"$SHA")
HOSTIP4=192.168.$((193+((0x$SHA / 254) % 63))).$((0x$SHA % 254))
hostdefaults
HOSTNAME=
function gethost() {
if ${HOSTS[$1]}; then
eval $(echo ${!CONF[@]} | tr ' ' '\n' | sed -rn "/^$1;/{ s/$1;//; s/.*/HOST&=\\\${CONF[$1;&]}/p }")
HOSTNAME=$1
return 0
else
read HOSTTAP HOSTMAC HOSTIP4 <<<"$HOSTNETWORK"
echo "Unknown host: $1"
return 1
fi
return 0
}
SOCAT=$ROOT/bin/socat
function hostlist() {
for H in ${!HOSTS[@]}; do echo $H; done | sort
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment