-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.sh
155 lines (135 loc) · 4.06 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="codeforum-web"
EXECUTABLE_JAR_NAME="codeforum-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"
# Environment, SSH remote, working directory
ENV_PRO="prod"
SSH_HOST_PRO=("[email protected]")
WORK_DIR_PRO="/home/admin/workspace/codeforum/"
# Log file
declare LOG_FILES
LOG_BACKUP_FOLDER="logs/"
function stop() {
# Kill the application
echo "--- Stopping the application ---"
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 the application
echo "===== Starting the application: ====="
run
}
function restart() {
work_dir=`dirname $0`
cd ${work_dir}
stop
# Run the application
echo "===== Restarting the application: ====="
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 "---- Starting to build the JAR file ----"
echo "Installing dependencies: mvn clean install -Dmaven.test.skip=True -P${1}"
mvn clean install -Dmaven.test.skip=True -P${1}
cd ${WEB_PATH}
echo "Building runnable 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 file build completed -------------"
}
function upload() {
# Upload the JAR file
# Rename to *.jar.bak
scp ${EXECUTABLE_JAR_PATH} $1:$2${TMP_EXECUTABLE_JAR_NAME}
ret=$?
if [[ ${ret} -ne 0 ]] ; then
echo 'Failed to upload the JAR file'
return 1
fi
# Upload the deploy.sh script
scp ${DEPLOY_SCRIPT} $1:$2
ret=$?
if [[ ${ret} -ne 0 ]] ; then
echo 'Failed to upload 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 environment: $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 the application
echo "******* Start service: ${host} *******"
ssh ${host} "bash ${WORK_DIR}${DEPLOY_SCRIPT} ${START_FUNC_NAME}"
echo "******* Done *******"
done
}
# Check for command-line arguments
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 "Deploy the JAR to the server: ./deploy.sh prod"
echo "Restart the application on the server: ./deploy.sh restart"
echo "Stop the application on the server: ./deploy.sh stop"
fi