job "moimoin" {
  region      = "global"
  datacenters = ["samfelag"]
  type        = "service"

  group "moimoin" {
    count = 1

    restart {
      attempts = 10
      delay    = "30s"
    }

    # VOLUMES

    volume "mysql" {
      type      = "host"
      read_only = false
      source    = "moimoin"
    }

    # NETWORK

    network {
      port "moimoin-front" {
        to = 5000
      }
      port "moimoin-back" {
        to = 3000
      }
      port "mysql" {
        static = 33306
        to = 3306
      }
    }

    # TASKS

    task "mysql" {
      driver = "docker"

      user = 1001

      volume_mount {
        volume      = "mysql"
        destination = "/var/lib/mysql"
        read_only   = false
      }

      env {
        MYSQL_DATABASE = "xat-osr"
        MYSQL_ROOT_PASSWORD = "estrell4galicia"
      }

      config {
        image = "arm64v8/mysql:latest"
        ports = ["mysql"]
      }

      resources {
        cpu    = 500
        memory = 512
      }
    }

    task "moimoin-back" {
      driver = "docker"

      config {
        image = "marc.sastre.cat/moimoin-back:latest"
        ports = ["moimoin-back"]
      }

      env {
        MYSQL_HOST      = "${attr.unique.network.ip-address}" # "100.91.225.117"
        MYSQL_PORT      = "33306"
        MYSQL_USER      = "root"
        MYSQL_PASSWORD  = "estrell4galicia"
        MYSQL_NAME      = "xat-osr"
        CHAT_ADMIN_PSWD = "cervesaEspecial"
        CLIENT_HOST     = "marc.sastre.cat/moimoin"
      }

      resources {
        cpu    = 100
        memory = 128
      }

      service {
        name = "moimoin-back"
        port = "moimoin-back"

        check {
          type     = "tcp"
          port     = "moimoin-back"
          interval = "30s"
          timeout  = "10s"
        }
      }
    }

    task "moimoin-front" {
      driver = "docker"

      config {
        image = "marc.sastre.cat/moimoin-front:latest"
        ports = ["moimoin-front"]
      }

      env {
        CLIENT_PORT = "5000"
      }

      resources {
        cpu    = 100
        memory = 128
      }

      service {
        name = "moimoin-front"
        port = "moimoin-front"

        check {
          type     = "tcp"
          port     = "moimoin-front"
          interval = "30s"
          timeout  = "10s"
        }
      }
    }

  }
}
