#!/bin/bash DATE=`date +%Y-%m-%d_%H-%M-%S` METHOD=$1 BRANCH=$2 GROUP_LIST=$3 function IP_list(){ if [[ ${GROUP_LIST} == "online-group1" ]];then Server_IP="192.168.7.105" echo ${Server_IP} elif [[ ${GROUP_LIST} == "online-group2" ]];then Server_IP="192.168.7.106" echo ${Server_IP} ssh [email protected]192.168.7.103 ""echo enable server myapp/192.168.7.105" | socat stdio /run/haproxy/admin.sock" ssh [email protected]192.168.7.104 ""echo enable server myapp/192.168.7.105" | socat stdio /run/haproxy/admin.sock" elif [[ ${GROUP_LIST} == "online-all" ]];then Server_IP="192.168.7.105 192.168.7.106" echo ${Server_IP} fi } function clone_code(){ rm -rf /data/git/linux37/web1 cd /data/git/linux37 git clone -b ${BRANCH} [email protected]192.168.7.101:linux37/web1.git } function scanner_code(){ cd /data/git/linux37/web1 && /usr/local/sonar-scanner/bin/sonar-scanner } function make_zip(){ cd /data/git/linux37/web1 && zip -r code.zip ./* } down_node(){ for node in ${Server_IP};do ssh [email protected]2.168.7.103 "echo "disable server myapp/${node}" | socat stdio /run/haproxy/admin.sock" echo "${node} 在负载均衡192.168.7.103下线成功" ssh [email protected] "echo "disable server myapp/${node}" | socat stdio /run/haproxy/admin.sock" echo "${node} 在负载均衡192.168.7.104下线成功" done } function stop_tomcat(){ for node in ${Server_IP};do ssh [email protected]${node} "/etc/init.d/tomcat stop" done } function start_tomcat(){ for node in ${Server_IP};do ssh [email protected]${node} "/etc/init.d/tomcat start" #sleep 5 done } function scp_zipfile(){ for node in ${Server_IP};do scp /data/git/linux37/web1/code.zip [email protected]${node}:/data/tomcat/tomcat_appdir/code-${DATE}.zip ssh [email protected]${node} "unzip /data/tomcat/tomcat_appdir/code-${DATE}.zip -d /data/tomcat/tomcat_webdir/code-${DATE} && rm -rf /data/tomcat/tomcat_webapps/myapp && ln -sv /data/tomcat/tomcat_webdir/code-${DATE} /data/tomcat/tomcat_webapps/myapp" 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/myapp/index.html` if [[ ${NUM} -eq 200 ]];then echo "${node} 测试通过,即将添加到负载" add_node ${node} else echo "${node} 测试失败,请检查该服务器是否成功启动tomcat" fi done } function add_node(){ node=$1 echo ${node},"----->" if [[ ${GROUP_LIST} == "online-all" ]];then ssh [email protected] ""echo enable server myapp/192.168.7.105" | socat stdio /run/haproxy/admin.sock" ssh [email protected] ""echo enable server myapp/192.168.7.105" | socat stdio /run/haproxy/admin.sock" fi ########################################## if [ ${node} == "192.168.7.105" ];then echo "192.168.7.105 部署完毕,请进行代码测试!" else ssh [email protected] ""echo enable server myapp/${node}" | socat stdio /run/haproxy/admin.sock" ssh [email protected] ""echo enable server myapp/${node}" | socat stdio /run/haproxy/admin.sock" fi } function rollback_last_version(){ for node in ${Server_IP};do NOW_VERSION=`ssh [email protected]${node} ""/bin/ls -l -rt /data/tomcat/tomcat_webapps/ | awk -F"->" ‘{print $2}‘ | tail -n1""` NOW_VERSION=`basename ${NOW_VERSION}` echo $NOW_VERSIONG NAME=`ssh [email protected]${node} ""ls -l -rt /data/tomcat/tomcat_webdir/ | grep -B 1 ${NOW_VERSION}| head -n 1| awk ‘{print $9}‘""` ssh [email protected]${node} "rm -rf /data/tomcat/tomcat_webapps/myapp && ln -sv /data/tomcat/tomcat_webdir/${NAME} /data/tomcat/tomcat_webapps/myapp" echo "${node} 回滚至${NAME}成功" done } main(){ case $1 in deploy) IP_list; clone_code; scanner_code; make_zip; down_node; stop_tomcat; scp_zipfile; start_tomcat; web_test; ;; rollback_last_version) IP_list; echo ${Server_IP} down_node; stop_tomcat; rollback_last_version; start_tomcat; web_test; ;; esac } main $1 $2 $3
原文地址:https://www.cnblogs.com/maxuebin/p/11623546.html
时间: 2024-10-04 02:17:51