#!/bin/bash

# ☢️ 核弹级 Nomad 重置执行脚本 ☢️

set -e

echo "☢️☢️☢️ 核弹级 Nomad 集群重置 ☢️☢️☢️"
echo ""
echo "这个脚本将："
echo "1. 完全摧毁所有 Nomad 进程和数据"
echo "2. 重新下载并安装 Nomad 二进制文件"
echo "3. 创建全新的配置文件"
echo "4. 重新启动整个集群"
echo ""
echo "⚠️  警告：这是不可逆的操作！⚠️"
echo ""

# 检查是否在正确的目录
if [[ ! -f "scripts/utilities/NUCLEAR-NOMAD-RESET.yml" ]]; then
    echo "❌ 错误：请在 /root/mgmt 目录下运行此脚本"
    exit 1
fi

# 确认操作
read -p "你确定要进行核弹级重置吗？输入 'NUCLEAR' 确认: " confirm
if [[ "$confirm" != "NUCLEAR" ]]; then
    echo "❌ 操作已取消"
    exit 1
fi

echo ""
echo "🚀 开始核弹级重置..."
echo ""

# 设置 Ansible 配置
export ANSIBLE_HOST_KEY_CHECKING=False
export ANSIBLE_STDOUT_CALLBACK=yaml

# 执行核弹级重置
echo "📡 执行 Ansible playbook..."
cd /root/mgmt/configuration

ansible-playbook \
    -i inventories/production/nomad-cluster.ini \
    ../scripts/utilities/NUCLEAR-NOMAD-RESET.yml \
    --extra-vars "ansible_ssh_common_args='-o StrictHostKeyChecking=no'" \
    -v

echo ""
echo "⏰ 等待集群稳定..."
sleep 30

echo ""
echo "🔍 检查集群状态..."

# 检查集群成员
echo "📊 集群成员状态："
nomad server members || echo "❌ 无法获取集群成员状态"

echo ""
echo "📊 节点状态："
nomad node status || echo "❌ 无法获取节点状态"

echo ""
echo "🎯 如果上面显示错误，请等待几分钟后再次检查"
echo "集群可能需要一些时间来完全启动和同步"

echo ""
echo "🔧 有用的命令："
echo "  检查集群成员: nomad server members"
echo "  检查节点状态: nomad node status"
echo "  查看日志: journalctl -u nomad -f"
echo "  Web UI: http://100.116.158.95:4646"

echo ""
echo "☢️ 核弹级重置完成！☢️"