# SPDX-License-Identifier: MIT
---
- name: Ensure ansible_facts used by role
  setup:
    gather_subset: "{{ __metrics_required_facts_subsets }}"
  when: __metrics_required_facts | difference(ansible_facts.keys() | list) | length > 0

- name: Determine if system is booted with systemd
  when: __metrics_is_booted is not defined
  block:
    - name: Run systemctl
    # noqa command-instead-of-module
      command: systemctl is-system-running
      register: __is_system_running
      changed_when: 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:
      # see https://www.man7.org/linux/man-pages/man1/systemctl.1.html#:~:text=is-system-running%20output
        __metrics_is_booted: "{{ __is_system_running.stdout != 'offline' }}"

- name: Add Elasticsearch to metrics domain list
  set_fact:
    __metrics_domains: "{{ __metrics_domains + ['elasticsearch'] }}"
  when: metrics_from_elasticsearch | d(false) | bool

- name: Add SQL Server to metrics domain list
  set_fact:
    __metrics_domains: "{{ __metrics_domains + ['mssql'] }}"
  when: metrics_from_mssql | d(false) | bool

- name: Add Postfix to metrics domain list
  set_fact:
    __metrics_domains: "{{ __metrics_domains + ['postfix'] }}"
  when: metrics_from_postfix | d(false) | bool

- name: Add bpftrace to metrics domain list
  set_fact:
    __metrics_domains: "{{ __metrics_domains + ['bpftrace'] }}"
  when: metrics_from_bpftrace | d(false) | bool

- name: Setup metrics access for roles
  set_fact:
    __metrics_accounts:
      - {user: "{{ metrics_username }}", sasluser: "{{ metrics_username }}", saslpassword: "{{ metrics_password }}"}
  when: metrics_username | d("") | length > 0

# yamllint disable rule:line-length
- name: Configure Elasticsearch metrics
  vars:
    elasticsearch_agent: "{{ metrics_from_elasticsearch | d(false) | bool }}"
    elasticsearch_export_type: "{{ metrics_provider }}-metrics"
    elasticsearch_export_index: "{{ metrics_provider }}"
    elasticsearch_export_metrics: "{{ metrics_into_elasticsearch | d(false) | bool }}"
    elasticsearch_metrics_provider: "{{ metrics_provider }}"
  include_role:
  # noqa role-name[path]
    name: "fedora.linux_system_roles.private_metrics_subrole_elasticsearch"
  when: >
    metrics_from_elasticsearch | d(false) | bool or
    metrics_into_elasticsearch | d(false) | bool
# yamllint enable rule:line-length

- name: Configure Spark metrics
  vars:
    spark_metrics_agent: "{{ metrics_from_spark | d(false) | bool }}"
    spark_export_metrics: "{{ metrics_into_spark | d(false) | bool }}"
    spark_metrics_provider: "{{ metrics_provider }}"
  include_role:
  # noqa role-name[path]
    name: "fedora.linux_system_roles.private_metrics_subrole_spark"
  when: >
    metrics_from_spark | d(false) | bool or
    metrics_into_spark | d(false) | bool

- name: Configure SQL Server metrics.
  vars:
    mssql_metrics_provider: "{{ metrics_provider }}"
  include_role:
  # noqa role-name[path]
    name: "fedora.linux_system_roles.private_metrics_subrole_mssql"
  when: metrics_from_mssql | d(false) | bool

- name: Configure Postfix metrics.
  vars:
    postfix_metrics_provider: "{{ metrics_provider }}"
  include_role:
  # noqa role-name[path]
    name: "fedora.linux_system_roles.private_metrics_subrole_postfix"
  when: metrics_from_postfix | d(false) | bool

- name: Setup bpftrace metrics.
  vars:
    bpftrace_users: "{{ __metrics_accounts }}"
    bpftrace_metrics_provider: "{{ metrics_provider }}"
  include_role:
  # noqa role-name[path]
    name: "fedora.linux_system_roles.private_metrics_subrole_bpftrace"
  when: metrics_from_bpftrace | d(false) | bool

- name: Setup metric querying service.
  include_role:
  # noqa role-name[path]
    name: "fedora.linux_system_roles.private_metrics_subrole_keyserver"
  when: metrics_query_service | bool

- name: Setup metric collection service.
  vars:
    pcp_pmie_endpoint: "{{ metrics_webhook_endpoint }}"
    pcp_pmlogger_discard: "{{ metrics_retention_days }}"
    pcp_target_hosts: "{{ metrics_monitored_hosts }}"
    pcp_optional_agents: "{{ __metrics_domains }}"
    pcp_accounts: "{{ __metrics_accounts }}"
    pcp_rest_api: "{{ metrics_query_service | bool or metrics_graph_service | bool }}"
  include_role:
  # noqa role-name[path]
    name: "fedora.linux_system_roles.private_metrics_subrole_pcp"
  when: metrics_provider == 'pcp'

- name: Setup metric graphing service.
  vars:
    grafana_metrics_provider: "{{ metrics_provider }}"
  include_role:
  # noqa role-name[path]
    name: "fedora.linux_system_roles.private_metrics_subrole_grafana"
  when: metrics_graph_service | bool

- name: Configure firewall
  include_tasks: firewall.yml

- name: Configure selinux
  include_tasks: selinux.yml
