# SPDX-License-Identifier: MIT
---

- name: Ensure extra performance rule group directories exist
  file:
    path: "{{ __pcp_pmieconf_path }}/{{ item }}"
    state: directory
    owner: root
    group: root
    mode: "0755"
  loop: "{{ __pcp_pmieconf_groups | default([]) }}"
  register: __pcp_register_changed_group_dir

- name: Ensure extra performance rule group link directories exist
  file:
    path: "{{ __pcp_pmieconf_link_path }}/{{ item }}"
    state: directory
    owner: root
    group: root
    mode: "0755"
  loop: "{{ __pcp_pmieconf_groups | default([]) }}"
  register: __pcp_register_changed_group_link_dir

- name: Ensure extra performance rules are installed for targeted hosts
  copy:
    src: "files/{{ item }}"
    dest: "{{ __pcp_pmieconf_path }}/{{ item }}"
    owner: root
    group: root
    mode: "0644"
  loop: "{{ __pcp_pmieconf_rules | default([]) }}"
  register: __pcp_register_changed_rules_for_hosts

- name: Ensure performance rule actions are installed for targeted hosts
  set_fact:
    local_pmie: "default"

- name: Check if global pmie webhook action is configured
  lineinfile:
    state: absent
    path: "{{ __pcp_pmie_config_path }}/config.{{ item }}"
    regexp: "//.*global webhook_action = yes"
  check_mode: true
  changed_when: false
  register: __pcp_global_webhook_action_status
  loop: "{{ pcp_target_hosts + [local_pmie] }}"
  when:
    - pcp_pmie_endpoint | length > 0

- name: Configure global webhook action
  # yamllint disable rule:line-length
  command: "pmieconf -f {{ __pcp_pmie_config_path }}/config.{{ item.item }} modify global webhook_action yes"
  loop: "{{ __pcp_global_webhook_action_status.results }}"
  changed_when:
    - pcp_pmie_endpoint | length > 0
    - item.found | d(-1) <= 0
  when:
    - pcp_pmie_endpoint | length > 0
    - item.found | d(-1) <= 0
  register: __pcp_register_changed_actions_for_hosts
  # yamllint enable rule:line-length

- name: Check if global webhook endpoint is configured
  lineinfile:
    state: absent
    path: "{{ __pcp_pmie_config_path }}/config.{{ item }}"
    regexp: "//.*global webhook_endpoint = \"{{ pcp_pmie_endpoint }}\""
  check_mode: true
  changed_when: false
  register: __pcp_global_webhook_endpoint_status
  loop: "{{ pcp_target_hosts + [local_pmie] }}"

- name: Configure global webhook endpoint
  # yamllint disable rule:line-length
  command: "pmieconf -f {{ __pcp_pmie_config_path }}/config.{{ item.item }} modify global webhook_endpoint {{ pcp_pmie_endpoint }}"
  loop: "{{ __pcp_global_webhook_endpoint_status.results }}"
  changed_when:
    - pcp_pmie_endpoint | length > 0
    - item.found | d(-1) <= 0
  when:
    - pcp_pmie_endpoint | length > 0
    - item.found | d(-1) <= 0
  register: __pcp_register_changed_actions_for_hosts
  # yamllint enable rule:line-length

- name: Ensure extra rules symlinks have been created for targeted hosts
  file:
    src: "{{ __pcp_pmieconf_path }}/{{ item }}"
    dest: "{{ __pcp_pmieconf_link_path }}/{{ item }}"
    state: link
    force: true
  loop: "{{ __pcp_pmieconf_rules | default([]) }}"
  register: __pcp_register_changed_symlinks_for_hosts

- name: Enable performance metric inference for targeted hosts (with control.d)
  template:
    src: pmie.controld.j2
    dest: "{{ __pcp_pmie_control_d_path }}/{{ item }}"
    mode: "0644"
  loop: "{{ pcp_target_hosts | default([]) }}"
  register: __pcp_register_changed_target_hosts_controld
  when:
    - not pcp_single_control | d(false) | bool
    - pcp_target_hosts | d([]) | length > 0

- name: Enable performance metric inference for targeted hosts (single control)
  template:
    src: pmie.control.j2
    dest: "{{ __pcp_pmie_control_path }}"
    mode: "0644"
  register: __pcp_register_changed_target_hosts_single
  when:
    - pcp_single_control | d(true) | bool
    - pcp_target_hosts | d([]) | length > 0

- name: Set variable to do pmie restart if needed
  set_fact:
    __pcp_restart_pmie: "{{
      __pcp_register_changed_group_dir is changed or
      __pcp_register_changed_group_link_dir is changed or
      __pcp_register_changed_rules_for_hosts is changed or
      __pcp_register_changed_actions_for_hosts is changed or
      __pcp_register_changed_symlinks_for_hosts is changed or
      __pcp_register_changed_target_hosts_controld is changed or
      __pcp_register_changed_target_hosts_single is changed }}"

- name: Ensure performance metric inference is running and enabled on boot
  service:
    name: pmie
    state: "{{ 'started' if __pcp_is_booted else omit }}"
    enabled: true
  when: not __pcp_restart_pmie | bool

- name: Ensure performance metric inference is restarted and enabled on boot
  service:
    name: pmie
    state: "{{ 'restarted' if __pcp_is_booted else omit }}"
    enabled: true
  when: __pcp_restart_pmie | bool
