0%

Shell脚本项目部署

当需要更新多台服务器的时候,通过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
#!/bin/sh


check_url(){
if [ "$1" == "help" ] || [ "$1" == "" ];then
echo "参数1 参数2 参数3"
echo "URL 页面内容 轮询检测[r](如:check_url http://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
}

dates=`date +%Y-%m-%d-%H%M`
DATADIR="/data/application/event-service"


cd /data/jenkins/workspace/event_dao_new/event-bean
/usr/local/maven3/bin/mvn clean install -Dmaven.test.skip=true -U

cd /data/jenkins/workspace/event_dao_new/event-dao
/usr/local/maven3/bin/mvn clean install -Dmaven.test.skip=true -U

cd /data/jenkins/workspace/event_dao_new/event-daoservice
/usr/local/maven3/bin/mvn clean compile -U

echo -e "\033[32m 编译成功 \033[0m"

echo -e "\033[32m 开始更新event-service \033[0m"

echo -e "\033[32m 开始打包event-service \033[0m"
cd /data/jenkins/workspace/event_dao_new/event-daoservice/target/classes

rm -rf com.zip
zip -r com.zip com


host=("10.80.220.200" "10.31.54.54" "10.80.60.167" "10.80.112.69" "10.31.52.46" "10.241.104.193" "10.80.226.189")

for i in ${host[@]}; do

ssh -p 50750 root@$i "rm $DATADIR/classes/com.zip"
echo -e "\033[32m 远程临时文件删除完成event-service \033[0m"

ssh -p 50750 root@$i "cp $DATADIR/lib/event-bean-1.0-SNAPSHOT.jar $DATADIR/lib/event-bean-1.0-SNAPSHOT.jar$dates"
ssh -p 50750 root@$i "mv $DATADIR/classes/com $DATADIR/classes/com$dates"
echo -e "\033[32m 备份event-bean、com完成event-service \033[0m"

scp -P 50750 /data/jenkins/workspace/event_dao_new/event-bean/target/event-bean-1.0-SNAPSHOT.jar $i:$DATADIR/lib/
scp -P 50750 /data/jenkins/workspace/event_dao_new/event-daoservice/target/classes/com.zip $i:$DATADIR/classes/
echo -e "\033[32m event-bean、com上传完成event-service \033[0m"

ssh -p 50750 root@$i "unzip $DATADIR/classes/com.zip -d $DATADIR/classes/"
echo -e "\033[32m com解压完成event-service \033[0m"

echo -e "\033[32m 探测event-service67是否启动成功 \033[0m"
ssh -p 50750 root@$i "/data/application/event-service/bin/event-service.sh restart"
check_url http://$i:7700/getServiceVersion "version"
while [ $? != "0" ];do
sleep 1s
echo -n "."
check_url http://$i:7700/getServiceVersion "version"
done

curl -s --connect-timeout 5 -m 8 http://$i:7700/getServiceVersion

done