---
# The role is making the assumption that telegraf is installed in the base machine image.
# This is to avoid locking errors with apt.

- name: ensure target directory exists
  file:
    path: "{{ network_dashboard_repo_path }}"
    state: directory

- name: write private key to file
  copy:
    content: "{{ network_dashboard_private_key }}"
    dest: "{{ network_dashboard_sk_path }}"
    mode: '0600'

- name: check if network dashboard repo already cloned
  stat:
    path: "{{ network_dashboard_repo_path }}/.git"
  register: repo_stat

- name: clone the network dashboard repo
  git:
    repo: "{{ network_dashboard_github_url }}"
    dest: "{{ network_dashboard_repo_path }}"
    version: "{{ network_dashboard_branch }}"
    key_file: "{{ network_dashboard_sk_path }}"
    accept_hostkey: yes
  when: not repo_stat.stat.exists

- name: update the network dashboard repo
  git:
    repo: "{{ network_dashboard_github_url }}"
    dest: "{{ network_dashboard_repo_path }}"
    version: "{{ network_dashboard_branch }}"
    key_file: "{{ network_dashboard_sk_path }}"
    accept_hostkey: yes
    force: yes
  when: repo_stat.stat.exists

- name: copy telegraf environment file
  copy:
    src: "{{ network_dashboard_repo_path }}/telegraf/EnvironmentFile/uploaders/telegraf"
    dest: /etc/default/telegraf
    remote_src: yes

- name: replace SAFE_UPLOADER_TESTNET_NAME value
  replace:
    path: "/etc/default/telegraf"
    regexp: 'SAFE_UPLOADER_TESTNET_NAME=UNDEFINED'
    replace: 'SAFE_UPLOADER_TESTNET_NAME={{ testnet_name | upper }}'

- name: replace SAFE_UPLOADER_HOST_ROLE value
  replace:
    path: "/etc/default/telegraf"
    regexp: 'SAFE_UPLOADER_HOST_ROLE=UNDEFINED'
    replace: 'SAFE_UPLOADER_HOST_ROLE=UPLOADER_NODE'

# The real branch name will be supplied later, when it is available in `safenode`.
- name: replace SAFE_UPLOADER_BRANCH_NAME value
  replace:
    path: "/etc/default/telegraf"
    regexp: "SAFE_UPLOADER_BRANCH_NAME=UNDEFINED"
    replace: "SAFE_UPLOADER_BRANCH_NAME=STABLE"

# The real commit hash will be supplied later, when it is available in `safenode`.
- name: replace SAFE_UPLOADER_BRANCH_COMMIT value
  replace:
    path: "/etc/default/telegraf"
    regexp: "SAFE_UPLOADER_BRANCH_COMMIT=UNDEFINED"
    replace: "SAFE_UPLOADER_BRANCH_COMMIT=UNDEFINED"

- name: copy telegraf config file
  copy:
    src: "{{ network_dashboard_repo_path }}/telegraf/ConfigurationFile/uploaders/telegraf.conf"
    dest: /etc/telegraf/telegraf.conf
    remote_src: yes

- name: copy telegraf uploader config file
  copy:
    src: "{{ network_dashboard_repo_path }}/telegraf/ConfigurationFile/uploaders/telegraf_safe_uploader_node.conf"
    dest: /etc/telegraf/telegraf.d/telegraf_safe_uploader_node.conf
    remote_src: yes

- name: add telegraf user to sudoers for safe command
  lineinfile:
    path: /etc/sudoers
    line: '_telegraf ALL=(ALL) NOPASSWD: /usr/local/bin/safe'
    validate: '/usr/sbin/visudo -cf %s'
    state: present

- name: update telegraf service user to root
  lineinfile:
    path: /lib/systemd/system/telegraf.service
    regexp: '^User='
    line: 'User=root'
    backup: yes

- name: update telegraf service group to root
  lineinfile:
    path: /lib/systemd/system/telegraf.service
    regexp: '^Group='
    line: 'Group=root'
    backup: yes

- name: reload systemd to apply service file changes
  systemd:
    daemon_reload: yes

- name: start telegraf service
  systemd:
    name: telegraf
    enabled: yes
    state: started