#!/bin/bash action=$1 target=$2 DE_BASE=/opt DE_RUNNING_BASE=${DE_BASE}/dataease2.0 need_init_apisix=false compose_files="-f docker-compose.yml" compose_cmd="docker-compose" set -a source ${DE_RUNNING_BASE}/.env set +a export COMPOSE_HTTP_TIMEOUT=180 docker-compose version >/dev/null 2>&1 if [ $? -ne 0 ]; then docker compose version >/dev/null 2>&1 if [ $? -ne 0 ]; then compose_cmd="docker compose" fi fi if [[ ! ${DE_EXTERNAL_MYSQL} ]] || [ "${DE_EXTERNAL_MYSQL}" = "false" ]; then compose_files="${compose_files} -f docker-compose-mysql.yml" fi function usage() { echo "DATAEASE 控制脚本" echo echo "Usage: " echo " ./dectl [COMMAND] [ARGS...]" echo " ./dectl --help" echo echo "Commands: " echo " status 查看 DATAEASE 服务运行状态" echo " start 启动 DATAEASE 服务" echo " stop 停止 DATAEASE 服务" echo " restart 重启 DATAEASE 服务" echo " reload 重新加载 DATAEASE 服务" echo " upgrade 升级 DATAEASE 服务" echo " clear-images 清理 DATAEASE 旧版本的相关镜像" echo " version 查看 DATAEASE 版本" } function _generate_compose_file_args() { if [[ $DE_INSTALL_MODE != "community" ]];then compose_files="${compose_files} -f docker-compose-apisix.yml" fi } function _check_apisix_init() { if [[ $DE_INSTALL_MODE != "community" ]];then _prepare_apisix fi } function _check_task_init() { if [[ $DE_INSTALL_MODE != "community" ]];then _prepare_task fi } function _prepare_task() { compose_files="${compose_files} -f docker-compose-task.yml" } function _prepare_apisix() { if [[ -z $DE_APISIX_KEY ]];then need_init_apisix=true DE_APISIX_KEY=$(uuidgen | sed 's/-//g') export DE_APISIX_KEY=$DE_APISIX_KEY cd $DE_RUNNING_BASE env | grep DE_ >.env sed -i -e "s/DE_APISIX_KEY/${DE_APISIX_KEY}/g" $DE_RUNNING_BASE/apisix/apisix_conf/config.yaml sed -i -e "s/DE_APISIX_KEY/${DE_APISIX_KEY}/g" $DE_RUNNING_BASE/conf/application.yml fi compose_files="${compose_files} -f docker-compose-apisix.yml" } function _init_apisix() { DE_APISIX_KEY=$DE_APISIX_KEY sh $DE_RUNNING_BASE/bin/apisix/init.sh } function _healthcheck() { echo container_name=$(grep "container_name" $DE_RUNNING_BASE/docker-compose.yml | awk -F': ' '{print $2}') sleep 1 if [ -z $(docker ps --filter "name=$container_name" -q) ];then echo "未找到容器 $container_name。" exit 1 fi for b in {1..90} do sleep 1 read status healthy<<<$(docker inspect $container_name --format '{{.State.Status}} {{.State.Health.Status}}') if [[ "$healthy" == "starting" ]];then printf "\rDataEase 服务状态 : 正在启动 ... %3ds" $b elif [[ "$status" == "running" ]] && [[ "$healthy" == "healthy" ]];then printf "\rDataEase 服务状态 : 正常运行 " echo return; else echo echo -e "DataEase 服务状态 : \033[31m无法访问\033[0m" exit 1 fi done echo echo "【警告】服务在等待时间内未完全启动!请稍后使用 dectl status 检查服务运行状况。" echo } function _get_current_version() { de_current_version=$(grep "^ image:.*dataease:" ${DE_RUNNING_BASE}/docker-compose.yml | awk -F'dataease:' '{print $2}') echo $de_current_version } function status() { echo echo "DataEase 容器运行状态" _generate_compose_file_args cd ${DE_RUNNING_BASE} ${compose_cmd} ${compose_files} ps echo docker_root_dir=$(docker info | grep "Docker Root Dir" | awk -F': ' '{print $2}') echo "Docker 目录及文件大小 : " du -sh $docker_root_dir echo echo "Docker 目录所在磁盘使用情况 : " df -H $docker_root_dir echo echo "日志文件大小 : " du -Sh ${DE_RUNNING_BASE}/logs/ echo echo "DataEase 运行目录及文件大小 : " du -sh ${DE_BASE} echo echo "DataEase 运行目录使用情况 : " df -H ${DE_BASE} _healthcheck } function start() { echo _check_apisix_init _check_task_init cd ${DE_RUNNING_BASE} ${compose_cmd} ${compose_files} up -d _healthcheck if [ $? -eq 0 ] && [[ $need_init_apisix = true ]]; then _init_apisix fi } function stop() { echo _generate_compose_file_args cd ${DE_RUNNING_BASE} ${compose_cmd} ${compose_files} down -v ${target} } function restart() { stop start } function reload() { start } function version() { echo _get_current_version } function upgrade() { echo git_urls=('github.com') if [[ -x "$(command -v python)" ]]; then py_cmd='python' elif [[ -x "$(command -v python3)" ]]; then py_cmd='python3' fi 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 "没有找到稳定的下载服务器,请访问 https://community.fit2cloud.com/#/products/dataease/downloads 下载离线安装包" exit 1 fi if [[ "${server_url}" == "gitee.com" ]]; then owner='fit2cloud-feizhiyun' repo='DataEase' else owner='dataease' repo='dataease' fi export DE_VERSION=$(_get_current_version) if test -z $DE_VERSION; then echo "获取当前版本失败,请检查当前版本是否正确" exit 1 fi echo "检测当前版本为${DE_VERSION}" rm -f /tmp/de_latest_release $py_cmd - < /tmp/de_latest_release") EOF if [ ! -f /tmp/de_latest_release ]; then echo "获取最新版本失败,请检查网络连接是否正常" exit 1 fi latest_version=$(cat /tmp/de_latest_release) if [ "${latest_version}" = "" ]; then echo "未获取到最新版本" exit 1 elif [ "${latest_version}" = "${DE_VERSION}" ]; then echo "最新版本与当前版本一致,退出升级过程" exit 0 else echo "检测到 ${server_url} 上最新版本为 ${latest_version} 即将执行在线升级..." fi sleep 2 echo "开始下载最新版本..." echo "使用下载服务器 ${server_url}" cd /tmp installer_file="dataease-online-installer-${latest_version}.tar.gz" download_url="https://${server_url}/${owner}/${repo}/releases/download/${latest_version}/$installer_file" curl -LOk -m 60 -o $installer_file $download_url if [ $? -ne 0 ]; then echo -e "\e[31m升级失败:连接下载服务器超时!\n可手动下载升级包,然后执行\e[1;33m /bin/bash install.sh \e[0;31m离线升级,也可以重新执行一次 dectl upgrade 命令。\e[0m" return 2 fi if [ ! -f $installer_file ]; then echo "下载在线安装包失败,请试试重新执行一次安装命令。" exit 1 fi tar zxvf $installer_file if [ $? != 0 ]; then echo "下载在线安装包失败,请试试重新执行一次安装命令。" rm -f $installer_file exit 1 fi cd ${installer_file%.tar.gz} /bin/bash install.sh cd .. rm -rf /tmp/${installer_file%.*} } function clear_images() { echo for f in $DE_RUNNING_BASE/*.yml; do [[ -e "$f" ]] || break grep "^ image:.*:" "$f" | uniq | while read -r component_image_defined; do component_version=$(echo ${component_image_defined} | awk -F":" '{print $3}' | uniq) component_image=$(echo ${component_image_defined} | awk -F'image: ' '{print $2}' | awk -F':' '{print $1}') if test -z $(docker images | grep "$component_image" | grep -v " $component_version "); then echo "$component_image 不存在旧版本镜像" else echo "存在旧版本镜像 : " docker images | grep "$component_image" | grep -v " $component_version " echo "清理${component}镜像" docker rmi $(docker images | grep "$component_image" | grep -v " $component_version " | awk -F' ' '{print $1":"$2}') echo "清理完毕" fi done done if [ $(docker images -f dangling=true -q | wc -l) -gt 0 ]; then echo "清理虚悬镜像" docker rmi $(docker images -f dangling=true -q) echo "清理完毕" fi } function main() { case "${action}" in status) status ;; start) start ;; stop) stop ;; restart) restart ;; reload) reload ;; upgrade) upgrade ;; clear-images) clear_images ;; version) version ;; help) usage ;; --help) usage ;; *) echo "不支持的参数,请使用 help 或 --help 参数获取帮助" ;; esac } main