# SPDX-License-Identifier: MIT
---

- name: List optional metric collection agents to be enabled
  debug:
    msg: "NeedInstall agent: {{ item }} from {{ pcp_optional_agents }}"
  loop: "{{ pcp_optional_agents | default([]) }}"

- name: Extract metric collection configuration file content
  command: "cat {{ __pcp_pmcd_conf }}"
  register: pmcd_conf
  changed_when: false

- name: Ensure optional metric collection agents are enabled
  file:
    path: "{{ __pcp_agents_path }}/{{ item }}/.NeedInstall"
    mode: u=rw,g=r,o=r
    state: touch
  loop: "{{ pcp_optional_agents | default([]) }}"
  when: pmcd_conf.stdout.find(item) == -1
  register: __pcp_register_changed_optional_agents

- name: Ensure explicit metric label path exists
  file:
    path: "{{ __pcp_explicit_labels_path }}"
    state: directory
    mode: "0755"
    owner: root
    group: root

- name: Ensure implicit metric label path exists
  file:
    path: "{{ __pcp_implicit_labels_path }}"
    state: directory
    mode: "0755"
    owner: root
    group: root

- name: Ensure any explicit metric labels are configured
  template:
    src: pmcd.explicit.labels.j2
    dest: "{{ __pcp_explicit_labels_path }}/ansible-managed"
    mode: "0644"
  register: __pcp_register_changed_explicit_labels

- name: Ensure any implicit metric labels are configured
  template:
    src: pmcd.implicit.labels.j2
    dest: "{{ __pcp_implicit_labels_path }}/ansible-managed"
    mode: "0644"
  register: __pcp_register_changed_implicit_labels

- name: Ensure performance metric collector is configured
  template:
    src: pmcd.defaults.j2
    dest: "{{ __pcp_pmcd_defaults_path }}"
    mode: "0644"
  register: __pcp_register_changed_defaults_config

- name: Ensure performance metric collector system accounts are configured
  user:
    name: "{{ item.user }}"
    system: true
  when: item.user is defined
  with_items: "{{ pcp_accounts }}"

# yamllint disable rule:line-length
- name: Ensure performance metric collector SASL accounts are configured
  shell: |
    set -eu
    if set -o | grep -q pipefail; then
      set -o pipefail  # pipefail not supported on debian, some ubuntu
    fi
    if ! sasldblistusers2 -f "{{ __pcp_pmcd_sasldb_path }}" | grep -q "^{{ item.sasluser }}@"; then
      echo "Creating new {{ item.sasluser }} user in {{ __pcp_pmcd_sasldb_path }}"
      echo "{{ item.saslpassword }}" | saslpasswd2 -a pmcd "{{ item.sasluser }}"
      chown root:pcp "{{ __pcp_pmcd_sasldb_path }}"
      chmod 640 "{{ __pcp_pmcd_sasldb_path }}"
    fi
  register: ensure_sasl_configured
  changed_when: "'Creating new SASL account' in ensure_sasl_configured.stdout"
  when: item.sasluser is defined
  with_items: "{{ pcp_accounts }}"
# yamllint enable rule:line-length

- name: Ensure performance metric collector authentication is configured
  template:
    src: pmcd.sasl2.conf.j2
    dest: "{{ __pcp_pmcd_saslconf_path }}"
    mode: "0644"
  register: __pcp_register_changed_authentication
  when: pcp_accounts | d([]) | length > 0

- name: Set variable to do pmcd restart if needed
  set_fact:
    __pcp_restart_pmcd: "{{
      __pcp_register_changed_optional_agents is changed or
      __pcp_register_changed_explicit_labels is changed or
      __pcp_register_changed_implicit_labels is changed or
      __pcp_register_changed_defaults_config is changed or
      (__pcp_register_changed_authentication is defined and
       __pcp_register_changed_authentication is changed) }}"

# yamllint disable rule:line-length
- name: Report performance metric collector restart state
  debug:
    msg:
      - "optional_agents: {{ __pcp_register_changed_optional_agents is changed }}"
      - "explicit_labels: {{ __pcp_register_changed_explicit_labels is changed }}"
      - "implicit_labels: {{ __pcp_register_changed_implicit_labels is changed }}"
      - "defaults_config: {{ __pcp_register_changed_defaults_config is changed }}"
      - "authentication: {{ (__pcp_register_changed_authentication is defined and __pcp_register_changed_authentication is changed) }}"
      - "restart_pmcd: {{ __pcp_restart_pmcd | bool }}"
# yamllint enable rule:line-length

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

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