diff --git a/deploy/setup_env.ps1 b/deploy/setup_env.ps1 deleted file mode 100644 index 6f611cb..0000000 --- a/deploy/setup_env.ps1 +++ /dev/null @@ -1,218 +0,0 @@ -#Requires -Version 5.1 -<# -.SYNOPSIS - crypto_monitor 一键环境部署(Windows PowerShell) - -.DESCRIPTION - - 为各子项目创建 Python venv 并安装依赖 - - 从 .env.example 复制 .env(不覆盖已有) - - 创建 static/images 等运行时目录 - - 可选安装 PM2(需已安装 Node.js) - -.EXAMPLE - .\deploy\setup_env.ps1 - .\deploy\setup_env.ps1 -Only binance,gate_bot - .\deploy\setup_env.ps1 -SkipPm2 -#> -param( - [string]$Only = "all", - [switch]$SkipPm2, - [switch]$SkipEnvCopy, - [switch]$RecreateVenv -) - -$ErrorActionPreference = "Stop" -[Console]::OutputEncoding = [System.Text.Encoding]::UTF8 - -$DeployDir = $PSScriptRoot -$RepoRoot = (Resolve-Path (Join-Path $DeployDir "..")).Path -$ReqFile = Join-Path $RepoRoot "requirements.txt" -$HubReqFile = Join-Path $RepoRoot "manual_trading_hub\requirements.txt" - -$MonitorProjects = @( - @{ Key = "binance"; Dir = "crypto_monitor_binance" }, - @{ Key = "gate"; Dir = "crypto_monitor_gate" }, - @{ Key = "gate_bot"; Dir = "crypto_monitor_gate_bot" }, - @{ Key = "okx"; Dir = "crypto_monitor_okx" } -) -$HubProject = @{ Key = "hub"; Dir = "manual_trading_hub" } - -function Write-Step([string]$Msg) { - Write-Host "" - Write-Host "==> $Msg" -ForegroundColor Cyan -} - -function Test-Python310 { - $py = Get-Command python -ErrorAction SilentlyContinue - if (-not $py) { - throw "未找到 python。请安装 Python 3.10+ 并加入 PATH:https://www.python.org/downloads/" - } - $verText = & python -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')" - $parts = $verText.Trim() -split "\." - $major = [int]$parts[0] - $minor = [int]$parts[1] - if ($major -lt 3 -or ($major -eq 3 -and $minor -lt 10)) { - throw "需要 Python 3.10+,当前: $verText" - } - Write-Host "Python: $(python --version 2>&1)" -ForegroundColor DarkGray -} - -function Should-Include([string]$Key, [string[]]$Selected) { - if ($Selected -contains "all") { return $true } - return $Selected -contains $Key -} - -# 复制 .env 时统一为 LF,避免上传到 Linux 后 PM2 source 报 $'\r': command not found -function Copy-EnvFileLf([string]$Src, [string]$Dst) { - $raw = [System.IO.File]::ReadAllText($Src) - $lf = ($raw -replace "`r`n", "`n") -replace "`r", "`n" - $utf8NoBom = New-Object System.Text.UTF8Encoding $false - [System.IO.File]::WriteAllText($Dst, $lf, $utf8NoBom) -} - -function Setup-MonitorProject([hashtable]$Proj) { - $projPath = Join-Path $RepoRoot $Proj.Dir - if (-not (Test-Path $projPath)) { - Write-Host " 跳过(目录不存在): $($Proj.Dir)" -ForegroundColor Yellow - return - } - Write-Step "$($Proj.Dir)" - Push-Location $projPath - try { - $venvDir = Join-Path $projPath ".venv" - $venvPy = Join-Path $venvDir "Scripts\python.exe" - $venvPip = Join-Path $venvDir "Scripts\pip.exe" - - if ($RecreateVenv -and (Test-Path $venvDir)) { - Write-Host " 删除旧 venv ..." - Remove-Item -Recurse -Force $venvDir - } - if (-not (Test-Path $venvPy)) { - Write-Host " 创建 venv ..." - & python -m venv .venv - } - - Write-Host " 升级 pip ..." - & $venvPy -m pip install -U pip setuptools wheel -q - - Write-Host " 安装依赖 (requirements.txt) ..." - & $venvPip install -r $ReqFile -q - - if (-not $SkipEnvCopy) { - $envExample = Join-Path $projPath ".env.example" - $envFile = Join-Path $projPath ".env" - if ((Test-Path $envExample) -and -not (Test-Path $envFile)) { - Copy-EnvFileLf $envExample $envFile - Write-Host " 已复制 .env.example -> .env (LF)" -ForegroundColor Green - } elseif (Test-Path $envFile) { - Write-Host " 保留已有 .env" -ForegroundColor DarkGray - } else { - Write-Host " 无 .env.example,请手动配置 .env" -ForegroundColor Yellow - } - } - - $staticDirs = @( - "static\images", - "static\images\order_charts" - ) - foreach ($d in $staticDirs) { - $full = Join-Path $projPath $d - if (-not (Test-Path $full)) { - New-Item -ItemType Directory -Path $full -Force | Out-Null - } - } - Write-Host " 完成: $venvPy" -ForegroundColor Green - } finally { - Pop-Location - } -} - -function Setup-HubProject() { - $projPath = Join-Path $RepoRoot $HubProject.Dir - if (-not (Test-Path $projPath)) { - Write-Host " 跳过 hub(目录不存在)" -ForegroundColor Yellow - return - } - Write-Step $HubProject.Dir - Push-Location $projPath - try { - $venvDir = Join-Path $projPath ".venv" - $venvPy = Join-Path $venvDir "Scripts\python.exe" - $venvPip = Join-Path $venvDir "Scripts\pip.exe" - - if ($RecreateVenv -and (Test-Path $venvDir)) { - Remove-Item -Recurse -Force $venvDir - } - if (-not (Test-Path $venvPy)) { - & python -m venv .venv - } - & $venvPy -m pip install -U pip setuptools wheel -q - if (Test-Path $HubReqFile) { - & $venvPip install -r $HubReqFile -q - } - - if (-not $SkipEnvCopy) { - $envExample = Join-Path $projPath ".env.example" - $envFile = Join-Path $projPath ".env" - if ((Test-Path $envExample) -and -not (Test-Path $envFile)) { - Copy-EnvFileLf $envExample $envFile - Write-Host " 已复制 .env.example -> .env (LF)" -ForegroundColor Green - } - } - Write-Host " 完成: $venvPy" -ForegroundColor Green - } finally { - Pop-Location - } -} - -function Install-Pm2IfNeeded() { - if ($SkipPm2) { return } - $node = Get-Command node -ErrorAction SilentlyContinue - if (-not $node) { - Write-Host "未检测到 Node.js,跳过 PM2。安装 Node 后执行: npm install -g pm2" -ForegroundColor Yellow - return - } - Write-Step "PM2(可选进程托管)" - $pm2 = Get-Command pm2 -ErrorAction SilentlyContinue - if ($pm2) { - Write-Host " PM2 已安装: $(pm2 -v)" -ForegroundColor Green - return - } - Write-Host " 正在全局安装 pm2 ..." - & npm install -g pm2 - Write-Host " PM2 安装完成。在各子目录执行: pm2 start ecosystem.config.cjs" -ForegroundColor Green -} - -# --- main --- -Write-Host "crypto_monitor 环境部署" -ForegroundColor White -Write-Host "仓库根目录: $RepoRoot" -ForegroundColor DarkGray - -if (-not (Test-Path $ReqFile)) { - throw "缺少 $ReqFile" -} - -Test-Python310 - -$selected = ($Only -split "[,;\s]+" | ForEach-Object { $_.Trim().ToLower() } | Where-Object { $_ }) -if (-not $selected -or $selected.Count -eq 0) { $selected = @("all") } - -foreach ($p in $MonitorProjects) { - if (Should-Include $p.Key $selected) { - Setup-MonitorProject $p - } -} -if (Should-Include $HubProject.Key $selected) { - Setup-HubProject -} - -Install-Pm2IfNeeded - -Write-Host "" -Write-Host "部署完成。下一步:" -ForegroundColor Green -Write-Host " 1. 编辑各子目录 .env(API Key、密码、代理等)" -Write-Host " 2. 启动示例(Binance):" -Write-Host " cd crypto_monitor_binance" -Write-Host " .\.venv\Scripts\activate" -Write-Host " python app.py" -Write-Host " 3. Linux 服务器可用: bash deploy/setup_env.sh" -Write-Host "" diff --git a/manual_trading_hub/使用说明.md b/manual_trading_hub/使用说明.md index 088d3d1..1a89cf8 100644 --- a/manual_trading_hub/使用说明.md +++ b/manual_trading_hub/使用说明.md @@ -51,8 +51,10 @@ ### 2.1 依赖安装 -```powershell -cd c:\Users\dekun\Desktop\crypto_monitor\manual_trading_hub +```bash +cd /opt/crypto_monitor/manual_trading_hub +python3 -m venv .venv +source .venv/bin/activate pip install -r requirements.txt ``` @@ -71,8 +73,9 @@ pip install -r requirements.txt ### 2.3 强制关闭某账户 -```powershell -$env:HUB_DISABLED_IDS="1" # 默认即关闭 OKX(id=1) +```bash +# 在 manual_trading_hub/.env 中设置,或临时: +export HUB_DISABLED_IDS=1 # 默认即关闭 OKX(id=1) ``` 与设置页「启用」取 **与** 关系:环境变量强制关闭时,网页勾选框会灰掉且无法启用。 @@ -229,22 +232,14 @@ Chrome **桌面快捷方式**图标来自站点 `favicon` / `manifest`(已配 - `DB_PATH` → 独立库(如 `crypto_gate2.db`),**勿**与 5000/5002 共用 `crypto.db` - `GATE_API_KEY` / `GATE_API_SECRET` → **该子账户** 密钥 - `HUB_BRIDGE_TOKEN` → 与中控、其它实例 **相同** -3. 安装 venv 与依赖(与 Gate 部署文档相同),启动: +3. 安装 venv 与依赖(`bash /opt/crypto_monitor/deploy/setup_env.sh --only gate` 或按 Gate 部署文档),启动: -```powershell -cd crypto_monitor_gate_2 -$env:PYTHONPATH=".." -python app.py +```bash +cd /opt/crypto_monitor/crypto_monitor_gate_2 +pm2 start ecosystem.config.cjs ``` -4. 另开终端,**cwd 必须为新目录**,启动子代理: - -```powershell -cd crypto_monitor_gate_2 -$env:EXCHANGE="gate" -$env:PORT="15204" -python ..\manual_trading_hub\agent.py -``` +4. 在中控 `ecosystem.config.cjs` 增加对应 agent,或单独 `run_agent.sh` 配置后 `pm2 restart`(勿与已有 agent 端口冲突)。 验收:`curl http://127.0.0.1:5005/login` 能开页;`curl http://127.0.0.1:15204/status` 返回 `ok`。 @@ -382,9 +377,8 @@ PM2:仓库 `ecosystem.config.cjs` 默认只有四 agent;第五户需自行 ` 手动探测实例桥接: -```powershell -$tok = "你的令牌" -Invoke-WebRequest -Uri "http://127.0.0.1:5001/api/hub/ping" -Headers @{"X-Hub-Token"=$tok} +```bash +curl -sS -H "X-Hub-Token: 你的令牌" http://127.0.0.1:5001/api/hub/ping ``` --- @@ -399,10 +393,10 @@ Invoke-WebRequest -Uri "http://127.0.0.1:5001/api/hub/ping" -Headers @{"X-Hub-To 子代理 `agent.py` 仍负责持仓与全平;`HUB_AGENTS` 环境变量在新版 hub 中 **不再使用**(以设置文件为准)。 -**PM2 守护(推荐 Linux)**: +**PM2 守护**: ```bash -cd manual_trading_hub +cd /opt/crypto_monitor/manual_trading_hub python3 -m venv .venv source .venv/bin/activate pip install -r requirements.txt diff --git a/一键部署.bat b/一键部署.bat deleted file mode 100644 index f0b4690..0000000 --- a/一键部署.bat +++ /dev/null @@ -1,16 +0,0 @@ -@echo off -chcp 65001 >nul -setlocal -cd /d "%~dp0" -echo crypto_monitor 一键环境部署 ... -powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0deploy\setup_env.ps1" %* -set ERR=%ERRORLEVEL% -if not "%ERR%"=="0" ( - echo. - echo 部署失败,退出码 %ERR% - pause - exit /b %ERR% -) -echo. -pause -exit /b 0