Files
wrong-notebook/packaging/docker/docker-container.sh
margrop c175cc4538 fix: 修复 QNAP 数据目录丢失、健康检查与密钥暴露
- 数据目录不再回退到会被卸载删除的 QPKG 目录:优先 /share/Container,其次 /share/Public,都不存在时明确报错
- 容器就绪优先检查 docker inspect 的健康状态,无 curl/wget 也可判定,unhealthy 时输出日志
- 容器启动补充日志轮转、WN_PLATFORM=qnap 与 NEXTAUTH_SECRET_FILE,避免密钥出现在命令行和 ps
- 入口脚本支持从 NEXTAUTH_SECRET_FILE 读取已有密钥,兼容旧套件的 config/nextauth-secret
- 补充数据目录、健康检查与容器参数的脚本测试
Refs: https://github.com/margrop/wrong-notebook/issues/114
2026-12-04 01:34:34 +08:00

174 lines
6.5 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/sh
# 威联通 QPKG 的 Docker 容器控制(群晖套件改由 Container Manager 的 docker-project 资源管理容器)。
# 调用方先设置 WN_IMAGE_TAG、WN_DATA_DIR、WN_CONFIG_DIR,再 source 本文件;
# 套件内置镜像时另设 WN_IMAGE_ARCHIVE(docker save 导出的归档)。
# 注意:本文件会被 qinstall.sh(经 package_routines)和 wrongnotebook.sh source,
# 严禁在顶层执行 set -e / set -u,以免污染 qinstall.sh 的 shell 选项导致安装后半段(75%)异常退出。
WN_CONTAINER_NAME="${WN_CONTAINER_NAME:-wrong-notebook}"
WN_HOST_PORT="${WN_HOST_PORT:-39300}"
WN_IMAGE_TAG="${WN_IMAGE_TAG:?WN_IMAGE_TAG 未设置}"
WN_IMAGE="${WN_IMAGE:-ghcr.io/margrop/wrong-notebook:${WN_IMAGE_TAG}}"
# 找到 docker CLI:PATH 中没有时检查 Container Station(含 QuTS hero ZFS 路径)与 Container Manager 的常见位置
wn_docker_bin() {
if command -v docker >/dev/null 2>&1; then
command -v docker
return 0
fi
if [ -x /sbin/getcfg ]; then
cs_dir="$(/sbin/getcfg container-station Install_Path -f /etc/config/qpkg.conf 2>/dev/null || true)"
if [ -n "${cs_dir}" ] && [ -x "${cs_dir}/bin/docker" ]; then
printf '%s\n' "${cs_dir}/bin/docker"
return 0
fi
fi
for candidate in \
/usr/local/bin/docker \
/usr/bin/docker \
/var/packages/ContainerManager/target/usr/bin/docker \
/share/CACHEDEV1_DATA/.qpkg/container-station/bin/docker \
/share/CACHEDEV2_DATA/.qpkg/container-station/bin/docker \
/share/CACHEDEV3_DATA/.qpkg/container-station/bin/docker; do
if [ -x "${candidate}" ]; then
printf '%s\n' "${candidate}"
return 0
fi
done
return 1
}
wn_docker() {
docker_bin="$(wn_docker_bin)" || {
echo "未找到 docker CLI,请先安装 Container Manager(群晖)或 Container Station(威联通)" >&2
return 1
}
"${docker_bin}" "$@"
}
# 数据目录必须落在卸载套件不会删除的持久化共享目录;QPKG 安装目录会被卸载清理
wn_resolve_data_root() {
share_root="${WN_SHARE_ROOT:-/share}"
if [ -d "${share_root}/Container" ]; then
printf '%s\n' "${share_root}/Container/wrong-notebook"
return 0
fi
if [ -d "${share_root}/Public" ]; then
printf '%s\n' "${share_root}/Public/wrong-notebook"
return 0
fi
echo "未找到 Container 或 Public 共享目录,无法定位持久化数据目录;请先创建其中一个共享文件夹。" >&2
return 1
}
wn_ensure_dirs() {
mkdir -p "${WN_DATA_DIR}" "${WN_CONFIG_DIR}"
}
wn_generate_secret() {
if [ -s "${WN_CONFIG_DIR}/nextauth-secret" ]; then
return 0
fi
if command -v openssl >/dev/null 2>&1; then
openssl rand -hex 32 > "${WN_CONFIG_DIR}/nextauth-secret"
else
head -c 32 /dev/urandom | base64 | tr -d '\n\r' > "${WN_CONFIG_DIR}/nextauth-secret"
fi
chmod 600 "${WN_CONFIG_DIR}/nextauth-secret"
}
wn_has_image() {
wn_docker image inspect "${WN_IMAGE}" >/dev/null 2>&1
}
# 准备镜像:本地已有时直接使用(重启和断网时不依赖镜像仓库);
# 否则先加载套件内置的归档,没有归档或加载后仍缺少该版本时才联网拉取。
wn_ensure_image() {
if wn_has_image; then
return 0
fi
if [ -n "${WN_IMAGE_ARCHIVE:-}" ] && [ -s "${WN_IMAGE_ARCHIVE}" ]; then
wn_docker load -i "${WN_IMAGE_ARCHIVE}" >/dev/null || echo "加载内置镜像失败,改为联网拉取" >&2
if wn_has_image; then
return 0
fi
fi
wn_docker pull --quiet "${WN_IMAGE}" >/dev/null
}
# 升级后清理本套件其他版本的镜像,避免每次升级多占一份空间;仍被使用的镜像删除会失败,忽略即可
wn_prune_old_images() {
repo="${WN_IMAGE%:*}"
for old in $(wn_docker images --format '{{.Repository}}:{{.Tag}}' "${repo}" 2>/dev/null); do
if [ "${old}" != "${WN_IMAGE}" ]; then
wn_docker rmi "${old}" >/dev/null 2>&1 || true
fi
done
}
wn_wait_health() {
attempt=0
while [ "${attempt}" -lt 120 ]; do
health="$(wn_docker inspect -f '{{.State.Health.Status}}' "${WN_CONTAINER_NAME}" 2>/dev/null || true)"
if [ "${health}" = "healthy" ]; then
return 0
fi
if [ "${health}" = "unhealthy" ]; then
echo "容器健康检查失败,最近日志:" >&2
wn_docker logs --tail 20 "${WN_CONTAINER_NAME}" >&2 || true
return 1
fi
# 旧版 docker 或容器尚无健康状态时,有 curl/wget 则直接探测接口
if command -v curl >/dev/null 2>&1 && curl -sf "http://127.0.0.1:${WN_HOST_PORT}/api/health" >/dev/null 2>&1; then
return 0
fi
if command -v wget >/dev/null 2>&1 && wget -q -O- "http://127.0.0.1:${WN_HOST_PORT}/api/health" >/dev/null 2>&1; then
return 0
fi
running="$(wn_docker inspect -f '{{.State.Running}}' "${WN_CONTAINER_NAME}" 2>/dev/null || echo false)"
if [ "${running}" != "true" ]; then
echo "容器已退出,最近日志:" >&2
wn_docker logs --tail 20 "${WN_CONTAINER_NAME}" >&2 || true
return 1
fi
sleep 1
attempt=$((attempt + 1))
done
echo "健康检查超时,容器仍在运行;首次启动可能仍在初始化数据库" >&2
return 0
}
wn_start_container() {
wn_ensure_dirs || return 1
wn_generate_secret || return 1
wn_ensure_image || return 1
wn_docker rm -f "${WN_CONTAINER_NAME}" >/dev/null 2>&1 || true
wn_docker run -d \
--name "${WN_CONTAINER_NAME}" \
--restart unless-stopped \
--log-opt max-size=10m \
--log-opt max-file=3 \
-p "${WN_HOST_PORT}:3000" \
-v "${WN_DATA_DIR}:/app/data" \
-v "${WN_CONFIG_DIR}:/app/config" \
-e PRESERVE_EXISTING_DATA=true \
-e AUTH_TRUST_HOST=true \
-e WN_PLATFORM=qnap \
-e NEXTAUTH_SECRET_FILE=/app/config/nextauth-secret \
"${WN_IMAGE}" >/dev/null || return 1
wn_prune_old_images
wn_wait_health
}
wn_stop_container() {
if wn_docker inspect "${WN_CONTAINER_NAME}" >/dev/null 2>&1; then
wn_docker stop -t 20 "${WN_CONTAINER_NAME}" >/dev/null 2>&1 || true
wn_docker rm -f "${WN_CONTAINER_NAME}" >/dev/null 2>&1 || true
fi
}
wn_status_container() {
running="$(wn_docker inspect -f '{{.State.Running}}' "${WN_CONTAINER_NAME}" 2>/dev/null || echo false)"
[ "${running}" = "true" ]
}