version : "3.7"

#Everything in this dockerfile is duplicated: there is a "standard" instance and a "week" instance.
#Each one of them has its own 3 volumes, 2 networks and 3 containers.
#The settings.json and settings_week.json in the directory of this docker-compose are bound (mounted) to the app containers.
#It also requires the existence of "secret" files containing passwords.
#This docker-compose requires the existence of an external network, "proxy". Traefik labels have been added, so "proxy" can be a traefik reverse-proxy.

volumes:
  #DATABASES
  standard_db:
    name: etherpad-db-pg
  week_db:
    name: weekpad-db
  #BACKUP OF DELETED PADS
  standard_deleted-pads:
    name: deleted-pads-standard
  week_deleted-pads:
    name: deleted-pads-week
  #FOR SHARING THE API KEY between app and dpad (written to a text file by app)
  standard_api-key:
    name: etherpad_standard_api-key
  week_api-key:
    name: etherpad_week_api-key


networks:
  #app-db: used by the app to query its postgresql database
  standard_app-db:
    name: etherpad_standard_app-queries-database
  week_app-db:
    name: etherpad_week_app-queries-database
  #dpad-app: used by the dpad script to query the app via its web API
  standard_dpad-app: 
    name: etherpad_standard_dpad-queries-app
  week_dpad-app:
    name:   etherpad_week-dpad-queries-app
  #this network has access to the apps, exposing their web frontends
  proxy:
    external: true

services:

  standard_db:
    image: postgres:12
    container_name: etherpad_standard_db
    volumes:
      - standard_db:/var/lib/postgresql/data
    env_file: ./secrets/etherpad-db.secrets
    networks:
      - standard_app-db
    restart: unless-stopped

  week_db:
    image: postgres:12
    container_name: etherpad_week_db
    volumes:
      - week_db:/var/lib/postgresql/data
    env_file: ./secrets/etherpad-week-db.secrets
    networks:
      - week_app-db


  standard_app:
    image: registry.picasoft.net/pica-etherpad:1.8.6
    build: .
    container_name: etherpad_standard_app
    env_file: ./secrets/etherpad-app.secrets
    volumes:
      - ./settings.json:/opt/etherpad-lite/settings.json
      - standard_deleted-pads:/opt/etherpad-lite/deleted_pads
      - standard_api-key:/mountedAPIkey
    command: ["--apikey", "/mountedAPIkey/APIKEY.txt"]
    labels:
      traefik.http.routers.etherpad-app.entrypoints: websecure
      traefik.http.routers.etherpad-app.rule: Host(`pad.picasoft.net`)
      traefik.http.services.etherpad-app.loadbalancer.server.port: 8080
      traefik.enable: true
    environment:
      DB_HOST: "standard_db"
      LOGLEVEL: "INFO"
      MINIFY: "true"
      PORT: 8080
      THEME: "colibris"
      TITLE: "Picapad"
      TRUST_PROXY: "true"
    networks:
      - proxy
      - standard_app-db
      - standard_dpad-app
    depends_on:
      - standard_db
    restart: unless-stopped

  week_app:
    image: registry.picasoft.net/pica-etherpad:1.8.6
    container_name: etherpad_week_app
    build: .
    env_file: ./secrets/etherpad-week-app.secrets
    volumes:
      - ./settings_week.json:/opt/etherpad-lite/settings.json
      - week_deleted-pads:/opt/etherpad-lite/deleted_pads
      - week_api-key:/mountedAPIkey
    command: ["--apikey", "/mountedAPIkey/APIKEY.txt"]
    labels:
      traefik.http.routers.etherpad-week-app.entrypoints: websecure
      traefik.http.routers.etherpad-week-app.rule: Host(`week.pad.picasoft.net`)
      traefik.http.services.etherpad-week-app.loadbalancer.server.port: 8080
      traefik.enable: true
    environment:
      DB_HOST: "week_db"
      LOGLEVEL: "INFO"
      MINIFY: "true"
      PORT: 8080
      THEME: "colibris"
      TITLE: "Picapad Hebdo"
      TRUST_PROXY: "true"
    depends_on:
      - week_db
    networks:
      - proxy
      - week_app-db
      - week_dpad-app
    restart: unless-stopped





  # script COMPLEMENTARY to etherpad plugin ep_delete_after_delay
  standard_delete-pad-after-delay:
    build:
      context: https://gitlab.utc.fr/picasoft/projets/delete-pad-after-delay.git
    volumes:
      - type: volume
        source: standard_api-key
        target: /mountedAPIkey
        read_only: true
        volume:
          nocopy: true
      - standard_deleted-pads:/opt/etherpad-lite/deleted_pads
    networks:
      - standard_dpad-app
    environment:
      URL: "http://standard_app:8080" #must be coherent with etherpad-wee-app.environment.port
      DEL: 71712000 #in seconds (2 years). MUST be COHERENT with settings.json ep_delete_after_delay{
      DIR: "/opt/etherpad-lite/deleted_pads"
      APIKEY_PATH: "/mountedAPIkey/APIKEY.txt"
    container_name: etherpad_standard_delete-pad-after-delay
    depends_on:
     - standard_app


  # script COMPLEMENTARY to etherpad plugin ep_delete_after_delay
  week_delete-pad-after-delay:
    build:
      context: https://gitlab.utc.fr/picasoft/projets/delete-pad-after-delay.git
    volumes:
      - type: volume
        source: week_api-key
        target: /mountedAPIkey
        read_only: true
        volume:
          nocopy: true
      - week_deleted-pads:/opt/etherpad-lite/deleted_pads
    networks:
      - week_dpad-app
    environment:
      URL: "http://week_app:8080" #must be coherent with etherpad-wee-app.environment.port
      DEL: 1209600 #14 days. MUST be COHERENT with settings_week.json ep_delete_after_delay{
      DIR: "/opt/etherpad-lite/deleted_pads"
      APIKEY_PATH: "/mountedAPIkey/APIKEY.txt"
    container_name: etherpad_week_delete-pad-after-delay
    depends_on:
     - week_app
