-
Notifications
You must be signed in to change notification settings - Fork 1
/
mydeploy.sh
218 lines (185 loc) · 4.85 KB
/
mydeploy.sh
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/bin/bash
# Dir List
# mkdir -p /deploy/code/web-demo
# mkdir -p /deploy/config/web-demo/base
# mkdir -p /deploy/config/web-demo/other
# mkdir -p /deploy/tar
# mkdir -p /deploy/tmp
# mkdir -p /opt/webroot
# mkdir /webroot
# chown -R www.www /deploy
# chown -R www.www /opt/webroot
# chown -R www.www /webroot
# SSH Port
SSH_PORT="6122"
# Node List
NODE_IP="192.168.200."
PRE_LIST="221"
GROUP1_LIST="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/mydeploy.lock"
usage(){
echo $"Usage: $0 [ deploy | rollback]"
}
writelog(){
LOGINFO=$1
echo "${CDATE} ${CTIME}: ${SHELL_NAME} : ${LOGINFO} " >> ${SHELL_LOG}
}
shell_lock(){
touch ${LOCK_FILE}
}
url_test(){
URL=$1
echo -e "\033[1;32m[INFO]: Add to cluster...\033[0m"
curl -s --head $URL | grep '200 OK' &>/dev/null && echo -e "\033[1;32m[INFO]: HTTP/1.1 200 OK...\033[0m"
if [ $? -ne 0 ];then
shell_unlock;
writelog "test error";
echo -e "\033[1;31m[ERROR]: This is project deploy Filad !\033[0m"&&exit
else
echo -e "\033[1;32m[INFO]: This is project deploy Successfully!\033[0m"
fi
}
shell_unlock(){
rm -f ${LOCK_FILE}
}
code_get(){
writelog "code_get";
cd $CODE_DIR && git pull
cp -r ${CODE_DIR} ${TMP_DIR}/
API_VERL=$(git show | grep commit | xargs | cut -d ' ' -f2)
API_VER=`echo ${API_VERL:0:6}`
}
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"
}
code_scp(){
writelog "code_scp"
for number in $PRE_LIST $GROUP1_LIST
do
for num in $number
do
scp -P ${SSH_PORT} ${TMP_DIR}/${PKG_NAME}.tar.gz ${NODE_IP}$num:/opt/webroot/
done
done
}
pre_deploy(){
writelog "remove from cluster"
for number in $PRE_LIST
do
for num in $number
do
ssh -p ${SSH_PORT} ${NODE_IP}$num "cd /opt/webroot && tar zxf ${PKG_NAME}.tar.gz"
ssh -p ${SSH_PORT} ${NODE_IP}$num "rm -f /webroot/web-demo && ln -s /opt/webroot/${PKG_NAME} /webroot/web-demo"
done
done
}
pre_test(){
for number in $PRE_LIST
do
for num in $number
do
url_test "http://${NODE_IP}$num/index.html";
done
done
}
group1_deploy(){
writelog "remove from cluster"
for number in $GROUP1_LIST
do
for num in $number
do
ssh -p ${SSH_PORT} ${NODE_IP}$num "cd /opt/webroot && tar zxf ${PKG_NAME}.tar.gz"
ssh -p ${SSH_PORT} ${NODE_IP}$num "rm -f /webroot/web-demo && ln -s /opt/webroot/${PKG_NAME} /webroot/web-demo"
scp -P ${SSH_PORT} ${CONFIG_DIR}/other/192.168.200.222.crontab.xml ${NODE_IP}$num:/webroot/web-demo/crontab.xml
done
done
}
group1_test(){
for number in $GROUP1_LIST
do
for num in $number
do
url_test "http://${NODE_IP}$num/index.html";
done
done
}
rollback(){
writelog "rollback"
for number in $PRE_LIST $GROUP1_LIST
do
for num in $number
do
ssh -p ${SSH_PORT} ${NODE_IP}$num "rm -f /webroot/web-demo && ln -s /opt/webroot/$ROLLBACK_VER /webroot/web-demo"
done
done
}
main(){
if [ -f $LOCK_FILE ];then
echo "Deploy is running" && exit;
fi
DEPLOY_METHOD=$1
ROLLBACK_VER=$2
case $DEPLOY_METHOD in
deploy)
shell_lock;
code_get;
code_build;
code_config;
code_tar;
code_scp;
pre_deploy;
pre_test;
group1_deploy;
group1_test;
shell_unlock;
;;
rollback)
shell_lock;
if [ "$2" == "list" ]
then
ls -l /opt/webroot/*.tar.gz
elif [ "$2" != "" ]
then
if [ -d /opt/webroot/$2 ];then
rollback $ROLLBACK_VER;
pre_test;
group1_test;
else
echo -e "\033[1;31m[ERROR]: Directory is not found!\033[0m"
fi
else
echo " Usage: $0 rollback [ list | version] "
fi
shell_unlock;
;;
*)
usage;
esac
}
main $1 $2