#!/bin/bash
set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
COMMON_DIR="$SCRIPT_DIR/common"

FTP_SERVER="${FTP_SERVER:-172.30.0.40}"
FTP_USER="${FTP_USER:-ftpuser}"
FTP_PASSWORD="${FTP_PASSWORD:-ZGClab1234!}"
FTP_PORT="${FTP_PORT:-21}"

FTP_HOST="${FTP_SERVER}"

echo "[04] 检测GPU环境..."
# 检测GPU环境
if bash "$COMMON_DIR/check-gpu.sh"; then
    echo "[04] GPU环境可用，继续执行GPU节点安装"
    GPU_AVAILABLE=true
else
    echo "[04] GPU环境不可用，跳过GPU节点安装"
    GPU_AVAILABLE=false
    exit 0
fi

echo "[04] 进入测试节点执行安装..."
echo "[04] 使用 FTP 地址: ${FTP_HOST}:${FTP_PORT}"

docker exec argus-metric-test-gpu-node bash -c "
set -e

if ! command -v curl &>/dev/null; then
    echo '[04] curl 未安装，正在安装...'
    apt-get update && apt-get install -y curl
fi

cd /tmp
echo '[04] 下载 setup.sh...'
curl -u ${FTP_USER}:${FTP_PASSWORD} ftp://${FTP_HOST}:${FTP_PORT}/setup.sh -o setup.sh

echo '[04] 执行安装...'
chmod +x setup.sh
bash setup.sh --server ${FTP_HOST} --user ${FTP_USER} --password '${FTP_PASSWORD}' --port ${FTP_PORT}

echo '[04] 安装完成'
"

echo "[04] 完成"
