# vim: ft=yaml.ansible
---
- name: Create Matrix volume folders
  file:
    name: "{{ services.matrix.volume_folder }}/{{ volume }}"
    state: directory
    owner: "991"
    group: "991"
  loop:
    - "data"
    - "data/uploads"
    - "data/media"
  loop_control:
    loop_var: volume

- name: Create Matrix DB folder
  file:
    name: "{{ services.matrix.volume_folder }}/db"
    state: "directory"

- name: Create Element volume folders
  file:
    name: "{{ services.element.volume_folder }}/{{ volume }}"
    state: directory
  loop:
    - "data"
  loop_control:
    loop_var: volume

- name: Upload Element config.json
  template:
    src: files/configs/element/config.json
    dest: "{{ services.element.volume_folder }}/data/config.json"

- name: Upload Element riot.im.conf
  template:
    src: files/configs/element/riot.im.conf
    dest: "{{ services.element.volume_folder }}/data/riot.im.conf"

- name: upload vhost config for matrix domain
  template:
    src: files/configs/matrix/vhost-matrix
    dest: "{{ services.nginx_proxy.volume_folder }}/vhost/{{ services.matrix.domain }}"

- name: Upload vhost config for Element domain
  template:
    src: files/configs/matrix/vhost-element
    dest: "{{ services.nginx_proxy.volume_folder }}/vhost/{{ item }}"
  loop: "{{ services.element.domains }}"

- name: Upload homeserver.yaml
  template:
    src: "files/configs/matrix/homeserver.yaml.j2"
    dest: "{{ services.matrix.volume_folder }}/data/homeserver.yaml"

- name: upload matrix logging config
  template:
    src: "files/configs/matrix/matrix.data.coop.log.config"
    dest: "{{ services.matrix.volume_folder }}/data/matrix.data.coop.log.config"

- name: Set up Matrix and Element
  docker_compose:
    project_name: matrix
    pull: true
    definition:
      version: "3.6"
      services:
        postgres:
          image: "postgres:{{ services.matrix.postgres_version }}"
          restart: unless-stopped
          networks:
            - matrix
          volumes:
            - "{{ services.matrix.volume_folder }}/db:/var/lib/postgresql/data"
          environment:
            POSTGRES_USER: "synapse"
            POSTGRES_PASSWORD: "{{ postgres_passwords.matrix }}"

        synapse:
          image: "matrixdotorg/synapse:{{ services.matrix.version }}"
          restart: unless-stopped
          networks:
            - matrix
            - external_services
          volumes:
            - "{{ services.matrix.volume_folder }}/data:/data"
          environment:
            SYNAPSE_CONFIG_PATH: "/data/homeserver.yaml"
            SYNAPSE_CACHE_FACTOR: "2"
            SYNAPSE_LOG_LEVEL: "INFO"
            VIRTUAL_HOST: "{{ services.matrix.domain }}"
            VIRTUAL_PORT: "8008"
            LETSENCRYPT_HOST: "{{ services.matrix.domain }}"
            LETSENCRYPT_EMAIL: "{{ letsencrypt_email }}"

        element:
          image: "avhost/docker-matrix-element:{{ services.element.version }}"
          restart: unless-stopped
          networks:
            - matrix
            - external_services
          expose:
            - 8080
          volumes:
            - "{{ services.element.volume_folder }}/data:/data"
          environment:
            VIRTUAL_HOST: "{{ services.element.domains | join(',') }}"
            VIRTUAL_PORT: "8080"
            LETSENCRYPT_HOST: "{{ services.element.domains | join(',') }}"
            LETSENCRYPT_EMAIL: "{{ letsencrypt_email }}"

      networks:
        external_services:
          external:
            name: external_services
        matrix:
          name: "matrix"