标签:自动化部署脚本
2 自动化部署流程设计一个集群有10个节点:
#!/bin/bash
#NODE LIST
NODE_LIST="10.0.0.205 10.0.0.203"
# 生产中有预热节点,一般预热节点为一台。
PRE_LIST="10.0.0.203"
GROUP2_LIST="10.0.0.205"
ROLLBACK_LIST="10.0.0.205 10.0.0.203"
# Date/Time variables
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/${PRO_NAME}"
CONFIG_DIR="/deploy/config/${PRO_NAME}"
TMP_DIR="/deploy/tmp"
TAR_DIR="deploy/tar"
LOCK_FILE="/tmp/deploy.lock"
# FUNCTION
url_test(){
URL=$1
curl -s -I "$URL"|grep ‘200 OK‘
if [ $? -ne 0 ];then
shell_unlock;
writelog "test error" && exit
fi
}
usage(){
echo "usage: $0 { deploy | rollback [list | version]}"
}
writelog(){
LOGINFO=$1
echo "${CDATE} ${CTIME}: ${SHELL_NAME} :${LOGINFO}" >> ${SHELL_LOG}
}
shell_lock(){
touch ${LOCK_FILE}
}
shell_unlock(){
rm -f $LOCK_FILE
}
code_get(){
writelog ‘code_get‘;
# $CODE_DIR目录下只能进行git pull
cd $CODE_DIR && git pull
# git pull 之后就copy走
cp -r ${CODE_DIR} ${TMP_DIR}/
# 定义版本号,下面的PKG_NAME需要用到。
API_VER=`git show|grep commit|cut -d " " -f 2|head -c 6`
}
code_build(){
echo code_build
}
code_config(){
writelog code_config
# 加 -r 参数 ,覆盖原来的老文件。
# 这里为什么是/bin/cp ?? 后面为什么加“”
/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(){
writelog "code_tar"
cd ${TMP_DIR} && tar -cf ${PKG_NAME}.tar.gz $PKG_NAME
}
code_scp(){
writelog "code_scp"
for node in $NODE_LIST
do
# /opt/webroot 目录要存在
scp -P52113 ${TMP_DIR}/${PKG_NAME}.tar.gz $node:/opt/webroot
done
}
cluster_node_remove(){
writelog "cluster_node_remove"
}
pre_deploy(){
# 先上预热节点,当他测试没问题的时候再接着上下面的节点。
writelog ‘remove from cluster‘
ssh -p52113 ${PRE_LIST} "cd /opt/webroot && tar xf ${PKG_NAME}.tar.gz"
ssh -p52113 ${PRE_LIST} "rm -f /webroot/web-demo && ln -s /opt/webroot/${PKG_NAME} /webroot/web-demo"
}
pre_test(){
url_test http://${PRE_LIST}/index.html
}
group2_deploy(){
# 包含
writelog ‘remove from cluster.‘
for node in $GROUP2_LIST
do
ssh -p52113 $node "cd /opt/webroot && tar xf ${PKG_NAME}.tar.gz"
ssh -p52113 $node "rm -f /webroot/web-demo && ln -s /opt/webroot/${PKG_NAME} /webroot/web-demo"
done
# 差异配置文件,应该放到最后去完成。如果软连接没完成,而配置文件拷贝过去就尴尬了。
scp -P52113 ${CONFIG_DIR}/other/10.0.0.205.crontab.xml 10.0.0.205:/webroot/web-demo/crontab.xml
}
group2_test(){
for node in $GROUP2_LIST
do
url_test http://${node}/index.html
done
}
cluster_node_in(){
echo cluster_node_in
}
rollback_fun(){
ROLLBACK_VER=$1
for node in $ROLLBACK_LIST
do
ssh -p52113 $node "[ -d /opt/webroot/"$ROLLBACK_VER" ] && rm -f /webroot/web-demo && ln -s /opt/webroot/${ROLLBACK_VER} /webroot/web-demo || echo rollback failed."
done
}
rollback(){
ROLLBACK_VER=$1
[ -z "$ROLLBACK_VER" ] && echo "exit! no version." && shell_unlock && exit
case $ROLLBACK_VER in
list)
#ssh -p52113 10.0.0.203 ‘ls -l /opt/webroot/*.tar.gz |awk -F "[/.]" "{print $(NF-2)}"‘
ssh -p52113 10.0.0.203 "find /opt/webroot/ -maxdepth 1 -type d|cut -d "/" -f 4"
;;
*)
rollback_fun $ROLLBACK_VER
esac
}
main(){
# 进程锁
[ -f "$LOCK_FILE" ] && echo "deploy is running " && exit
DEPLOY_METHOD=$1
ROLLBACK_VER=$2
case $DEPLOY_METHOD in
deploy)
shell_lock;
code_get;
code_build;
code_config;
code_tar;
code_scp;
cluster_node_remove;
pre_deploy;
pre_test;
group2_deploy;
group2_test;
cluster_node_in;
shell_unlock;
;;
rollback)
shell_lock;
rollback $ROLLBACK_VER;
shell_unlock;
;;
*)
usage;
esac
}
# $1: deploy | rollback
# $2: rollback version
main $1 $2
标签:自动化部署脚本
原文地址:http://blog.51cto.com/damaicha/2118712