---
# 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: ensure _telegraf can run safenode-manager without a password
  lineinfile:
    path: /etc/sudoers
    state: present
    line: "_telegraf ALL=NOPASSWD: /usr/local/bin/safenode-manager"
    validate: "visudo -cf %s"

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

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

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

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

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

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

- name: copy telegraf multi node config file
  copy:
    src: "{{ network_dashboard_repo_path }}/telegraf/ConfigurationFile/safenodes/telegraf_safe_multi_node.conf"
    dest: /etc/telegraf/telegraf.d/telegraf_safe_multi_node.conf
    remote_src: yes

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