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

  group "nextcloud" {
    count = 1

    restart {
      attempts = 5
      delay    = "1m"
    }

    # Network

    network {
      port "nextcloud" {
        static = 8080
        to     = 80
      }
      port "mariadb" {
        static = 3306
        to     = 3306
      }
      # port "collabora" {
      #   to     = 9980
      # }
    }

    # Volumes

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

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

    # volume "collabora" {
    #   type      = "host"
    #   read_only = false
    #   source    = "collabora"
    # }

    ###
    # NEXTCLOUD
    ###

    task "nextcloud" {
      driver = "docker"

      user = 1001

      env {
        MYSQL_PASSWORD            = "hxKOD13MUh"
        MYSQL_DATABASE            = "nextcloud"
        MYSQL_USER                = "nextcloud"
        MYSQL_HOST                = "${NOMAD_ADDR_mariadb}"
        NEXTCLOUD_TRUSTED_DOMAINS = "nextcloud.samfelag.xyz"
        OVERWRITEPROTOCOL         = "https"
      }

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

      config {
        image = "nextcloud:latest"
        ports = ["nextcloud"]
      }

      resources {
        cpu    = 2000
        memory = 512
      }

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

    ###
    # MARIADB
    ###

    task "mariadb" {
      driver = "docker"

      user = 1001

      env {
        MYSQL_ROOT_PASSWORD = "hxKOD13MUh"
        MYSQL_ROOT_HOST     = "${NOMAD_IP_mariadb}"
        MYSQL_PASSWORD      = "hxKOD13MUh"
        MYSQL_DATABASE      = "nextcloud"
        MYSQL_USER          = "nextcloud"
      }

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

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

      resources {
        cpu    = 1000
        memory = 256
      }

      service {
        name = "mariadb"
        tags = ["mariadb"]

        port = "mariadb"

        check {
          type     = "tcp"
          port     = "mariadb"
          interval = "30s"
          timeout  = "2s"
        }
      }
    }

    ###
    # COLLABORA
    ###

    # task "collabora" {
    #   driver = "docker"

    #   env {
    #       aliasgroup1 	= "https://nextcloud.lajuntament.space:443"
    #       username  	= "lajuntament"
    #       password  	= "eLn1lIm4rc"
    #   }

    #   volume_mount {
    #     volume      = "collabora"
    #     destination = "/etc/loolwsd"
    #     read_only   = false
    #   }

    #   config {
    #     image = "collabora/code:latest"
    #     ports = ["collabora"]
    #   }

    #   resources {
    #     cpu    = 2000
    #     memory = 1024
    #   }

    #   service {
    #     name = "collabora"
    #     port = "collabora"

    #     check {
    #       type     = "tcp"
    #       port     = "collabora"
    #       interval = "30s"
    #       timeout  = "2s"
    #     }
    #   }

    # }
  }
}
