修改配置

This commit is contained in:
vet
2026-04-13 20:55:50 +07:00
parent ad326b7500
commit 90c36c525d
2 changed files with 126 additions and 9 deletions

View File

@@ -6,6 +6,9 @@
# ./check-conn.sh # 同时检查 MongoDB 和 S3
# ./check-conn.sh mongo # 只检查 MongoDB
# ./check-conn.sh s3 # 只检查 S3
#
# 环境:
# CHECK_CONN_AUTO_INSTALL=1 以 root 运行时尝试用 apt/dnf/yum 安装 awscli、mongosh可选
# =============================================================================
set -euo pipefail
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/common.sh"
@@ -14,12 +17,90 @@ init_script_log # ← 脚本执行日志
TARGET="${1:-all}"
# ── 按系统给出安装提示(避免在 Linux 服务器上误导为 brew──────────────────────
_hint_install_mongosh() {
if [[ "$(uname -s)" == "Darwin" ]]; then
echo "brew install mongosh"
elif command -v apt-get &>/dev/null; then
echo "sudo apt-get update && sudo apt-get install -y mongodb-mongosh"
elif command -v dnf &>/dev/null; then
echo "sudo dnf install -y mongodb-mongosh"
else
echo "https://www.mongodb.com/docs/mongodb-shell/install/"
fi
}
_hint_install_awscli() {
if [[ "$(uname -s)" == "Darwin" ]]; then
echo "brew install awscli"
elif command -v apt-get &>/dev/null; then
echo "sudo apt-get update && sudo apt-get install -y awscli"
elif command -v dnf &>/dev/null; then
echo "sudo dnf install -y aws-cli"
elif command -v yum &>/dev/null; then
echo "sudo yum install -y aws-cli"
else
echo "https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html"
fi
}
# root + CHECK_CONN_AUTO_INSTALL=1 时尝试安装(失败则仅告警,不退出)
_try_install_mongosh() {
[[ "${CHECK_CONN_AUTO_INSTALL:-0}" == "1" ]] || return 0
command -v mongosh &>/dev/null && return 0
[[ "$(id -u)" -eq 0 ]] || {
info "CHECK_CONN_AUTO_INSTALL=1 但非 root跳过 mongosh 自动安装"
return 0
}
info "CHECK_CONN_AUTO_INSTALL尝试安装 mongosh ..."
if command -v apt-get &>/dev/null; then
if DEBIAN_FRONTEND=noninteractive apt-get update -qq \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y mongodb-mongosh; then
command -v mongosh &>/dev/null && success "mongosh 已可用" && return 0
fi
elif command -v dnf &>/dev/null; then
if dnf install -y mongodb-mongosh; then
command -v mongosh &>/dev/null && success "mongosh 已可用" && return 0
fi
fi
warn "mongosh 自动安装未成功,请手动: $(_hint_install_mongosh)"
return 0
}
_try_install_awscli() {
[[ "${CHECK_CONN_AUTO_INSTALL:-0}" == "1" ]] || return 0
command -v aws &>/dev/null && return 0
[[ "$(id -u)" -eq 0 ]] || {
info "CHECK_CONN_AUTO_INSTALL=1 但非 root跳过 awscli 自动安装"
return 0
}
info "CHECK_CONN_AUTO_INSTALL尝试安装 awscli ..."
if command -v apt-get &>/dev/null; then
if DEBIAN_FRONTEND=noninteractive apt-get update -qq \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y awscli; then
command -v aws &>/dev/null && success "awscli 已可用" && return 0
fi
elif command -v dnf &>/dev/null; then
if dnf install -y aws-cli; then
command -v aws &>/dev/null && success "awscli 已可用" && return 0
fi
elif command -v yum &>/dev/null; then
if yum install -y aws-cli; then
command -v aws &>/dev/null && success "awscli 已可用" && return 0
fi
fi
warn "awscli 自动安装未成功,请手动: $(_hint_install_awscli)"
return 0
}
header "远程服务连接检查"
# ──────────────────────────────────────────────────────────────────────────────
# MongoDB
# ──────────────────────────────────────────────────────────────────────────────
check_mongo() {
_try_install_mongosh
step "MongoDB: ${MONGO_HOST}:${MONGO_PORT}/${MONGO_DATABASE}"
echo -e " Host: ${MONGO_HOST}"
@@ -52,7 +133,7 @@ check_mongo() {
info "mongosh 未安装,使用 nc 检查端口..."
if nc -z -w5 "${MONGO_HOST}" "${MONGO_PORT}" 2>/dev/null; then
success "MongoDB 端口 ${MONGO_HOST}:${MONGO_PORT} 可达 ✓"
warn "(未验证认证,安装 mongosh 可做完整测试"
warn "(未验证认证,安装 mongosh 可做完整测试: $(_hint_install_mongosh)"
else
error "MongoDB 端口不可达: ${MONGO_HOST}:${MONGO_PORT}"
fi
@@ -73,7 +154,7 @@ except Exception as e:
PYEOF
[[ $? -eq 0 ]] && success "MongoDB 连接正常" || error "MongoDB 连接失败"
else
warn "跳过 MongoDB 连接验证(请安装 mongosh: brew install mongosh"
warn "跳过 MongoDB 连接验证(请安装 mongosh: $(_hint_install_mongosh)"
echo -e " 手动验证: mongosh \"${MONGO_URI}\""
fi
}
@@ -138,12 +219,14 @@ _check_s3_bucket() {
warn "S3 写入测试失败Bucket 可读但可能无写权限)"
fi
else
warn "awscli 未安装,跳过验证(brew install awscli"
warn "awscli 未安装,跳过验证($(_hint_install_awscli)"
echo " 手动验证: AWS_ACCESS_KEY_ID=${key_id} aws s3 ls s3://${bucket}"
fi
}
check_s3() {
_try_install_awscli
step "S3 (open-im-server) — IM 文件存储"
_check_s3_bucket \
"openim" \