糖醋鱼的小破站

🐟生成长记

什么都明白的人 最温柔也最冷漠
你一定要记得 别让一直温柔待你的人寒了心
我是一个不会哭的孩子,所以一直没有糖吃。
  menu
109 文章
4 评论
6886 浏览
0 当前访客
ღゝ◡╹)ノ❤️

自动化部署学习之——自动化部署加回滚脚本

自动化部署加回滚脚本

脚本内容

#!/bin/bash

# Node List
# 预热节点
PRE_LIST="192.168.0.239"
# 生产节点 
GROUP1_LIST="192.168.0.112"
# 回滚节点
ROLLBACK_LIST="192.168.0.112"

# Date
LOG_DATE='date "+%Y-%m-%d"'
LOG_TIME='date "+%H-%M-%S"'

CDATE=$(date "+%Y-%m-%d")
CTIME=$(date "+%H-%M-%S")

# SHELL ENV
SHELL_NAME="deploy.sh"
SHELL_DIR="/home/www"
SHELL_LOG="${SHELL_DIR}/${SHELL_NAME}.log"

# Code ENV
PRO_NAME="web-demo"
CODE_DIR="/deploy/code/web-demo"
CONFIG_DIR="/deploy/config/web-demo"
TMP_DIR="/deploy/tmp"
TAR_DIR="/deploy/tar"
LOCK_FILE="/tmp/deploy.lock"
API_VER="1.2.2"


usage(){
    echo $"Usage: $0 [ deploy | rollback ]"
}

write_log(){
    LOG_INFO=$1
    echo "${CDATE} ${CTIME}: ${SHELL_NAME} : ${LOG_INFO} " >> ${SHELL_LOG}
}

shell_lock(){
    touch ${LOCK_FILE}
}

shell_unlock(){
    rm ${LOCK_FILE} -f
}

url_test(){
    URL=$1
    write_log "begin test"

    curl -s --head $URL | grep "200 OK"
    if [ $? -ne 0 ];then
        shell_unlock;
        write_log "pre test error" && exit;
    fi
}

# 代码获取函数
# 只进行git pull
code_get(){
    write_log "code_get";
    cd ${CODE_DIR} && echo "git pull" ;
    cp -r ${CODE_DIR} ${TMP_DIR}/;
    # git commmit id
    API_VERL=$(git show | grep commit | cut -d ' ' -f2)
    API_VER=$(echo ${API_VERL:0:6})
}

code_build(){
    echo code_build

}

code_config(){
    write_log "code_config"
    /bin/cp -r ${CONFIG_DIR}/base/* ${TMP_DIR}/${PRO_NAME}
    PKG_NAME="${PRO_NAME}"_"$API_VER"_"${CDATE}-${CTIME}"
    cd ${TMP_DIR} && mv ${PRO_NAME} ${PKG_NAME}
}

code_tar(){
    write_log "code_tar"
    cd ${TMP_DIR} && tar czf ${PKG_NAME}.tar.gz ${PKG_NAME}
    write_log "${PKG_NAME}.tar.gz"
 
}
# 可以使用 salt 做公钥私钥的认证
code_scp(){
    write_log "code_scp"
    for node in $PRE_LIST;
        do /usr/bin/scp ${TMP_DIR}/${PKG_NAME}.tar.gz www@$node:/opt/webroot/
    done
    for node in $GROUP1_LIST;
        do /usr/bin/scp ${TMP_DIR}/${PKG_NAME}.tar.gz www@$node:/opt/webroot/
    done
}

# 预生产节点
pre_deploy(){
    write_log "remove from cluster"

    ssh ${PRE_LIST} "cd /opt/webroot && tar zxf ${PKG_NAME}.tar.gz"
    # 精髓:使用软链接 🔗
    ssh ${PRE_LIST} "rm -f /webroot/${PRO_NAME} && ln -s /opt/webroot/${PKG_NAME} /webroot/${PRO_NAME}"

}

pre_test(){
    url_test "http://${PRE_LIST}:index.html"
    write_log "add to cluster"
}

# 分组部署、并行和串行
group1_deploy(){
    write_log "remove from cluster"
    for node in $GROUP1_LIST;do
        ssh $node "cd /opt/webroot && tar zxf ${PKG_NAME}.tar.gz"
        # 精髓:使用软链接 🔗
        ssh $node "rm -f /webroot/${PRO_NAME} && ln -s /opt/webroot/${PKG_NAME} /webroot/${PRO_NAME}"
        /usr/bin/scp ${CONFIG_DIR}/other/$node.crontab.xml $node:/webroot/${PRO_NAME}/crontab.xml
    done
}

group1_test(){
    url_test "http://192.168.0.239:index.html"
    write_log "add to cluster"
}

# 回滚执行函数
rollback_fun(){
    for node in $ROLLBACK_LIST;do
        ssh $node "rm -f /webroot/web-demo && ln -s /opt/webroot/$1 /webroot/web-demo"
    done
}

# 回滚函数
rollback(){
    if [ -z $1 ];then
        shell_unlock;
        echo "Please input rollback version" && exit;
    fi
    case $1 in
        list)
            ls -l /opt/webroot/*.tar.gz
            ;;
        *)
            rollback_fun $1
    esac
}


main(){
    # 锁文件
    if [ -f $LOCK_FILE ];then
        echo "Deploy is runing" && exit;
    fi
        DEPLOY_METHOD=$1
        ROLLBACK_VER=$2
        case $DEPLOY_METHOD in
            # 部署
            deploy)
                shell_lock;
                code_get;
                code_build;
                code_config;
                code_tar;
                code_scp;
                pre_deploy;
                pre_test;
                group1_deploy;
                group1_test;
                shell_unlock;
                ;;
            # 回滚
            rollback)
                shell_lock;
                rollback $ROLLBACK_VER;
                shell_unlock;
                ;;
            # 退出
            *)
                usage;
            esac
}

main $1



标题:自动化部署学习之——自动化部署加回滚脚本
作者:expoli
地址:https://expoli.tech/articles/2019/08/18/1566110347365.html

评论