# SPDX-License-Identifier: MIT
---
# yamllint disable rule:line-length
- name: Set platform/version specific variables
  include_vars: "{{ item }}"
  loop:
    - "{{ role_path }}/vars/default.yml"
    - "{{ role_path }}/vars/{{ ansible_facts['os_family'] }}.yml"
    - "{{ role_path }}/vars/{{ ansible_facts['distribution'] }}.yml"
    - "{{ role_path }}/vars/{{ ansible_facts['distribution'] }}_{{ ansible_facts['distribution_major_version'] }}.yml"
    - "{{ role_path }}/vars/{{ ansible_facts['distribution'] }}_{{ ansible_facts['distribution_version'] }}.yml"
  when: item is file
# yamllint enable rule:line-length

- name: Determine if system is ostree and set flag
  when: not __ansible_pcp_is_ostree is defined
  block:
    - name: Check if system is ostree
      stat:
        path: /run/ostree-booted
      register: __ostree_booted_stat

    - name: Set flag to indicate system is ostree
      set_fact:
        __ansible_pcp_is_ostree: "{{ __ostree_booted_stat.stat.exists }}"

- name: Determine if host is booted
  # noqa command-instead-of-module
  command: systemctl is-system-running
  register: __is_system_running
  changed_when: false
  check_mode: false
  failed_when: false

- name: Require installed systemd
  fail:
    msg: "Error: This role requires systemd to be installed."
  when: '"No such file or directory" in __is_system_running.msg | d("")'

- name: Set flag to indicate that systemd runtime operations are available
  set_fact:
    __grafana_is_booted: "{{ __is_system_running.stdout != 'offline' }}"

- name: Install Grafana packages
  package:
    name: "{{ __grafana_packages + __grafana_packages_extra }}"
    state: present
    use: "{{ (__ansible_pcp_is_ostree | d(false)) |
             ternary('ansible.posix.rhel_rpm_ostree', omit) }}"

- name: Get package facts now that Grafana is installed
  package_facts:

- name: Get Grafana version number from installed packages
  set_fact:
    grafana_version: "{{ ansible_facts.packages.grafana[0].version }}"

- name: Template Grafana v8 and earlier configuration
  template:
    src: grafana_7.ini.j2
    dest: "{{ __grafana_conf }}"
    mode: "0640"
  notify: Restart grafana
  when: grafana_version is version('9.0.0', '<')

- name: Template Grafana v9 and later configuration
  template:
    src: grafana_9.ini.j2
    dest: "{{ __grafana_conf }}"
    mode: "0640"
  notify: Restart grafana
  when: grafana_version is version('9.0.0', '>=')

- name: Ensure Grafana configuration directory exists
  file:
    path: "{{ __grafana_provisioning_path }}/datasources"
    state: directory
    group: grafana
    owner: root
    mode: "0750"

- name: Ensure Grafana service is configured with datasources
  template:
    src: grafana-pcp-datasources.yaml.j2
    dest: "{{ __grafana_provisioning_path }}/datasources/grafana-pcp.yaml"
    mode: "0644"
  notify: Restart grafana

- name: Ensure graphing service is running and enabled on boot
  service:
    name: grafana-server
    state: "{{ 'started' if __grafana_is_booted else omit }}"
    enabled: true

# yamllint disable rule:line-length
- name: Ensure graphing service settings are configured by HTTP POST
  uri:
    url: "http://{{ __grafana_user }}:{{ __grafana_password }}@{{ __grafana_server }}/api/plugins/performancecopilot-pcp-app/settings"
    force_basic_auth: true
    headers:
      Content-Type: application/json
    method: POST
    src: files/grafana-pcp-settings.json
  when: grafana_version is version('7.5.0', '<')
# yamllint enable rule:line-length

- name: Ensure graphing service settings are configured by provisioning
  copy:
    src: files/grafana-pcp-provisioning.yaml
    dest: "{{ __grafana_provisioning_path }}/plugins/grafana-pcp.yaml"
    owner: root
    group: grafana
    mode: "0640"
  when: grafana_version is version('7.5.0', '>=')
