自动化部署
时间:2016.08.12
笔者Q:552408925、572891887
架构师群:471443208
bjstack运维社区:524721466
有任何疑问请加群互动,或上社区发帖 bjstack运维社区
首先在执行脚本前,先做好初始化环境,最好是通过saltstack来完成。我这里没有那么多机器,就先用两台演示。 linux-node1 192.168.90.201 linux-node2 192.168.90.202
useradd xuliangwei passwd xuliangwei
[root@linux-node1~]#su-xuliangwei#切换至普通用户 [xuliangwei@Linux-node1~]$ ssh-keygen-t dsa#生成密钥 Generating public/privatedsa key pair. Enterfileinwhich to save the key(/home/xuliangwei/.ssh/id_dsa): #默认回车一路 /home/xuliangwei/.ssh/id_dsa already exists. Overwrite (y/n)?y Enterpassphrase(emptyfor nopassphrase): Entersame passphrase again: Youridentification has been savedin /home/xuliangwei/.ssh/id_dsa. #这是钥匙 Your publickey has been savedin /home/xuliangwei/.ssh/id_dsa.pub. #这是锁 Thekey fingerprintis: 39:6f:d8:42:61:d7:c3:f9:4b:e7:fb:b0:af:52:cd:59xuliangwei@nfs-server Thekey's randomart image is: +--[ DSA 1024]----+ | | | o . | | o . = | | . + o E| | S oo+| | . = ..=o| | o + .o .| | o . o.| | .o++| +-----------------+ [xuliangwei@nfs-server ~]$ ssh-copy-id -i ~/.ssh/id_dsa.pub xuliangwei@192.168.90.202 #公钥分发给其他服务器 xuliangwei@192.168.90.202's password: Now tryloggingintothe machine, with "ssh 'xuliangwei@192.168.90.202'", andcheckin: .ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting. [xuliangwei@nfs-server~]$ ssh-p22 xuliangwei@192.168.90.202 #使用xuliangwei用户登录node2节点服务器 Lastlogin: Wed Aug 10 17:59:02 2016 from 192.168.90.201 [xuliangwei@linux-node2~]$#成功登录
mkdir/deploy/ mkdir-p/deploy/code/rainbow_pro mkdir-p/deploy/source/rainbow_pro mkdir-p/deploy/config/rainbow_pro/config mkdir-p/deploy/config/rainbow_pro/admin_config mkdir-p/deploy/tmp mkdir-p/home/xuliangwei/webroot chown-R www:www/deploy chown-R www:www/home/xuliangwei/webroot
user xuliangwei; #指定用户 root/home/xuliangwei/webroot/rainbow_pro; #指定项目路径
1. centos7部署gitlab
2.克隆项目可参考 git远程仓库创建与建立
3.最终效果如下:
[xuliangwei@linux-node1~]$ ll/deploy/source/rainbow_pro/ 总用量 4 -rw-rw-r-- 1xuliangwei xuliangwei34 8月 12 18:05index.html
查看脚本,具体配和 自动化运维之自动化部署系统构建及演变文章来解读
[xuliangwei@linux-node1~]$ cat deploy.sh #!/bin/bash # useradd xuliangwei && sshkey需认证 # Nginx 权限必须让xuliangwei用户可访问 # Dir List # mkdir -p /deploy/code/rainbow_pro # web code的仓库 # mkdir -p /deploy/source/rainbow_pro #存放git源码仓库 # mkdir -p /deploy/config/rainbow_pro/config #存放config信息 # mkdir -p /deploy/config/rainbow_pro/admin_config #存放后台config信息 # mkdir -p /deploy/tmp # Web Root Dir # mkdir -p /home/xuliangwei/webroot # chown web # chown -R www:www /deploy # chown -R www:www /home/xuliangwei/webroot # Code Env PRO_NAME="rainbow_pro" CODE_DIR="/deploy/code/$PRO_NAME" SOURCE_DIR="/deploy/source/$PRO_NAME" CONFIG_DIR="/deploy/config/$PRO_NAME" WEB_DIR="/home/xuliangwei/webroot" TMP_DIR="/deploy/tmp" # Node List PRE_LIST="192.168.90.201" GROUP1_LIST="192.168.90.202" ROLLBACK_LIST="192.168.90.201 192.168.90.202" # Date/Time Veriables CTIME=$(date"+%F-%H-%M") # Shell Env SHELL_NAME="deploy.sh" SHELL_DIR="/home/xuliangwei/webroot" # Log LOG_FILE="${SHELL_DIR}/${SHELL_NAME}".log LOCK_FILE="/tmp/deploy.lock" # Lock shell_lock(){ touch ${LOCK_FILE} } shell_unlock(){ rm-f ${LOCK_FILE} } # URL Test url_test(){ URL=$1 curl-s--head $URL|egrep'200|301|302' if [$? -ne0 ];then shell_unlock; echo"test error" && exit; fi } # log writelog(){ LOGINFO=$1 echo"${CTIME}: ${SHELL_NAME} : ${LOGINFO}" >>${LOG_FILE} } code_get(){ writelog"code_get" cd $SOURCE_DIR&&git pull GIT_CID=$(git log|awk'NR==1{print $2}'|cut-c1-6) PKG_VER="${CTIME}_${GIT_CID}" PKG_NAME="${PRO_NAME}_${PKG_VER}" cp-r ${SOURCE_DIR}${TMP_DIR}/${PKG_NAME} } code_bulid(){ echo code_bulid#由于php没有编译过程 } code_config(){ writelog"code_config" /bin/cp-r ${CONFIG_DIR}/config.php ${TMP_DIR}/${PKG_NAME}/config.php } code_tar(){ writelog"code_tar" cd ${TMP_DIR} &&tar czf ${PKG_NAME}_tar.gz ${PKG_NAME} writelog"${PKG_NAME}_tar.gz" } code_scp(){ writelog"code_scp" fornodein$PRE_LIST;do scp ${TMP_DIR}/${PKG_NAME}_tar.gz $node:${TMP_DIR}/ done fornodein$GROUP1_LIST;do scp ${TMP_DIR}/${PKG_NAME}_tar.gz $node:${TMP_DIR}/ done } pre_deploy(){ writelog"remove from cluster" ssh $PRE_LIST"cd ${TMP_DIR} && tar xf ${PKG_NAME}_tar.gz -C ${CODE_DIR}/" ssh $PRE_LIST"rm -f $WEB_DIR/${PRO_NAME} && ln -s ${CODE_DIR}/${PKG_NAME} $WEB_DIR/${PRO_NAME}" } pre_test(){ url_test"http://${PRE_LIST}/index.html" echo"Rre add to cluster" } group1_deploy(){ writelog"remove from cluster" fornodein$GROUP1_LIST;do ssh $node"cd ${TMP_DIR} && tar xf ${PKG_NAME}_tar.gz -C ${CODE_DIR}/" ssh $node"rm -f $WEB_DIR/${PRO_NAME} && ln -s ${CODE_DIR}/${PKG_NAME} $WEB_DIR/${PRO_NAME}" done #scp ${CONFIG_DIR}/other/192.168.90.201.crontab.xml 192.168.90.201:/$WEB_DIR/${PRO_NAME}/crontab.xml 额外不一样的配置可能需要用到 } group1_test(){ url_test"http://${GROUP1_LIST}/index.html" echo"group1_node add to cluster" } code_reload(){ systemctl restart php-fpm#重启php清楚opcode缓存 } rollback_list(){ fornodein$GROUP1_LIST;do ssh $node ls-l"$WEB_DIR" &&/ ssh $node find"$CODE_DIR/" -maxdepth1 -mtime-2|sed1d|awk-F'/' '{print $5}' #find "$CODE_DIR" -type d -name "$PRO_NAME*" -mtime +30|xargs rm -fr done } rollback_fun(){ if [ -z $ROOLBACK];then shell_unlock; echo"Please input rollback version" && exit; else fornodein$ROLLBACK_LIST;do ssh $node rm-f $WEB_DIR/${PRO_NAME} &&/ ssh $node ln-s ${CODE_DIR}/$ROOLBACK $WEB_DIR/${PRO_NAME} done fi } main(){ if [ -f"$LOCK_FILE" ];then echo"Deploy is Running" && exit; fi DEPLOY_METHOD="$1" ROOLBACK="$2" case$DEPLOY_METHODin deploy) shell_lock; code_get; code_bulid; code_config; code_tar; code_scp; pre_deploy; pre_test; group1_deploy; group1_test; # group2_deploy; # group2_test; shell_unlock; ;; list) rollback_list; ;; rollback) shell_lock; rollback_fun $ROLLBACK; shell_unlock; ;; *) echo"$Usage:$0 [ deploy | list | rollback ]" esac } main"$1" "$2"
执行上线:
[xuliangwei@linux-node1~]$ sh deploy.sh deploy Alreadyup-to-date. code_bulid rainbow_pro_2016-08-12-21-16_46236b_tar.gz100% 6016KB 5.9MB/s00:00 rainbow_pro_2016-08-12-21-16_46236b_tar.gz100% 6016KB 5.9MB/s00:00 rainbow_pro_2016-08-12-21-16_46236b_tar.gz100% 6016KB 5.9MB/s00:00 HTTP/1.1 200OK Rreadd to cluster HTTP/1.1 200OK HTTP/1.1 200OK group1_node add to cluster php-fpm restart
查看当前版本
[xuliangwei@linux-node1~]$ sh deploy.sh list 总用量 16 lrwxrwxrwx1xuliangwei xuliangwei60 8月 12 21:16rainbow_pro-> /deploy/code/rainbow_pro/rainbow_pro_2016-08-12-21-16_46236b rainbow_pro_2016-08-12-19-24_46236b rainbow_pro_2016-08-12-21-16_46236b 总用量 0 lrwxrwxrwx1xuliangwei xuliangwei60 8月 12 21:16rainbow_pro-> /deploy/code/rainbow_pro/rainbow_pro_2016-08-12-21-16_46236b rainbow_pro_2016-08-12-19-24_46236b rainbow_pro_2016-08-12-21-16_46236b
执行回滚操作
[xuliangwei@linux-node1~]$ sh deploy.sh rollback#直接执行是不允许的 Pleaseinput rollback version [xuliangwei@linux-node1~]$ sh deploy.sh rollback rainbow_pro_2016-08-12-19-24_46236b#这个操作一定要配合list来做。 查看已经回退到19点的版本 [xuliangwei@linux-node1~]$ sh deploy.sh list 总用量 16 -rw-rw-r-- 1xuliangwei xuliangwei14224 8月 12 21:16deploy.sh.log lrwxrwxrwx1xuliangwei xuliangwei60 8月 12 21:18rainbow_pro-> /deploy/code/rainbow_pro/rainbow_pro_2016-08-12-19-24_46236b rainbow_pro_2016-08-12-19-24_46236b rainbow_pro_2016-08-12-21-16_46236b 总用量 0 lrwxrwxrwx1xuliangwei xuliangwei60 8月 12 21:18rainbow_pro-> /deploy/code/rainbow_pro/rainbow_pro_2016-08-12-19-24_46236b rainbow_pro_2016-08-12-19-24_46236b rainbow_pro_2016-08-12-21-16_46236b
查看日志:
[xuliangwei@linux-node1~]$ cat webroot/deploy.sh.log 2016-08-12-21-35:deploy.sh:code_get 2016-08-12-21-35:deploy.sh:code_config 2016-08-12-21-35:deploy.sh:code_tar 2016-08-12-21-35:deploy.sh:rainbow_pro_2016-08-12-21-35_46236b_tar.gz 2016-08-12-21-35:deploy.sh:code_scp 2016-08-12-21-35:deploy.sh:removefromcluster 2016-08-12-21-35:deploy.sh:removefromcluster
当然此脚本功能不仅仅如此,可以通过git分支,git tag包等方式来上线,这些根据贵公司的具体业务来进行调整,也可以自己开发一个web界面来调用此脚本,这样就可以将上线工作交给开发,当然也可以使用开源jenkins来实现。
(如上脚本)+gitlab+jenkins+Sonar 构建自动化部署代码,并代码发布前质量管理
笔者Q:552408925、572891887
架构师群:471443208
bjstack运维社区:524721466
有任何疑问请加群互动,或上社区发帖 bjstack运维社区