0%

Shell脚本全量、增量发布项目部署

1、nginx非plus版本没有健康流量检测
2、通过修改nginx后端地址避免流量转发
3、如不修改也可采用backup模式

环境:

1
2
3
4
10.25.208.243 tomcat1 nginx 
10.66.220.113 tomcat2 脚本

两个tomcat已做session共享

Shell脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/bash

date_name=`date +%Y-%m-%d_%H-%M`

rn(){
echo '按任意键继续!'
read -n 1
}

# 检测项目是否启动
check_url(){
if [ "$1" == "help" ] || [ "$1" == "" ];then
echo "参数1 参数2 参数3"
echo "URL 页面内容 轮询检测[r](如:check_url https://www.baidu.com/ "百度")"
exit 0
fi
echo "正在检查 [$1] 能否访问!"
if [ "$3" == "r" ];then
curl -s --connect-timeout 5 -m 8 $1 | grep $2
while [ $? != "0" ];do
sleep 1s
echo -n .
curl -s --connect-timeout 5 -m 8 $1 | grep $2
done
return $?
else
curl -s --connect-timeout 5 -m 8 $1 | grep $2
return $?
fi
}

# 全量更新并检测项目是否启动
nt_demo(){
dates=`date +%Y-%m-%d_%H:%M`
apppath="/data/application/tomcat-demo"
cd /data/project/svn/nt_demo/ns_possecu
svn up
echo 'SVN更新成功!'
rn
ret=`mvn -U clean compile war:war -Pdemo`
echo "$ret"
echo "$ret" | grep 'BUILD SUCCESS'
if [ $? -ne 0 ];then
echo '编译失败!'
exit
fi
date=`date +%Y%m%d%H%M`
echo "开始更新demo1"
echo "调整nginx负载均衡地址"
ssh -p 50750 root@10.25.208.243 "sed -i 's/\<server 10.25.208.243:23080\>/#&/' /usr/local/nginx-1.14.2/conf/nginx.conf"
ssh -p 50750 root@10.25.208.243 "/usr/local/nginx-1.14.2/sbin/nginx -s reload"
ssh -p 50750 root@10.25.208.243 "cp -rf $apppath/webapps/possecu_cs.war $apppath/backup/possecu_cs.war$date"
ssh -p 50750 root@10.25.208.243 "rm -rf $apppath/webapps/possecu_cs.war"
ssh -p 50750 root@10.25.208.243 "rm -rf $apppath/webapps/possecu_cs"
scp -P 50750 ./target/ns_possecu.war 10.25.208.243:$apppath/webapps/possecu_cs.war
ssh -p 50750 root@10.25.208.243 "/data/application/tomcat-demo/r.sh rr"
echo "########检查demo1是否更新成功########"
stat='0'
i='0'
while [ $stat != '1' ]; do
code=`curl -s http://x.x.x.x:23080/possecu_cs/login/forward_login.htm | grep "<title>" | awk -F "title>" '{print $2}' | head -n 1 | grep 管理系统`
if [ $? -eq 0 ];then
echo 'demo1启动成功'
stat='1'
ssh -p 50750 root@10.25.208.243 "sed -i '/\<server 10.25.208.243:23080\>/s/#//' /usr/local/nginx-1.14.2/conf/nginx.conf"
ssh -p 50750 root@10.25.208.243 "/usr/local/nginx-1.14.2/sbin/nginx -s reload"
else
sleep 1
echo -n $i'...'
i=`expr $i + 1`;
if [ $i = '120' ];then
i='0'
echo '120秒无相应,尝试重新启动tomcat'
ssh -p 50750 root@10.25.208.243 "/data/application/tomcat-demo/r.sh rr"
fi
fi
done
echo "开始更新demo2"
echo "调整nginx负载均衡地址"
ssh -p 50750 root@10.25.208.243 "sed -i 's/\<server 10.66.220.113:23080\>/#&/' /usr/local/nginx-1.14.2/conf/nginx.conf"
ssh -p 50750 root@10.25.208.243 "/usr/local/nginx-1.14.2/sbin/nginx -s reload"
cp -rf $apppath/webapps/possecu_cs.war $apppath/backup/possecu_cs.war$date
rm -rf $apppath/webapps/possecu_cs.war
rm -rf $apppath/webapps/possecu_cs
cp ./target/ns_possecu.war $apppath/webapps/possecu_cs.war
/data/application/tomcat-demo/r.sh rr
echo "########检查demo2是否更新成功########"
stat='0'
i='0'
while [ $stat != '1' ]; do
code=`curl -s http://x.x.x.x:23080/possecu_cs/login/forward_login.htm | grep "<title>" | awk -F "title>" '{print $2}' | head -n 1 | grep 管理系统`
if [ $? -eq 0 ];then
echo 'demo2启动成功'
stat='1'
ssh -p 50750 root@10.25.208.243 "sed -i '/\<server 10.66.220.113:23080\>/s/#//' /usr/local/nginx-1.14.2/conf/nginx.conf"
echo "调整nginx负载均衡地址"
ssh -p 50750 root@10.25.208.243 "/usr/local/nginx-1.14.2/sbin/nginx -s reload"
else
sleep 1
echo -n $i'...'
i=`expr $i + 1`;
if [ $i = '120' ];then
i='0'
echo '120秒无相应,尝试重新启动tomcat'
/data/application/tomcat-demo/r.sh rr
fi
fi
done
}

