---

- name: Install dependencies
  yum: name={{ metabase_packages }}
  tags: metabase

- name: Stop the service during upgrades
  service: name=metabase state=stopped
  when: metabase_install_mode == 'upgrade'
  tags: metabase

- when: metabase_install_mode != 'none' and metabase_rows_limit is not defined
  block:
    - name: Download metabase JAR file
      get_url:
        url: "{{ metabase_jar_url }}"
        dest: "{{ metabase_root_dir }}/tmp/"
        checksum: sha256:{{ metabase_jar_sha256 }}
      retries: 5 # Download from github can fails sometimes
      delay: 10

    - name: Move the JAR to the app dir
      copy: src={{ metabase_root_dir }}/tmp/metabase.jar dest={{ metabase_root_dir }}/app/ mode=644 remote_src=True
      notify: restart metabase
      
  tags: metabase

# When rows limit is supplied, download the source tarball and rebuild metabase from source
- when: metabase_install_mode != 'none' and metabase_rows_limit is defined
  block:

    - name: Install build dependencies
      package: name={{ metabase_build_packages }}

    - name: Download metabase archive
      get_url:
        url: "{{ metabase_archive_url }}"
        dest: "{{ metabase_root_dir }}/tmp/"
        checksum: sha256:{{ metabase_archive_sha256 }}

    - name: Install yarn
      npm:
        name: yarn
        global: True

    - name: Extract metabase archive
      unarchive:
        src: "{{ metabase_root_dir }}/tmp/metabase-{{ metabase_version }}.tar.gz"
        dest: "{{ metabase_root_dir }}/tmp/"
        remote_src: True

    - name: Render rows limit patch from template
      template: src=metabase_rows_limit.diff.j2 dest={{ metabase_root_dir }}/tmp/metabase_rows_limit.diff

    - name: Patch metabase to customize rows limit
      patch:
        src: "{{ metabase_root_dir }}/tmp/metabase_rows_limit.diff"
        dest: "{{ metabase_root_dir }}/tmp/metabase-{{ metabase_version }}/src/metabase/query_processor/middleware/constraints.clj"
        remote_src: True

    - name: Ensure metabase user can write in the source tree
      file: path={{ metabase_root_dir }}/tmp/metabase-{{ metabase_version }}/ owner={{ metabase_user }} recurse=True

    - name: Build metabase uberjar
      command: ./bin/build
      args:
        chdir: "{{ metabase_root_dir }}/tmp/metabase-{{ metabase_version }}/"
      become_user: "{{ metabase_user }}"

    - name: Move the freshly built JAR to the app dir
      copy: src={{ metabase_root_dir }}/tmp/metabase-{{ metabase_version }}/target/uberjar/metabase.jar dest={{ metabase_root_dir }}/app/ mode=644 remote_src=True
      notify: restart metabase

  tags: metabase


- name: Deploy systemd unit
  template: src=metabase.service.j2 dest=/etc/systemd/system/metabase.service
  register: metabase_unit
  notify: restart metabase
  tags: metabase

- name: Reload systemd
  systemd: daemon_reload=True
  when: metabase_unit.changed
  tags: metabase

# Create the database
- import_tasks: ../includes/webapps_create_mysql_db.yml
  vars:
    - db_name: "{{ metabase_db_name }}"
    - db_user: "{{ metabase_db_user }}"
    - db_server: "{{ metabase_db_server }}"
    - db_pass: "{{ metabase_db_pass }}"
  when: metabase_db_engine == 'mysql'
  tags: metabase

- when: metabase_db_engine == 'postgres'
  block:
    - name: Install postgresql client
      package: name=postgresql16

    - name: Create postgres user
      postgresql_user:
        db: postgres
        name: "{{ metabase_db_user }}"
        password: "{{ metabase_db_pass }}"
        login_host: "{{ metabase_db_server }}"
        login_user: sqladmin
        login_password: "{{ pg_admin_pass }}"

    - name: Create the PostgreSQL database
      postgresql_db:
        name: "{{ metabase_db_name }}"
        encoding: UTF-8
        lc_collate: C
        lc_ctype: C
        template: template0
        owner: "{{ metabase_db_user }}"
        login_host: "{{ metabase_db_server }}"
        login_user: sqladmin
        login_password: "{{ pg_admin_pass }}"

  tags: metabase

- name: Install pre and post backup hooks
  template: src={{ item }}-backup.j2 dest=/etc/backup/{{ item }}.d/metabase mode=700
  loop:
    - pre
    - post
  tags: metabase
