Files
crypto_monitor/deploy/setup_env.sh
T
2026-05-21 11:45:49 +08:00

185 lines
4.9 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.
#!/usr/bin/env bash
# crypto_monitor 一键环境部署(Linux / macOS / Git Bash
#
# 用法:
# bash deploy/setup_env.sh
# bash deploy/setup_env.sh --only binance,gate_bot
# bash deploy/setup_env.sh --skip-pm2
# bash deploy/setup_env.sh --recreate-venv
#
set -euo pipefail
DEPLOY_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "${DEPLOY_DIR}/.." && pwd)"
REQ_FILE="${REPO_ROOT}/requirements.txt"
HUB_REQ="${REPO_ROOT}/manual_trading_hub/requirements.txt"
ONLY="all"
SKIP_PM2=0
SKIP_ENV_COPY=0
RECREATE_VENV=0
usage() {
sed -n '2,12p' "$0" | sed 's/^# \?//'
exit "${1:-0}"
}
while [[ $# -gt 0 ]]; do
case "$1" in
--only) ONLY="${2:-all}"; shift 2 ;;
--skip-pm2) SKIP_PM2=1; shift ;;
--skip-env-copy) SKIP_ENV_COPY=1; shift ;;
--recreate-venv) RECREATE_VENV=1; shift ;;
-h|--help) usage 0 ;;
*) echo "未知参数: $1" >&2; usage 1 ;;
esac
done
step() { echo ""; echo "==> $*"; }
should_include() {
local key="$1"
if [[ "${ONLY}" == "all" ]]; then
return 0
fi
local item
IFS=',' read -ra PARTS <<< "${ONLY}"
for item in "${PARTS[@]}"; do
item="$(echo "${item}" | tr '[:upper:]' '[:lower:]' | xargs)"
[[ "${item}" == "${key}" ]] && return 0
done
return 1
}
find_python() {
if command -v python3 >/dev/null 2>&1; then
echo python3
return
fi
if command -v python >/dev/null 2>&1; then
echo python
return
fi
echo "未找到 python3/python,请先安装 Python 3.10+" >&2
exit 1
}
check_python_version() {
local py="$1"
local ver
ver="$("${py}" -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")')"
local major minor
major="${ver%%.*}"
minor="${ver#*.}"
if [[ "${major}" -lt 3 ]] || [[ "${major}" -eq 3 && "${minor}" -lt 10 ]]; then
echo "需要 Python 3.10+,当前: ${ver}" >&2
exit 1
fi
echo "Python: $("${py}" --version 2>&1)"
}
setup_monitor() {
local dir_name="$1"
local proj="${REPO_ROOT}/${dir_name}"
if [[ ! -d "${proj}" ]]; then
echo " 跳过(目录不存在): ${dir_name}"
return
fi
step "${dir_name}"
cd "${proj}"
local py
py="$(find_python)"
if [[ "${RECREATE_VENV}" -eq 1 && -d .venv ]]; then
echo " 删除旧 venv ..."
rm -rf .venv
fi
if [[ ! -x .venv/bin/python ]]; then
echo " 创建 venv ..."
"${py}" -m venv .venv
fi
echo " 升级 pip ..."
.venv/bin/python -m pip install -U pip setuptools wheel -q
echo " 安装依赖 ..."
.venv/bin/pip install -r "${REQ_FILE}" -q
if [[ "${SKIP_ENV_COPY}" -eq 0 ]]; then
if [[ -f .env.example && ! -f .env ]]; then
cp -n .env.example .env 2>/dev/null || cp .env.example .env
echo " 已复制 .env.example -> .env"
elif [[ -f .env ]]; then
echo " 保留已有 .env"
else
echo " 无 .env.example,请手动配置 .env"
fi
fi
mkdir -p static/images/order_charts
echo " 完成: ${proj}/.venv/bin/python"
}
setup_hub() {
local proj="${REPO_ROOT}/manual_trading_hub"
if [[ ! -d "${proj}" ]]; then
echo " 跳过 hub(目录不存在)"
return
fi
step "manual_trading_hub"
cd "${proj}"
local py
py="$(find_python)"
if [[ "${RECREATE_VENV}" -eq 1 && -d .venv ]]; then
rm -rf .venv
fi
if [[ ! -x .venv/bin/python ]]; then
"${py}" -m venv .venv
fi
.venv/bin/python -m pip install -U pip setuptools wheel -q
if [[ -f "${HUB_REQ}" ]]; then
.venv/bin/pip install -r "${HUB_REQ}" -q
fi
if [[ "${SKIP_ENV_COPY}" -eq 0 && -f .env.example && ! -f .env ]]; then
cp -n .env.example .env 2>/dev/null || cp .env.example .env
echo " 已复制 .env.example -> .env"
fi
echo " 完成: ${proj}/.venv/bin/python"
}
install_pm2() {
if [[ "${SKIP_PM2}" -eq 1 ]]; then
return
fi
step "PM2(可选)"
if ! command -v node >/dev/null 2>&1; then
echo " 未检测到 Node.js,跳过。安装后执行: npm install -g pm2"
return
fi
if command -v pm2 >/dev/null 2>&1; then
echo " PM2 已安装: $(pm2 -v)"
return
fi
echo " 正在安装 pm2 ..."
npm install -g pm2
echo " 各子目录: pm2 start ecosystem.config.cjs"
}
echo "crypto_monitor 环境部署"
echo "仓库根目录: ${REPO_ROOT}"
[[ -f "${REQ_FILE}" ]] || { echo "缺少 ${REQ_FILE}" >&2; exit 1; }
PY="$(find_python)"
check_python_version "${PY}"
should_include binance && setup_monitor crypto_monitor_binance
should_include gate && setup_monitor crypto_monitor_gate
should_include gate_bot && setup_monitor crypto_monitor_gate_bot
should_include okx && setup_monitor crypto_monitor_okx
should_include hub && setup_hub
install_pm2
echo ""
echo "部署完成。下一步:"
echo " 1. 编辑各子目录 .env"
echo " 2. 启动示例: cd crypto_monitor_binance && source .venv/bin/activate && python app.py"
echo " 3. Windows 可用: powershell -File deploy/setup_env.ps1"
echo ""