---
- block:
  - name: Check if Weka is Installed
    stat:
      path: /usr/bin/weka
    register: st

  - name: Get Weka Version
    shell:
      cmd: "weka --version | cut -d' ' -f4"
    register: weka_installed_version
    when: st.stat.exists
    check_mode: false
    changed_when: false

  - name: Download binary tar
    get_url:
      dest: "/tmp/{{ weka_tar_name }}"
      url: "https://z3.zooxlabs.com/zoox-third-party/{{ weka_tar_sha }}/{{ weka_tar_name }}"
      checksum: "sha256:{{ weka_tar_sha }}"
    when: not st.stat.exists or weka_version != weka_installed_version.stdout

  - name: Extract Weka tar
    unarchive:
      src: "/tmp/{{ weka_tar_name }}"
      dest: "/tmp/"
      remote_src: yes
    when: not st.stat.exists or weka_version != weka_installed_version.stdout

  - name: Delete Weka tar
    file:
      path: "/tmp/{{ weka_tar_name }}"
      state: absent
    when: not st.stat.exists or weka_version != weka_installed_version.stdout

  - name: Install Weka
    shell: "{{ weka_binary_extract_path }}/install.sh"
    args:
      chdir: "{{ weka_binary_extract_path }}"
    when: not st.stat.exists or weka_version != weka_installed_version.stdout

  - name: Delete Weka binary extraction
    file:
      path: "{{ weka_binary_extract_path }}"
      state: absent
    when: not st.stat.exists or weka_version != weka_installed_version.stdout

  - name: reboot nodes
    reboot:
      post_reboot_delay: 30
      reboot_timeout: 600
    when: not st.stat.exists or weka_version != weka_installed_version.stdout

  - name: Install chronyd
    apt:
      state: present
      name:
        - chrony

  - name: Get Weka Binary
    stat:
      path: /usr/bin/weka
    register: weka_binary

  - name: Add the host to Weka cluster
    command: "/usr/bin/weka -H {{ weka_cluster_host }} cluster host add {{ inventory_hostname_short }} --ip {{ ansible_default_ipv4.address }} --json"
    environment: "{{ weka_auth_credentials }}"
    check_mode: false
    register: weka_host_add_result
    when: not st.stat.exists and weka_binary.stat.exists

  - set_fact:
      weka_host_add_status: "{{ weka_host_add_result.stdout | from_json }}"
    when: not st.stat.exists and weka_binary.stat.exists

  - name: Add an interface to talk to Weka cluster
    command: "/usr/bin/weka -H {{ weka_cluster_host }} cluster host net add {{ weka_host_add_status.host_id }} {{ ansible_default_ipv4.interface }}"
    environment: "{{ weka_auth_credentials }}"
    check_mode: false
    when: not st.stat.exists and weka_binary.stat.exists and weka_host_add_status is defined

  - name: Dedicate this node to Weka cluster
    command: "/usr/bin/weka -H {{ weka_cluster_host }} cluster host dedicate {{ weka_host_add_status.host_id }} on"
    environment: "{{ weka_auth_credentials }}"
    check_mode: false
    when: not st.stat.exists and weka_binary.stat.exists and weka_host_add_status is defined

  - name: Initialize cores on this node
    command: "/usr/bin/weka -H {{ weka_cluster_host }} cluster host cores {{ weka_host_add_status.host_id }} 16 --frontend-dedicated-cores=2 --drives-dedicated-cores=2"
    environment: "{{ weka_auth_credentials }}"
    check_mode: false
    when: not st.stat.exists and weka_binary.stat.exists and weka_host_add_status is defined

  - name: Add first NVMe disk
    command: "/usr/bin/weka -H {{ weka_cluster_host }} cluster drive add {{ weka_host_add_status.host_id }} /dev/nvme0n1"
    environment: "{{ weka_auth_credentials }}"
    check_mode: false
    when: not st.stat.exists and weka_binary.stat.exists and weka_host_add_status is defined and add_nvme_drives is defined

  - name: Add second NVMe disk
    command: "/usr/bin/weka -H {{ weka_cluster_host }} cluster drive add {{ weka_host_add_status.host_id }} /dev/nvme1n1"
    environment: "{{ weka_auth_credentials }}"
    check_mode: false
    when: not st.stat.exists and weka_binary.stat.exists and weka_host_add_status is defined and add_nvme_drives is defined

  - name: Scan for new NVMe disks
    command: "/usr/bin/weka -H {{ weka_cluster_host }} cluster drive scan"
    environment: "{{ weka_auth_credentials }}"
    check_mode: false
    when: not st.stat.exists and weka_binary.stat.exists and weka_host_add_status is defined and add_nvme_drives is defined

  - name: Activate the new host
    command: "/usr/bin/weka -H {{ weka_cluster_host }} cluster host activate {{ weka_host_add_status.host_id }}"
    environment: "{{ weka_auth_credentials }}"
    check_mode: false
    when: not st.stat.exists and weka_binary.stat.exists and weka_host_add_status is defined

  - name: Apply the resources to new node
    command: "/usr/bin/weka -H {{ weka_cluster_host }} cluster host apply {{ weka_host_add_status.host_id }} --force"
    environment: "{{ weka_auth_credentials }}"
    check_mode: false
    when: not st.stat.exists and weka_binary.stat.exists and weka_host_add_status is defined

  become: yes
  tags: weka-server
