#!/bin/bash
# Copyright: 2023 Virtuozzo International GmbH
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
[ -n "${TOMCATREPLICATIONLIB_VERSION:-}" ] && return 0;
TOMCATREPLICATIONLIB_VERSION="0.1";
TOMCAT_HOME='/opt/tomcat/'
TOMCAT_FILE='conf/tomcat-env.sh'
TOMCAT_CONF_DIR=$TOMCAT_HOME'conf/'
WEBXML=$TOMCAT_CONF_DIR"web.xml"
SERVERXML=$TOMCAT_CONF_DIR"server.xml"
TOMCAT_HA_CONFIG='/opt/tomcat/conf/jelastic-ha.xml'
VTUN_SERVICE="vtund"
VTUN_INIT_SCRIPT="/etc/init.d/vtund"
TOMCAT_SERVICE="tomcat"
VTUND_CONF="/etc/vtund.conf"
IF_NAME='tap'
$PROGRAM 'ifconfig';
$PROGRAM 'grep';
$PROGRAM 'awk';
$PROGRAM 'sed';
$PROGRAM 'ip';
$PROGRAM 'chkconfig';
inherit os default;
include output;
function cleanMac(){
count=$($GREP -c 'ip \"link' $VTUND_CONF )
if [ "$count" -ne 0 ]
then
$SED -i '/^ip \"link/d' $VTUND_CONF
fi
$SED -i "s/REMOTE_HOST[\=[:digit:]\.]\{1,\}/REMOTE_HOST=/" $VTUN_INIT_SCRIPT
}
function setMac(){
mac=''
until [ -n "$mac" ]
do
sleep 1
mac=$($IP l show tap | $SED -n -re '/\s*link/{s/.*ether\s+(\S+)\s+.*/\1/;p}')
done
$SED -i "/^ifconfig/iip \"link set %% addr $mac\";" $VTUND_CONF
}
function setHost() {
local IP=$1
$SED -i "s/^REMOTE_HOST=/REMOTE_HOST=$IP/" $VTUN_INIT_SCRIPT
chmod a+x $VTUN_INIT_SCRIPT 2>>"$JEM_CALLS_LOG"
}
function disableReplication(){
stopServiceSilent $VTUN_SERVICE
$CHKCONFIG --level 3 $VTUN_SERVICE off
cleanMac
count=$($GREP -c '' $WEBXML)
if [ "$count" -ne 0 ]
then
$SED -i '/distributable/d' $WEBXML
fi
$SED -i '1i ' $TOMCAT_HA_CONFIG;
$SED -i "s/^\VTUNIP[\=[:digit:]\.\"]\{1,\}/VTUNIP=\"\"/" $TOMCAT_HOME$TOMCAT_FILE
stopServiceSilent $TOMCAT_SERVICE
startServiceSilent $TOMCAT_SERVICE
}
function enableReplication(){
if [[ -z "$1" ]]
then
echo "Wrong arguments" 1>&2
exit 1
fi
local ip="$1";
if [[ ! -e $TOMCAT_HA_CONFIG ]]
then
echo "No tomcat-ha file" 1>&2
exit 1
fi
stopServiceSilent $VTUN_SERVICE
cleanMac
setHost $ip
startServiceSilent $VTUN_SERVICE
$CHKCONFIG --level 3 $VTUN_SERVICE on
stopServiceSilent $TOMCAT_SERVICE
count=$($GREP -c '' $WEBXML )
if [ "$count" -ne 1 ]
then
$SED -i '/<\/web-app>/i' $WEBXML
fi
$SED -i '//d' $TOMCAT_HA_CONFIG;
VIP=''
until [ -n "$VIP" ]
do
VIP=$($IP a l | $GREP tap| $GREP 'inet ' | $AWK '{print $2}'| $SED 's/\/[0-9]*//g');
sleep 1
done
$SED -i "s/^VTUNIP[\=\"\.[:digit:]]\{0,\}$/VTUNIP=\"$VIP\"/" $TOMCAT_HOME$TOMCAT_FILE
startServiceSilent $TOMCAT_SERVICE
setMac ;
}