neural-sync/deploy.sh

350 lines
10 KiB
Bash
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/bash
# Neural Sync 腾讯云一键部署脚本
set -e
echo "========================================"
echo " Neural Sync 腾讯云部署脚本"
echo "========================================"
# 颜色定义
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
# 配置变量
PROJECT_DIR="/opt/neural-sync"
DB_PASSWORD=${1:-"neural_sync_2024"}
GIT_REPO=${2:-""}
# 检查是否为 root 用户
if [ "$EUID" -ne 0 ]; then
echo -e "${RED}❌ 请使用 root 用户运行此脚本${NC}"
exit 1
fi
echo -e "${GREEN}✅ 开始部署...${NC}"
# ========== 1. 安装系统依赖 ==========
echo -e "${YELLOW}[1/9] 安装系统依赖 (包括 Docker)...${NC}"
echo -e "${YELLOW}>>> 清理旧的 Docker 仓库配置...${NC}"
rm -f /etc/apt/sources.list.d/docker*.list 2>/dev/null || true
rm -f /etc/apt/keyrings/docker.gpg 2>/dev/null || true
echo -e "${YELLOW}>>> 添加 Docker 国内镜像源...${NC}"
# 添加 Docker 阿里云镜像源
mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg 2>/dev/null || \
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg 2>/dev/null || true
echo "deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] http://mirrors.aliyun.com/docker-ce/linux/debian bullseye stable" > /etc/apt/sources.list.d/docker.list
# 更新包列表
apt-get update || apt-get update --allow-insecure-repositories
echo -e "${YELLOW}>>> 安装基础工具...${NC}"
apt-get install -y --no-install-recommends ca-certificates curl wget git nginx software-properties-common || true
echo -e "${YELLOW}>>> 安装 Docker...${NC}"
apt-get install -y --no-install-recommends docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin || true
# 启动 Docker
systemctl start docker || true
systemctl enable docker || true
echo -e "${YELLOW}>>> 配置 Docker 镜像加速...${NC}"
mkdir -p /etc/docker
cat > /etc/docker/daemon.json << 'DOCKEREOF'
{
"registry-mirrors": [
"https://docker.mirrors.ustc.edu.cn",
"https://hub-mirror.c.163.com",
"https://mirror.baidubce.com"
]
}
DOCKEREOF
systemctl daemon-reload
systemctl restart docker || true
# 安装 Python
apt-get install -y --no-install-recommends python3.9 python3-pip python3-venv python3-dev libpq-dev || true
# 安装 Node.js
curl -fsSL https://deb.nodesource.com/setup_18.x | bash - || true
apt-get install -y --no-install-recommends nodejs || true
python3 --version
node --version
npm --version
docker --version
echo -e "${GREEN}✅ 系统依赖安装完成${NC}"
# ========== 2. 配置 PostgreSQL ==========
echo -e "${YELLOW}[2/9] 配置 PostgreSQL 数据库...${NC}"
apt-get install -y --no-install-recommends postgresql postgresql-contrib || true
# 启动 PostgreSQL
echo "启动 PostgreSQL..."
systemctl start postgresql || true
systemctl enable postgresql || true
# 等待 PostgreSQL 就绪 (最多等待 30 秒)
echo "等待 PostgreSQL 就绪..."
for i in {1..30}; do
if su - postgres -c "psql -c 'SELECT 1;'" > /dev/null 2>&1; then
echo "PostgreSQL 已就绪"
break
fi
sleep 1
done
# 创建数据库
echo "创建数据库..."
su - postgres -c "psql -c \"CREATE DATABASE neural_sync;\"" 2>/dev/null || true
su - postgres -c "psql -c \"CREATE USER neuraluser WITH PASSWORD '$DB_PASSWORD';\"" 2>/dev/null || true
su - postgres -c "psql -c \"GRANT ALL PRIVILEGES ON DATABASE neural_sync TO neuraluser;\"" 2>/dev/null || true
su - postgres -c "psql -d neural_sync -c \"CREATE EXTENSION IF NOT EXISTS vector;\"" 2>/dev/null || true
echo -e "${GREEN}✅ PostgreSQL 配置完成${NC}"
# ========== 3. 拉取项目代码 ==========
echo -e "${YELLOW}[3/9] 拉取项目代码...${NC}"
cd /opt
if [ -d "$PROJECT_DIR" ]; then
mv "$PROJECT_DIR" "${PROJECT_DIR}_backup_$(date +%Y%m%d_%H%M%S)"
fi
if [ -n "$GIT_REPO" ]; then
echo "从 Git 仓库拉取: $GIT_REPO"
git clone "$GIT_REPO" neural-sync
else
echo -e "${YELLOW}请选择代码获取方式:${NC}"
echo " 1) 从 GitHub 拉取"
echo " 2) 从 Gitee (码云) 拉取"
echo " 3) 使用默认示例仓库"
echo ""
read -p "请输入选项 [1-3]: " choice
case $choice in
1)
echo -e "${YELLOW}请输入 GitHub 仓库地址:${NC}"
echo " 格式: https://github.com/用户名/仓库名.git"
read -p "仓库地址: " GIT_REPO
if [ -n "$GIT_REPO" ]; then
git clone "$GIT_REPO" neural-sync
else
echo -e "${RED}❌ 未输入仓库地址${NC}"
exit 1
fi
;;
2)
echo -e "${YELLOW}请输入 Gitee 仓库地址:${NC}"
echo " 格式: https://gitee.com/用户名/仓库名.git"
read -p "仓库地址: " GIT_REPO
if [ -n "$GIT_REPO" ]; then
git clone "$GIT_REPO" neural-sync
else
echo -e "${RED}❌ 未输入仓库地址${NC}"
exit 1
fi
;;
3)
echo -e "${YELLOW}克隆官方示例仓库...${NC}"
git clone https://github.com/anomalyco/neural-sync.git neural-sync 2>/dev/null || \
git clone https://gitee.com/ai-tools/neural-sync.git neural-sync 2>/dev/null || {
echo -e "${RED}❌ 无法拉取示例仓库,请手动配置仓库地址${NC}"
exit 1
}
;;
*)
echo -e "${RED}❌ 无效选项${NC}"
exit 1
;;
esac
fi
if [ ! -d "$PROJECT_DIR" ]; then
echo -e "${RED}❌ 项目代码拉取失败${NC}"
exit 1
fi
echo -e "${GREEN}✅ 项目代码拉取完成${NC}"
# ========== 4. 配置后端 ==========
echo -e "${YELLOW}[4/9] 配置后端服务...${NC}"
cd "$PROJECT_DIR/backend"
python3 -m venv venv
source venv/bin/activate
pip install --upgrade pip
pip install -r requirements.txt
cp .env.example .env 2>/dev/null || true
if [ -f .env ]; then
cat > .env << EOF
DATABASE_URL=postgresql+asyncpg://neuraluser:${DB_PASSWORD}@localhost:5432/neural_sync
OLLAMA_HOST=http://127.0.0.1:11434
BUSINESS_CONTEXT=Pixelle-Video: An AI-powered automated short video generation platform.
EOF
fi
alembic upgrade head 2>/dev/null || echo "迁移完成或跳过"
deactivate
echo -e "${GREEN}✅ 后端配置完成${NC}"
# ========== 5. 构建前端 ==========
echo -e "${YELLOW}[5/9] 构建前端...${NC}"
cd "$PROJECT_DIR/frontend"
npm install
npm run build
if [ ! -d "dist" ]; then
echo -e "${RED}❌ 前端构建失败${NC}"
exit 1
fi
echo -e "${GREEN}✅ 前端构建完成${NC}"
# ========== 6. 配置 Nginx ==========
echo -e "${YELLOW}[6/9] 配置 Nginx...${NC}"
cat > /etc/nginx/sites-available/neural-sync << 'EOF'
server {
listen 80;
server_name _;
location / {
root /opt/neural-sync/frontend/dist;
index index.html;
try_files $uri $uri/ /index.html;
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2)$ {
expires 1y;
add_header Cache-Control "public, immutable";
}
}
location /api {
proxy_pass http://127.0.0.1:8000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_cache_bypass $http_upgrade;
}
location /ws {
proxy_pass http://127.0.0.1:8000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
EOF
ln -sf /etc/nginx/sites-available/neural-sync /etc/nginx/sites-enabled/
rm -f /etc/nginx/sites-enabled/default
nginx -t
systemctl reload nginx
echo -e "${GREEN}✅ Nginx 配置完成${NC}"
# ========== 7. 配置 Systemd 服务 ==========
echo -e "${YELLOW}[7/9] 配置 Systemd 服务...${NC}"
cat > /etc/systemd/system/neural-sync.service << EOF
[Unit]
Description=Neural Sync Backend API
After=network.target postgresql.service
[Service]
Type=simple
User=root
WorkingDirectory=$PROJECT_DIR/backend
Environment="PATH=$PROJECT_DIR/backend/venv/bin"
Environment="PYTHONPATH=$PROJECT_DIR/backend"
EnvironmentFile=$PROJECT_DIR/backend/.env
ExecStart=$PROJECT_DIR/backend/venv/bin/uvicorn app.main:app --host 127.0.0.1 --port 8000
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable neural-sync
systemctl restart neural-sync
sleep 3
if systemctl is-active --quiet neural-sync; then
echo -e "${GREEN}✅ 后端服务启动成功${NC}"
else
echo -e "${RED}❌ 后端服务启动失败,查看日志:${NC}"
journalctl -u neural-sync -n 20
fi
# ========== 9. 开放防火墙端口 ==========
echo -e "${YELLOW}[9/9] 开放防火墙端口...${NC}"
# Ubuntu ufw
if command -v ufw &> /dev/null; then
ufw allow 80/tcp 2>/dev/null || true
ufw allow 443/tcp 2>/dev/null || true
ufw allow 8000/tcp 2>/dev/null || true
fi
echo -e "${GREEN}✅ 防火墙配置完成${NC}"
# 获取服务器 IP
SERVER_IP=$(hostname -I | awk '{print $1}')
echo ""
echo "========================================"
echo " 🎉 部署完成!"
echo "========================================"
echo ""
echo -e "${GREEN}访问地址:${NC}"
echo " - 前端: http://$SERVER_IP"
echo " - API: http://$SERVER_IP/api"
echo " - API文档: http://$SERVER_IP/api/docs"
echo ""
echo -e "${YELLOW}常用命令:${NC}"
echo " - 查看后端日志: journalctl -u neural-sync -f"
echo " - 重启后端: systemctl restart neural-sync"
echo " - 重载 Nginx: nginx -s reload"
echo " - Docker: docker ps / docker logs 等"
echo ""
echo -e "${YELLOW}配置信息:${NC}"
echo " - 数据库: neural_sync"
echo " - 数据库用户: neuraluser"
echo " - 数据库密码: $DB_PASSWORD"
echo ""
echo -e "${YELLOW}后续步骤:${NC}"
echo " 1. 如需 HTTPS,请配置 SSL 证书 (使用 Let's Encrypt)"
echo " 2. 如需 AI 功能,请安装 Ollama"
echo " 3. 修改 .env 文件中的 BUSINESS_CONTEXT 为你的业务描述"
echo ""