forked from metersphere/installer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msctl
executable file
·310 lines (292 loc) · 10.4 KB
/
msctl
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
#!/bin/bash
action=$1
target=$2
args=$@
source ~/.msrc >/dev/null
MS_BASE=${MS_BASE:-/opt}
COMPOSE_FILES=$(cat ${MS_BASE}/metersphere/compose_files 2>/dev/null || echo "")
source ${MS_BASE}/metersphere/install.conf
export COMPOSE_HTTP_TIMEOUT=180
function usage() {
echo "MeterSphere 控制脚本"
echo
echo "Usage: "
echo " ./msctl.sh [COMMAND] [ARGS...]"
echo " ./msctl.sh --help"
echo
echo "Commands: "
echo " status 查看 MeterSphere 服务运行状态"
echo " start 启动 MeterSphere 服务"
echo " stop 停止 MeterSphere 服务"
echo " restart 重启 MeterSphere 服务"
echo " reload 重新加载 MeterSphere 服务"
echo " upgrade 升级 MeterSphere 至最新版本"
echo " upgrade [RELEASE] 根据版本号搜索离线包,升级 MeterSphere 至对应版本"
echo " uninstall 卸载 MeterSphere 服务"
echo " version 查看 MeterSphere 版本信息"
}
function generate_compose_files() {
compose_files="-f docker-compose-base.yml"
mkdir -p ${MS_BASE}/metersphere/data/jmeter
mkdir -p ${MS_BASE}/metersphere/data/body
case ${MS_INSTALL_MODE} in
allinone)
compose_files="${compose_files} -f docker-compose-data-streaming.yml -f docker-compose-node-controller.yml -f docker-compose-eureka.yml -f docker-compose-gateway.yml -f docker-compose-api-test.yml -f docker-compose-performance-test.yml -f docker-compose-project-management.yml -f docker-compose-system-setting.yml -f docker-compose-report-stat.yml -f docker-compose-test-track.yml -f docker-compose-workstation.yml"
;;
server)
compose_files="${compose_files} -f docker-compose-eureka.yml -f docker-compose-gateway.yml -f docker-compose-api-test.yml -f docker-compose-performance-test.yml -f docker-compose-project-management.yml -f docker-compose-system-setting.yml -f docker-compose-report-stat.yml -f docker-compose-test-track.yml -f docker-compose-workstation.yml"
;;
node-controller)
compose_files="${compose_files} -f docker-compose-node-controller.yml"
;;
selenium-hub)
compose_files="${compose_files} -f docker-compose-seleniarm.yml"
;;
middleware)
compose_files="${compose_files} -f docker-compose-data-streaming.yml"
;;
*)
log "... 不支持的安装模式,请从 [ allinone | server | node-controller | selenium-hub | middleware ] 中进行选择"
;;
esac
if [ "${MS_INSTALL_MODE}" != "node-controller" ] && [ "${MS_INSTALL_MODE}" != "selenium-hub" ] && [ "${MS_INSTALL_MODE}" != "server" ]; then
# 是否使用外部数据库
if [ "${MS_EXTERNAL_MYSQL}" = "false" ]; then
mkdir -p ${MS_BASE}/metersphere/data/mysql
chmod 655 ${MS_BASE}/metersphere/conf/my.cnf
compose_files="${compose_files} -f docker-compose-mysql.yml"
# 中间件去掉 depends_on
if [ "${MS_INSTALL_MODE}" = "middleware" ]; then
sed -i -e '/system-setting/,+3d' ${MS_BASE}/metersphere/docker-compose-mysql.yml
fi
fi
# 是否使用外部 Kafka
if [ "${MS_EXTERNAL_KAFKA}" = "false" ]; then
mkdir -p ${MS_BASE}/metersphere/data/kafka
compose_files="${compose_files} -f docker-compose-kafka.yml"
if [ "${MS_INSTALL_MODE}" = "middleware" ]; then
sed -i -e '/data-streaming/,+3d' ${MS_BASE}/metersphere/docker-compose-kafka.yml
fi
fi
# 是否使用外部 Prometheus
if [ "${MS_EXTERNAL_PROM}" = "false" ]; then
mkdir -p ${MS_BASE}/metersphere/data/prometheus
compose_files="${compose_files} -f docker-compose-prometheus.yml"
fi
# 是否使用外部 Redis
if [ "${MS_EXTERNAL_REDIS}" = "false" ]; then
mkdir -p ${MS_BASE}/metersphere/data/redis
compose_files="${compose_files} -f docker-compose-redis.yml"
if [ "${MS_INSTALL_MODE}" = "middleware" ]; then
sed -i -e '/system-setting/,+3d' ${MS_BASE}/metersphere/docker-compose-redis.yml
fi
fi
# 是否使用外部 minio
if [ "${MS_EXTERNAL_MINIO}" = "false" ]; then
mkdir -p ${MS_BASE}/metersphere/data/minio
compose_files="${compose_files} -f docker-compose-minio.yml"
fi
# 是否创建 ui 容器
if [ "${MS_UI_ENABLED}" = "true" ]; then
compose_files="${compose_files} -f docker-compose-ui-test.yml"
# 是否使用外部grid
if [ "${MS_EXTERNAL_SELENIUM}" = "false" ]; then
compose_files="${compose_files} -f docker-compose-seleniarm.yml"
fi
fi
fi
echo ${compose_files} >${MS_BASE}/metersphere/compose_files
mkdir -p ${MS_BASE}/metersphere/logs && chmod 777 -R ${MS_BASE}/metersphere/logs
mkdir -p ${MS_BASE}/metersphere/data/body && chmod 777 -R ${MS_BASE}/metersphere/data/body
mkdir -p ${MS_BASE}/metersphere/data/api-folder && chmod 777 -R ${MS_BASE}/metersphere/data/api-folder
mkdir -p ${MS_BASE}/metersphere/data/node && chmod 777 -R ${MS_BASE}/metersphere/data/node
chmod +rw -R ${MS_BASE}/metersphere/conf
chmod +rw -R ${MS_BASE}/metersphere/*.yml
COMPOSE_FILES=$(cat ${MS_BASE}/metersphere/compose_files 2>/dev/null || echo "")
}
function download() {
# wget -nv -T 60 -t 1 --no-check-certificate https://github.com/metersphere/metersphere/releases/download/${MS_LATEST_VERSION}/metersphere-online-installer-${MS_LATEST_VERSION}.tar.gz -O /tmp/metersphere-online-installer-${MS_LATEST_VERSION}.tar.gz
git_urls=('github.com' 'hub.fastgit.org' 'ghproxy.com/https://github.com')
for git_url in ${git_urls[*]}; do
success="true"
for i in {1..3}; do
echo -ne "检测 ${git_url} ... ${i} "
curl -m 5 -kIs https://${git_url} >/dev/null
if [ $? != 0 ]; then
echo "failed"
success="false"
break
else
echo "ok"
fi
done
if [ ${success} == "true" ]; then
server_url=${git_url}
break
fi
done
if [ "x${server_url}" == "x" ]; then
echo "没有找到稳定的下载服务器,请稍候重试"
exit 1
fi
echo "使用下载服务器 ${server_url}"
cd /tmp
wget -nv -T 60 -t 1 --no-check-certificate https://${server_url}/metersphere/metersphere/releases/download/${MS_LATEST_VERSION}/metersphere-online-installer-${MS_LATEST_VERSION}.tar.gz -O /tmp/metersphere-online-installer-${MS_LATEST_VERSION}.tar.gz
if [ $? -ne 0 ]; then
echo -e "\e[31m升级失败:连接下载服务器超时!\n可手动下载升级包,然后执行\e[1;33m msctl upgrade ${MS_LATEST_VERSION} \e[0;31m离线升级\e[0m"
return 2
fi
}
function status() {
echo
cd ${MS_BASE}/metersphere
docker-compose ${COMPOSE_FILES} ps
}
function start() {
echo
cd ${MS_BASE}/metersphere
docker-compose ${COMPOSE_FILES} start ${target}
}
function stop() {
echo
cd ${MS_BASE}/metersphere
docker-compose ${COMPOSE_FILES} stop ${target}
}
function restart() {
echo
cd ${MS_BASE}/metersphere
docker-compose ${COMPOSE_FILES} stop ${target}
docker-compose ${COMPOSE_FILES} start ${target}
}
function reload() {
echo
cd ${MS_BASE}/metersphere
docker-compose ${COMPOSE_FILES} up -d --remove-orphans
}
function uninstall() {
echo
cd ${MS_BASE}/metersphere
docker-compose ${COMPOSE_FILES} down ${target}
}
function version() {
echo
cat ${MS_BASE}/metersphere/version
}
function upgrade() {
if [ -z "$target" ]; then
curl -s https://api.github.com/repos/metersphere/metersphere/releases >/dev/null
if [ $? -ne 0 ]; then
echo -e "\e[31m获取最新版本信息失败,请检查服务器到GitHub的网络连接是否正常!\e[0m"
return 2
fi
MS_VERSION=$(cat ${MS_BASE}/metersphere/version)
export MS_VERSION=${MS_VERSION%-*}
echo -e "\e[32m 检测当前版本为\e[1;33m${MS_VERSION} \e[0m"
python - <<EOF
# -*- coding: UTF-8 -*-
import os
import json
import re
latest_release=""
release_pattern=""
# 判断是否是LTS版本
current_version=os.environ.get("MS_VERSION")
if current_version.endswith("-lts") or current_version.startswith("v1."):
release_pattern="v\d+\.\d+\.\d+-lts$"
else:
release_pattern="v\d+\.\d+\.\d+$"
def get_releases(page):
# 根据是否是LTS版本获取对应的最新版本号
try:
releases=os.popen("curl -s https://api.github.com/repos/metersphere/metersphere/releases?page=%d" % (page)).read()
releases=[ x["name"] for x in json.loads(releases) if x["prerelease"] == False ]
except Exception as e:
print(str(e))
print("获取Release信息失败,请检查服务器到GitHub的网络连接是否正常")
exit(1)
else:
for release in releases:
if re.search(release_pattern,release) != None:
return release
page = 1
while (page <= 10):
latest_release = get_releases(page)
if (latest_release != "" and latest_release != None):
break
page += 1
# 记录最新版本号
os.popen("echo "+latest_release+" > /tmp/ms_latest_release")
EOF
MS_LATEST_VERSION=$(cat /tmp/ms_latest_release)
if [ "${MS_LATEST_VERSION}" = "" ]; then
echo -e "未获取到最新版本"
exit 1
elif [ "${MS_LATEST_VERSION}" = "${MS_VERSION}" ]; then
echo -e "最新版本与当前版本一致,退出升级过程"
exit 0
else
echo -e "\e[32m 检测到GitHub上最新版本为\e[1;33m${MS_LATEST_VERSION}\e[0;32m 即将执行在线升级...\e[0m"
fi
sleep 5s
download
else
MS_LATEST_VERSION=${target}
download
fi
if [ ! -f "/tmp/metersphere-online-installer-${MS_LATEST_VERSION}.tar.gz" ]; then
if [ ! -f "/tmp/metersphere-offline-installer-${MS_LATEST_VERSION}.tar.gz" ]; then
echo -e "\e[31m未找到升级包\e[1;33m/tmp/metersphere-*-installer-${MS_LATEST_VERSION}.tar.gz\e[31m,请检查!\e[0m"
echo -e "参考下载地址:\e[4;7mwget -T60 -t1 --no-check-certificate https://github.com/metersphere/metersphere/releases/download/${MS_LATEST_VERSION}/metersphere-online-installer-${MS_LATEST_VERSION}.tar.gz -O /tmp/metersphere-online-installer-${MS_LATEST_VERSION}.tar.gz\e[0m"
return 2
fi
fi
cd /tmp
tar zxvf metersphere-online-installer-${MS_LATEST_VERSION}.tar.gz
cd metersphere-online-installer-${MS_LATEST_VERSION}
/bin/bash install.sh
rm -rf /tmp/metersphere-online-installer-${MS_LATEST_VERSION}
}
function main() {
case "${action}" in
status)
status
;;
start)
start
;;
stop)
stop
;;
restart)
restart
;;
reload)
generate_compose_files
reload
;;
upgrade)
upgrade
;;
uninstall)
uninstall
;;
version)
version
;;
help)
usage
;;
--help)
usage
;;
generate_compose_files)
generate_compose_files
;;
*)
echo
cd ${MS_BASE}/metersphere
docker-compose ${COMPOSE_FILES} $@
;;
esac
}
main $@