首先在这里我要说很多,
需求:
1.我写任何脚本的核心要求就是必须得有一台跳板机,单方面认证即可
2.需要一台服务器挂载前台所有web站点
3.需要一个报警通知的接口
个人对自动的化上线的理解:
1.执行上线脚本就可以高枕无忧,把脚本放到后台就可以了
2.如何达到高枕无忧,svn自动化上线实现起来不简单
一.备份块
楼主在备份这块现在还是比较犹豫,楼主有两个想法
(1)不需要备份,可以利用-r+版本号回退到上一个版本,但是有弊端,比如测试经常会上错包,这个不能只靠着别人不犯错
(2)每台web全路径备份,回滚会很快,但是有些没有必要,比如我这次上线,只上个jsp文件,我也要全备份吗,这样务必会影响效率
楼主现在还是选择了第二个方案二.上线块
(1)svn上线会有自己的弊端,比如手动更新线上的文件,下次再更新此包就会出现G的问题,代表此文件没有更新成功
(2)svn上线如果之前版本库里没有此文件,而你又手动上了一个文件,在手动上线后,svn更新会报错
(3)楼主的前台web,有23台之多,分为6个集群,每个集群的路径和进程名都不同,如何实现判断每次更新的群组
(4)如此庞大的web集群,如何能够精简脚本的行数
三.检测块
(1)自动化的精髓就是检测,无检测何来的自动化
(2)检测前台集群所有web在上线过程中,500错是否正常
(3)检测前台nginx日志,500错是否正常
(4)检测svn上线是否文件都更新成功了,只针对上线文件比对md5码
下面贴上代码
第一部分上线篇
楼主有个公用函数脚本,这才是核心脚本
vim function.sh
#!/bin/bash
one=$1 two=$2three=/svnlog/$3date=`date +%Y%m%d%H%M`function youyuanjar(){ echo "--------------------------resin restart--------------------------------" 2>&1 | tee -a /tmp/shangxianlog/svn.$one echo "---------------------------------$one------------------------------------" 2>&1 | tee -a /tmp/shangxianlog/svn.$one ssh $two "sh /usr/local/resin/bin/resin.sh stop -server youyuan" 2>&1 | tee -a /tmp/shangxianlog/svn.$one echo "$one stopped" 2>&1 | tee -a /tmp/shangxianlog/svn.$one sleep 3 ssh $two "cp -rf /www/youyuan.com.1/htdocs /opt/$date" e=`cat $three |grep -v Updated|awk -F "/" '{print $(NF-1)"/"$NF}'` for i in $e do ssh $two "rm -f /www/youyuan.com.1/$e" if [ "$?" -eq 0 ]; then echo delet ok fi done ssh $two "svn up $svnversion /www/youyuan.com.1/htdocs" 2>&1 | tee -a /tmp/shangxianlog/svn.$one echo "$one svnup ok " 2>&1 | tee -a /tmp/shangxianlog/svn.$one sleep 15 b=($(cat $three |grep -v Updated|awk -F "/" '{print $NF}'|awk -F "." '{print $1}')) c=($(cat $three |grep -v Updated|awk -F "/" '{print $(NF-1)"/"$NF}')) echo $c for ((d=0;d<${#c[@]};d++)) do touch /home/shangxianlog/${b[$d]} a=`ssh $two "md5sum /www/youyuan.com.1/${c[$d]}"` echo $two $a >> /home/shangxianlog/${b[$d]} done ssh $two "sh /usr/local/resin/bin/resin.sh restart -server youyuan" 2>&1 | tee -a /tmp/shangxianlog/svn.$one sleep 20 echo "$one restart " 2>&1 | tee -a /tmp/shangxianlog/svn.$one}function iosjar(){ touch /tmp/shangxianlog/svni.$one echo "--------------------------resin restart--------------------------------" 2>&1 | tee -a /tmp/shangxianlog/svni.$one echo "---------------------------------$one------------------------------------" 2>&1 | tee -a /tmp/shangxianlog/svni.$one ssh $two "sh /usr/local/resin/bin/resin.sh stop -server ios" 2>&1 | tee -a /tmp/shangxianlog/svni.$one echo "$one stopped" 2>&1 | tee -a /tmp/shangxianlog/svni.$one sleep 3 ssh $two "cp -rf /www/ios.youyuan.com/htdocs /opt/ios$three" c=`cat $three |egrep '(ios|iosui|css|lib)'|grep -v Updated|awk -F "/" '{print $(NF-1)"/"$NF}'` echo $c for d in $c do ssh $two "rm -f /www/ios.youyuan.com/$d" if [ "$?" -eq 0 ]; then echo delet ok fi done ssh $two "svn up $svnversion /www/ios.youyuan.com/htdocs/WEB-INF/lib" 2>&1 | tee -a /tmp/shangxianlog/svni.$one ssh $two "svn up $svnversion /www/ios.youyuan.com/htdocs/WEB-INF/ios" 2>&1 | tee -a /tmp/shangxianlog/svni.$one ssh $two "svn up $svnversion /www/ios.youyuan.com/htdocs/WEB-INF/iosui" 2>&1 | tee -a /tmp/shangxianlog/svni.$one ssh $two "svn up $svnversion /www/ios.youyuan.com/htdocs/css/" 2>&1 | tee -a /tmp/shangxianlog/svni.$one echo "$one svnup ok " 2>&1 | tee -a /tmp/shangxianlog/svni.$one ssh $two "sh /usr/local/resin/bin/resin.sh restart -server ios" 2>&1 | tee -a /tmp/shangxianlog/svni.$one sleep 20 echo "$one restart " 2>&1 | tee -a /tmp/shangxianlog/svni.$one}function ios(){ touch /tmp/shangxianlog/svni.$one echo "---------------------------------$oneios------------------------------------" 2>&1 | tee -a /tmp/shangxianlog/svni.$one ssh $two "cp -rf /www/ios.youyuan.com/htdocs /opt/ios$three" c=`cat $three |egrep '(ios|iosui|css)'|grep -v Updated|awk -F "/" '{print $(NF-1)"/"$NF}'` echo $c for d in $c do ssh $two "rm -f /www/ios.youyuan.com/$d" if [ "$?" -eq 0 ]; then echo delet ok fi done ssh $two "svn up $svnversion /www/ios.youyuan.com/htdocs/WEB-INF/ios" 2>&1 | tee -a /tmp/shangxianlog/svni.$one ssh $two "svn up $svnversion /www/ios.youyuan.com/htdocs/WEB-INF/iosui" 2>&1 | tee -a /tmp/shangxianlog/svni.$one ssh $two "svn up $svnversion /www/ios.youyuan.com/htdocs/css/" 2>&1 | tee -a /tmp/shangxianlog/svni.$one sleep 3 echo "53ios svnupok" 2>&1 | tee -a /tmp/shangxianlog/svni.$one}function youyuan(){ echo "---------------------------------$one------------------------------------" 2>&1 | tee -a /tmp/shangxianlog/svn.$one#1.备份 ssh $two "cp -rf /www/youyuan.com.1/htdocs /opt/"#2.上线之前先把本次需要上线的文件全部删除,为的就是彻底避免svn上线存在的一些问题 e=`cat $three |grep -v Updated|awk -F "/" '{print $(NF-1)"/"$NF}'` for i in $e do ssh $two "rm -f /www/youyuan.com.1/$e" if [ "$?" -eq 0 ]; then echo delet ok fi done#3.版本更新 ssh $two "svn up $svnversion /www/youyuan.com.1/htdocs" 2>&1 | tee -a /tmp/shangxianlog/svn.$one echo "$one svnup ok " 2>&1 | tee -a /tmp/shangxianlog/svn.$one sleep 15#4.没更新一个文件,都会和跳板机的正确文件版本号进行md5码比对 a=($(cat /svnlog/$filename|grep -v Updated|awk '{print $2}'|awk -F "/" '{print $3"/"$4}')) h=($(cat /svnlog/$filename |grep -v Updated|awk -F "/" '{print $NF}'|awk -F "." '{print $1}')) for ((z=0;z<${a[@]};z++)) do b=`ssh $two "md5sum /www/youyuan.com.1/${a[$z]}"|awk '{print $NF}'` grep -w $b /home/shangxianlog/${h[$z]} if [ "$?" -eq 0 ]; then echo good else sh /www/shell/syslog_2.sh $two-/www/youyuan.com.1/${a[$z]}-uploaderror `hostname` 1 2 1 6 ssh $two "rm -f /www/youyuan.com.1/${a[$z]}" ssh $two "svn up /www/youyuan.com.1/htdocs" fi done# o=`cat /tmp/shangxianlog/svn.$one|grep "G"|wc -l`# if [ "$o" -gt 0 ]; then# b=`cat /tmp/shangxianlog/svn.$one|awk '/G/{print $NF}'`# #c=`cat /svnlog/$a|egrep -o "(([0-9]{3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}||G))"|grep -B1 G|sed -n '1'p`# #c=`cat /svnlog/$a|egrep -o "((192.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}||G))"|grep -B1 G|grep "192"`# e=`date +%H%M%S`# for i in $b# do# ssh $two "cp -f $i /tmp/$e"# if [ "$?" == 0 ]; then# echo ok# ssh $two "rm -f $i"# if [ "$?" == 0 ]; then# echo ok# ssh $two "svn up /www/youyuan.com.1/htdocs"| tee -a /tmp/$e# fi# fi# done# fi sleep 3 echo "$one svnupok" 2>&1 | tee -a /tmp/shangxianlog/svn.$one}如何调用函数
分为4个调用函数脚本
iosjar.sh
#!/bin/bash
source /opt/shangxian/function.shiosjarios.sh
#!/bin/bash
source /opt/shangxian/function.shiosyouyuanjar.sh
#!/bin/bash
source /opt/shangxian/function.shyouyuanjaryouyuan.sh
#!/bin/bash
source /opt/shangxian/function.shyouyuansvn上线脚本
svn.sh
#!/bin/bash
source /opt/shangxian/function.shyouyuanjar[root@02 shangxian]# cat youyuan.sh#!/bin/bashsource /opt/shangxian/function.shyouyuan[root@02 shangxian]# cat svn.sh#!/bin/bash###创建日志###filename=`date '+%Y%m%d_%H%M%S.svnup.log'`svnversion=''touch /svnlog/$filename#ip=`cat /tmp/ip.txt|awk -F "." '{print $NF}'`#for i in $ip#do#touch /tmp/shangxianlog/svn.$i#donea=($(cat /opt/shangxian/shangxiantest.txt|awk '{print $2}'))b=($(awk '$2~/youyuan/{print $1}' /opt/shangxian/shangxiantest.txt))d=($(awk '$2~/ios/{print $1}' /opt/shangxian/shangxiantest.txt))c=($(awk '$2~/youyuan/{print $1}' /opt/shangxian/shangxiantest.txt|awk -F "." '{print $NF}'))e=($(awk '$2~/ios/{print $1}' /opt/shangxian/shangxiantest.txt|awk -F "." '{print $NF}'))#在跳板机上先把需要上线的文件写入日志中,以便后面进行判断/usr/bin/svn up $svnversion /svnsignal/htdocs 2>&1 | tee -a /svnlog/$filename#记录本次上线正确文件的md5码,以便让所有前台服务器对比本次上线是否更新成功f=($(cat /svnlog/$filename|grep -v Updated|awk '{print $2}'|awk -F "/" '{print $3"/"$4}'))g=($(cat /svnlog/$filename |grep -v Updated|awk -F "/" '{print $NF}'|awk -F "." '{print $1}'))for ((z=0;z<${#a[@]};z++))do svn up /www/youyuan.com.1/${a[$z]} touch /home/shangxianlog/${h[$z]} md5sum /www/youyuan.com.1/${a[$z]} >> /home/shangxianlog/${h[$z]}done###判断是否重启###restartsignal=`cat /svnlog/$filename | awk '/.jar$|.conf$|classes|lib/' | wc -l`if [ $restartsignal -gt 0 ]then#根据/opt/shangxian/shangxiantest.txt这个文件的第二列来判断运用那些函数,并把参数传入函数中,因为我们这里不同的站点都放在一个svn更新脚本中 for (( i=0;i<${#i[@]};i++ )) do if [ "${a[i]}" == youyuan ]; then sh /opt/shangxian/youyuanjar.sh ${c[i]} ${b[i]} $filename else sh /opt/shangxian/iosjar.sh ${e[$i]} ${d[$]} $filename fi sleep 5 doneelse for (( i=0;i<${#i[@]};i++ )) do if [ "${a[i]}" == youyuan ]; then sh /opt/shangxian/youyuan.sh ${c[i]} ${b[i]} $filename else sh /opt/shangxian/ios.sh ${e[$i]} ${d[$]} $filename fi sleep 5 donefi#h=`cat /svnlog/$filename |grep -v Updated|awk -F "/" '{print $NF}'|awk -F "." '{print $1}'`# k=`awk '!a[$2$3]++' /home/shangxianlog/$h|wc -l`# if [ $k -gt 1 ]; then# i=`awk '!a[$2$3]++' /home/shangxianlog/$g|awk 'END{print}'|awk '{print $1}'`# j=`awk '!a[$2$3]++' /home/shangxianlog/$g|awk 'END{print}'|awk '{print $NF}'`# y=`awk '!a[$2$3]++' /home/shangxianlog/$g|awk 'END{print}'|awk -F "/" '{print $1"/"$2"/"$3"/"$4"/"}'`# ssh $i "rm -f $j && svn up $y"# if [ "$?" -eq 0 ]; then## echo ok# fi# fi二.检测篇
检测nginx500错
#!/bin/bash
a=($(tail -n20000 /usr/local/nginx/logs/access.log | grep "\" 500 " |awk '{++a[$(NF-2)]}END{for ( i in a )print i,a[i]}'|sort -k2 -rn|head -n10|awk '{print $1}'))b=($(tail -n20000 /usr/local/nginx/logs/access.log | grep "\" 500 " |awk '{++a[$(NF-2)]}END{for ( i in a )print i,a[i]}'|sort -k2 -rn|head -n10|awk '{print $2}'))for ((i=0;i<${#a[@]};i++ ))doif [ "${b[i]}" -gt 20 ]; then# ssh ${b[i]} "/usr/local/resin/bin/resin.sh restart -server youyuan" sh /www/shell/syslog_2.sh nginx500error-must-${a[i]}- ${b[i]} 1 2 1 6fidone检测resin500错
#!/bin/bash
time=`date +%Y-%m-%d" "%H`for station in 23 41 42 43 44 51 53 123 132 130 131 128 211 126 127 138 139 56 do for line in `find /logs/youyuan.com.?/$station -name root-error.log` do echo $station count=`cat $line | grep "$time" | egrep '(500error|500错误)' |wc -l` echo $count a=`awk 'END{print}' /home/$station/500error.txt` echo $a countjia=`echo "scale=0;$count-$a"|bc` if [ "$countjia" -gt 20 ]; then sh /www/shell/syslog_2.sh 192.168.0.$line-resin-500erroradd-$countjia `hostname` 1 2 1 6 fi cat $line | grep "$time" | egrep '(500error|500错误)' |wc -l>>/home/$station/500error.txt donedone