#!/bin/bash
#
################################################################################
#
# $Name: xxx.sh
# $Version: xxx
# $Function: xxx
# $Author: xxxx
# $organization: xxxx
# $Create Date: xxx
# $Description: xxx
#
################################################################################
# Source function library.
. /etc/init.d/functions
function separator(){
for ((i=1;i<=100;i++))
do
echo -n "="
done
echo -e
}
separator
# Define node list
group_list="x.x.x.x"
# Define Shell Env
current=`xxx`
port="xxx"
prog="jxxxt"
prog_path="xxx"
package_name="xxx.war"
source_dir="${JENKINS_HOME}/jobs/${PROMOTED_JOB_NAME}/builds/${PROMOTED_NUMBER}/archive/xxx/target"
source_file="${PROMOTED_JOB_NAME}/builds/${PROMOTED_NUMBER}/archive/yuan-market-web/target/$package_name"
destination_dir="/data/xxx/"
logsprog="xxx"
destination_log_dir="/data/logs"
back_dir="/data/xxx/backup/backend/$prog"
back_prog="xxx"
back_time=`date +"%Y-%m-%d-%s"`
NOW=`date +"%Y/%m/%d %T"`
echo -e "$NOW: 后端 $prog 代码上线将开始 ... "
cat << EOF
------------------------------------------------------
|******** 开始部署测试环境后端 $prog ********|
------------------------------------------------------
EOF
separator
echo -e "测试环境后端 $prog 代码 $package_name 在 $source_dir 目录下."
usage(){
echo $"Usage: $0 deployment"
}
group_deployment(){
for node in $group_list;
do
separator
echo "$node 检测代码目录是否已存在 ... "
chk_dest_dir=$(salt $node file.directory_exists $destination_dir --out=json|awk 'NR==2 {print $NF}')
echo 检测代码目录 = $chk_dest_dir
if [[ $chk_dest_dir == 'true' ]]
then
echo "$node '$destination_dir' 目录已存在."
elif [[ $chk_back_dir == 'false' ]]
then
echo "$node '$destination_dir' 目录不存在,将新建代码目录 ... "
salt $node file.mkdir $destination_dir
else
echo "错误!现在退出脚本 …… "
exit 2
fi
echo "$node 检测备份目录是否已存在 ... "
chk_back_dir=$(salt $node file.directory_exists $back_dir --out=json|awk 'NR==2 {print $NF}')
echo 检测备份目录 = $chk_back_dir
if [[ $chk_back_dir == 'true' ]]
then
echo "$node '$back_dir' 备份目录已存在."
elif [[ $chk_back_dir == 'false' ]]
then
echo "$node '$back_dir' 备份目录不存在,将新建代码备份目录 ... "
salt $node file.mkdir $back_dir
else
echo "错误!现在退出脚本 …… "
exit 3
fi
echo "$node 检测 $prog 程序服务是否为可用 ... "
chk_java=$(salt $node service.available java-yuan-market --out=json|awk 'NR==2 {print $NF}')
echo 检测java = $chk_java
if [[ $chk_java == 'true' ]]
then
echo "$node $prog 服务可用并处于活跃状态,即将停止 $prog 服务 ... "
else [[ $chk_java == 'false' ]]
echo "$node $prog 服务不可用或不存在, 请检查 $prog 程序!"
echo "错误!现在退出 …… "
exit 4
fi
echo "开始停止 $prog 服务 ... "
salt $node service.stop java-yuan-market
java_status=$(salt $node service.status java-yuan-market --out=json|awk 'NR==2 {print $NF}')
echo java 服务状态 = $java_status
if [[ $java_status == 'false' ]]
then
echo "$node $prog 服务停止成功."
else [[ $java_status == 'true' ]]
echo "$node $prog 服务停止失败!"
echo "错误!现在退出脚本 …… "
exit 5
fi
echo "$node 删除之前版本 '$destination_dir/$package_name' 文件 ... "
salt $node file.remove $destination_dir/$package_name
echo -e "当前将推送版本目录 '$source_dir'"
echo -e "当前将推送版本文件 ' …… $source_file'"
echo -e "目标目录 '$destination_dir'"
echo -e "开始拷贝推送 $prog $package_name 到 $node ... "
salt $node cp.get_file salt://$source_file $destination_dir/$package_name
chk_copy=$(salt $node file.file_exists $destination_dir/$package_name --out=json|awk 'NR==2 {print $NF}')
echo 验证推送文件 = $chk_copy
if [[ $chk_copy == 'true' ]]
then
echo "推送 $prog $package_name 到 $node 成功."
else [[ $chk_copy == 'false' ]]
echo "拷贝 $prog $package_name 到 $node 失败!"
echo "错误!现在退出脚本 …… "
exit 6
fi
echo -e "$node 开始备份当前 $prog 代码 ... "
salt $node cmd.run "cd $destination_dir && zip -rq ${back_prog}.${back_time}.zip ./$prog && mv -v $destination_dir/${back_prog}.${back_time}.zip $back_dir"
echo -e "$node 开始部署 $prog 代码 ... "
salt $node cmd.run "cd $destination_dir && rm -rf $prog/*"
salt $node cmd.run "cp -v $destination_dir/$package_name $destination_dir/$prog && chown -R tomcat:root $destination_dir/$prog"
echo "$node 代码部署完成,开始启动 $prog 服务 ... "
salt $node service.start java-yuan-market
java_status=$(salt $node service.status java-yuan-market --out=json|awk 'NR==2 {print $NF}')
echo 服务状态 $java_status
if [[ $java_status == 'true' ]]
then
echo "$node $prog 服务启动成功."
else [[ $java_status == 'false' ]]
echo "$node $prog 服务启动失败!"
echo "错误!退出当前脚本 …… "
exit 7
fi
NOW=`date +"%Y/%m/%d %T"`
echo "$NOW: $node 代码部署完成,程序服务启动成功,开始检查应用测试页 ... "
for n in $(seq 10)
do
sleep 15
echo "检查 'http://${node}:${port}/$prog_path/index.html' http 状态码 ... "
curl_status=`curl --max-time 1 -o /dev/null -s -w %{http_code} http://${node}:${port}/$prog_path/index.html`
echo "http 状态码 $curl_status ."
separator
salt $node cmd.run "tail -n 200 ${destination_log_dir}/${logsprog}/ssf-json.log"
if [[ ${curl_status} -eq 404 ]]
then
separator
echo "$node 后端 $prog 应用启动失败,http 状态码 :${curl_status},请查看 ${destination_log_dir}/${logsprog} 日志文件 !"
exit 8
fi
if [[ ${curl_status} -eq 200 ]]
then
separator
echo "$node 后端 $prog 应用启动成功."
break
fi
NOW=`date +"%Y/%m/%d %T"`
echo "$NOW: 完成第 $n 次应用测试页检查 ... "
done
if [[ ${curl_status} -eq 200 ]]
then
continue
fi
separator
echo "$node 后端 $prog 应用启动失败,http 状态码 :${curl_status},请查看 ${destination_log_dir}/${logsprog} 日志文件!!!"
exit 9
done
}
main(){
deployment_method=$1
case $deployment_method in
deployment)
group_deployment;
;;
*)
usage;
esac
}
main $1
separator
NOW=`date +"%Y/%m/%d %T"`
echo -e "$NOW: 后端 $prog 代码部署上线任务完成."
separator