Jenkins 灰度
Jenkins 灰度
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#!/bin/bash DATE=`date +%Y-%m-%d_%H-%M-%S` METHOD=$1 BRANCH=$2 GROUP_LIST=$3 function IP_list(){ if [[ ${GROUP_LIST} == "GROUP1" ]];then Server_IP="192.168.88.12" echo ${Server_IP} #ssh sdn@192.168.88.12 ""echo disable server linux38-web-80/192.168.88.13" |sudo socat stdio /run/haproxy/admin.sock" elif [[ ${GROUP_LIST} == "GROUP2" ]];then Server_IP="192.168.88.13" echo ${Server_IP} #ssh sdn@192.168.88.12 ""echo enable server linux38-web-80/192.168.88.12" |sudo socat stdio /run/haproxy/admin.sock" elif [[ ${GROUP_LIST} == "GROUP3" ]];then Server_IP="192.168.88.12 192.168.88.13" echo ${Server_IP} fi } function clone_code(){ rm -rf /var/lib/jenkins/workspace/test cd /var/lib/jenkins/workspace/ git clone -b ${BRANCH} git@192.168.88.10:root/test.git } function scanner_code(){ echo "cd /var/lib/jenkins/workspace/test && /usr/local/sonarscanner/bin/sonar-scanner" } function make_zip(){ cd /var/lib/jenkins/workspace/test && zip -r app1.zip ./* } function node_down(){ for node in ${Server_IP};do ssh sdn@192.168.88.12 "echo "disable server linux38-web-80/${node}" |sudo socat stdio /run/haproxy/admin.sock" echo "${node} 从负载均衡下线成功" done } function scp_zipfile(){ for node in ${Server_IP};do scp /var/lib/jenkins/workspace/test/app1.zip sdn@${node}:/data/tomcat/tomcat_appdir/app1-${DATE}.zip ssh sdn@${node} "unzip /data/tomcat/tomcat_appdir/app1-${DATE}.zip -d /data/tomcat/tomcat_webdir/app1-${DATE} && sudo rm -rf /var/lib/tomcat8/webapps/ROOT && sudo ln -sv /data/tomcat/tomcat_webdir/app1-${DATE}/ROOT /var/lib/tomcat8/webapps/" done } function stop_tomcat(){ for node in ${Server_IP};do echo "${node} 开始停止tomcat" ssh sdn@${node} "sudo /etc/init.d/tomcat8 stop" echo "${node} tomcat停止成功" done } function start_tomcat(){ for node in ${Server_IP};do ssh sdn@${node} "sudo /etc/init.d/tomcat8 start" #sleep 5 done } function web_test(){ sleep 20 for node in ${Server_IP};do NUM=`curl -s -I -m 10 -o /dev/null -w %{http_code} http://${node}:8080/index.html` if [[ ${NUM} -eq 200 ]];then echo "${node} 测试通过,即将添加到负载" add_node ${node} else echo "${node} 测试失败,请检查该服务器是否成功启动tomcat" fi done } function add_node(){ node=$1 ssh sdn@192.168.88.12 ""echo enable server linux38-web-80/${node}" |sudo socat stdio /run/haproxy/admin.sock" echo "${node} 部署完毕,请进行代码测试!" # echo ${node},"----->" # if [[ ${GROUP_LIST} == "online-all" ]];then # ssh sdn@192.168.88.12 ""echo enable server myapp/192.168.88.13" | socat stdio /run/haproxy/admin.sock" #ssh root@172.31.0.104 ""echo enable server myapp/172.31.0.105" | socat stdio /run/haproxy/admin.sock" # fi ########################################## # if [ ${node} == "192.168.88.13" ];then # echo "192.168.88.12 部署完毕,请进行代码测试!" # else # ssh root@192.168.88.12 ""echo enable server linux38-web-80/${node}" | socat stdio /run/haproxy/admin.sock" #ssh root@172.31.0.104 ""echo enable server linux38-web-80/${node}" | socat stdio /run/haproxy/admin.sock" # fi } function rollback_last_version(){ for node in ${Server_IP};do NOW_VERSION=`ssh sdn@${node} ""ls -l -rt /var/lib/tomcat8/webapps/ | awk -F"->" '{print $2}' | tail -n1|awk -F'ROOT' '{print $1}'""` NOW_VERSION=`basename ${NOW_VERSION}` echo $NOW_VERSION NAME=`ssh sdn@${node} ""ls -l -rt /data/tomcat/tomcat_webdir/ | grep -B 1 ${NOW_VERSION} | head -n1 | awk '{print $9}'""` ssh sdn@${node} "sudo rm -rf /var/lib/tomcat8/webapps/ROOT && sudo ln -sv /data/tomcat/tomcat_webdir/${NAME}/ROOT /var/lib/tomcat8/webapps/" done } main(){ case $1 in deploy) IP_list; clone_code; scanner_code; make_zip; node_down; stop_tomcat; scp_zipfile; start_tomcat; web_test; ;; rollback_last_version) IP_list; echo ${Server_IP} node_down; stop_tomcat; rollback_last_version; start_tomcat; web_test; ;; esac } main $1 $2 $3