#!/bin/bash
#nohup bash check_new_pkgs_dev.sh &
#steps below:
##发布的机器上运行这个脚本
#定时遍历发布包存放路径
#1.遍历所有要发布的包
#2.对于每一个包,找到这个包的时候,停止相应的服务
#3.将deployed下面相应的已发布目录归档到history中
#4.将压缩包解压到deployed下面相应的项目目录中
#5.将压缩包移到history中归档
#6.启动相应服务
###注意进程只能启一个,!!!!!
##NOTE 发布zjzjc-voting的各个环境,单机版,zjdev环境之外的需要修改各dir从zjdev改到相应的环境
##取脚本名字
PROGNAME=`basename $0`
pacBaseNameArr=(zjzc-web-flow zjzc-web-api zjzc-pay zjzc-web-frontEnd zjzc-web-backoffice zjzc-voting-front zjzc-voting zjzc-wze-proxy zjzc-hy-core zjzc-hy-pre zjzc-hy-admin-pre zjzc-backoffice-sso-server zjzc-hy-front zjzc-hy-web-admin zjzc-message-proxy)
serviceNameArr=(tomcat8081 tomcat8082 tomcat8083 no no no tomcat8080 tomcat8084 tomcat8085 tomcat8086 tomcat8087 tomcat8088 no no tomcat8089)
tomcatPathKeyword=(8081 8082 8083 no no no 8080 8084 8085 8086 8087 8088 no no 8089)
##历史发布目录
UPLOAD_DIR=/t/deploy/zjdev/deployableApps
##应用部署的目录
DEPLOYED_DIR=/t/deploy/zjdev/deployedApps
##历史应用目录
HISTORY_FOLDER=/t/deploy/zjdev/deployableApps/history/folder
##历史的war包
HISTORY_WAR=/t/deploy/zjdev/deployableApps/history/war
if [ -d "$UPLOAD_DIR" ]
then
:
else
mkdir -p $UPLOAD_DIR
fi
if [ -d "$DEPLOYED_DIR" ]
then
:
else
mkdir -p $DEPLOYED_DIR
fi
if [ -d "$HISTORY_FOLDER" ]
then
:
else
mkdir -p $HISTORY_FOLDER
fi
if [ -d "$HISTORY_WAR" ]
then
:
else
mkdir -p $HISTORY_WAR
fi
##pacBaseNameArr=(zjzc-web-flow zjzc-web-api zjzc-pay zjzc-web-frontEnd zjzc-web-backoffice zjzc-voting-front zjzc-voting zjzc-wze-proxy zjzc-hy-core zjzc-hy-pre zjzc-hy-admin-pre zjzc-backoffice-sso-server zjzc-hy-front zjzc-hy-web-admin zjzc-message-proxy)
##pacBaseNameArr 数组长度
appArrLen=${#pacBaseNameArr[*]}
while true
do
DATETIME=`date +%Y''%m''%d'-'%H''%M''%S`
### check if there is new war at UPLOAD_DIR ###
#ls -l $UPLOAD_DIR/${APPNAME}*.war >/dev/null 2>&1
#if [ $? -eq "0" ]
#then
for((i=0;i<"$appArrLen";i=i+1))
do
##UPLOAD_DIR=/t/deploy/zjdev/deployableApps
UPLOAD_DIR_WAR_COUNT=`ls -l $UPLOAD_DIR | grep -E "${pacBaseNameArr[$i]}.*.(rar|zip|war)$" | wc -l`
if [ $UPLOAD_DIR_WAR_COUNT -ne "1" ]
then
echo "$DATETIME ${pacBaseNameArr[$i]} has too many pkgs or no pkg, please check it, now continue! " >>/var/log/${PROGNAME}.out
continue
fi
###no need to restart service for static web
###
##serviceNameArr=(tomcat8081 tomcat8082 tomcat8083 no no no tomcat8080 tomcat8084 tomcat8085 tomcat8086 tomcat8087 tomcat8088 no no tomcat8089)
##静态资源不重启,tomcat重启
if [ ${serviceNameArr[$i]} != "no" ]
then
###stop service
service ${serviceNameArr[$i]} stop
##每个应用的进程
procIds=`ps -ef | grep -E java.*${tomcatPathKeyword[$i]} | awk '{printf $2 " "}'`
procNums=`echo $procIds | wc -w`
if [ $procNums -gt "1" ]
then
echo "service with keyword: ${tomcatPathKeyword[i]} still has process ids: $procIds survived, kill them after 10 seconds"
sleep 10
kill $procIds
fi
echo "service ${serviceNameArr[$i]} stopped"
fi
echo "echo UPLOAD_DIR_WAR_COUNT:$UPLOAD_DIR_WAR_COUNT at $DATETIME for ${pacBaseNameArr[$i]}";
### move current tomcat to HISTORY_FOLDER
##DEPLOYED_DIR=/t/deploy/zjdev/deployedApps
##移动当前应用目录 到历史目录下HISTORY_FOLDER=/t/deploy/zjdev/deployableApps/history/folder
##HISTORY_FOLDER=/t/deploy/zjdev/deployableApps/history/folder
mv $DEPLOYED_DIR/${pacBaseNameArr[$i]} $HISTORY_FOLDER/${pacBaseNameArr[$i]}_$DATETIME
##UPLOAD_DIR=/t/deploy/zjdev/deployableApps
##取应用的名字zjzc-web-api-1.1.17.war_20160708-151024
NEW_WAR_NAME=`ls -l $UPLOAD_DIR/${pacBaseNameArr[$i]}*.* | awk -F"/" '{ print $NF }'`
### unzip new war
##应用目录,判断backoffice,后面添加/backoffice
subfolder="${pacBaseNameArr[$i]}";
if [ ${pacBaseNameArr[$i]} == "zjzc-web-backoffice" ]
then
subfolder="$subfolder/backoffice"
fi
##DEPLOYED_DIR=/t/deploy/zjdev/deployedApps
mkdir -p $DEPLOYED_DIR/$subfolder
##把打包好的包,解压到对应目录
unzip -o $UPLOAD_DIR/$NEW_WAR_NAME -d $DEPLOYED_DIR/$subfolder
### mov new war to HISTORY_WAR
##UPLOAD_DIR=/t/deploy/zjdev/deployableApps
##把打包好的包移走到/t/deploy/zjdev/deployableApps/history/war
##HISTORY_WAR=/t/deploy/zjdev/deployableApps/history/war
mv $UPLOAD_DIR/$NEW_WAR_NAME $HISTORY_WAR/${NEW_WAR_NAME}_$DATETIME
###no need to restart service for static web
if [ ${serviceNameArr[$i]} != "no" ]
then
###start service
service ${serviceNameArr[$i]} start
echo "service started"
fi
done
#fi
sleep 10
done