job "mysql" {
  datacenters = ["alo"]

  meta {
    uuid = uuidv4()
  }

  group "db" {
    # Run on primary storage node (zippy) for local disk performance
    constraint {
      attribute = "${meta.storage_role}"
      value     = "primary"
    }

    network {
      port "db" {
        static = 3306
      }
      port "exporter" {
      }
    }

    task "mysqld" {
      driver = "docker"

      config {
        image = "mysql:9.4"
        ports = ["db"]
        volumes = [
          "/data/services/mysql:/var/lib/mysql",
        ]
      }

      env {
        MYSQL_ROOT_PASSWORD = "{{ with nomadVar \"secrets/mysql\" }}{{ .root_password }}{{ end }}"
      }

      service {
        name = "mysql"
        port = "db"
        check {
          type = "tcp"
          port = "db"
          interval = "30s"
          timeout = "2s"
        }
      }

      resources {
        cpu = 3000
        memory = 3500
      }
    }

    task "exporter" {
      driver = "docker"

      config {
        image = "prom/mysqld-exporter"
        ports = ["exporter"]
        args = [
          "--mysqld.address=mysql.service.consul:3306",
          "--mysqld.username=exporter:Veeng0Yei5haesheagai",
          "--web.listen-address=0.0.0.0:${NOMAD_PORT_exporter}",
        ]
      }

      service {
        name = "mysql-exporter"
        port = "exporter"
        tags = ["metrics"]
      }
    }
  }
}
