# Nomad 多数据中心集群模块
# 支持跨地域部署：CN(dc1) + KR(dc2) + US(dc3)

terraform {
  required_providers {
    oci = {
      source  = "oracle/oci"
      version = "~> 7.20"
    }
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

# 本地变量
locals {
  nomad_version = "1.10.5"
  
  # 通用 Nomad 配置
  nomad_encrypt_key = "NVOMDvXblgWfhtzFzOUIHnKEOrbXOkPrkIPbRGGf1YQ="
  
  # 数据中心配置
  datacenters = {
    dc1 = {
      name     = "dc1"
      region   = "cn"
      location = "China"
      provider = "existing" # 现有的 semaphore 节点
    }
    dc2 = {
      name     = "dc2" 
      region   = "kr"
      location = "Korea"
      provider = "oracle"
    }
    dc3 = {
      name     = "dc3"
      region   = "us"
      location = "US"
      provider = "aws" # 暂时使用AWS替代华为云
    }
  }
  
  # 用户数据模板
  user_data_template = templatefile("${path.module}/templates/nomad-userdata.sh", {
    nomad_version     = local.nomad_version
    nomad_encrypt_key = local.nomad_encrypt_key
    VERSION_ID        = "20.04"  # Ubuntu 20.04
    NOMAD_VERSION     = local.nomad_version
    NOMAD_ZIP         = "nomad_${local.nomad_version}_linux_amd64.zip"
    NOMAD_URL         = "https://releases.hashicorp.com/nomad/${local.nomad_version}/nomad_${local.nomad_version}_linux_amd64.zip"
    NOMAD_SHA256_URL  = "https://releases.hashicorp.com/nomad/${local.nomad_version}/nomad_${local.nomad_version}_SHA256SUMS"
    bind_addr         = "auto"
    nomad_servers     = "\"127.0.0.1\""
  })
}

# 数据源：获取现有的 semaphore 节点信息
data "external" "semaphore_info" {
  program = ["bash", "-c", <<-EOF
    echo '{
      "ip": "100.116.158.95",
      "datacenter": "dc1",
      "status": "existing"
    }'
  EOF
  ]
}

# Oracle Cloud 韩国节点 (dc2)
resource "oci_core_instance" "nomad_kr_node" {
  count = var.deploy_korea_node ? 1 : 0
  
  # 基础配置
  compartment_id      = var.oracle_config.compartment_ocid
  display_name        = "nomad-master-kr"
  availability_domain = var.oracle_availability_domain
  shape               = "VM.Standard.E2.1.Micro" # 免费层
  
  # 源配置
  source_details {
    source_type = "image"
    source_id   = var.oracle_ubuntu_image_id
  }
  
  # 网络配置
  create_vnic_details {
    subnet_id        = var.oracle_subnet_id
    display_name     = "nomad-kr-vnic"
    assign_public_ip = true
  }
  
  # 元数据
  metadata = {
    ssh_authorized_keys = var.ssh_public_key
    user_data           = base64encode(templatefile("${path.module}/templates/nomad-userdata.sh", {
      datacenter        = "dc2"
      nomad_version     = local.nomad_version
      nomad_encrypt_key = local.nomad_encrypt_key
      bootstrap_expect  = 1
      bind_addr        = "auto"
      server_enabled   = true
      client_enabled   = true
      VERSION_ID       = "20.04"  # Ubuntu 20.04
      NOMAD_VERSION    = local.nomad_version
      NOMAD_ZIP        = "nomad_${local.nomad_version}_linux_amd64.zip"
      NOMAD_URL        = "https://releases.hashicorp.com/nomad/${local.nomad_version}/nomad_${local.nomad_version}_linux_amd64.zip"
      NOMAD_SHA256_URL = "https://releases.hashicorp.com/nomad/${local.nomad_version}/nomad_${local.nomad_version}_SHA256SUMS"
      nomad_servers    = "\"127.0.0.1\""
    }))
  }
  
  # 标签
  defined_tags = merge(var.common_tags, {
    "Name"       = "nomad-master-kr"
    "Datacenter" = "dc2"
    "Role"       = "nomad-server"
    "Provider"   = "oracle"
  })
}

# 华为云美国节点 (dc3) - 暂时禁用
# resource "huaweicloud_compute_instance_v2" "nomad_us_node" {
#   count = var.deploy_us_node ? 1 : 0
#   
#   name              = "nomad-ash3c-us"
#   image_id          = var.huawei_ubuntu_image_id
#   flavor_id         = "s6.small.1" # 1vCPU 1GB
#   
#   # 网络配置
#   network {
#     uuid = var.huawei_subnet_id
#   }
#   
#   # 元数据
#   metadata = {
#     ssh_authorized_keys = var.ssh_public_key
#     user_data           = base64encode(templatefile("${path.module}/templates/nomad-userdata.sh", {
#       datacenter        = "dc3"
#       nomad_version     = local.nomad_version
#       nomad_encrypt_key = local.nomad_encrypt_key
#       bootstrap_expect  = 1
#       bind_addr        = "auto"
#       server_enabled   = true
#       client_enabled   = true
#     }))
#   }
#   
#   # 标签
#   tags = merge(var.common_tags, {
#     Name       = "nomad-ash3c-us"
#     Datacenter = "dc3"
#     Role       = "nomad-server"
#     Provider   = "huawei"
#   })
# }