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

  - name: Download server certification tar
    get_url:
      dest: "/home/zoox/{{ weka_server_certification_tar_name }}"
      url: "https://z3.zooxlabs.com/zoox-third-party/{{ weka_server_certification_tar_sha }}/{{ weka_server_certification_tar_name }}"
    when: not st.stat.exists

  - name: Delete server certification binary path if present
    file:
      path: "{{ weka_server_certification_binary_path }}"
      state: absent
    when: not st.stat.exists

  - name: Create server certification binary path
    file:
      path: "{{ weka_server_certification_binary_path }}"
      state: directory
      owner: zoox
      group: zoox
      mode: u=rwx,g=r,o=r
    when: not st.stat.exists

  - name: Extract server certification tar
    unarchive:
      src: "/home/zoox/{{ weka_server_certification_tar_name }}"
      dest: "{{ weka_server_certification_binary_path }}"
      remote_src: yes
    when: not st.stat.exists

  - name: Delete server certification tar
    file:
      path: "/home/zoox/{{ weka_server_certification_tar_name }}"
      state: absent
    when: not st.stat.exists

  - name: Run server certification script
    command: "./server-certification.sh --conf"
    args:
      chdir: "{{ weka_server_certification_binary_path }}/server-certification/bin"
    environment:
      # This is needed so that the services that need to be restarted for
      # installation, apt does the default thing for them
      DEBIAN_FRONTEND: 'noninteractive'
    retries: 3
    become: yes
    when: not st.stat.exists

  - name: Delete server certification binary path
    file:
      path: "{{ weka_server_certification_binary_path }}"
      state: absent
    when: not st.stat.exists

  - name: Install Weka agent
    shell: "curl http://{{ weka_cluster_host }}:14000/dist/v1/install | sh"
    when: not st.stat.exists
    become: yes

  - name: reboot nodes
    reboot:
      post_reboot_delay: 30
      reboot_timeout: 600
    when: not st.stat.exists
    become: yes

  tags: weka-client

- block:
  - name: Create Weka mountpoint
    file:
      path: "{{ weka_mount_point }}"
      state: directory
      mode: 0777

  - name: create weka mount config
    template:
      src: mnt-weka.mount
      dest: /etc/systemd/system/mnt-weka.mount

  - name: Mount weka FS
    systemd:
      name: mnt-weka.mount
      daemon_reload: yes
      enabled: yes
      state: restarted

  become: yes
  tags: weka-automount

- name: disable mlocate for wekafs
  lineinfile:
    line: PRUNEFS="NFS nfs nfs4 rpc_pipefs afs binfmt_misc proc smbfs autofs iso9660 ncpfs coda devpts ftpfs devfs devtmpfs fuse.mfs shfs sysfs cifs lustre tmpfs usbfs udf fuse.glusterfs fuse.sshfs curlftpfs ceph fuse.ceph fuse.rozofs ecryptfs fusesmb wekafs"
    regexp: ^PRUNEFS=
    path: /etc/updatedb.conf
  become: yes
  tags: updatedb
