定义action、target、args分别为传入的第一个、第二个以及所有参数
action=$1
target=$2
args=$@
读取 ~/.msrc 文件内容,加载 MS_BASE 变量
source ~/.msrc >/dev/null
假如${MS_BASE}变量不存在或为空,则赋值为”/opt“
MS_BASE=${MS_BASE:-/opt}
将 compose_files文件的内容(一连串 “-f docker-compose-xxx.yml”字符)赋值给COMPOSE_FILES变量。读取install.conf内容,并设置COMPOSE_HTTP_TIMEOUT变量
COMPOSE_FILES=$(cat ${MS_BASE}/metersphere/compose_files 2>/dev/null || echo "")
source ${MS_BASE}/metersphere/install.conf
export COMPOSE_HTTP_TIMEOUT=180
帮助函数,输出msctl可用命令
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 版本信息"
}
generate_compose_files函数,一般情况下是在reload时调用
该函数根据install.conf中设定的安装模式(allinone\server\node-controllerr\Selenium-hub|middleware),构造docker-compose执行参数,并回写到${MS_BASE}/metersphere/compose_files 文件中,最后赋值给COMPOSE_FILES变量
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"
# 如果是middleware模式,去掉 depends_on
if [ "${MS_INSTALL_MODE}" = "middleware" ]; then
sed -i -e '/system-setting/,+3d' ${MS_BASE}/metersphere/docker-compose-mysql.yml
fi
fi
# 是否使用外部 Kafka,如果使用内置kafka,则创建kafka数据目录,compose_files变量追加-f docker-compose-kafka.yml
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,如果使用内置 Prometheus,则创建 prometheus 数据目录,compose_files 变量追加-f docker-compose-prometheus.yml
if [ ${MS_EXTERNAL_PROM} = "false" ]; then
mkdir -p ${MS_BASE}/metersphere/data/prometheus
compose_files="${compose_files} -f docker-compose-prometheus.yml"
fi
# 是否使用外部 Redis,如果使用内置 Redis,则创建 redis 数据目录,compose_files 变量追加-f docker-compose-redis.yml
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,如果使用内置 minio,则创建 minio 数据目录,compose_files 变量追加-f docker-compose-redis.yml
if [ "${MS_EXTERNAL_MINIO}" = "false" ]; then
mkdir -p ${MS_BASE}/metersphere/data/minio
compose_files="${compose_files} -f docker-compose-minio.yml"
fi
# 根据是否启动 UI 模块以及是否使用外部 Selenium,修改 compose_files 变量
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
# 将compose_files变量内容写入${MS_BASE}/metersphere/compose_files文件
echo ${compose_files} >${MS_BASE}/metersphere/compose_files
#赋予${MS_BASE}/metersphere读写权限
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变量
COMPOSE_FILES=$(cat ${MS_BASE}/metersphere/compose_files 2>/dev/null || echo "")
}
MeterSphere在线安装包下载函数
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
}
MeterSphere服务状态查看函数,进入安装目录,使用docker-compose -f docker-compose-xxx.yml ps命令查看相关服务状态
function status() {
echo
cd ${MS_BASE}/metersphere
docker-compose ${COMPOSE_FILES} ps
}
MeterSphere服务启动函数,进入安装目录,使用docker-compose -f docker-compose-xxx.yml start(容器名)启动相关服务,msctl start后面不接容器名则启动所有相关服务
function start() {
echo
cd ${MS_BASE}/metersphere
docker-compose ${COMPOSE_FILES} start ${target}
}
MeterSphere服务停止函数,进入安装目录,使用docker-compose -f docker-compose-xxx.yml stop(容器名)停止相关服务,msctl stop后面不接容器名则停止所有相关服务
function stop() {
echo
cd ${MS_BASE}/metersphere
docker-compose ${COMPOSE_FILES} stop ${target}
}
MeterSphere服务重启函数,是上面两个命令的组合,先stop再start
function restart() {
echo
cd ${MS_BASE}/metersphere
docker-compose ${COMPOSE_FILES} stop ${target}
docker-compose ${COMPOSE_FILES} start ${target}
}
MeterSphere服务重新加载函数,进入安装目录,使用docker-compose -f docker-compose-xxx.ym up -d --remove-orphans命令,--remove-orphans: 删除服务中没有在compose文件中定义的容器
reload是根据compose file文件重启创建容器,所以如果修改了.env文件或yml文件配置后,需要使用msctl reload,配置才会生效。执行msctl restart,对应修改的配置项是不会生效的。
function reload() {function reload() {
echo
cd ${MS_BASE}/metersphere
docker-compose ${COMPOSE_FILES} up -d --remove-orphans
}}
MeterSphere服务卸载函数,进入安装目录,使用docker-compose -f docker-compose-xxx.yml down命令停止并删除相关容器和网络,uninstall仅仅是删除了MeterSphere服务容器和网路,并没有删除MeterSphere应用以及数据。
function uninstall() {
echo
cd ${MS_BASE}/metersphere
docker-compose ${COMPOSE_FILES} down ${target}
}
MeterSphere版本查看函数,通过查看${MS_BASE}/metersphere/version文件内容
function version() {
echo
cat ${MS_BASE}/metersphere/version
}
MeterSphere升级函数,在线下载指定版本安装包,解压后执行安装脚本,最后删除安装包
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}
}
main函数,根据msctl后传入的参数调用对应的函数
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 $@