#!/bin/bash
#creator qituo
#date 2017-11-09
#email 776336152@qq.com
base_dir="/data/shell"
tomcat_dir="$base_dir/tomcat"
function usage()
{
echo "usage: chsa_addtomcat.sh -p [project] -m [module] -S [Server port] -H [HTTP-Connector] -A [AJP-Connector]";
echo "eg: mysqlha_login.sh -p staging -m exchange-web -S 8106 -H 8156 -A 8206";
}
echo $#
if [ $# -ne 10 ] ;
then
usage
exit 1
else
while getopts p:m:S:H:A: OPTION
do
case "$OPTION" in
p)project=$OPTARG;;
m)module=$OPTARG;;
S)server_port=$OPTARG;;
H)http_port=$OPTARG;;
A)ajp_port=$OPTARG;;
*) echo "input error! please check your input."; usage ; exit 2; ;;
esac
done
fi
function check_all()
{
##check tomcat
if [ ! -d $tomcat_dir ]; then
echo "This server is not installed tomcat ,Now try installing"
mkdir -p $base_dir
if [ -f $base_dir/*tomcat*.tar.gz ]; then
echo "This server already exists plase check version"
ls $base_dir/*tomcat*.tar.gz
exit
fi
cd $base_dir
http://mirrors.hust.edu.cn/apache/tomcat/tomcat-8/v8.0.53/bin/apache-tomcat-8.0.53.tar.gz
if [[ $? != 0 ]]; then
echo "Tomcat download failed"
exit
fi
sleep 5
tar -xvzf apache-tomcat-8.0.53.tar.gz
if [[ $? != 0 ]]; then
echo "Tomcat decompression failed"
exit
fi
mv apache-tomcat-8.0.53 tomcat
sed -i '163i <Valve className="org.apache.catalina.valves.RemoteIpValve"' $tomcat_dir/conf/server.xml
if [[ $? != 0 ]]; then
echo "insert server.html failed"
exit
fi
sed -i '164i remoteIpHeader="X-Forwarded-For"' $tomcat_dir/conf/server.xml
if [[ $? != 0 ]]; then
echo "insert server.html failed"
exit
fi
sed -i '165i protocolHeader="X-Forwarded-Proto"' $tomcat_dir/conf/server.xml
if [[ $? != 0 ]]; then
echo "insert server.html failed"
exit
fi
sed -i '166i protocolHeaderHttpsValue="https"/>' $tomcat_dir/conf/server.xml
if [[ $? != 0 ]]; then
echo "insert server.html failed"
exit
fi
sed -i '150i <Context path="" docBase="'${module}'" reloadable="true" />' $tomcat_dir/conf/server.xml
if [[ $? != 0 ]]; then
echo "insert server.html failed"
exit
fi
fi
##check port
for oneport in $server_port $http_port $ajp_port
do
reson=`netstat -lnp|grep $oneport`
if [ X"$reson" = X ]; then
echo "$oneport is OK"
else
echo "the $oneport is exists"
fi
done
}
function copy_tomcat()
{
if [ -d $base_dir/tomcat-$project-$module ]; then
echo "then $project $module is exists"
exit
fi
cd $base_dir
cp -r tomcat tomcat-$project-$module
cd tomcat-$project-$module/conf
sed -i s/8005/$server_port/g server.xml
if [[ $? != 0 ]]; then
echo "replace port failedd"
exit
fi
sed -i s/8080/$http_port/g server.xml
if [[ $? != 0 ]]; then
echo "replace port failedd"
exit
fi
sed -i s/8009/$ajp_port/g server.xml
if [[ $? != 0 ]]; then
echo "replace port failedd"
exit
fi
sed -i '150d' server.xml
if [[ $? != 0 ]]; then
echo "replace port failedd"
exit
fi
if [ $module != 'stats-job' ] && [ $module != 'kline-job' ] && [ $module != 'schedule-web' ]; then
sed -i '150i <Context path="" docBase="'${module}'" reloadable="true" />' $base_dir/tomcat-$project-$module/conf/server.xml
fi
if [[ $? != 0 ]]; then
echo "insert server.html failed"
exit
fi
chown -R work:work $base_dir/tomcat-$project-$module
}
function main()
{
check_all
copy_tomcat
}
main