shell 实现war包的配置更新和自动发布

Posted xiao_pai_pai

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell 实现war包的配置更新和自动发布相关的知识,希望对你有一定的参考价值。

此脚本主要用来实现非maven tomcat项目的war包手动发布,

1、将测试war包上传至指定目录

2、备份目前生产代码

3、自动配置文件替换

4、新版本代码的发布

#!/bin/bash

############Setup basic path and configuration file name #########################
date=`date +%Y%m%d`
tomcat_home=/usr/local/kencery/tomcat
app_name=bqjr_fss
backup_home=~/appbak
app_home=$tomcat_home/webapps/$app_name
upload_war_home=/home/bqadm/deploy_directory
declare -a config_file=(WEB-INF/classes/dbconfig.properties WEB-INF/classes/global.properties WEB-INF/classes/mq.properties WEB-INF/classes/sso.properties WEB-INF/classes/config.properties WEB-INF/classes/log4j.properties WEB-INF/classes/quartz.properties WEB-INF/classes/uncheckedspxinfotemplate.xlsx )

#config_file主要用于配置需要替换的配置文件;

#################################################################################
process_check(){
count=`ps -ef |grep $1 |grep -v "grep" |wc -l`
if [ $count -eq 1 ];then
id=`ps -ef|grep $1 |grep -v "grep"|awk ‘{print $2}‘`
echo -e "$1 is running,and PID is $id.\n"
elif [ $count -eq 0 ]; then
echo -e "$1 is Not running , start later\n"
return 41
fi
}

tomcat_stop(){
log_directory=`pwd`
echo -e "Start to stop tomcat:\n"
/bin/sh /$1/bin/shutdown.sh >> $log_directory/stop.log
i=0
while [ `ps -ef|grep $1|grep -v grep|wc -l` -eq 1 ]; do
echo -e "..........................\n"
if [ $(($i+1)) -gt 3 ]; then
echo "Stop tomcat failed,Kill tomcat\n"
PID=`ps -ef|grep $1|grep -v grep|awk ‘{print $2}‘`
kill -9 $PID
break
fi
sleep 10
done
if [ `ps -ef|grep $1|grep -v grep|wc -l` -eq 0 ]; then
echo -e "Success to stop `basename $1`: \033[41;36m [OK] \033[0m\n"
else
echo -e "Failed to stop `basename $1`: \033[41;36m [Faild] \033[0m\n"
fi
}

tomcat_start(){
log_directory=`pwd`
echo -e "Start to start tomcat:\n"
echo -e "..........................\n"
/bin/sh /$1/bin/startup.sh >> $log_directory/start.log
rm -rf $1/webapps/*
cp -r $upload_war_home/$app_name.war $1/webapps/
j=0
while [ `ps -ef|grep $tomcat_home|grep -v grep|wc -l` -ne 1 ]; do
if [ $(($j+1)) -gt 3 ]; then
echo "Start failed,please check log\n";break
fi
sleep 10
done
if [ `ps -ef|grep $1|grep -v grep|wc -l` -eq 1 ]; then
echo -e "Success to start `basename $1`: \033[41;36m [OK] \033[0m\n"
else
echo -e "Failed to start `basename $1`: \033[41;36m [Faild] \033[0m\n";exit 33
fi
}
##########Backup && War package upload#############################################
if [ -d $bak_$date/$app_name ];then
echo -e "Backuped, No more backup.\n"
else
mkdir -p $backup_home/bak_$date
scp -r $app_home $backup_home/bak_$date/
ls -lsh $backup_home/bak_$date
echo -e "Backup was done, Start to check war package.\n"
fi

a=y
#read -p "Please ensure that war package has been uploaded:(y/n):" a
cd ..
cd $upload_war_home
if [ $a == ‘y‘ ] ;then
if [ -f $app_name.war ]; then
echo -e "War package was uploaded.Will replace the configuration file.\n"
else
echo -e "No War package,Please upload the war package to $upload_war_home.\n"; exit 0
fi
else
echo "Choice is NO. War package hasnot been uploaded,Please upload the war file to $upload_war_home."; exit 0

fi

###########Replace configuration file#######################################################################

echo -e "Start to replace confige file"
mkdir -p $upload_war_home
for var in ${config_file[@]};
do
cd $app_home
cp --parents $var $upload_war_home
done
cd $upload_war_home
for var in ${config_file[@]};
do
jar -uvf $app_name.war $var
done

############Application deploy##################################################################################
process_check `basename $tomcat_home`
if [ $? -eq 41 ]; then
tomcat_start $tomcat_home
else
tomcat_stop $tomcat_home
tomcat_start $tomcat_home
fi































































































以上是关于shell 实现war包的配置更新和自动发布的主要内容,如果未能解决你的问题,请参考以下文章

配置jenkins的shell自动打包的脚本

java+maven工程 实现 自动对war包进行复制并修改和替换每个的配置文件

Nginx和Tomcat配合实现Java Web服务热部署

shell脚本

jar包和war包的介绍和区别

理解:jar包和war包的区别