---

- name: Configure firewall
  community.general.ufw:
    rule: allow
    port: "{{ item }}"
    proto: tcp
  loop:
    - 80
    - 443
    - 9000

- name: Allow UDP port 6969
  community.general.ufw:
    rule: allow
    port: "6969"
    proto: udp

- name: Install Caddy
  ansible.builtin.import_role:
    name: nvjacobo.caddy


- name: Configure Caddyfile
  ansible.builtin.template:
    src: 'templates/Caddyfile.j2'
    dest: /etc/caddy/Caddyfile
    mode: "0644"
  notify: restart caddy


# @todo performance enhancement is to run aquatic outside of docker.
# @see https://github.com/greatest-ape/aquatic/blob/34b45e923f84421181fc43cf5e20709e69ce0dfd/docker/aquatic_udp.Dockerfile#L5
- name: Setup docker container
  community.docker.docker_container:
    name: tracker
    image: gitea.futureporn.net/futureporn/tracker:latest
    pull: always
    state: started
    ports:
      - "6969:6969/udp" # aquatic_udp
      - "5063:5063" # tracker-helper
      - "9000:9000" # aquatic metrics
    env:
      TRACKER_HELPER_ACCESSLIST_URL: https://tracker.futureporn.net/accesslist
      TRACKER_HELPER_USERNAME: "{{ lookup('dotenv', 'TRACKER_HELPER_USERNAME', file='../.env') }}"
      TRACKER_HELPER_PASSWORD: "{{ lookup('dotenv', 'TRACKER_HELPER_PASSWORD', file='../.env') }}"
      TRACKER_URL: https://tracker.futureporn.net:6969
      TRACKER_HELPER_ACCESSLIST_PATH: /var/lib/aquatic/accesslist