Files
wrong-notebook/docker-entrypoint.sh
margrop bd0befa85b feat: 全新安装改用一次性初始化链接创建管理员,不再预置固定密码的默认管理员
- 服务启动时若还没有管理员,生成一次性令牌保存到配置目录的 setup-token,并在日志中打印 /initialize?token= 链接;已有管理员时删除残留令牌
- 新增 /initialize 页面与 POST /api/initialize:凭令牌设置管理员名称、邮箱和密码(不能用 123456),成功后自动登录进入安装向导;令牌改名占用,并发提交只会创建一个管理员
- 登录页在尚未创建管理员时说明各安装方式到哪里找初始化链接,注册状态接口增加 initialized
- prisma/seed.ts、scripts/seed-admin.js 不再创建 admin@localhost,只为已有的默认管理员恢复角色;E2E 准备脚本改为创建测试管理员
- 已有实例升级保留原账号,旧默认管理员仍走强制改密流程
- 同步中英文 README、Wiki、安装包说明与 npm、飞牛OS 的首次使用提示

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 03:45:48 +08:00

126 lines
4.4 KiB
Bash

#!/bin/sh
set -e
# Define paths
SOURCE_DB="/app/prisma/dev.db"
TARGET_DB="/app/data/dev.db"
SEED_MARKER="/app/data/.seed_completed"
VERSION_FILE="/app/data/.app_version"
# Use local Prisma CLI from node_modules
PRISMA_BIN="node /app/node_modules/prisma/build/index.js"
SEED_ADMIN_SCRIPT="/app/dist-scripts/scripts/seed-admin.js"
REBUILD_TAGS_SCRIPT="/app/dist-scripts/scripts/rebuild-system-tags.js"
# Get current app version from package.json
CURRENT_VERSION=$(node -p "require('./package.json').version" 2>/dev/null || echo "unknown")
# Fix permissions for data and config directories
chown -R nextjs:nodejs /app/data /app/config
# Remember whether this is an existing installation before copying seed data.
DB_ALREADY_EXISTS=false
# Check if the persistent database exists
if [ ! -s "$TARGET_DB" ]; then
echo "[Entrypoint] Initializing database..."
if [ -f "$SOURCE_DB" ]; then
echo "[Entrypoint] Copying pre-packaged database from $SOURCE_DB to $TARGET_DB"
cp "$SOURCE_DB" "$TARGET_DB"
# Ensure correct permissions
chown nextjs:nodejs "$TARGET_DB"
# Mark as seeded since pre-packaged DB includes seed data
touch "$SEED_MARKER"
# Record initial version
echo "$CURRENT_VERSION" > "$VERSION_FILE"
else
echo "[Entrypoint] Source database not found at $SOURCE_DB. Initializing with migrations."
fi
else
DB_ALREADY_EXISTS=true
echo "[Entrypoint] Database already exists at $TARGET_DB."
fi
# Check for version upgrade
PREVIOUS_VERSION=""
if [ -f "$VERSION_FILE" ]; then
PREVIOUS_VERSION=$(cat "$VERSION_FILE")
fi
# Run migrations to ensure DB schema is available and up to date.
echo "[Entrypoint] Running database migrations to sync schema..."
cd /app
if ! $PRISMA_BIN migrate deploy --schema=./prisma/schema.prisma; then
echo "[Entrypoint] Database migration failed; refusing to start." >&2
exit 1
fi
echo "[Entrypoint] Migrations completed successfully."
# Bootstrap new installations; deployments preserve existing account state.
if [ "$DB_ALREADY_EXISTS" = "true" ] && [ "${PRESERVE_EXISTING_DATA:-false}" = "true" ]; then
echo "[Entrypoint] Preserving existing accounts; skipping admin seed."
else
echo "[Entrypoint] Restoring admin role for the existing default admin, if any..."
node "$SEED_ADMIN_SCRIPT"
fi
touch "$SEED_MARKER" 2>/dev/null
# Check if version changed - rebuild system tags automatically
if [ "$PREVIOUS_VERSION" != "$CURRENT_VERSION" ]; then
echo "[Entrypoint] Version upgrade detected: $PREVIOUS_VERSION -> $CURRENT_VERSION"
if [ "$DB_ALREADY_EXISTS" = "true" ] && [ "${PRESERVE_EXISTING_DATA:-false}" = "true" ]; then
echo "[Entrypoint] Preserving existing system tags and associations."
else
echo "[Entrypoint] Rebuilding system tags to sync with new version..."
node "$REBUILD_TAGS_SCRIPT"
fi
# Update version marker
echo "$CURRENT_VERSION" > "$VERSION_FILE"
fi
# HTTPS Setup
CERT_DIR="/app/certs"
CERT_FILE="$CERT_DIR/cert.pem"
KEY_FILE="$CERT_DIR/key.pem"
if [ "$HTTPS_ENABLED" = "true" ]; then
echo "[Entrypoint] HTTPS enabled"
# 确保证书目录存在
mkdir -p "$CERT_DIR"
chown nextjs:nodejs "$CERT_DIR"
# 检查证书是否存在
if [ ! -f "$CERT_FILE" ] || [ ! -f "$KEY_FILE" ]; then
echo "[Entrypoint] 证书不存在,自动生成自签名证书..."
# 获取证书 CN(优先使用环境变量,否则使用 localhost)
CERT_CN="${CERT_DOMAIN:-localhost}"
# 生成自签名证书(有效期 10 年)
openssl req -x509 -newkey rsa:2048 \
-keyout "$KEY_FILE" \
-out "$CERT_FILE" \
-days 3650 \
-nodes \
-subj "/CN=$CERT_CN" \
2>/dev/null
if [ $? -eq 0 ]; then
echo "[Entrypoint] 自签名证书生成成功: CN=$CERT_CN"
chown nextjs:nodejs "$CERT_FILE" "$KEY_FILE"
else
echo "[Entrypoint] 警告: 证书生成失败,HTTPS 将不可用"
fi
else
echo "[Entrypoint] 使用已有证书: $CERT_FILE"
fi
# 启动 HTTPS 代理
if [ -f "$CERT_FILE" ] && [ -f "$KEY_FILE" ]; then
echo "[Entrypoint] 启动 HTTPS 代理 (端口 443)..."
su-exec nextjs:nodejs node /app/https-server.js &
fi
fi
# Execute the main container command as nextjs user
exec su-exec nextjs:nodejs "$@"