# 增量更新并根据参数是否重启项目
# 如 deploy zl_nt_demo r 意味着重启,否则不重启
#
#
zl_nt_demo(){
mvn ntdemo
for i in `cat /data/project/nthttp.txt | sed "s/src\/main\/java/webapp\/WEB-INF\/classes/g" | sed "s/\.java/\.class/g"`;do
path=`echo $i | awk -F 'webapp' '{print $2}'`
echo "备份node1 10.25.208.243"
ssh -p 50750 10.25.208.243 "mv /data/application/tomcat-demo/webapps/possecu_cs/$path /data/application/tomcat-demo/webapps/possecu_cs/$path-$date_name"
echo "备份node2 10.66.220.113"
mv /data/application/tomcat-demo/webapps/possecu_cs/$path /data/application/tomcat-demo/webapps/possecu_cs/$path-$date_name
echo "复制node1 10.25.208.243"
scp -r -P 50750 /data/project/svn/nt_demo/ns_possecu/target/ns_possecu/$path 10.25.208.243:/data/application/tomcat-demo/webapps/possecu_cs/$path
echo "复制node2 10.66.220.113"
\cp -rf /data/project/svn/nt_demo/ns_possecu/target/ns_possecu/$path /data/application/tomcat-demo/webapps/possecu_cs/$path
echo "输出替换文件node1"
ssh -p 50750 10.25.208.243 "ls -al /data/application/tomcat-demo/webapps/possecu_cs/$path"
echo "输出替换文件node2"
ls -al /data/application/tomcat-demo/webapps/possecu_cs/$path
done
if [ "$1" = "r" ];then
echo "重启node1"
ssh -P 50750 10.25.208.243 "/data/application/tomcat-demo/r.sh rr"
check_url http://10.25.208.243:23080/possecu_cs/login/forward_login.htm "管理系统"
while [ $? != "0" ];do
sleep 1s
echo -n "."
check_url http://10.25.208.243:23080/possecu_cs/login/forward_login.htm "管理系统"
done
echo "重启node2"
/data/application/tomcat-demo/r.sh rr
check_url http://127.0.0.1:23080/possecu_cs/login/forward_login.htm "管理系统"
while [ $? != "0" ];do
sleep 1s
echo -n "."
check_url http://127.0.0.1:23080/possecu_cs/login/forward_login.htm "管理系统"
done

fi
}


case "$1" in
nt_demo )
nt_demo
;;

*)
echo "更新nt_demo"
exit 1
esac
exit 0