Shell script to automatically deploy Jenkins

Note: This script is used to share Shell scripts, because of different circumstances, it can not be directly used in other environments with deployment.

! # / bin / bash
# Time: July 29, 2019
# Author: Shaw Hill
# micro letter: helpxiaoshan
# Uses: For XXXX UAT environment ATG mall project Jenkins automatic deployment use.

############ Caution: ################################## ###############################################

Note 1: The need to revise the target host / etc / ssh / sshd_config, MaxSessions 50 (recommended value), reload sshd service configuration.

Note 2: The script must follow the parameters, the parameters for the project name, for example: sh deploy.sh store

Note that the line 3:50, sleep 15 seconds, the background process 20 x 15 seconds = 300s, the detection time of a total of 300 seconds, when the start time tomcat items of more than 300 seconds, this value is increased.

############################################################################################################

= startTime date +'%Y-%m-%d %H:%M:%S'
project_name = $ 1 # first parameter is the project name

[$ 1] || echo "ERROR :! No project_name" # judge $ 1 is empty, then exit the script yet!
[$ 1] || exit 1

if [ $project_name = "store" ];then
#第一步,停掉目标项目进程。
ip=192.168.220.34
echo "INFO: project_name is store"
echo -e "\n"
echo "INFO: Begain stop store..."
ssh -n -l oracle $ip "pgrep -f -l store | grep -v 'tail -f' | grep -v grep | cut -d' ' -f1 | xargs kill -9" >>/dev/null 2>&1
stat=$?
#echo $stat
[ $stat -eq 0 ] && echo "INFO: store is been killed"
[ $stat -ne 0 ] && echo "INFO: store is not started"

#第二步,删除旧代码并清除缓存。
echo -e "\n"
echo "INFO: Begain delete old file and cache..."
ssh -n -l oracle $ip "rm -rf /home/oracle/Oracle/Middleware/Oracle_Home/user_projects/domains/base_domain/servers/prod_lockserver/stage/prod_lockserver.ear/prod_lockserver.ear"
ssh -n -l oracle $ip "rm -rf /home/oracle/Oracle/Middleware/Oracle_Home/user_projects/domains/base_domain/servers/prod_lockserver/tmp"
ssh -n -l oracle $ip "rm -rf /home/oracle/Oracle/Middleware/Oracle_Home/user_projects/domains/base_domain/servers/prod_lockserver/cache"
echo "INFO: already delete old file and cache"

#第三步,拷贝最新代码到项目路径下。
echo -e "\n"
echo "INFO: Begain copy code to project host..."
scp -qrp /file/uat_ears/prod_lockserver.ear oracle@$ip:/home/oracle/Oracle/Middleware/Oracle_Home/user_projects/domains/base_domain/servers/prod_lockserver/stage/prod_lockserver.ear
[ $stat -eq 0 ] && echo "INFO: copy Success"
[ $stat -ne 0 ] && echo "ERROR: copy failed!!!" && exit 1

#第四步,启动项目进程
echo -e "\n"
echo "INFO: Begain start store Weblogic"
ssh -n -l oracle $ip "nohup /home/oracle/Oracle/Middleware/Oracle_Home/user_projects/domains/base_domain/bin/startManagedWebLogic.sh prod_lockserver http://192.168.220.35:7001 -Djava.rmi.server.hostname=192.168.220.34 -Dguangda.merchant.parameter.file.path=/file/cebbank/ceb_merchant.properties -Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,address=8466,server=y,suspend=n -Djava.compiler=NONE -Datg.dynamo.server.name=prod_lockserver -Xms6G -Xmx6G -XX:PermSize=512m -XX:MaxPermSize=512m -XX:+UseG1GC -XX:MaxGCPauseMillis=1000 -XX:InitiatingHeapOccupancyPercent=45 -XX:ConcGCThreads=4 -XX:G1ReservePercent=15 -XX:+DisableExplicitGC -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:+PrintGCCause -XX:+PrintGCDateStamps -XX:+PrintHeapAtGC -XX:+PrintGCApplicationStoppedTime -Xloggc:/home/oracle/store_gc.log -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/home/oracle/store_heap.bin -Dcom.sun.management.jmxremote.port=12345 -Djava.rmi.server.hostname=192.168.220.34 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djavax.management.builder.initial=weblogic.management.jmx.mbeanserver.WLSMBeanServerBuilder -Datg.dynamo.data-dir=/home/oracle/ATG-Data/ > /home/oracle/output/logs/prod_lockserver.log &"
echo "INFO: Weblogic starting..."

#第五步,启动精确检测weblogic启动完毕的后台进程
echo -e "\n"
>${project_name}_${ip}_status.log  #清空检测Weblogic是否启动的日志文件内容
for i in `seq 20`    #启动10个检测Weblogic启动日志后台进程,当后台进程被杀掉后,日志才会被写入log文件
do
    ssh -n -l oracle $ip "tail -fn 0 /home/oracle/output/logs/prod_lockserver.log" | grep "The server started in RUNNING mode" >>${project_name}_${ip}_status.log &
done

#检测Weblogic启动状态的变量stats1,检测机制为检测目标服务器上的项目日志文件,是否新打印出"The server started in RUNNING mode";
t=0
stats1=0
while [ $stats1 -eq 0 ]
do
    sleep 15   # 10个后台进程,20x15=300s,检测时间共300秒,当有项目的tomcat启动时间超过100秒,则增大该数值。
    let t+=15  #t=`expr $t + 10`
    echo "检测 $project_name Weblogic 是否启动完毕,已用时 $t 秒..."
    echo -e "\n"
    ssh -n -l oracle $ip "pgrep -f 'tail -fn' | head -n 1 | xargs kill -9"  # kill掉一个后台进程
    [ $? -eq 0 ] && echo "INFO: 成功杀掉 tail -fn 后台进程。"
    stats1=`cat ${project_name}_${ip}_status.log | wc -l `   # 每kill一个后台进程,状态文件就更新一次
    #[ $stats1 -eq 0 ] && echo "INFO:未检测到 started in RUNNING mode 关键词。"
    #[ $stats1 -ge 1 ] && echo "INFO:成功检测到 started in RUNNING mode 关键词。"
done

ssh -n -l oracle $ip "pgrep -f 'tail -fn' | xargs kill -9" # kill掉剩余无用后台进程。
echo -e "\n"
echo "INFO:检测到启动关键词:"
head -n 1 ${project_name}_${ip}_status.log  #  输出抓取的 started in RUNNING mode 日志。
echo -e "\n"
echo "打印最后864行日志"
ssh -n -l oracle $ip "tail -n 864 /home/oracle/output/logs/prod_lockserver.log"

be

# Script calculated total time
the endtime = date +'%Y-%m-%d %H:%M:%S'
start_seconds = $ (DATE --date = "$ startTime" +% S);
; end_seconds = $ (DATE --date = "$ the endtime" +% S)
when echo "with this update : "$ ((end_seconds-start_seconds ))" s "

Guess you like

Origin blog.51cto.com/helpxiaoshan/2424619