---
#--------------------------------------------------------------#
# Install process_exporter                 [process_exporter_install]
#--------------------------------------------------------------#
- name: detect process_exporter architecture
  tags: [process_exporter, process_exporter_install]
  command: uname -m
  register: process_exporter_uname
  changed_when: false

- name: map process_exporter architecture
  tags: [process_exporter, process_exporter_install]
  set_fact:
    process_exporter_arch: >-
      {% if process_exporter_uname.stdout == 'x86_64' %}amd64{% elif process_exporter_uname.stdout in ['aarch64', 'arm64'] %}arm64{% else %}{% endif %}

- name: validate process_exporter architecture
  tags: [process_exporter, process_exporter_install]
  assert:
    that:
      - process_exporter_arch | length > 0
    fail_msg: "Unsupported process_exporter architecture: {{ process_exporter_uname.stdout }}"

- name: ensure process_exporter config directory exists
  tags: [process_exporter, process_exporter_config]
  file:
    path: "{{ process_exporter_config_dir }}"
    state: directory
    owner: root
    group: root
    mode: '0755'

- name: download process_exporter release archive
  tags: [process_exporter, process_exporter_install]
  get_url:
    url: "https://github.com/ncabatoff/process-exporter/releases/download/v{{ process_exporter_version }}/process-exporter-{{ process_exporter_version }}.linux-{{ process_exporter_arch }}.tar.gz"
    dest: "/tmp/process-exporter-{{ process_exporter_version }}.linux-{{ process_exporter_arch }}.tar.gz"
    mode: '0644'

- name: extract process_exporter release archive
  tags: [process_exporter, process_exporter_install]
  unarchive:
    src: "/tmp/process-exporter-{{ process_exporter_version }}.linux-{{ process_exporter_arch }}.tar.gz"
    dest: /tmp
    remote_src: true
    creates: "/tmp/process-exporter-{{ process_exporter_version }}.linux-{{ process_exporter_arch }}/process-exporter"

- name: install process_exporter binary
  tags: [process_exporter, process_exporter_install]
  copy:
    src: "/tmp/process-exporter-{{ process_exporter_version }}.linux-{{ process_exporter_arch }}/process-exporter"
    dest: "{{ process_exporter_binary }}"
    owner: root
    group: root
    mode: '0755'
    remote_src: true

- name: render process_exporter config
  tags: [process_exporter, process_exporter_config]
  template:
    src: process_exporter.yml
    dest: "{{ process_exporter_config_file }}"
    owner: root
    group: root
    mode: '0644'

- name: render process_exporter systemd unit
  tags: [process_exporter, process_exporter_config]
  template:
    src: process_exporter.svc
    dest: "{{ systemd_dir }}/process_exporter.service"
    owner: root
    group: root
    mode: '0644'

- name: launch process_exporter
  tags: [process_exporter, process_exporter_launch]
  systemd:
    name: process_exporter
    state: restarted
    enabled: true
    daemon_reload: true

- name: wait for process_exporter service online
  tags: [process_exporter, process_exporter_launch]
  wait_for:
    host: 127.0.0.1
    port: "{{ process_exporter_port }}"
    state: started
    timeout: 15
