deploy.sh
13.6 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#!/bin/bash
################################################################
#@Authro Lijian
#@Date 2007-06-08
#@param1 -h target host
#@param2 -p ssh port for target host
#@param3 -v git pull version or commit
#@param4 -c if true, only change autoconfig, don't update source code
#@param5 -g git group, not null
#@param6 -b app branch for git
#@param7 -a app name
#@param8 -e Deploy or Rollback or other
#@param9 -s servers aws qcloud dev test
#@param10 -r rollback file
#@param11 -t tag
#/bin/sh deploy.sh -gyoho30 -p22 -bdev -ayoho-message -eDeploy -sdev
################################################################
BASE_BIN_DIR=`dirname $0`
#. $BASE_BIN_DIR/scripts/iptable/qcloud_gray/app-iptable.sh
#. $BASE_BIN_DIR/scripts/iptable/aws/app-iptable.sh
#. $BASE_BIN_DIR/scripts/iptable/qcloud/app-iptable.sh
#. $BASE_BIN_DIR/scripts/iptable/test/app-iptable.sh
#. $BASE_BIN_DIR/scripts/iptable/dev/app-iptable.sh
#. $BASE_BIN_DIR/scripts/iptable/test_nj_1/app-iptable.sh
#. $BASE_BIN_DIR/scripts/iptable/test_nj_2/app-iptable.sh
. $BASE_BIN_DIR/scripts/env.sh
################### Input parameter for user ###################################
SSH_HOST=""
SSH_PORT=22
CODE_VERSION=""
NOT_UPDATE="false"
GIT_GROUP=$DEFAULT_GIT_GROUP
APP_BRANCH=""
APP_NAME=""
EXE_COMMOND=""
SERVERS=""
ROLLBACK_FILE=""
TAG_NAME=""
PACKAGE_RESULT=""
param(){
if [ $1 ]; then
prefix=`expr substr $1 1 2`
if [ $prefix = "-h" ]; then
SSH_HOST=`echo $1|awk '{print substr($0, 3)}'`
fi
if [ $prefix = "-p" ]; then
SSH_PORT=`echo $1|awk '{print substr($0, 3)}'`
fi
if [ $prefix = "-v" ]; then
CODE_VERSION=`echo $1|awk '{print substr($0,3)}'`
fi
if [ $prefix = "-c" ]; then
NOT_UPDATE="true"
fi
if [ $prefix = "-g" ]; then
GIT_GROUP=`echo $1|awk '{print substr($0,3)}'`
fi
if [ $prefix = "-b" ]; then
APP_BRANCH=`echo $1|awk '{print substr($0,3)}'`
fi
if [ $prefix = "-a" ]; then
APP_NAME=`echo $1|awk '{print substr($0,3)}'`
fi
if [ $prefix = "-e" ]; then
EXE_COMMOND=`echo $1|awk '{print substr($0,3)}'`
fi
if [ $prefix = "-s" ]; then
SERVERS=`echo $1|awk '{print substr($0,3)}'`
fi
if [ $prefix = "-r" ]; then
ROLLBACK_FILE=`echo $1|awk '{print substr($0,3)}'`
fi
if [ $prefix = "-t" ]; then
TAG_NAME=`echo $1|awk '{print substr($0,3)}'`
fi
fi
}
#parse parameter
param $1
param $2
param $3
param $4
param $5
param $6
param $7
param $8
param $9
param ${10}
param ${11}
if [ -z $GIT_GROUP ]; then
echo "**************************************************************************"
echo "*Error: git group is null [$GIT_BASE_URL GIT_GROUP/$APP_NAME.git]*********"
echo "*Please add -g args. For example: sh deploy -gyoho30, yoho30 is git group*"
echo "**************************************************************************"
exit 1
fi
echo -e "\033[33m[INFO] GIT_BASE_URL=$GIT_BASE_URL$GIT_GROUP"
echo -e "\033[33m[INFO] APP_BRANCH=$APP_BRANCH"
echo -e "\033[33m[INFO] SSH_HOST=$SSH_HOST"
echo -e "\033[33m[INFO] SSH_PORT=$SSH_PORT"
echo -e "\033[33m[INFO] CODE_VERSION=$CODE_VERSION"
echo -e "\033[33m[INFO] NOT_UPDATE=$NOT_UPDATE"
echo -e "\033[33m[INFO] APP_NAME=$APP_NAME"
echo -e "\033[33m[INFO] EXE_COMMOND=$EXE_COMMOND"
echo -e "\033[33m[INFO] SERVERS=$SERVERS"
echo -e "\033[33m[INFO] ROLLBACK_FILE=$ROLLBACK_FILE"
echo -e "\033[33m[INFO] TAG_NAME=$TAG_NAME"
IDENTITY_FILE=
echo -ne "\033[0m"
################################################################################
################################ Init path for app #############################
BASE_HOME=$HOME
#OPPS_HOME=$BASE_HOME/opps
OPPS_HOME=$BASE_HOME/auto_deploy_test
OPPS_DEPLOY_HOME=$OPPS_HOME/deploy
APPS_BASE_HOME=$OPPS_HOME/apps
BACKUP_BASE_HOME=$OPPS_HOME/backup
init(){
REMOTE_BASE_HOME=/home/$USER_LOGIN
PROJECT_TARGET=$APP_NAME.zip
PROJECT_SOURCE_BASE_DIR=$APPS_BASE_HOME/$APP_NAME
PROJECT_SOURCE_DIR=$PROJECT_SOURCE_BASE_DIR/$SERVERS
ROJECT_SOURCE_BRANCH_DIR=$PROJECT_SOURCE_DIR/$APP_BRANCH
WEB_APP_BACKUP_DIR_PARENT=$BACKUP_BASE_HOME/$APP_NAME
WEB_APP_BACKUP_DIR=$WEB_APP_BACKUP_DIR_PARENT/$SERVERS
WEB_APP_ROOT=$REMOTE_BASE_HOME/$APP_NAME
#WEB_APP_HOME=$WEB_APP_ROOT/deploy
AUTOCONFIG_HOME=$OPPS_DEPLOY_HOME/autoconfig/$SERVERS
IPATBLE_HOME=$OPPS_DEPLOY_HOME/scripts/iptable/$SERVERS
AUTOCONFIG_HOME_BASE=$OPPS_DEPLOY_HOME/autoconfig
IPATBLE_HOME_BASE=$OPPS_DEPLOY_HOME/scripts/iptable
}
################################# main ######################################
main(){
if [ $EXE_COMMOND = "Deploy" ]; then
deploy
elif [ $EXE_COMMOND = "Rollback" ]; then
rollback
elif [ $EXE_COMMOND = "Back" ]; then
break
else
echo "EXE_COMMOND error!"
fi
}
################################Update source code & package####################
package(){
####update autoconfig
if [ -d "$AUTOCONFIG_HOME" ]; then
echo "[INFO] start to update config..."
cd $AUTOCONFIG_HOME_BASE
echo `pwd`
git pull origin master
fi
####update iptables
if [ -d "$IPATBLE_HOME" ]; then
echo "[INFO] start to update ips..."
cd $IPATBLE_HOME_BASE
echo `pwd`
git pull origin master
fi
## always remove old code
echo " start to remove existed src folder: $ROJECT_SOURCE_BRANCH_DIR "
rm -rf $ROJECT_SOURCE_BRANCH_DIR
###update source code
if [ ! -d "$PROJECT_SOURCE_DIR" ]; then
echo "[INFO] git clone -b $APP_BRANCH $GIT_BASE_URL$GIT_GROUP/$APP_NAME.git $APP_BRANCH"
if [ ! -d "$APPS_BASE_HOME" ]; then
echo "[INFO] make APPS_BASE_HOME :" $APPS_BASE_HOME
mkdir $APPS_BASE_HOME
fi
if [ ! -d "$PROJECT_SOURCE_BASE_DIR" ]; then
echo "[INFO] make PROJECT_SOURCE_BASE_DIR :" $PROJECT_SOURCE_BASE_DIR
mkdir $PROJECT_SOURCE_BASE_DIR
fi
if [ ! -d "$PROJECT_SOURCE_DIR" ]; then
echo "[INFO] make PROJECT_SOURCE_DIR :" $PROJECT_SOURCE_DIR
mkdir $PROJECT_SOURCE_DIR
fi
fi
cd $PROJECT_SOURCE_DIR
git clone -b $APP_BRANCH $GIT_BASE_URL$GIT_GROUP/$APP_NAME.git $APP_BRANCH
## finaly, checkout to tags
if [ "$TAG_NAME" != "" ]; then
echo "start to checkout out to : $TAG_NAME"
cd $ROJECT_SOURCE_BRANCH_DIR
git tag -l
git checkout tags/$TAG_NAME -b branch-$TAG_NAME
fi
cd $ROJECT_SOURCE_BRANCH_DIR/bin
###package app
echo "[INFO] start to package application..."
if [ ! -d $AUTOCONFIG_HOME ]; then
mkdir -p $AUTOCONFIG_HOME
fi
result=`sh $ROJECT_SOURCE_BRANCH_DIR/bin/mvn-package.sh $AUTOCONFIG_HOME/${APP_NAME}-autoconfig.properties $AUTOCONFIG_HOME/global-autoconfig.properties`
echo "$result"
if [[ "$result" =~ "FAILURE" ]] && [[ "$result" =~ "ERROR" ]];then
PACKAGE_RESULT="FAILED"
else
PACKAGE_RESULT="SUCCESS"
fi
echo "[INFO] package application finish."
###commit autoconfig
ciautoconfig
}
####################################commit autoconfig#################################
ciautoconfig(){
echo "[INFO] start to commit autoconfig..."
cd $AUTOCONFIG_HOME
echo "[INFO] current path: "`pwd`
echo "[INFO] git pull origin master"
git pull origin master > /dev/null
echo "[INFO] git add $AUTOCONFIG_HOME/${APP_NAME}-autoconfig.properties"
git add $AUTOCONFIG_HOME/${APP_NAME}-autoconfig.properties > /dev/null
echo "[INFO] git commit -m "commit autoconfig." $AUTOCONFIG_HOME/${APP_NAME}-autoconfig.properties"
git commit -m "commit autoconfig." $AUTOCONFIG_HOME/${APP_NAME}-autoconfig.properties > /dev/null
echo "[INFO] git -u push origin master"
git push -u origin master > /dev/null
echo "[INFO] commit autoconfig finish."
}
####################################Backup app########################################
backup(){
if [ -d $BACKUP_BASE_HOME ]; then
echo "[INFO] BACKUP_BASE_HOME dir exist."
else
mkdir $BACKUP_BASE_HOME
fi
if [ -d $WEB_APP_BACKUP_DIR_PARENT ]; then
echo "[INFO] WEB_APP_BACKUP_DIR_PARENT dir exist."
else
mkdir $WEB_APP_BACKUP_DIR_PARENT
fi
if [ -d $WEB_APP_BACKUP_DIR ]; then
echo "[INFO] backup dir exist."
else
mkdir $WEB_APP_BACKUP_DIR
fi
echo "[INFO] backup dir is [$WEB_APP_BACKUP_DIR]"
if [ -f $ROJECT_SOURCE_BRANCH_DIR/deploy/target/$PROJECT_TARGET ]; then
echo "[INFO] cp -f $ROJECT_SOURCE_BRANCH_DIR/deploy/target/$PROJECT_TARGET $WEB_APP_BACKUP_DIR/$PROJECT_TARGET.$TIMESTAMP"
cp -f $ROJECT_SOURCE_BRANCH_DIR/deploy/target/$PROJECT_TARGET $WEB_APP_BACKUP_DIR/$PROJECT_TARGET.$TIMESTAMP
fi
}
######################################synch app########################################
synch(){
echo "[INFO] synch " $1
if [ $1 = "localhost" ]; then
echo "[INFO] mkdir $OPPS_DEPLOY_HOME/$APP_NAME"
mkdir $OPPS_DEPLOY_HOME/$APP_NAME
echo "[INFO] cp -r $OPPS_DEPLOY_HOME/$APP_NAME $BASE_HOME"
cp -r $OPPS_DEPLOY_HOME/$APP_NAME $BASE_HOME
echo "[INFO] rm -rf $OPPS_DEPLOY_HOME/$APP_NAME"
rm -rf $OPPS_DEPLOY_HOME/$APP_NAME
echo "[INFO] cp $ROJECT_SOURCE_BRANCH_DIR/deploy/target/$PROJECT_TARGET $BASE_HOME/$APP_NAME"
cp $ROJECT_SOURCE_BRANCH_DIR/deploy/target/$PROJECT_TARGET $BASE_HOME/$APP_NAME
echo "[INFO] cp $OPPS_DEPLOY_HOME/scripts/function.sh $BASE_HOME/$APP_NAME"
cp $OPPS_DEPLOY_HOME/scripts/function.sh $BASE_HOME/$APP_NAME
else
if [ -z $USER_HOME ]; then
USER_LOGIN=`awk -v item=$1 'BEGIN{ split (item, host, "@"); print host[1]}'`
init
fi
echo "[INFO] mkdir $OPPS_DEPLOY_HOME/$APP_NAME"
mkdir $OPPS_DEPLOY_HOME/$APP_NAME
echo "[INFO] scp -p $IDENTITY_FILE -r -P $SSH_PORT $OPPS_DEPLOY_HOME/$APP_NAME $1:$REMOTE_BASE_HOME"
scp -p $IDENTITY_FILE -r -P $SSH_PORT $OPPS_DEPLOY_HOME/$APP_NAME $1:$REMOTE_BASE_HOME
echo "[INFO] rm -rf $OPPS_DEPLOY_HOME/$APP_NAME"
rm -rf $OPPS_DEPLOY_HOME/$APP_NAME
echo "[INFO] scp -p $IDENTITY_FILE -P $SSH_PORT $ROJECT_SOURCE_BRANCH_DIR/deploy/target/$PROJECT_TARGET $1:$WEB_APP_ROOT"
scp -p $IDENTITY_FILE -P $SSH_PORT $ROJECT_SOURCE_BRANCH_DIR/deploy/target/$PROJECT_TARGET $1:$WEB_APP_ROOT
echo "[INFO] scp -p $IDENTITY_FILE -P $SSH_PORT $OPPS_DEPLOY_HOME/scripts/function.sh $1:$WEB_APP_ROOT"
scp -p $IDENTITY_FILE -P $SSH_PORT $OPPS_DEPLOY_HOME/scripts/function.sh $1:$WEB_APP_ROOT
fi
}
#################################################################################
##################################deploy app########################################
deploy(){
init
package
if [[ $PACKAGE_RESULT = "FAILED" ]];then
echo "maven package failed"
exit
fi
backup
#后加载ips
. $IPATBLE_HOME/app-iptable.sh
app_ips=${APP_NAME//-/_}_${SERVERS}_ips
if [ -z $SSH_HOST ]; then
if [ -z `eval echo \\${$app_ips[@]}` ]; then
app_ips=DEV_DEFAULT_SERVER
fi
echo "[INFO] app_ips = ${app_ips[*]}"
for var in `eval echo \\${$app_ips[@]}`; do
synch $var
if [ $var = "localhost" ]; then
sh $BASE_HOME/$APP_NAME/function.sh $BASE_HOME $APP_NAME
else
ssh $IDENTITY_FILE -p $SSH_PORT $var "sh $WEB_APP_ROOT/function.sh $REMOTE_BASE_HOME $APP_NAME Deploy"
fi
done
else
echo "[INFO] SSH_HOST = $SSH_HOST"
arr=(${SSH_HOST//,/ })
for var in ${arr[@]}; do
synch $var
ssh $IDENTITY_FILE -p $SSH_PORT $var "sh $WEB_APP_ROOT/function.sh $REMOTE_BASE_HOME $APP_NAME Deploy"
done
fi
}
#################################rollback app########################################
rollback(){
init
#后加载ips
. $IPATBLE_HOME/app-iptable.sh
echo "[INFO] App start to rollback[$WEB_APP_BACKUP_DIR]......."
echo -e "\033[33mPlease choose a version to rollback..."
echo -ne "\033[0m"
echo "[INFO] ROLLBACK_FILE=$ROLLBACK_FILE"
app_ips=${APP_NAME//-/_}_${SERVERS}_ips
if [ -z $SSH_HOST ]; then
if [ -z `eval echo \\${$app_ips[@]}` ]; then
app_ips=DEV_DEFAULT_SERVER
fi
echo "[INFO] app_ips = ${app_ips[*]}"
for var in `eval echo \\${$app_ips[@]}`; do
synch $var
if [ $var = "localhost" ]; then
echo "cp $WEB_APP_BACKUP_DIR/$ROLLBACK_FILE $BASE_HOME/$APP_NAME/$PROJECT_TARGET"
cp $WEB_APP_BACKUP_DIR/$ROLLBACK_FILE $BASE_HOME/$APP_NAME/$PROJECT_TARGET
echo "cp $OPPS_DEPLOY_HOME/scripts/function.sh $BASE_HOME/$APP_NAME"
cp $OPPS_DEPLOY_HOME/scripts/function.sh $BASE_HOME/$APP_NAME
echo "sh $BASE_HOME/$APP_NAME/function.sh $BASE_HOME $APP_NAME Deploy"
sh $BASE_HOME/$APP_NAME/function.sh $BASE_HOME $APP_NAME Deploy
else
if [ -z $USER_HOME ]; then
USER_LOGIN=`awk -v item=$var 'BEGIN{ split (item, host, "@"); print host[1]}'`
init
fi
echo "scp -p $IDENTITY_FILE -P $SSH_PORT $WEB_APP_BACKUP_DIR/$ROLLBACK_FILE $var:$WEB_APP_ROOT/$PROJECT_TARGET"
scp -p $IDENTITY_FILE -P $SSH_PORT $WEB_APP_BACKUP_DIR/$ROLLBACK_FILE $var:$WEB_APP_ROOT/$PROJECT_TARGET
echo "scp -p $IDENTITY_FILE -P $SSH_PORT $OPPS_DEPLOY_HOME/scripts/function.sh $var:$WEB_APP_ROOT"
scp -p $IDENTITY_FILE -P $SSH_PORT $OPPS_DEPLOY_HOME/scripts/function.sh $var:$WEB_APP_ROOT
echo "ssh $IDENTITY_FILE -p $SSH_PORT $var sh $WEB_APP_ROOT/function.sh $REMOTE_BASE_HOME $APP_NAME Deploy"
ssh $IDENTITY_FILE -p $SSH_PORT $var "sh $WEB_APP_ROOT/function.sh $REMOTE_BASE_HOME $APP_NAME Deploy"
fi
done
else
echo "[INFO] SSH_HOST = $SSH_HOST"
arr=(${SSH_HOST//,/ })
for var in ${arr[@]}; do
if [ -z $USER_HOME ]; then
USER_LOGIN=`awk -v item=$var 'BEGIN{ split (item, host, "@"); print host[1]}'`
init
fi
echo "scp -p $IDENTITY_FILE -P $SSH_PORT $WEB_APP_BACKUP_DIR/$ROLLBACK_FILE $var:$WEB_APP_ROOT/$PROJECT_TARGET"
scp -p $IDENTITY_FILE -P $SSH_PORT $WEB_APP_BACKUP_DIR/$ROLLBACK_FILE $var:$WEB_APP_ROOT/$PROJECT_TARGET
echo "scp -p $IDENTITY_FILE -P $SSH_PORT $OPPS_DEPLOY_HOME/scripts/function.sh $var:$WEB_APP_ROOT"
scp -p $IDENTITY_FILE -P $SSH_PORT $OPPS_DEPLOY_HOME/scripts/function.sh $var:$WEB_APP_ROOT
echo "ssh $IDENTITY_FILE -p $SSH_PORT $var sh $WEB_APP_ROOT/function.sh $REMOTE_BASE_HOME $APP_NAME Deploy"
ssh $IDENTITY_FILE -p $SSH_PORT $var "sh $WEB_APP_ROOT/function.sh $REMOTE_BASE_HOME $APP_NAME Deploy"
done
fi
}
main