高级构架之自动化部署代码

  本脚本实现自动从git下载代码然后自动部署

  用户所有的web服务,都应该使用普通用户。所有web服务都不应该监听80端口,除了负载均衡

  新建www用户 

useradd -u 1000 www

  切换至www用户

  部署机生成秘钥

 ssh-keygen -t rsa

  复制秘钥

 cat /home/www/.ssh/id_rsa.pub

  把内容复制到客户端的 ~.ssh/authorized_keys   (权限必须设置为600)

chmod 600 .ssh/authorized_keys

  可以通过ssh ip 直接免密码登录

  自动部署脚本(本次试验部署机与客户端为同一台)

  deploy.sh

#!/bin/bash
#Node List
NODE_LIST="10.13.74.222"
GROUP_LIST="10.13.74.222"
ROLLBACK_LIST="10.13.74.222"
#Date/Time Veriables
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"

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

writelog(){
    LOGINFO=$1
    echo "${CDATE} ${CTIME}: ${SHELL_NAME}: ${LOGINFO}" >> ${SHELL_LOG}
}

shell_lock(){
    touch ${LOCK_FILE}	
}

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

#切换至获取代码的目录通过git pull命令获取代码
#git设置好,设置步骤不详述
#获取代码以后拷贝至TMP目录
code_get(){
    writelog "code_get";
    cd $CODE_DIR && git pull 
    cp -r ${CODE_DIR} ${TMP_DIR}  
    API_VER="123"
}

code_build(){
    echo code_build
}

#拷贝配置文件
code_config () {
    writelog "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(){
    writelog "code_tar"
    cd ${TMP_DIR} && tar czf ${PKG_NAME}.tar.gz ${PKG_NAME}
    writelog "${PKG_NAME}.tar.gz"
}

#scp至目标机
code_scp(){
    writelog "code_scp"
    for node in ${NODE_LIST};do
	scp ${TMP_DIR}/${PKG_NAME}.tar.gz  $node:/opt/webroot
	done
}

cluster_node_remove(){
    echo cluster_node_remove
    writelog "cluster_node_remove"
}

code_deploy() {
    for node in $NODE_LIST;do
    	ssh $node "cd /opt/webroot && tar zxf ${PKG_NAME}.tar.gz"
    done
    rm -rf /var/www/html && ln -s /opt/webroot/${PKG_NAME} /var/www/html
}

config_diff() {
    echo config_diff
}

code_test() {
    echo code_test
}

cluster_node_in(){
    echo cluster_node_in
}

rollback_fun() {
    for node in $ROLLBACK_LIST;do
      if [ -d /opt/webroot/$1 ];then
      ssh $node "rm -f /var/www/html && ln -s /opt/webroot/$1 /var/www/html"
      fi
    done
}

rollback() {
    case $1 in
	list)
	for node in $ROLLBACK_LIST;do
	ssh $node 	"ls -l /opt/webroot/*.tar.gz"
	done
	;;
	*)
	rollback_fun $1
    esac
}

main() {
    if [ -f $LOCK_FILE ];then
    echo "Deploy is running" && exit;
    fi
    DEPLOY_METHOD=$1
    case $DEPLOY_METHOD in
    deploy)
	shell_lock;
   	code_get;
	code_build;
	code_config;
	code_tar;
	code_scp;
	cluster_node_remove;
	code_deploy;
	code_test;
	cluster_node_in;
	shell_unlock;
	;;
    rollback)
	shell_lock;
	rollback;
	shell_unlock;
	;;
    *)
	usage;
    esac
}

main $1
[www@3181bf ~]$ cat deploy.sh 
#!/bin/bash
#Node List
NODE_LIST="10.13.74.222"
GROUP_LIST="10.13.74.222"
ROLLBACK_LIST="10.13.74.222"
#Date/Time Veriables
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"

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

writelog(){
    LOGINFO=$1
    echo "${CDATE} ${CTIME}: ${SHELL_NAME}: ${LOGINFO}" >> ${SHELL_LOG}
}

shell_lock(){
    touch ${LOCK_FILE}	
}

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

#切换至获取代码的目录通过git pull命令获取代码
#git设置好,设置步骤不详述
#获取代码以后拷贝至TMP目录
code_get(){
    writelog "code_get";
    cd $CODE_DIR && git pull 
    cp -r ${CODE_DIR} ${TMP_DIR}  
    API_VER="123"
}

code_build(){
    echo code_build
}

#拷贝配置文件
code_config () {
    writelog "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(){
    writelog "code_tar"
    cd ${TMP_DIR} && tar czf ${PKG_NAME}.tar.gz ${PKG_NAME}
    writelog "${PKG_NAME}.tar.gz"
}

#scp至目标机
code_scp(){
    writelog "code_scp"
    for node in ${NODE_LIST};do
	scp ${TMP_DIR}/${PKG_NAME}.tar.gz  $node:/opt/webroot
	done
}

cluster_node_remove(){
    echo cluster_node_remove
    writelog "cluster_node_remove"
}

code_deploy() {
    for node in $NODE_LIST;do
    	ssh $node "cd /opt/webroot && tar zxf ${PKG_NAME}.tar.gz"
    done
    rm -rf /var/www/html && ln -s /opt/webroot/${PKG_NAME} /var/www/html
}

config_diff() {
    echo config_diff
}

code_test() {
    echo code_test
}

cluster_node_in(){
    echo cluster_node_in
}

rollback_fun() {
    for node in $ROLLBACK_LIST;do
      if [ -d /opt/webroot/$1 ];then
      ssh $node "rm -f /var/www/html && ln -s /opt/webroot/$1 /var/www/html"
      fi
    done
}

rollback() {
    case $1 in
	list)
	for node in $ROLLBACK_LIST;do
	ssh $node 	"ls -l /opt/webroot/*.tar.gz"
	done
	;;
	*)
	rollback_fun $1
    esac
}

main() {
    if [ -f $LOCK_FILE ];then
    echo "Deploy is running" && exit;
    fi
    DEPLOY_METHOD=$1
    case $DEPLOY_METHOD in
    deploy)
	shell_lock;
   	code_get;
	code_build;
	code_config;
	code_tar;
	code_scp;
	cluster_node_remove;
	code_deploy;
	code_test;
	cluster_node_in;
	shell_unlock;
	;;
    rollback)
	shell_lock;
	rollback;
	shell_unlock;
	;;
    *)
	usage;
    esac
}

main $1

  实现了自动化部署,未实现回滚,待完善

  

posted @ 2017-06-03 22:09  minseo  阅读(423)  评论(0编辑  收藏  举报