type
Post
status
Published
slug
2019/08/18/1566110347365
summary
自动化部署学习之——自动化部署加回滚脚本
tags
工具
Linux
运维
category
Linux
icon
password
new update day
Property
Oct 22, 2023 01:31 PM
created days
Last edited time
Oct 22, 2023 01:31 PM
自动化部署加回滚脚本
脚本内容
#!/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
欢迎加入“喵星计算机技术研究院”,原创技术文章第一时间推送。
- 作者:tangcuyu
- 链接:https://expoli.tech/articles/2019/08/18/1566110347365
- 声明:本文采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。
相关文章