job "consul-cluster" {
  datacenters = ["dc1"]
  type = "service"
  
  constraint {
    attribute = "${node.unique.name}"
    operator = "regexp"
    value = "^(master|ash3c|hcs)$"
  }

  group "consul" {
    count = 3
    
    network {
      port "http" {
        static = 8500
      }
      port "serf_lan" {
        static = 8301
      }
      port "serf_wan" {
        static = 8302
      }
      port "server" {
        static = 8300
      }
      port "dns" {
        static = 8600
      }
    }

    service {
      name = "consul"
      port = "http"
      
      check {
        type = "http"
        path = "/v1/status/leader"
        interval = "10s"
        timeout = "2s"
      }
    }

    task "consul" {
      driver = "podman"
      
      config {
        image = "consul:1.15.4"
        network_mode = "host"
        
        args = [
          "agent",
          "-server",
          "-bootstrap-expect=3",
          "-ui",
          "-data-dir=/consul/data",
          "-config-dir=/consul/config",
          "-bind={{ env \"attr.unique.network.ip-address\" }}",
          "-client=0.0.0.0",
          "-retry-join=100.117.106.136",
          "-retry-join=100.116.80.94",
          "-retry-join=100.84.197.26"
        ]
        
        volumes = [
          "consul-data:/consul/data",
          "consul-config:/consul/config"
        ]
      }
      
      resources {
        cpu = 500
        memory = 512
      }
      
      env {
        CONSUL_BIND_INTERFACE = "tailscale0"
      }
    }
  }
}