码迷,mamicode.com
首页 > 其他好文 > 详细

jenkins之代码部署回滚脚本

时间:2019-10-05 00:37:16      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:admin   stop   start   linu   cal   pfile   index   scp   for   

#!/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 root@192.168.7.103 ""echo enable  server myapp/192.168.7.105" | socat stdio /run/haproxy/admin.sock"
     ssh root@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} git@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 root@192.168.7.103 "echo "disable server myapp/${node}" | socat stdio   /run/haproxy/admin.sock"
    echo "${node} 在负载均衡192.168.7.103下线成功"
    ssh root@192.168.7.104 "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 tomcat@${node}   "/etc/init.d/tomcat stop"
  done
}

function start_tomcat(){
  for node in ${Server_IP};do
    ssh tomcat@${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 tomcat@${node}:/data/tomcat/tomcat_appdir/code-${DATE}.zip
    ssh tomcat@${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 root@192.168.7.103 ""echo enable  server myapp/192.168.7.105" | socat stdio /run/haproxy/admin.sock"
      ssh root@192.168.7.104 ""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 root@192.168.7.103 ""echo enable  server myapp/${node}" | socat stdio /run/haproxy/admin.sock"
      ssh root@192.168.7.104 ""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 tomcat@${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  tomcat@${node}  ""ls  -l  -rt  /data/tomcat/tomcat_webdir/ | grep -B 1 ${NOW_VERSION}| head -n 1| awk ‘{print $9}‘""`
    ssh tomcat@${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
                

 

jenkins之代码部署回滚脚本

标签:admin   stop   start   linu   cal   pfile   index   scp   for   

原文地址:https://www.cnblogs.com/maxuebin/p/11623546.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!