Files
amb_rag/deploy.sh
T
2026-09-02 14:34:46 +08:00

88 lines
2.7 KiB
Bash

#!/bin/bash
# ============================================================
# AI Knowledge Link — 首次部署脚本
# 使用服务器本机 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. 创建数据库(如果不存在)
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 已启动且密码正确"
# 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://$(curl -s ifconfig.me 2>/dev/null):8000/api/healthz"
echo " 前端: http://$(curl -s ifconfig.me 2>/dev/null):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 "=========================================="