---
- name: Create container volumes
  containers.podman.podman_volume:
    state: present
    name: "{{ item }}"
  loop:
    - "{{ spoolman_dir }}"
    - "{{ prometheus_dir }}"
    - "{{ prometheus_config_dir }}"
    - "{{ grafana_dir }}"
    - "{{ database_dir }}"

- name: Create Spoolman container
  containers.podman.podman_container:
    name: spoolman
    state: quadlet
    network: "{{ podman_network }}"
    image: ghcr.io/donkie/spoolman:latest
    restart_policy: "on-failure"
    volumes: "{{ spoolman_dir }}:/home/app/.local/share/spoolman"
    ports: "80:8000"
    security_opt: "label=disable"
    env:
      TZ: US/Central
      SPOOLMAN_DB_TYPE: postgres
      SPOOLMAN_DB_HOST: "{{ app }}-pgdb"
      SPOOLMAN_DB_PORT: "5432"
      SPOOLMAN_DB_NAME: "{{ app }}"
      SPOOLMAN_DB_USERNAME: "{{ app }}"
      SPOOLMAN_DB_PASSWORD: "mcGdDizPrFFv9SS4c55m"
      SPOOLMAN_METRICS_ENABLED: true

- name: Create prometheus container
  containers.podman.podman_container:
    name: prometheus
    state: quadlet
    network: "{{ podman_network }}"
    image: docker.io/prom/prometheus:latest
    volumes:
      - "{{ prometheus_config }}:/etc/prometheus/prometheus.yml"
      - "{{ prometheus_dir }}:/prometheus"
    ports: "9091:9090"
    security_opt: "label=disable"
    quadlet_options:
      - "AutoUpdate=registry"
      - |
        [Install]
        WantedBy=default.target

- name: Create Prometheus config file
  ansible.builtin.template:
    mode: "0666"
    src: prometheus.j2
    dest: "{{ prometheus_config }}"

- name: Create Grafana container
  containers.podman.podman_container:
    name: grafana
    state: quadlet
    network: "{{ podman_network }}"
    image: grafana/grafana:latest
    security_opt: "label=disable"
    env:
      GF_SECURITY_ADMIN_USER: admin
      GF_SECURITY_ADMIN_PASSWORD: admin
    ports:
      - "3000:3000"
    volume:
      - "{{ grafana_dir }}:/var/lib/grafana"

- name: Start systemd units
  ansible.builtin.systemd:
    name: '{{ item }}'
    daemon_reload: true
    state: started
    enabled: true
  loop:
    - spoolman
    - prometheus
    - grafana
