forked from itwanger/paicoding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.sh
155 lines (134 loc) · 3.79 KB
/
deploy.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
#!/usr/bin/env bash
# pid file
PID_FILE_NAME="pid.log"
# file to upload
WEB_PATH="paicoding-web"
EXECUTABLE_JAR_NAME="paicoding-web-0.0.1-SNAPSHOT.jar"
TMP_EXECUTABLE_JAR_NAME=${EXECUTABLE_JAR_NAME}".tmp"
BAK_EXECUTABLE_JAR_NAME=${EXECUTABLE_JAR_NAME}".bak"
EXECUTABLE_JAR_PATH="./${WEB_PATH}/target/${EXECUTABLE_JAR_NAME}"
DEPLOY_SCRIPT="deploy.sh"
START_FUNC_NAME="start"
STOP_FUNC_NAME="stop"
RESTART_FUNC_NAME="restart"
#env, ssh remote, work dir
ENV_PRO="prod"
SSH_HOST_PRO=("[email protected]")
WORK_DIR_PRO="/home/admin/workspace/paicoding-forum/"
# log file
declare LOG_FILES
LOG_BACKUP_FOLDER="logs/"
function stop() {
# kill
echo "--- 应用线下 ---"
if [ -f "${PID_FILE_NAME}" ]; then
pid=$(cat ${PID_FILE_NAME})
echo "kill -9 ${pid}"
kill -9 ${pid}
fi
echo "----------------"
}
function start() {
work_dir=`dirname $0`
cd ${work_dir}
stop
mv ${EXECUTABLE_JAR_NAME} ${BAK_EXECUTABLE_JAR_NAME}
mv ${TMP_EXECUTABLE_JAR_NAME} ${EXECUTABLE_JAR_NAME}
chmod 755 ${EXECUTABLE_JAR_NAME}
# run
echo "===== 启动脚本:====="
run
}
function restart() {
work_dir=`dirname $0`
cd ${work_dir}
stop
# run
echo "===== 启动重启:====="
run
}
function run() {
echo "nohup java -server -Xms512m -Xmx512m -Xmn512m -XX:NativeMemoryTracking=detail -XX:-OmitStackTraceInFastThrow -jar ${EXECUTABLE_JAR_NAME} > /dev/null 2>&1 &"
echo "==================="
nohup java -server -Dspring.devtools.restart.enabled=false -Xms512m -Xmx512m -Xmn512m -XX:NativeMemoryTracking=detail -XX:-OmitStackTraceInFastThrow -jar ${EXECUTABLE_JAR_NAME} "$@" > /dev/null 2>&1 &
echo $! > ${PID_FILE_NAME}
}
function compile() {
echo "---- start to build jar ----"
echo "安装依赖:mvn clean install -Dmaven.test.skip=True -P${1}"
mvn clean install -Dmaven.test.skip=True -P${1}
cd ${WEB_PATH}
echo "构建可运行jar:mvn clean package spring-boot:repackage -Dmaven.test.skip=true -P${1}"
mvn clean package spring-boot:repackage -Dmaven.test.skip=true -P${1}
cd -
ret=$?
if [[ ${ret} -ne 0 ]] ; then
return 1
fi
echo "---------- jar包构建完成 -------------"
}
function upload() {
# upload jar
# rename to *.jar.bak
scp ${EXECUTABLE_JAR_PATH} $1:$2${TMP_EXECUTABLE_JAR_NAME}
ret=$?
if [[ ${ret} -ne 0 ]] ; then
echo 'Failed to scp jar'
return 1
fi
# upload script
scp ${DEPLOY_SCRIPT} $1:$2
ret=$?
if [[ ${ret} -ne 0 ]] ; then
echo 'Failed to scp deploy.sh'
return 1
fi
}
function deploy() {
# package
echo "*******Start to package*******"
compile $1
ret=$?
if [[ ${ret} -ne 0 ]] ; then
echo 'Failed to compile'
exit ${ret}
fi
if [ "$1" = "${ENV_PRO}" ]; then
SSH_HOST=${SSH_HOST_PRO[@]}
WORK_DIR=${WORK_DIR_PRO}
else
echo "Unknown env: $1"
exit
fi
for host in ${SSH_HOST[@]}
do
# upload jar and deploy.sh
echo "*******Start to upload:${host} *******"
upload ${host} ${WORK_DIR}
ret=$?
if [[ ${ret} -ne 0 ]] ; then
echo 'Failed to upload files'
exit ${ret}
fi
done
for host in ${SSH_HOST[@]}
do
# run
echo "*******Start service:${host} *******"
ssh ${host} "bash ${WORK_DIR}${DEPLOY_SCRIPT} ${START_FUNC_NAME}"
echo "*******Done*******"
done
}
if [ "$1" = "${START_FUNC_NAME}" ]; then
start "$@"
elif [ "$1" = "${ENV_PRO}" ]; then
deploy $1
elif [ "$1" = "${STOP_FUNC_NAME}" ]; then
stop
elif [ "$1" = "${RESTART_FUNC_NAME}" ]; then
restart
else
echo "部署jar到服务器: ./deploy.sh prod"
echo "服务器上应用重启: ./deploy.sh restart"
echo "服务器上应用关闭: ./deploy.sh stop"
fi