#!/usr/bin/env bash # ============================================================= # 远端执行 docker compose 命令 # 用法: # ./remote-up.sh up # build + up -d # ./remote-up.sh build # 仅 build # ./remote-up.sh restart svc # 重启某服务 # ./remote-up.sh logs [svc] # 跟日志 # ./remote-up.sh ps # 服务状态 # ./remote-up.sh ssh # 登录到远端 # ============================================================= set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" if [ -f "$SCRIPT_DIR/server.env" ]; then # shellcheck disable=SC1091 set -a . "$SCRIPT_DIR/server.env" set +a fi SERVER_HOST="${SERVER_HOST:?SERVER_HOST 未配置,请先 cp server.env.example server.env}" SERVER_USER="${SERVER_USER:-root}" SERVER_PORT="${SERVER_PORT:-22}" REMOTE_DIR="${REMOTE_DIR:-/root/integral-shop}" if [ -n "${SSHPASS:-}" ]; then command -v sshpass >/dev/null 2>&1 || { echo "需要 sshpass"; exit 1; } export SSHPASS SSH=(sshpass -e ssh -p "$SERVER_PORT" -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -t) else SSH=(ssh -p "$SERVER_PORT" -o StrictHostKeyChecking=no -t) fi run_remote() { "${SSH[@]}" "${SERVER_USER}@${SERVER_HOST}" "cd ${REMOTE_DIR}/deploy/docker && $*" } cmd="${1:-up}" shift || true case "$cmd" in up) # 预检:必须存在 .env run_remote "test -f .env && test -f integral-resell/.env" || { cat <