diff --git a/README.md b/README.md index 8e55be8..4ccf79a 100644 --- a/README.md +++ b/README.md @@ -347,8 +347,9 @@ cd pc && rm -rf node_modules && yarn install |------|------| | **完整校验** | 安装 **mongosh** 可测认证与库访问;安装 **AWS CLI**(命令 `aws`)可列桶与读写探测。 | | **未安装工具时** | MongoDB 可用 **nc** 仅测端口连通;S3 会跳过并打印手动验证命令。 | -| **安装提示** | 脚本按系统给出命令(macOS 为 `brew`;Debian/Ubuntu 为 `apt-get`;Amazon/RHEL 为 `dnf`/`yum`),不会在 Linux 上误导为 `brew`。 | -| **可选自动安装** | 以 **root** 执行且 `CHECK_CONN_AUTO_INSTALL=1` 时,会尝试用 `apt`/`dnf`/`yum` 安装缺失的 `awscli`、`mongodb-mongosh`;失败仅告警,可按屏幕提示手动安装。 | +| **安装提示** | 脚本按系统给出命令(macOS 为 `brew`;Amazon/RHEL 为 `dnf`/`yum`),不会在 Linux 上误导为 `brew`。 | +| **Ubuntu/Debian 与 mongosh** | 默认 apt **没有** `mongodb-mongosh` 包(`Unable to locate package` 属正常)。需 [MongoDB 官方文档](https://www.mongodb.com/docs/mongodb-shell/install/) 添加 apt 源后再安装,或使用 `snap install mongosh`。 | +| **可选自动安装** | **root** 且 `CHECK_CONN_AUTO_INSTALL=1`:**mongosh** 依次默认 apt、snap、MongoDB 8.0 源。**aws** 先 `apt install awscli` / `dnf`/`yum` 装 `aws-cli`;若无包或失败再下载 **AWS CLI v2 官方 zip** 安装(需 `curl`/`unzip` 与联网)。 | | **默认值** | `01-init-env.sh` 生成的 `.env.deploy-test` 已写入 `CHECK_CONN_AUTO_INSTALL=1`;`check-conn.sh` 通过 `load_env` 读取。关闭则改为 `0` 或删行。 | ```bash diff --git a/check-conn.sh b/check-conn.sh index b00c2d1..25477cf 100755 --- a/check-conn.sh +++ b/check-conn.sh @@ -22,19 +22,43 @@ _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" + echo "Debian/Ubuntu 默认源通常无 mongodb-mongosh:添加 MongoDB 官方 apt 源后安装,或 snap install mongosh。文档: https://www.mongodb.com/docs/mongodb-shell/install/" elif command -v dnf &>/dev/null; then - echo "sudo dnf install -y mongodb-mongosh" + echo "sudo dnf install -y mongodb-mongosh (若无包见官方文档)" else echo "https://www.mongodb.com/docs/mongodb-shell/install/" fi } +# root + apt:添加 MongoDB 8.0 官方源后安装 mongodb-mongosh(Ubuntu/Debian) +_try_add_mongodb_apt_repo_and_install_mongosh() { + [[ -f /etc/os-release ]] || return 1 + # shellcheck source=/dev/null + source /etc/os-release + local codename="${VERSION_CODENAME:-}" + [[ -n "$codename" ]] || return 1 + local gpg=/usr/share/keyrings/mongodb-server-8.0.gpg + local list=/etc/apt/sources.list.d/mongodb-org-8.0.list + local deb_line="" + case "${ID:-}" in + ubuntu) deb_line="deb [ signed-by=${gpg} ] https://repo.mongodb.org/apt/ubuntu ${codename}/mongodb-org/8.0 multiverse" ;; + debian) deb_line="deb [ signed-by=${gpg} ] https://repo.mongodb.org/apt/debian ${codename}/mongodb-org/8.0 main" ;; + *) return 1 ;; + esac + + DEBIAN_FRONTEND=noninteractive apt-get install -y -qq curl ca-certificates gnupg >/dev/null 2>&1 || true + curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | gpg -o "$gpg" --dearmor 2>/dev/null || return 1 + echo "$deb_line" > "$list" + DEBIAN_FRONTEND=noninteractive apt-get update -qq || return 1 + DEBIAN_FRONTEND=noninteractive apt-get install -y mongodb-mongosh || return 1 + return 0 +} + _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" + echo "若 apt 无 awscli 包:使用 AWS CLI v2 官方 bundle(需 curl+unzip)见 https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html" elif command -v dnf &>/dev/null; then echo "sudo dnf install -y aws-cli" elif command -v yum &>/dev/null; then @@ -44,6 +68,37 @@ _hint_install_awscli() { fi } +# root:从 AWS 官方 zip 安装 CLI v2(适用于 apt 无 awscli 包的 Ubuntu/Debian 等) +_try_install_awscli_via_v2_bundle() { + local arch zipname + arch=$(uname -m) + case "$arch" in + x86_64) zipname=awscli-exe-linux-x86_64.zip ;; + aarch64) zipname=awscli-exe-linux-aarch64.zip ;; + *) return 1 ;; + esac + local tmp + tmp=$(mktemp -d) + if command -v apt-get &>/dev/null; then + DEBIAN_FRONTEND=noninteractive apt-get install -y -qq curl unzip ca-certificates >/dev/null 2>&1 || true + fi + if ! curl -fsSL "https://awscli.amazonaws.com/${zipname}" -o "${tmp}/awscliv2.zip"; then + rm -rf "$tmp" + return 1 + fi + if ! unzip -q "${tmp}/awscliv2.zip" -d "$tmp"; then + rm -rf "$tmp" + return 1 + fi + if ! "${tmp}/aws/install"; then + rm -rf "$tmp" + return 1 + fi + rm -rf "$tmp" + hash -r 2>/dev/null || true + command -v aws &>/dev/null +} + # root + CHECK_CONN_AUTO_INSTALL=1 时尝试安装(失败则仅告警,不退出) _try_install_mongosh() { [[ "${CHECK_CONN_AUTO_INSTALL:-0}" == "1" ]] || return 0 @@ -58,6 +113,13 @@ _try_install_mongosh() { && DEBIAN_FRONTEND=noninteractive apt-get install -y mongodb-mongosh; then command -v mongosh &>/dev/null && success "mongosh 已可用" && return 0 fi + if command -v snap &>/dev/null && snap install mongosh; then + command -v mongosh &>/dev/null && success "mongosh 已可用 (snap)" && return 0 + fi + info "尝试通过 MongoDB 官方 apt 源安装 mongodb-mongosh ..." + if _try_add_mongodb_apt_repo_and_install_mongosh; then + command -v mongosh &>/dev/null && success "mongosh 已可用 (MongoDB 官方源)" && 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 @@ -76,18 +138,20 @@ _try_install_awscli() { } 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 + DEBIAN_FRONTEND=noninteractive apt-get update -qq \ + && DEBIAN_FRONTEND=noninteractive apt-get install -y awscli || true elif command -v dnf &>/dev/null; then - if dnf install -y aws-cli; then - command -v aws &>/dev/null && success "awscli 已可用" && return 0 - fi + dnf install -y aws-cli || true elif command -v yum &>/dev/null; then - if yum install -y aws-cli; then - command -v aws &>/dev/null && success "awscli 已可用" && return 0 - fi + yum install -y aws-cli || true + fi + if command -v aws &>/dev/null; then + success "awscli 已可用 (系统包管理器)" + return 0 + fi + info "系统源无 aws/awscli 或安装失败,尝试 AWS CLI v2 官方安装包(需联网)..." + if _try_install_awscli_via_v2_bundle; then + command -v aws &>/dev/null && success "awscli 已可用 (AWS 官方 bundle)" && return 0 fi warn "awscli 自动安装未成功,请手动: $(_hint_install_awscli)" return 0