Linux通过shell执行自动化部署

时间:2023-10-01 10:52:50

背景

  通过shell判断是否存在补丁更新,进行自动化的部署

代码

 #!/bin/sh
#Edit:何彦霆
#version:1.0. beta #执行环境初始化
source /hxspace/production/bin/setenv.sh #变量说明
#$-外部提供,日期补丁(格式要求BD_2017--)
#Paths_Way-补丁存放目录
#Tomcat_Way-tomcat中间件目录
#Tomcat_Start_Way-tomcat中间件启动文件位置(加载so存在该限制,不能直接使用工程下的启动文件)
#Env_Wabapp_Way-web应用目录
#Env_Bus_Way-BUS工程目录 BD_Data=$
Paths_Way="/mnt/patches"
Tomcat_Way="/hxspace/production/server/apache-tomcat-8.5.9"
Tomcat_Start_Way="/hxspace/production/bin"
Env_Wabapp_Way="/hxspace/production/webroot"
Env_Bus_Way="/hxspace/production/bus" function Env_WEB_updata()
{
WEB_name=$
WEB_way=$ #判断Mall_WEB组件更新
if [ -d ./${WEB_name} ];then echo "存在${WEB_name}补丁,准备进行${WEB_name}环境更新" #down掉服务器tomcat
sh ${Tomcat_Way}/bin/shutdown.sh
sleep 5s #清空部署启动目录下,WEB应用文件
mkdir -p ${Env_Wabapp_Way}/${WEB_way}
rm -rf ${Env_Wabapp_Way}/${WEB_way}/*
cp -rf ${Paths_Way}/${BD_Data}/${WEB_name}/* ${Env_Wabapp_Way}/${WEB_way} #启动WEB
sh ${Tomcat_Start_Way}/tomcat_run.sh
echo "Mall_WEB环境更新完成" else echo "${WEB_name}更新补丁不存在,${WEB_name}更新结束" fi } function Env_BUS_updata()
{
BUS_name=$1
BUS_port=$2
BUS_way=$3
echo ${BUS_name}
echo ${BUS_port}
echo ${BUS_way}
#判断组件更新
#if [ -d ./${BUS_name}* ];then
result=`ls -l | grep ${BUS_name}*`
#if [[ "$result" != "" ]];then
if [ -d ./${result} ];then echo "存在${BUS_name}补丁,准备进行${BUS_name}环境更新" #判断Server 进程是否存在 - centos 使用判断监控PID
Sc_Pid=`netstat -nlp | grep ${BUS_port} | awk '{print $7}' | awk -F "/" '{print $1}'` #判断20025端口是否被监听,如果正在监听,kill
if [ ! $Sc_Pid ]; then echo "IS NULL" else echo "NOT NULL"
kill -9 $Sc_Pid fi #拷贝壳子进行覆盖
mkdir -p ${Env_Bus_Way}/${BUS_way}
cp -rf './TBAServer(2.1.9.4)'/* ${Env_Bus_Way}/${BUS_way} if [ -d ./${BUS_name}_conf ];then echo "${BUS_name}_conf存在补丁,准备进行环境更新" #清空部署启动目录下,Collect_bus_conf文件
rm -rf ${Env_Bus_Way}/${BUS_way}/config.ini
rm -rf ${Env_Bus_Way}/${BUS_way}/classes/*
rm -rf ${Env_Bus_Way}/${BUS_way}/*.so mkdir -p ${Env_Bus_Way}/${BUS_way}
cp -rf ${Paths_Way}/${BD_Data}/${BUS_name}_conf/* ${Env_Bus_Way}/${BUS_way} #启动bus-采用重启
sh ${Env_Bus_Way}/${BUS_way}/restart.sh
echo "${BUS_name}_conf补丁,更新完成" else echo "${BUS_name}_conf补丁不存在" fi if [ -d ./${BUS_name}_lib ];then echo "${BUS_name}_lib存在补丁,准备进行环境更新" #清空部署启动目录下,${BUS_name}_lib文件
mkdir -p ${Env_Bus_Way}/${BUS_way}/lib
rm -rf ${Env_Bus_Way}/${BUS_way}/lib/* cp -rf ${Paths_Way}/${BD_Data}/${BUS_name}_lib/* ${Env_Bus_Way}/${BUS_way}/lib #启动bus-采用重启
sh ${Env_Bus_Way}/${BUS_way}/restart.sh
echo "${BUS_name}_lib补丁,更新完成" else echo "${BUS_name}_lib补丁不存在" fi if [ -d ./${BUS_name}_classes ];then echo "${BUS_name}_classes存在补丁,准备进行环境更新" #清空部署启动目录下,${BUS_name}_classes文件
#工程classes文件同配置文件classes同目录,才用覆盖的方式
mkdir -p ${Env_Bus_Way}/${BUS_way}/classes cp -rf ${Paths_Way}/${BD_Data}/${BUS_name}_classes/* ${Env_Bus_Way}/${BUS_way}/classes #启动bus-采用重启
sh ${Env_Bus_Way}/${BUS_way}/restart.sh
echo "${BUS_name}_classes补丁,更新完成" else echo "${BUS_name}_classes补丁不存在" fi else
echo "${BUS_name}无补丁"
fi
} #切换至补丁目录
cd ${Paths_Way} #判断补丁目录下是否存在指定补丁
if [ -d ./$BD_Data ];then
echo "存在该补丁,准备进行环境更新"
cd ./$BD_Data
# BUS 更新,$1 - 补丁文件名 $2 - BUS监听端口 $3 BUS部署路径
Env_BUS_updata HX_ChenDu_LC_Mall_bus_test 20025 mall_bus Env_BUS_updata HX_ChenDu_LC_MallTimer_bus_test 20032 mall_timer Env_BUS_updata HX_ChenDu_pmc_bus_test 20028 pmc_bus Env_BUS_updata HX_ChenDu_pmc_Collect_bus_test 20042 collector_bus #WEB更新,$1 - 补丁文件名 $2 - WEB部署路径
Env_WEB_updata HX_ChenDu_LC_Mall_WEB_Linux_Test mall #Env_WEB_updata HX_ChenDu_pmc_Collect_bus_test mkt else
echo "补丁不存在,更新结束"
exit 0
fi

自动通过目录标示判断是否存在该模块补丁,进行补丁的移动合并和更新