#!/usr/bin/env bash # deploy/lib/install.sh — crypto_okx 一键部署 set -e set -u if [ -n "${BASH_VERSION:-}" ]; then set -o pipefail fi LIB_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # shellcheck source=common.sh source "${LIB_DIR}/common.sh" install_fresh() { step "克隆仓库" if [[ -d "${INSTALL_ROOT}" ]]; then die "目录已存在: ${INSTALL_ROOT},请选修复环境" fi mkdir -p "$(dirname "${INSTALL_ROOT}")" git clone -b "${GIT_BRANCH}" "${GIT_URL}" "${INSTALL_ROOT}" } install_repair() { step "修复环境(保留数据与配置)" bash "${REPO_ROOT}/deploy/setup_env.sh" --install-system-deps --recreate-venv } run_install_pipeline() { step "环境部署 setup_env.sh" bash "${REPO_ROOT}/deploy/setup_env.sh" --install-system-deps pm2_start_app pm2_save_startup install_backup_cron verify_deployment "$(detect_server_ip)" || true print_post_install_guide "$(detect_server_ip)" } handle_existing_install() { echo "" echo "检测到已部署安装: ${INSTALL_ROOT}" echo " a) 取消" echo " b) 修复环境(重建 venv,保留 .env 与数据库)" echo " c) 重新启动 PM2(不改依赖)" local choice="" cm_read choice "请选择 [a/b/c]: " case "${choice}" in b|B) install_repair if command -v pm2 >/dev/null 2>&1; then pm2_start_app pm2_save_startup fi verify_deployment "$(detect_server_ip)" || true print_post_install_guide "$(detect_server_ip)" ;; c|C) pm2_start_app pm2_save_startup verify_deployment "$(detect_server_ip)" || true print_post_install_guide "$(detect_server_ip)" ;; *) log "已取消" ;; esac } ensure_repo_ready() { if repo_ready "${INSTALL_ROOT}"; then REPO_ROOT="${INSTALL_ROOT}" elif [[ -n "${REPO_ROOT:-}" ]] && repo_ready "${REPO_ROOT}"; then : else REPO_ROOT="" fi if [[ -z "${REPO_ROOT}" ]]; then install_system_packages install_node_pm2 install_fresh REPO_ROOT="${INSTALL_ROOT}" else install_system_packages install_node_pm2 fi } main_install() { require_root require_ubuntu if repo_ready "${INSTALL_ROOT}"; then REPO_ROOT="${INSTALL_ROOT}" elif repo_ready "${REPO_ROOT}"; then : else REPO_ROOT="" fi if [[ -n "${REPO_ROOT}" ]] && is_deployed "${REPO_ROOT}"; then handle_existing_install return 0 fi ensure_repo_ready run_install_pipeline } main_install "$@"