forked from shuangmulin/rpush
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
195 lines (170 loc) · 5.76 KB
/
Copy pathdeploy.sh
File metadata and controls
195 lines (170 loc) · 5.76 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
#!/bin/bash
declare -A jarMap=(
["eureka"]="rpush-eureka-0.0.1-SNAPSHOT.jar"
["zuul"]="rpush-zuul-0.0.1-SNAPSHOT.jar"
# ["scheduler"]="rpush-scheduler-0.0.1-SNAPSHOT.jar"
["route1"]="rpush-route-0.0.1-SNAPSHOT.jar"
["server1"]="rpush-server-0.0.1-SNAPSHOT.jar"
)
java="/opt/appcenter/jdk/jdk1.8/bin/java"
basePath="/opt/appcenter/jar/rpush" # 根路径
function usage() {
echo -e "\e[36m命令使用示例: deploy.sh var1 var2"
echo -e "var1: assign the operation} signal:
start --启动服务(如果服务已启动,这个命令会先停止该服务再启动)
stop --停止服务
status --获取服务状态
init --初始化目录结构"
echo -e "var2: assign the application to operate:"
echo " all ==> 所有服务"
for key in "${!jarMap[@]}"; do
echo " ${key} ==> ${key}服务"
done
}
function progress() {
b="#"
sleeptime=$(echo "$1 50" | awk '{printf("%0.2f", $1/$2)}')
for ((i = 1; $i <= 50; i += 1)); do
prog=$(echo "$i" | awk '{printf("%0.0f", 2*$1)}')
progline="progress:[%-50s]%d%%\r"
printf "${progline}" ${b} ${prog}
b=#${b}
sleep ${sleeptime}
done
echo
}
function init() {
local appName=$1 # 服务名称
local jarPath=$2 # jar包路径 = 根路径 + 服务名称
local configPath=$3 # 配置文件路径 = jar包路径 + yml
local logPath=$4 # 日志路径
echoCommon "########################################[init ${appName}]########################################"
if [ ! -x "${jarPath}" ]; then
echoCommon "jar包路径不存在,尝试新建..."
mkdir -p "${jarPath}"
fi
if [ ! -x "${configPath}" ]; then
echoCommon "配置文件路径不存在,尝试新建..."
mkdir -p "${configPath}"
fi
if [ ! -x "${logPath}" ]; then
echoCommon "日志文件路径不存在,尝试新建..."
mkdir -p "${logPath}"
fi
if [ -x "${jarPath}" ]; then
echoSuccess "[${appName}]目录结构初始化成功..."
echoSuccess " jar包路径:${jarPath}"
echoSuccess "配置文件路径:${configPath}"
echoSuccess "日志文件路径:${logPath}"
else
echoError "[${appName}]目录结构初始化失败..."
fi
}
function echoError() {
echo -e "\033[31m $1 \033[0m"
}
function echoCommon() {
echo -e "\033[36m $1 \036"
}
function echoSuccess() {
echo -e "\033[32m $1 \033[0m"
}
function getPID() {
ps -ef | grep "$1/$2" | egrep -v "grep|$$" | awk 'NR==1{print $2}'
}
function start() {
local appName=$1 # 服务名称
local jarPath=$2 # jar包路径=根路径+服务名称
local configPath=$3 # 配置文件路径=jar包路径+yml
local logPath=$4 # 日志路径
local jarName=$5 # jar包名称
echoCommon "########################################[启动 ${appName}]########################################"
local PID
PID=$(getPID "$configPath")
# 先停服务
[ -n "$PID" ] && kill -9 $PID
if [ ! -f "${jarPath}/${jarName}" ]; then
echoError "错误:jar包[ ${jarPath}/${jarName} ]不存在,请确认!"
exit
fi
if [ ! -f "${jarPath}/${jarName}" ]; then
echoError "错误:配置文件[ ${configPath}/application.yml ]不存在,请确认!"
exit
fi
if [ ! -x "${logPath}" ]; then
echoError "错误:日志文件夹[ ${logPath} ]不存在,请确认!"
exit
fi
cd "${jarPath}" || exit
local s
s="nohup ${java} -Xms256m -Xmx256m -Dfile.encoding=UTF-8 -Duser.timezone=GMT+08 -jar ${jarPath}/${jarName} --spring.config.location=${configPath}/application.yml > ${logPath}/${appName}.log 2>&1 &"
echo "${s}"
nohup ${java} -Xms256m -Xmx256m -Dfile.encoding=UTF-8 -Duser.timezone=GMT+08 -jar ${jarPath}/${jarName} --spring.config.location=${configPath}/application.yml >${logPath}/${appName}.log 2>&1 &
progress 3
PID=$(ps -ef | grep "${jarPath}/${jarName}" | egrep -v "grep|$$" | awk 'NR==1{print $2}')
echoSuccess "[${appName}]启动成功..."
echoSuccess "日志文件路径:${logPath}"
}
function stop() {
local PID
PID=$(getPID "$configPath")
echoCommon "########################################[停止 $3]########################################"
[ -z "$PID" ] && echoCommon "PID: $PID"
# 停服务
[ -n "$PID" ] && kill -9 $PID
PID=$(getPID "$configPath")
if [ "$PID" == "" ]; then
echoSuccess "[${appName}]已停止..."
fi
}
function status() {
local PID
PID=$(getPID "$configPath")
echoCommon "########################################[$3 状态]########################################"
if [ "$PID" == "" ]; then
echoCommon "[${appName}]已停止..."
else
echoCommon "[${appName}]正常运行,进程ID:$PID..."
fi
}
function main() {
if [[ $# -lt 2 || $# -gt 3 ]]; then
usage
exit
fi
local appNameParam=$2 # 服务名称参数
local operation=$1
for key in ${!jarMap[*]}; do
local jarName=${jarMap[$key]} # jar包名称
local appName=${key} # 服务名称
if [ "${appNameParam}" != "all" ] && [ "${appNameParam}" != "${appName}" ]; then
continue
fi
local jarPath=${basePath} # jar包路径 = 根路径
local configPath=${jarPath}/yml/${appName} # 配置文件路径 = jar包路径 + yml + 服务名称
local logPath=${jarPath}/logs/ # 日志路径 + logs + 服务名称
case "${operation}" in
"init")
# 初始化目录结构
init "${appName}" "${jarPath}" "${configPath}" "${logPath}"
;;
"start")
# 服务启动
start "${appName}" "${jarPath}" "${configPath}" "${logPath}" "${jarName}"
;;
"stop")
# 服务停止
stop "${jarPath}" "${jarName}" "${appName}"
;;
"status")
# 服务状态
status "${jarPath}" "${jarName}" "${appName}"
;;
*)
usage
exit
;;
esac
done
}
main "$@"