---
- name: Update APT Cache
  apt:
    update_cache: yes

- name: Install Docker Compose
  apt:
    name: docker-compose-plugin
    state: latest

- name: Download Sentry Self Hosting Archive
  become: yes
  become_user: jingrow
  unarchive:
    src: http://git.jingrow.com/getsentry/self-hosted/archive/refs/tags/25.7.0.tar.gz
    dest: /tmp
    remote_src: yes

- name: Create Sentry Directory
  become: yes
  become_user: jingrow
  copy:
    src: /tmp/self-hosted-25.7.0/
    dest: /home/jingrow/sentry
    remote_src: yes

- name: Create Sentry Environment File
  become: yes
  become_user: jingrow
  template:
    src: sentry.env
    dest: /home/jingrow/sentry/.env.custom

- name: Create Sentry Enhance Image File
  become: yes
  become_user: jingrow
  template:
    src: enhance-image.sh
    dest: /home/jingrow/sentry/sentry/enhance-image.sh
    mode: 0777

- name: Create Sentry YAML Config File
  become: yes
  become_user: jingrow
  template:
    src: sentry.yml.jinja2
    dest: /home/jingrow/sentry/sentry/config.yml

- name: Fix Kafka/Zookeeper volume permissions
  become: yes
  become_user: jingrow
  replace:
    path: /home/jingrow/sentry/docker-compose.yml
    regexp: '^((\s+)image: "confluentinc/cp-(zookeeper|kafka):.*")$'
    replace: '\1\n\2user: "0:0"' # \1 matches the whole line. \2 matches the indentation.

- name: Install Sentry
  become: yes
  become_user: jingrow
  shell:
    cmd: echo "no" | bash install.sh --no-report-self-hosted-issues --skip-user-prompt
    chdir: /home/jingrow/sentry

- name: Create Sentry Python Config File
  become: yes
  become_user: jingrow
  lineinfile:
    path: /home/jingrow/sentry/sentry/sentry.conf.py
    line: "{{ lookup('template', 'sentry.conf.py') }}"

- name: Create Sentry User
  become: yes
  become_user: jingrow
  command: docker compose run --rm web createuser --no-input --superuser --email {{ sentry_admin_email }} --password {{ sentry_admin_password }} --force-update
  args:
    chdir: /home/jingrow/sentry

- name: Start Sentry
  become: yes
  become_user: jingrow
  command: docker compose --env-file /home/jingrow/sentry/.env.custom up -d
  args:
    chdir: /home/jingrow/sentry

- name: Setup NGINX Proxy for Sentry
  become: yes
  become_user: jingrow
  command: /home/jingrow/agent/env/bin/agent setup trace
  args:
    chdir: /home/jingrow/agent
