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

  group "lwt" {
    count = 1

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

    # VOLUMES

    volume "lwt-mariadb" {
      type      = "host"
      read_only = false
      source    = "lwt-mariadb"
    }

    volume "lwt" {
      type      = "host"
      read_only = false
      source    = "lwt"
    }

    # NETWORK

    network {
      port "lwt" {
        to = 80
      }
      port "mariadb" {
        static = 33306
        to = 3306
      }
    }

    # TASKS

    task "mariadb" {
      driver = "docker"

      user = 1001

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

      env {
        MYSQL_ALLOW_EMPTY_PASSWORD = "no"
        MYSQL_ROOT_PASSWORD = "root"
      }

      config {
        image = "mariadb:10.7"
        ports = ["mariadb"]
      }

      resources {
        cpu    = 100
        memory = 128
      }
    }

    task "lwt" {
      driver = "docker"

      config {
        image = "ghcr.io/hugofara/lwt:master"
        ports = ["lwt"]
      }

      volume_mount {
        volume      = "lwt"
        destination = "/var/www/html/media"
        read_only   = false
      }

      env {
        DB_HOST      = "${attr.unique.network.ip-address}:33306" # "100.91.225.117"
      }

      resources {
        cpu    = 100
        memory = 128
      }

      service {
        name = "lwt"
        port = "lwt"

        check {
          type     = "tcp"
          port     = "lwt"
          interval = "30s"
          timeout  = "10s"
        }
      }
    }

  }
}
