|
@@ -0,0 +1,80 @@
|
|
|
+#!/bin/bash
|
|
|
+set -e # 出错时终止脚本
|
|
|
+
|
|
|
+# 配置信息
|
|
|
+APP_DIR="/root/AutoScraperX"
|
|
|
+LOG_FILE="/var/log/autoscraperx_deploy.log"
|
|
|
+VENV_DIR="${APP_DIR}/venv"
|
|
|
+PYTHON="${VENV_DIR}/bin/python3"
|
|
|
+REQUIREMENTS="${APP_DIR}/requirements.txt"
|
|
|
+
|
|
|
+# 日志函数
|
|
|
+log() {
|
|
|
+ local msg="[$(date '+%Y-%m-%d %H:%M:%S')] $1"
|
|
|
+ echo "$msg"
|
|
|
+ echo "$msg" >> "$LOG_FILE"
|
|
|
+}
|
|
|
+
|
|
|
+# 错误处理函数
|
|
|
+handle_error() {
|
|
|
+ log "部署失败: $1"
|
|
|
+ exit 1
|
|
|
+}
|
|
|
+
|
|
|
+# 确保目录存在
|
|
|
+ensure_dir() {
|
|
|
+ if [ ! -d "$1" ]; then
|
|
|
+ log "创建目录: $1"
|
|
|
+ mkdir -p "$1" || handle_error "无法创建目录 $1"
|
|
|
+ fi
|
|
|
+}
|
|
|
+
|
|
|
+# 主函数
|
|
|
+main() {
|
|
|
+ log "===== 开始部署 AutoScraperX ====="
|
|
|
+
|
|
|
+ # 检查目录
|
|
|
+ ensure_dir "$APP_DIR"
|
|
|
+ cd "$APP_DIR" || handle_error "应用目录不存在"
|
|
|
+
|
|
|
+ # 拉取最新代码(如果是Git仓库)
|
|
|
+ log "拉取最新代码..."
|
|
|
+ if [ -d ".git" ]; then
|
|
|
+ git pull origin master || handle_error "Git拉取失败"
|
|
|
+ else
|
|
|
+ log "警告: 当前目录不是Git仓库,跳过拉取"
|
|
|
+ fi
|
|
|
+
|
|
|
+ # 创建或更新虚拟环境
|
|
|
+ log "检查虚拟环境..."
|
|
|
+ if [ ! -d "$VENV_DIR" ]; then
|
|
|
+ log "创建新的虚拟环境..."
|
|
|
+ python3 -m venv "$VENV_DIR" || handle_error "创建虚拟环境失败"
|
|
|
+ fi
|
|
|
+
|
|
|
+ # 安装依赖
|
|
|
+ log "安装Python依赖..."
|
|
|
+ "$PYTHON" -m pip install --upgrade pip || handle_error "更新pip失败"
|
|
|
+ "$PYTHON" -m pip install -r "$REQUIREMENTS" || handle_error "安装依赖失败"
|
|
|
+
|
|
|
+ # 停止现有服务
|
|
|
+ log "停止现有服务..."
|
|
|
+ pkill -f "$PYTHON main.py" || true # 忽略"无进程可杀"的错误
|
|
|
+
|
|
|
+ # 启动服务(使用nohup后台运行)
|
|
|
+ log "启动新服务..."
|
|
|
+ nohup "$PYTHON" main.py > /dev/null 2>&1 &
|
|
|
+ sleep 2 # 等待2秒,确保服务启动
|
|
|
+
|
|
|
+ # 检查服务状态
|
|
|
+ if pgrep -f "$PYTHON main.py" > /dev/null; then
|
|
|
+ log "服务已成功启动!"
|
|
|
+ else
|
|
|
+ handle_error "服务启动失败!"
|
|
|
+ fi
|
|
|
+
|
|
|
+ log "===== 部署完成 ====="
|
|
|
+}
|
|
|
+
|
|
|
+# 执行主函数
|
|
|
+main
|