#!/bin/bash

# NFS配置验证脚本

set -e

echo "🔍 验证NFS配置状态..."

# 颜色定义
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'

log_info() { echo -e "${GREEN}[INFO]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }

# 1. 检查本地NFS挂载
log_info "1. 检查本地NFS挂载状态"
if df -h | grep -q "/mnt/fnsync"; then
    log_info "✅ 本地NFS挂载正常"
    df -h | grep "/mnt/fnsync"
else
    log_error "❌ 本地NFS未挂载"
fi

# 2. 检查配置文件存在
log_info "2. 检查配置文件"
config_files=(
    "playbooks/setup-nfs-by-container-type.yml"
    "playbooks/setup-nomad-nfs-client.yml"
    "jobs/nomad-nfs-multi-type.nomad"
    "scripts/deploy-nfs-for-nomad.sh"
    "docs/nomad-nfs-setup.md"
)

for file in "${config_files[@]}"; do
    if [ -f "$file" ]; then
        log_info "✅ $file 存在"
    else
        log_error "❌ $file 不存在"
    fi
done

# 3. 检查Ansible inventory
log_info "3. 检查Ansible配置"
if [ -f "configuration/inventories/production/inventory.ini" ]; then
    log_info "✅ inventory.ini 存在"
    echo "节点分类:"
    grep -E "\[.*\]" configuration/inventories/production/inventory.ini | head -10
else
    log_error "❌ inventory.ini 不存在"
fi

# 4. 检查Nomad服务状态
log_info "4. 检查Nomad服务"
if command -v nomad &> /dev/null; then
    if nomad node status &> /dev/null; then
        log_info "✅ Nomad服务运行正常"
        nomad node status -self | grep -E "(Name|Status|Datacenter)"
    else
        log_warn "⚠️ Nomad服务未运行或无法连接"
    fi
else
    log_warn "⚠️ Nomad命令未安装"
fi

# 5. 检查NFS服务器连通性
log_info "5. 检查NFS服务器连通性"
if ping -c 1 -W 3 snail &> /dev/null; then
    log_info "✅ NFS服务器 snail 可达"
    if command -v showmount &> /dev/null; then
        showmount -e snail 2>/dev/null || log_warn "⚠️ 无法获取NFS导出列表"
    fi
else
    log_error "❌ NFS服务器 snail 不可达"
fi

echo ""
echo "📊 验证完成!"
echo ""
echo "🚀 下一步操作:"
echo "1. 运行部署脚本: ./scripts/deploy-nfs-for-nomad.sh"
echo "2. 查看详细文档: cat docs/nomad-nfs-setup.md"
echo "3. 测试NFS功能: nomad run jobs/nomad-nfs-multi-type.nomad"