deploy.sh
12.7 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
#!/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
################################################################
BASE_BIN_DIR=`dirname $0`
. $BASE_BIN_DIR/scripts/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=$USER ##使用当前登陆的系统账户作为git分支
if [ $APP_BRANCH = "root" ]; then
APP_BRANCH=master
fi
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
fi
}
#parse parameter
param $1
param $2
param $3
param $4
param $5
param $6
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"
IDENTITY_FILE="$7 $8"
echo -ne "\033[0m"
################################################################################
################################ Init path for app #############################
BASE_HOME=$HOME
OPPS_HOME=$BASE_HOME/opps
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/$APP_BRANCH
WEB_APP_BACKUP_DIR=$BACKUP_BASE_HOME/$APP_NAME
WEB_APP_ROOT=$REMOTE_BASE_HOME/$APP_NAME
#WEB_APP_HOME=$WEB_APP_ROOT/deploy
AUTOCONFIG_HOME=$OPPS_DEPLOY_HOME/autoconfig/$APP_BRANCH
}
################################# main ######################################
main(){
echo -e "\033[33mPlease choose app to deploy:"
echo -ne "\033[0m"
select item in `ls -l $APPS_BASE_HOME | awk 'NR>1 {print $9}'` Exit
do
if [ $item = "Exit" ]; then
exit
else
#temp=${app_name}_ips
#deploy \${$temp[@]}
APP_NAME=$item
echo -e "\033[33mPlease choose operation:"
echo -ne "\033[0m"
select item in Deploy Rollback Back
do
if [ $item = "Deploy" ]; then
deploy
elif [ $item = "Rollback" ]; then
rollback
elif [ $item = "Back" ]; then
break
else
echo "Input error!"
fi
done
fi
done
}
################################Update source code & package####################
package(){
####update autoconfig
if [ -d "$AUTOCONFIG_HOME" ]; then
echo "[INFO] start to update config..."
cd $AUTOCONFIG_HOME
echo `pwd`
git pull origin master
fi
###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"
cd $PROJECT_SOURCE_BASE_DIR
git clone -b $APP_BRANCH $GIT_BASE_URL$GIT_GROUP/$APP_NAME.git $APP_BRANCH
else
cd $PROJECT_SOURCE_DIR
echo `pwd`
if [ $NOT_UPDATE = "false" ]; then
echo "[INFO] start to update source code from [$GIT_BASE_URL$GIT_GROUP/$APP_NAME.git]"
if [ -z $CODE_VERSION ]; then
echo "[INFO] git pull origin $APP_BRANCH"
git pull origin $APP_BRANCH
else
echo "[INFO] start to update source code to $CODE_VERSION..."
git pull origin $APP_BRANCH $CODE_VERSION
fi
fi
fi
cd $PROJECT_SOURCE_DIR/bin
###package app
echo "[INFO] start to package application..."
if [ ! -d $AUTOCONFIG_HOME ]; then
mkdir -p $AUTOCONFIG_HOME
fi
sh $PROJECT_SOURCE_DIR/bin/mvn-package.sh $AUTOCONFIG_HOME/${APP_NAME}-autoconfig.properties $AUTOCONFIG_HOME/global-autoconfig.properties
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 $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 $PROJECT_SOURCE_DIR/deploy/target/$PROJECT_TARGET ]; then
echo "[INFO] cp -f $PROJECT_SOURCE_DIR/deploy/target/$PROJECT_TARGET $WEB_APP_BACKUP_DIR/$PROJECT_TARGET.$TIMESTAMP"
cp -f $PROJECT_SOURCE_DIR/deploy/target/$PROJECT_TARGET $WEB_APP_BACKUP_DIR/$PROJECT_TARGET.$TIMESTAMP
fi
}
######################################synch app########################################
synch(){
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 $PROJECT_SOURCE_DIR/deploy/target/$PROJECT_TARGET $BASE_HOME/$APP_NAME"
cp $PROJECT_SOURCE_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 $PROJECT_SOURCE_DIR/deploy/target/$PROJECT_TARGET $1:$WEB_APP_ROOT"
scp -p $IDENTITY_FILE -P $SSH_PORT $PROJECT_SOURCE_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
app_ips=${APP_NAME//-/_}_${APP_BRANCH}_ips
if [ $APP_BRANCH = "dev" ]; then
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"
fi
done
else
echo "[INFO] SSH_HOST = $SSH_HOST"
synch $SSH_HOST
ssh $IDENTITY_FILE -p $SSH_PORT $SSH_HOST "sh $WEB_APP_ROOT/function.sh $REMOTE_BASE_HOME $APP_NAME"
fi
else
backup
echo -e "\033[33mPlease choose host to deploy:"
echo -ne "\033[0m"
select item in All `eval echo \\${$app_ips[@]}` Back Home
do
if [ $item = "Back" ]; then
break
elif [ $item = "Home" ]; then
main
elif [ $item = "All" ]; then
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"
fi
done
else
synch $item
if [ $item = "localhost" ]; then
sh $BASE_HOME/$APP_NAME/function.sh $BASE_HOME $APP_NAME
else
ssh $IDENTITY_FILE -p $SSH_PORT $item "sh $WEB_APP_ROOT/function.sh $REMOTE_BASE_HOME $APP_NAME"
fi
fi
done
fi
}
#################################rollback app########################################
rollback(){
init
echo "[INFO] App start to rollback[$WEB_APP_BACKUP_DIR]......."
echo -e "\033[33mPlease choose a version to rollback..."
echo -ne "\033[0m"
select rollback_file_name in `ls -tl $WEB_APP_BACKUP_DIR | awk 'NR>1 {print $9}' | head -10` Back
do
if [ $rollback_file_name = "Back" ]; then
break
else
echo "[INFO] rollback_file_name=$rollback_file_name"
app_ips=${APP_NAME//-/_}_${USER}_ips
echo -e "\033[33mPlease choose host to rollback:"
echo -ne "\033[0m"
select item in All `eval echo \\${$app_ips[@]}` Back Home
do
if [ $item = "Back" ]; then
break
elif [ $item = "Home" ]; then
main
elif [ $item = "All" ]; then
for var in `eval echo \\${$app_ips[@]}`; do
if [ $var = "localhost" ]; then
echo "cp $WEB_APP_BACKUP_DIR/$rollback_file_name $BASE_HOME/$APP_NAME/$PROJECT_TARGET"
cp $WEB_APP_BACKUP_DIR/$rollback_file_name $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"
sh $BASE_HOME/$APP_NAME/function.sh $BASE_HOME $APP_NAME
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_name $var:$WEB_APP_ROOT/$PROJECT_TARGET"
scp -p $IDENTITY_FILE -P $SSH_PORT $WEB_APP_BACKUP_DIR/$rollback_file_name $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"
ssh $IDENTITY_FILE -p $SSH_PORT $var "sh $WEB_APP_ROOT/function.sh $REMOTE_BASE_HOME $APP_NAME"
fi
done
else
if [ $item = "localhost" ]; then
echo "cp $WEB_APP_BACKUP_DIR/$rollback_file_name $BASE_HOME/$APP_NAME/$PROJECT_TARGET"
cp $WEB_APP_BACKUP_DIR/$rollback_file_name $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"
sh $BASE_HOME/$APP_NAME/function.sh $BASE_HOME $APP_NAME
else
if [ -z $USER_HOME ]; then
USER_LOGIN=`awk -v item=$item 'BEGIN{ split (item, host, "@"); print host[1]}'`
init
fi
echo "scp -p $IDENTITY_FILE -P $SSH_PORT $WEB_APP_BACKUP_DIR/$rollback_file_name $item:$WEB_APP_ROOT/$PROJECT_TARGET"
scp -p $IDENTITY_FILE -P $SSH_PORT $WEB_APP_BACKUP_DIR/$rollback_file_name $item:$WEB_APP_ROOT/$PROJECT_TARGET
echo "scp -p $IDENTITY_FILE -P $SSH_PORT $OPPS_DEPLOY_HOME/scripts/function.sh $item:$WEB_APP_ROOT"
scp -p $IDENTITY_FILE -P $SSH_PORT $OPPS_DEPLOY_HOME/scripts/function.sh $item:$WEB_APP_ROOT
echo "ssh $IDENTITY_FILE -p $SSH_PORT $item sh $WEB_APP_ROOT/function.sh $REMOTE_BASE_HOME $APP_NAME"
ssh $IDENTITY_FILE -p $SSH_PORT $item "sh $WEB_APP_ROOT/function.sh $REMOTE_BASE_HOME $APP_NAME"
fi
fi
done
fi
done
}
main