shell自动化一键部署脚本,秒级一键回滚脚本

  1 #!/bin/bash
  2 
  3 # Node List
  4 PRE_LIST="192.168.222.163"        # 预生产环境节点
  5 GROUP1_LIST="192.168.222.164"        # 分组 1
  6 ROLLBACK_LIST="192.168.222.163 192.168.222.164" # 回滚列表
  7 
  8 # Date/Time Veriables
  9 LOG_DATE='date "+%Y-%m-%d"'
 10 LOG_TIME='date "+%H-%M-%S"'
 11 
 12 CDATE=$(date "+%Y-%m-%d")
 13 CTIME=$(date "+%H-%M-%S")
 14 
 15 # Shell Env
 16 SHELL_NAME="deploy_all.sh"
 17 SHELL_DIR="/home/www/"
 18 SHELL_LOG="${SHELL_DIR}/${SHELL_NAME}.log"
 19 
 20 # Code Env
 21 PRO_NAME="web-demo"
 22 CODE_DIR="/deploy/code/web-demo"
 23 CONFIG_DIR="/deploy/config/web-demo"
 24 TMP_DIR="/deploy/tmp"
 25 TAR_DIR="/deploy/tar"
 26 LOCK_FILE="/tmp/deploy.lock"
 27 
 28 usage(){
 29     echo $"Usage: $0 {deploy | rollback [ list | version ]}"
 30 }
 31 
 32 writelog(){
 33    LOGINFO=$1
 34    echo "${CDATE} ${CTIME}: ${SHELL_NAME} : ${LOGINFO} "  >> ${SHELL_LOG}
 35 }
 36 
 37 shell_lock(){
 38     touch ${LOCK_FILE}
 39 }
 40 
 41 shell_unlock(){
 42     rm -f ${LOCK_FILE}
 43 }
 44 
 45 url_test(){
 46     URL=$1
 47     curl -s --head $URL | grep '200 OK'
 48     if [ $? -ne 0 ];then
 49         shell_unlock;
 50         echo "test error" && exit;
 51     fi
 52 }
 53 
 54 code_get(){
 55     writelog "code_get";
 56     cd $CODE_DIR && git pull        # 事先要在部署机上 git clone 项目代码
 57     cp -r ${CODE_DIR} ${TMP_DIR}/
 58     API_VERL=$(git show | grep commit | cut -d ' ' -f2)
 59     API_VER=$(echo ${API_VERL:0:6})
 60 }
 61 
 62 code_build(){
 63     echo code_build
 64 }
 65 
 66 
 67 code_config(){
 68     writelog "code_config"
 69     /bin/cp -r ${CONFIG_DIR}/base/* ${TMP_DIR}/"${PRO_NAME}"
 70     PKG_NAME="${PRO_NAME}"_"$API_VER"_"${CDATE}-${CTIME}"
 71     cd ${TMP_DIR} && mv ${PRO_NAME} ${PKG_NAME}
 72 }
 73 
 74 code_tar(){
 75     writelog "code_tar"
 76     cd ${TMP_DIR} && tar czf ${PKG_NAME}.tar.gz ${PKG_NAME}
 77     writelog "${PKG_NAME}.tar.gz"
 78 }
 79 
 80 
 81 code_scp(){
 82     writelog "code_scp"
 83     for node in $PRE_LIST;do
 84         scp ${TMP_DIR}/${PKG_NAME}.tar.gz $node:/opt/webroot/
 85     done
 86 
 87     for node in $GROUP1_LIST;do
 88         scp ${TMP_DIR}/${PKG_NAME}.tar.gz $node:/opt/webroot/
 89     done
 90               scp ${TMP_DIR}/${PKG_NAME}.tar.gz 192.168.222.162:/opt/webroot/
 91 }
 92 
 93 pre_deploy(){
 94     writelog "remove from cluster"
 95     ssh $PRE_LIST "cd /opt/webroot && tar zxf ${PKG_NAME}.tar.gz"
 96     ssh 192.168.222.162 "cd /opt/webroot && tar zxf ${PKG_NAME}.tar.gz"
 97     ssh $PRE_LIST "rm -rf /webroot/web-demo && ln -s /opt/webroot/${PKG_NAME} /webroot/web-demo"
 98 }
 99 
100 pre_test(){
101     url_test "http://${PRE_LIST}/index.html"
102     echo "add to cluster"
103 }
104 
105 
106 group1_deploy(){
107     writelog "remove from cluster"
108     for node in $GROUP1_LIST;do
109         ssh $node "cd /opt/webroot && tar zxf ${PKG_NAME}.tar.gz"
110         ssh $node "rm -rf /webroot/web-demo && ln -s /opt/webroot/${PKG_NAME} /webroot/web-demo"
111     done
112     scp ${CONFIG_DIR}/other/192.168.222.163.crontab.xml 192.168.222.164:/webroot/web-demo/crontab.xml
113 }
114 
115 group1_test(){
116     url_test "http://192.168.222.163:80/index.html"        
117     echo "add to cluster"
118 }
119 
120 rollback_fun(){
121     for node in $ROLLBACK_LIST;do
122     ssh $node "rm -f /webroot/web-demo && ln -s /opt/webroot/$1 /webroot/web-demo"
123     done
124 }
125 
126 rollback(){
127 if [ -z $1 ];then
128     shell_unlock;
129     echo "Please input rollback version" && exit;
130 fi
131     case $1 in
132     list)
133         ls -l /opt/webroot/*.tar.gz
134         ;;
135     *)
136         rollback_fun $1
137     esac
138 }
139 
140 main(){
141    if [ -f $LOCK_FILE ];then
142     echo "Deploy is running" && exit;
143    fi
144     DEPLOY_METHOD=$1
145     ROLLBACK_VER=$2
146     case $DEPLOY_METHOD in
147     deploy)
148         shell_lock;
149         code_get;
150         code_build;
151         code_config;
152         code_tar;
153         code_scp;
154         pre_deploy;
155         pre_test;
156         group1_deploy;
157         group1_test;
158         shell_unlock;
159         ;;
160     rollback)
161         shell_lock;
162         rollback $ROLLBACK_VER;
163         shell_unlock;
164         ;;
165     *)
166         usage;
167     esac
168 }
169 main $1 $2

 

posted @ 2016-09-10 12:10  BigZero  阅读(1223)  评论(0编辑  收藏  举报