---
- name: check if safenode-manager binary exists
  ansible.builtin.stat:
    path: "{{ binary_dir}}/safenode-manager"
  register: safenode_manager_binary

- name: get the private IP for the instance
  set_fact:
    node_rpc_ip: "{{ instance_facts.instances[0].network_interfaces[0].private_ip_address }}"
  when: provider == "aws" and public_rpc and not safenode_manager_binary.stat.exists

- name: use the public IP as the RPC address on DO
  set_fact:
    node_rpc_ip: "{{ ansible_host }}"
  when: provider == "digital-ocean" and public_rpc and not safenode_manager_binary.stat.exists

- name: download the node manager binary
  ansible.builtin.get_url:
    url: "{{ node_manager_archive_url }}"
    dest: /tmp/{{ node_manager_archive_filename }}
  when: not safenode_manager_binary.stat.exists

- name: extract the node manager binary to /usr/local/bin
  become: True
  ansible.builtin.unarchive:
    src: "/tmp/{{ node_manager_archive_filename }}"
    dest: "{{ binary_dir }}"
    remote_src: True
  when: not safenode_manager_binary.stat.exists

- name: download the safenodemand binary
  ansible.builtin.get_url:
    url: "{{ safenodemand_archive_url }}"
    dest: /tmp/{{ safenodemand_archive_filename }}
  when: not safenode_manager_binary.stat.exists

- name: extract the safenodemand binary to /tmp/safenodemand
  become: True
  ansible.builtin.unarchive:
    src: "/tmp/{{ safenodemand_archive_filename }}"
    dest: /tmp/
    remote_src: True
  when: not safenode_manager_binary.stat.exists

- name: add safenodemand
  become: true
  command: safenode-manager -v daemon add --address {{ node_rpc_ip }} --path /tmp/safenodemand
  when: not safenode_manager_binary.stat.exists

- name: start safenodemand
  become: true
  command: safenode-manager -v daemon start
