Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
executable file 217 lines (157 sloc) 6.11 KB
#!/bin/bash
CMD_IP="/sbin/ip"
CMD_IPTABLES="/usr/sbin/iptables"
CMD_MXS2MXVIP="/usr/sbin/mxstartup2mxconfig"
RUNDIR="/run/mariux"
# expands *-pattern in pathnames to null if no matching files are found..
shopt -s nullglob
function mxvip_start_one() {
local vip=$1
local -i i mip mfwd
echo "starting ${vip} .."
. ${vip}
mip=${#MX_IP_ADDRESS[*]}
mfwd=${#MX_FWD_IP[*]}
# echo "ip addresses found: ${mip}"
#MX_FWD_DST_IP=([0]="141.14.30.222" [1]="141.14.20.135" [2]="141.14.27.44" [3]="141.14.27.44")
#MX_FWD_DST_PORT=([0]="8080" [1]="9191" [2]="9090" [3]="6789")
#MX_FWD_IP=([0]="-1" [1]="0" [2]="1" [3]="1")
#MX_FWD_SRC_IP=([0]="141.14.30.222" [1]="141.14.20.135" [2]="141.14.27.44" [3]="141.14.27.44")
#MX_FWD_SRC_PORT=([0]="80" [1]="91" [2]="90" [3]="80")
#MX_IP_ADDRESS=([0]="141.14.20.135" [1]="141.14.27.44")
#MX_IP_ADDRESSPREFIX=([0]="20" [1]="20")
#MX_IP_BROADCAST=([0]="141.14.31.255" [1]="141.14.31.255")
#MX_IP_DEVICE=([0]="eth0" [1]="eth0")
i=0
while [ ${i} -lt ${mip} ] ; do
# echo " $i -> ${MX_IP_ADDRESS[$i]}"
echo "adding ip ${MX_IP_ADDRESS[$i]}/${MX_IP_ADDRESSPREFIX[$i]} broadcast ${MX_IP_BROADCAST[$i]} to ${MX_IP_DEVICE[$i]}"
${CMD_IP} addr add ${MX_IP_ADDRESS[$i]}/${MX_IP_ADDRESSPREFIX[$i]} broadcast ${MX_IP_BROADCAST[$i]} dev ${MX_IP_DEVICE[$i]}
${CMD_IP} link set up dev ${MX_IP_DEVICE[$i]}
i=i+1
done
# echo "fwds found: ${mfwd}"
i=0
while [ ${i} -lt ${mfwd} ] ; do
echo "adding forward ${MX_FWD_SRC_IP[$i]}:${MX_FWD_SRC_PORT[$i]} <-> ${MX_FWD_DST_IP[$i]}:${MX_FWD_DST_PORT[$i]}"
${CMD_IPTABLES} -t nat -A PREROUTING -p tcp -j DNAT -d ${MX_FWD_SRC_IP[$i]} --dport ${MX_FWD_SRC_PORT[$i]} --to-destination ${MX_FWD_DST_IP[$i]}:${MX_FWD_DST_PORT[$i]}
${CMD_IPTABLES} -t nat -A OUTPUT -p tcp -j DNAT -d ${MX_FWD_SRC_IP[$i]} --dport ${MX_FWD_SRC_PORT[$i]} --to-destination ${MX_FWD_DST_IP[$i]}:${MX_FWD_DST_PORT[$i]}
${CMD_IPTABLES} -t nat -A POSTROUTING -p tcp -j SNAT --dst ${MX_FWD_DST_IP[$i]} --dport ${MX_FWD_SRC_PORT[$i]} --to-source ${MX_FWD_SRC_IP[$i]}
i=i+1
done
unset ${!MX_IP_*}
unset ${!MX_FWD_*}
mv ${vip}{,.r}
}
function mxvip_stop_one() {
local vip=$1
local -i i mip mfwd
echo "stopping ${vip} .."
. ${vip}
mip=${#MX_IP_ADDRESS[*]}
mfwd=${#MX_FWD_IP[*]}
# echo "ip addresses found: ${mip}"
#MX_FWD_DST_IP=([0]="141.14.30.222" [1]="141.14.20.135" [2]="141.14.27.44" [3]="141.14.27.44")
#MX_FWD_DST_PORT=([0]="8080" [1]="9191" [2]="9090" [3]="6789")
#MX_FWD_IP=([0]="-1" [1]="0" [2]="1" [3]="1")
#MX_FWD_SRC_IP=([0]="141.14.30.222" [1]="141.14.20.135" [2]="141.14.27.44" [3]="141.14.27.44")
#MX_FWD_SRC_PORT=([0]="80" [1]="91" [2]="90" [3]="80")
#MX_IP_ADDRESS=([0]="141.14.20.135" [1]="141.14.27.44")
#MX_IP_ADDRESSPREFIX=([0]="20" [1]="20")
#MX_IP_BROADCAST=([0]="141.14.31.255" [1]="141.14.31.255")
#MX_IP_DEVICE=([0]="eth0" [1]="eth0")
# echo "fwds found: ${mfwd}"
i=0
while [ ${i} -lt ${mfwd} ] ; do
echo "removing forward ${MX_FWD_SRC_IP[$i]}:${MX_FWD_SRC_PORT[$i]} <-> ${MX_FWD_DST_IP[$i]}:${MX_FWD_DST_PORT[$i]}"
${CMD_IPTABLES} -t nat -D PREROUTING -p tcp -j DNAT -d ${MX_FWD_SRC_IP[$i]} --dport ${MX_FWD_SRC_PORT[$i]} --to-destination ${MX_FWD_DST_IP[$i]}:${MX_FWD_DST_PORT[$i]}
${CMD_IPTABLES} -t nat -D OUTPUT -p tcp -j DNAT -d ${MX_FWD_SRC_IP[$i]} --dport ${MX_FWD_SRC_PORT[$i]} --to-destination ${MX_FWD_DST_IP[$i]}:${MX_FWD_DST_PORT[$i]}
${CMD_IPTABLES} -t nat -D POSTROUTING -p tcp -j SNAT --dst ${MX_FWD_DST_IP[$i]} --dport ${MX_FWD_SRC_PORT[$i]} --to-source ${MX_FWD_SRC_IP[$i]}
i=i+1
done
i=0
while [ ${i} -lt ${mip} ] ; do
# echo " $i -> ${MX_IP_ADDRESS[$i]}"
dev=${MX_IP_DEVICE[$i]}
if [ ! -d "/sys/class/net/${dev}" -a "${dev:0:3}" = "eth" ] ; then
net=$(printf "net%02d" "${dev:3}")
if [ -d "/sys/class/net/${net}" ] ; then
MX_IP_DEVICE[$i]=$net
fi
fi
echo "removing ip ${MX_IP_ADDRESS[$i]}/${MX_IP_ADDRESSPREFIX[$i]} broadcast ${MX_IP_BROADCAST[$i]} to ${MX_IP_DEVICE[$i]}"
${CMD_IP} addr del ${MX_IP_ADDRESS[$i]}/${MX_IP_ADDRESSPREFIX[$i]} broadcast ${MX_IP_BROADCAST[$i]} dev ${MX_IP_DEVICE[$i]}
# echo "${CMD_IP} link set up dev ${MX_IP_DEVICE[$i]}"
i=i+1
done
unset ${!MX_IP_*}
unset ${!MX_FWD_*}
rm ${vip}
}
function mxvip_start() {
local vip
local pattern=$1
: ${pattern:=*}
for vip in ${RUNDIR}/mxvip.${pattern}.cfg ; do
if [ -e ${vip}.r ] ; then
echo >&2 "skipping $vip: already running.."
continue
fi
if [ ! -r ${vip} ] ; then
echo >&2 "skipping $vip: can't read file"
continue
fi
if [ ! -O ${vip} ] ; then
echo >&2 "skipping $vip: possible hack attempt?"
continue
fi
mxvip_start_one ${vip}
done
}
function mxvip_stop() {
local vip
local pattern=$1
: ${pattern:=*}
for vip in ${RUNDIR}/mxvip.${pattern}.cfg.r ; do
if [ ! -r ${vip} ] ; then
echo >&2 "skipping $vip: can't read file"
continue
fi
if [ ! -O ${vip} ] ; then
echo >&2 "skipping $vip: possible hack attempt?"
continue
fi
mxvip_stop_one ${vip}
done
}
function create_run_dir_if_not_exists() {
if [ ! -d ${RUNDIR} ] ; then
mkdir -m 0700 ${RUNDIR}
fi
if [ ! -O ${RUNDIR} ] ; then
echo >&2 "${RUNDIR}: wrong owner: possible hack attempt? exiting.."
exit 1
fi
}
function create_mxvip() {
${CMD_MXS2MXVIP} ${RUNDIR} >/dev/null
}
##############################################################################
create_run_dir_if_not_exists
case "${1}" in
start)
create_mxvip
mxvip_start $2
;;
stop)
mxvip_stop $2
;;
restart)
mxvip_stop $2
create_mxvip
mxvip_start $2
;;
*)
echo >&2 "$0 start|stop"
;;
esac