From cdfbf0552441b5640cd9d2720cb3652d8778c03b Mon Sep 17 00:00:00 2001 From: wizardchen Date: Mon, 11 May 2026 12:12:29 +0800 Subject: [PATCH] fix(cloud-image): make firstboot idempotent and pin image versions Address review feedback on PR #1249: - prepare.sh: when WEKNORA_REF looks like a version tag (v*), write the matching WEKNORA_VERSION into .env so docker compose pulls images that match the compose YAML's git ref (previously stuck on :latest). - prepare.sh: detect docker binary path via `command -v docker` and template it into weknora.service (replacing hardcoded /usr/bin/docker that fails when docker lives in /usr/local/bin). - firstboot.sh: write a /opt/WeKnora/.firstboot.done marker immediately after rewriting .env, before `docker compose up -d`. If compose fails mid-run, the next boot is gated by ConditionPathExists=!marker so we never regenerate DB_PASSWORD against an already-initialized postgres volume (which previously bricked the database). - firstboot.sh: stop deleting its own unit file / script while the oneshot is still executing; rely on the marker + `systemctl disable` instead, avoiding "job failed" markings from systemd. - firstboot.sh: use detected docker path instead of /usr/bin/docker; add note in credentials file that .env is the source of truth. - weknora-firstboot.service: add ConditionPathExists=!.firstboot.done. - cleanup.sh: scope docker volume deletion to compose project label (com.docker.compose.project=) instead of fuzzy substring match that could nuke unrelated postgres/redis volumes. - cleanup.sh: also remove .firstboot.done marker, firstboot log, and any leftover /root/weknora-credentials.txt so the image is clean. - README.md: clarify how to actually disable registration (edit the `replace` call list in firstboot.sh, not run that command in shell). --- scripts/cloud-image/README.md | 2 +- scripts/cloud-image/cleanup.sh | 16 +++++-- scripts/cloud-image/firstboot.sh | 47 ++++++++++++++----- scripts/cloud-image/prepare.sh | 23 +++++++++ .../systemd/weknora-firstboot.service | 1 + scripts/cloud-image/systemd/weknora.service | 4 +- 6 files changed, 75 insertions(+), 18 deletions(-) diff --git a/scripts/cloud-image/README.md b/scripts/cloud-image/README.md index 1ae628a6..204450da 100644 --- a/scripts/cloud-image/README.md +++ b/scripts/cloud-image/README.md @@ -204,7 +204,7 @@ sudo bash /opt/weknora-tools/scripts/cloud-image/cleanup.sh 之后每次开机都由 `weknora.service` 接管。 > **注意**:`firstboot.sh` 默认**不**禁用注册(`DISABLE_REGISTRATION=false`),第一个注册的人会成为管理员。 -> 凭证文件里有「请尽快注册以防被抢注」的醒目提示。需要更严格控制可在 `firstboot.sh` 里把这一行 `replace DISABLE_REGISTRATION true`。 +> 凭证文件里有「请尽快注册以防被抢注」的醒目提示。需要更严格控制可在 `firstboot.sh` 的 `replace` 调用列表中追加一行 `replace DISABLE_REGISTRATION true` 后再重制镜像。 --- diff --git a/scripts/cloud-image/cleanup.sh b/scripts/cloud-image/cleanup.sh index 6211e35d..ad1f1a18 100755 --- a/scripts/cloud-image/cleanup.sh +++ b/scripts/cloud-image/cleanup.sh @@ -18,21 +18,29 @@ if [[ "${ans}" != "YES" ]]; then fi echo "[cleanup] 1/8 停止 WeKnora 容器" +COMPOSE_PROJECT="" if [[ -d "${WEKNORA_DIR}" ]]; then cd "${WEKNORA_DIR}" + # 优先用 compose ls 拿到真实 project 名 (默认是目录名小写, 如 weknora) + COMPOSE_PROJECT="$(docker compose ls --format json 2>/dev/null \ + | grep -oE '"Name":"[^"]+"' | head -1 | cut -d'"' -f4 || true)" docker compose down -v --remove-orphans || true fi -echo "[cleanup] 2/8 清空 WeKnora 业务数据" +echo "[cleanup] 2/8 清空 WeKnora 业务数据 + 首启 marker / 日志" if [[ -d "${WEKNORA_DIR}" ]]; then rm -rf "${WEKNORA_DIR}/data"/* "${WEKNORA_DIR}/logs"/* 2>/dev/null || true - rm -f "${WEKNORA_DIR}/.env" + rm -f "${WEKNORA_DIR}/.env" "${WEKNORA_DIR}/.firstboot.done" cp "${WEKNORA_DIR}/.env.example" "${WEKNORA_DIR}/.env" fi +rm -f /root/weknora-credentials.txt /var/log/weknora-firstboot.log echo "[cleanup] 3/8 清理残留 docker 卷与构建缓存" -docker volume ls -q | grep -Ei 'weknora|postgres|redis|minio|qdrant|elasticsearch|milvus|weaviate' \ - | xargs -r docker volume rm -f || true +# 严格按 compose project 名前缀匹配, 避免误伤同宿主上其它 postgres/redis 卷。 +if [[ -n "${COMPOSE_PROJECT}" ]]; then + docker volume ls -q --filter "label=com.docker.compose.project=${COMPOSE_PROJECT}" \ + | xargs -r docker volume rm -f || true +fi docker system prune -af --volumes || true echo "[cleanup] 4/8 清空系统日志" diff --git a/scripts/cloud-image/firstboot.sh b/scripts/cloud-image/firstboot.sh index 5aa85c30..dc17e8cd 100755 --- a/scripts/cloud-image/firstboot.sh +++ b/scripts/cloud-image/firstboot.sh @@ -1,21 +1,41 @@ #!/usr/bin/env bash # firstboot.sh - 由 weknora-firstboot.service 在新实例首次开机时自动执行。 -# 任务: 生成随机密钥写入 .env -> 启动容器 -> 输出凭证 -> 自删除。 +# 任务: 生成随机密钥写入 .env -> 启动容器 -> 输出凭证 -> 标记完成 + 自禁用。 +# +# 幂等性策略: +# 1) 一上来先写 ${MARKER} 标记 "已生成过密钥", +# 之后即便 docker compose 失败导致脚本中断, 重启后由 unit 的 +# ConditionPathExists=!${MARKER} 拦住, 不会再生成新密钥覆盖 .env。 +# (旧密钥已经写进 postgres 数据卷, 二次生成会导致数据库永久无法登录) +# 2) 失败时 unit 标记 failed, 用户可手动 docker compose up -d 恢复; +# 凭证仍可在 ${ENV_FILE} 中查到。 set -euo pipefail WEKNORA_DIR="${WEKNORA_DIR:-/opt/WeKnora}" ENV_FILE="${WEKNORA_DIR}/.env" CRED_FILE="/root/weknora-credentials.txt" LOG_FILE="/var/log/weknora-firstboot.log" +MARKER="${WEKNORA_DIR}/.firstboot.done" exec >>"${LOG_FILE}" 2>&1 echo "==== firstboot started at $(date -Iseconds) ====" +if [[ -f "${MARKER}" ]]; then + echo "marker ${MARKER} exists, skip (already initialized)" + exit 0 +fi + if [[ ! -f "${ENV_FILE}" ]]; then echo "ERROR: ${ENV_FILE} not found" exit 1 fi +DOCKER_BIN="$(command -v docker || true)" +if [[ -z "${DOCKER_BIN}" ]]; then + echo "ERROR: docker binary not found in PATH" + exit 1 +fi + # 生成 32 字节强随机字符串(用于 AES-256 key, 必须刚好 32 字节) gen32() { LC_ALL=C tr -dc 'A-Za-z0-9' /dev/null \ || curl -fsS --max-time 5 https://api.ipify.org 2>/dev/null \ || hostname -I | awk '{print $1}') -umask 077 cat >"${CRED_FILE}" <"${CRED_FILE}" <