Files
ai_mip/start_production.sh
2026-01-16 22:06:46 +08:00

260 lines
7.8 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
# MIP广告自动点击系统 - 生产环境启动脚本
# 适用于 Ubuntu/Debian 系统
set -e # 遇到错误立即退出
echo "============================================================"
echo "MIP广告自动点击系统 - 生产环境启动"
echo "============================================================"
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# 项目目录(脚本所在目录)
PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$PROJECT_DIR"
echo -e "${GREEN}项目目录: $PROJECT_DIR${NC}"
# 检查Python版本
echo ""
echo "============================================================"
echo "检查Python环境"
echo "============================================================"
if ! command -v python3 &> /dev/null; then
echo -e "${RED}错误: 未找到 python3${NC}"
echo "请安装Python 3.8+: sudo apt-get install python3 python3-pip python3-venv"
exit 1
fi
PYTHON_VERSION=$(python3 --version | awk '{print $2}')
echo -e "${GREEN}Python版本: $PYTHON_VERSION${NC}"
# 检查Python版本是否 >= 3.8
PYTHON_MAJOR=$(echo $PYTHON_VERSION | cut -d. -f1)
PYTHON_MINOR=$(echo $PYTHON_VERSION | cut -d. -f2)
if [ "$PYTHON_MAJOR" -lt 3 ] || ([ "$PYTHON_MAJOR" -eq 3 ] && [ "$PYTHON_MINOR" -lt 8 ]); then
echo -e "${RED}错误: Python版本过低需要 3.8+${NC}"
echo "当前版本: $PYTHON_VERSION"
exit 1
fi
# 虚拟环境目录
VENV_DIR="$PROJECT_DIR/venv"
# 创建虚拟环境(如果不存在)
if [ ! -d "$VENV_DIR" ]; then
echo ""
echo "============================================================"
echo "创建Python虚拟环境"
echo "============================================================"
# 检查是否安装了 venv 模块
if ! python3 -m venv --help &> /dev/null; then
echo -e "${YELLOW}警告: python3-venv 未安装,正在尝试安装...${NC}"
# 尝试安装需要sudo权限
if command -v apt-get &> /dev/null; then
sudo apt-get update
sudo apt-get install -y python3-venv
else
echo -e "${RED}错误: 无法自动安装 python3-venv${NC}"
echo "请手动执行: sudo apt-get install python3-venv"
exit 1
fi
fi
echo -e "${GREEN}正在创建虚拟环境...${NC}"
python3 -m venv "$VENV_DIR"
echo -e "${GREEN}✓ 虚拟环境创建成功${NC}"
else
echo ""
echo -e "${GREEN}✓ 虚拟环境已存在: $VENV_DIR${NC}"
fi
# 激活虚拟环境
echo ""
echo "============================================================"
echo "激活虚拟环境"
echo "============================================================"
source "$VENV_DIR/bin/activate"
if [ "$VIRTUAL_ENV" != "" ]; then
echo -e "${GREEN}✓ 虚拟环境已激活: $VIRTUAL_ENV${NC}"
echo -e "${GREEN}Python路径: $(which python)${NC}"
else
echo -e "${RED}错误: 虚拟环境激活失败${NC}"
exit 1
fi
# 升级pip
echo ""
echo "============================================================"
echo "升级pip"
echo "============================================================"
python -m pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple
# 安装依赖
echo ""
echo "============================================================"
echo "安装项目依赖"
echo "============================================================"
if [ -f "requirements.txt" ]; then
echo -e "${GREEN}从 requirements.txt 安装依赖...${NC}"
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
else
echo -e "${YELLOW}警告: requirements.txt 不存在${NC}"
echo "手动安装核心依赖..."
pip install flask playwright requests loguru apscheduler python-dotenv -i https://pypi.tuna.tsinghua.edu.cn/simple
# 安装Playwright浏览器
echo "安装Playwright浏览器驱动..."
playwright install chromium
fi
# 检查配置文件
echo ""
echo "============================================================"
echo "检查配置文件"
echo "============================================================"
if [ ! -f ".env.production" ]; then
echo -e "${YELLOW}警告: .env.production 不存在${NC}"
if [ -f ".env.example" ]; then
echo "从 .env.example 创建配置文件..."
cp .env.example .env.production
echo -e "${GREEN}✓ 已创建 .env.production${NC}"
echo -e "${RED}请编辑 .env.production 配置文件后重新启动${NC}"
exit 1
else
echo -e "${RED}错误: 缺少配置文件模板${NC}"
exit 1
fi
else
echo -e "${GREEN}✓ 配置文件存在: .env.production${NC}"
fi
# 初始化数据库
echo ""
echo "============================================================"
echo "初始化数据库"
echo "============================================================"
if [ ! -f "db/ai_mip_prod.db" ]; then
echo -e "${YELLOW}数据库不存在,正在初始化...${NC}"
if [ -f "db/init_databases.py" ]; then
# 自动创建生产数据库(跳过交互)
python << EOF
from pathlib import Path
import sqlite3
db_dir = Path('db')
db_path = db_dir / 'ai_mip_prod.db'
if not db_path.exists():
print(f"创建数据库: {db_path}")
# 读取并执行SQL脚本
init_sql = db_dir / 'init_sqlite.sql'
if init_sql.exists():
with open(init_sql, 'r', encoding='utf-8') as f:
sql_script = f.read()
conn = sqlite3.connect(str(db_path))
conn.executescript(sql_script)
conn.commit()
conn.close()
print("✓ 数据库初始化完成")
else:
print("错误: 找不到 init_sqlite.sql")
exit(1)
EOF
echo -e "${GREEN}✓ 数据库初始化成功${NC}"
else
echo -e "${RED}错误: 找不到数据库初始化脚本${NC}"
exit 1
fi
else
echo -e "${GREEN}✓ 数据库已存在: db/ai_mip_prod.db${NC}"
fi
# 创建必要的目录
echo ""
echo "============================================================"
echo "创建必要目录"
echo "============================================================"
mkdir -p logs data
echo -e "${GREEN}✓ 目录创建完成${NC}"
# 检查AdsPower连接
echo ""
echo "============================================================"
echo "检查AdsPower连接"
echo "============================================================"
python << EOF
import os
os.environ['ENV'] = 'production'
try:
from adspower_client import AdsPowerClient
client = AdsPowerClient()
profiles = client.list_profiles()
if profiles:
print("\033[0;32m✓ AdsPower连接正常\033[0m")
profile_count = len(profiles.get('data', {}).get('list', []))
print(f"\033[0;32m Profile数量: {profile_count}\033[0m")
else:
print("\033[1;33m警告: AdsPower连接失败请检查配置\033[0m")
except Exception as e:
print(f"\033[1;33m警告: AdsPower连接异常: {str(e)}\033[0m")
print("\033[1;33m 请确保AdsPower客户端已启动\033[0m")
EOF
# 启动服务
echo ""
echo "============================================================"
echo "启动Flask服务"
echo "============================================================"
export ENV=production
# 检查端口是否被占用
PORT=5000
if command -v netstat &> /dev/null; then
if netstat -tuln | grep ":$PORT " > /dev/null; then
echo -e "${YELLOW}警告: 端口 $PORT 已被占用${NC}"
echo "尝试查找占用进程..."
lsof -i :$PORT || true
fi
fi
echo ""
echo -e "${GREEN}启动服务中...${NC}"
echo "访问地址: http://127.0.0.1:5000"
echo "按 Ctrl+C 停止服务"
echo ""
# 使用nohup在后台运行可选
# nohup python app.py > logs/app.log 2>&1 &
# echo $! > app.pid
# echo -e "${GREEN}✓ 服务已启动(后台运行)${NC}"
# echo "PID: $(cat app.pid)"
# echo "日志: logs/app.log"
# 前台运行(便于调试)
python app.py