This commit is contained in:
amb
2026-09-02 12:22:34 +08:00
parent 73c4d83f42
commit be3a963654
4 changed files with 221 additions and 10 deletions
+85
View File
@@ -0,0 +1,85 @@
#!/bin/bash
# ============================================================
# AI Knowledge Link — 一键部署脚本
# 在服务器项目目录执行:bash deploy.sh
# ============================================================
set -e
echo "=== AI Knowledge Link 部署 ==="
# 1. 检查 .env
if [ ! -f .env ]; then
echo "创建 .env ..."
cp .env.production .env
# 自动生成 SECRET_KEY
SECRET=$(python3 -c "import secrets; print(secrets.token_urlsafe(48))" 2>/dev/null || openssl rand -base64 48)
sed -i "s/change-me-to-a-real-secret-key/$SECRET/" .env
echo "SECRET_KEY 已自动生成"
fi
# 2. 创建数据目录
mkdir -p data
# 3. 构建并启动
echo "构建镜像..."
docker compose build
echo "启动服务..."
docker compose up -d
# 4. 等待 MySQL 就绪
echo "等待 MySQL 就绪..."
sleep 10
# 5. 数据库迁移
echo "执行数据库迁移..."
docker compose exec -T backend alembic upgrade head
# 6. 创建内部管理员账号(如果不存在)
echo "检查管理员账号..."
docker compose exec -T backend python -c "
from app.core.db import get_session_factory
from app.models.user import User
from app.models.plan import Plan
from app.core.security import hash_password
from sqlalchemy import select
factory = get_session_factory()
with factory() as session:
admin = session.scalars(select(User).where(User.username == 'admin')).first()
if not admin:
plan = session.scalars(select(Plan).where(Plan.code == 'free')).first()
if not plan:
plan = Plan(code='free', name='免费版', storage_quota=104857600, max_file_size=20971520, is_active=True)
session.add(plan)
session.flush()
admin = User(
username='admin',
email='admin@company.com',
password_hash=hash_password('Lzcc6-01'),
status='active',
role='internal',
plan_id=plan.id,
)
session.add(admin)
session.commit()
print('管理员账号已创建: admin / Lzcc6-01')
else:
print('管理员账号已存在,跳过')
"
# 7. 完成
echo ""
echo "=========================================="
echo " 部署完成!"
echo ""
echo " 访问地址: http://$(curl -s ifconfig.me 2>/dev/null || echo '你的服务器IP')"
echo " 内部登录: http://$(curl -s ifconfig.me 2>/dev/null || echo '你的服务器IP')/internal-login"
echo ""
echo " 管理员: admin"
echo " 密码: Lzcc6-01"
echo ""
echo " 更新部署: bash update.sh"
echo "=========================================="