Files
2026-09-02 16:32:26 +08:00

90 lines
2.8 KiB
Bash
Raw Permalink 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/bash
# ============================================================
# AI Knowledge Link — 首次部署脚本
# Docker 跑 backend + frontend,服务器本机 MySQL,宝塔 Nginx 反代
# ============================================================
set -e
echo "=== AI Knowledge Link 部署 ==="
# 检查 Docker
if ! command -v docker &> /dev/null; then
echo "错误:未安装 Docker"
exit 1
fi
# 1. 创建 .env
if [ ! -f .env ]; then
echo "创建 .env ..."
SECRET=$(python3 -c "import secrets; print(secrets.token_urlsafe(48))" 2>/dev/null || openssl rand -base64 48)
cat > .env << EOF
SECRET_KEY=${SECRET}
MYSQL_PASSWORD=Lzcc6-01
EOF
echo ".env 已创建"
fi
# 2. 数据目录
mkdir -p data
# 3. 确保数据库存在(本机 MySQL 33306
echo "检查数据库..."
mysql -h 127.0.0.1 -P 33306 -u admin -pLzcc6-01 \
-e "CREATE DATABASE IF NOT EXISTS amb_rag CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;" \
2>/dev/null || echo "提示:无法连接本机 MySQL,请确认已启动(不影响后续,backend 启动时会再试)"
# 4. 构建并启动
echo "构建镜像..."
docker compose build
echo "启动服务..."
docker compose up -d
# 5. 数据库迁移
echo "执行数据库迁移..."
sleep 3
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('管理员已存在')
"
echo ""
echo "=========================================="
echo " 容器部署完成!"
echo ""
echo " 后端: http://127.0.0.1:8000/api/healthz"
echo " 前端: http://127.0.0.1:5173"
echo ""
echo " 下一步:宝塔 Nginx 反代(站点设置 → 反向代理):"
echo " /api/ → http://127.0.0.1:8000"
echo " /k/ → http://127.0.0.1:8000"
echo " / → http://127.0.0.1:5173"
echo "=========================================="