# these rules are common to kraken weka clients
# we download the docker image on all the clients
# to save time in launching the fallback kraken service
- hosts: all
  tasks:
    - block:
      - name: wait for unattended upgrade to be done
        raw: systemd-run --property="After=apt-daily.service apt-daily-upgrade.service" --wait /bin/true
      become: yes
      tags: prevent-unattended-upgrade

    - block:
      - 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

    - block:
      - name: Set numa_balancing to 0
        sysctl:
          name: kernel.numa_balancing
          value: '0'
          state: present
      become: yes
      tags: install_weka

    - block:
      - name: Install linux headers
        package:
          name: "linux-headers-{{ ansible_kernel }}"

      - name: Install required system packages
        apt: name={{ item }} state=latest update_cache=yes
        loop: [
            'apt-transport-https',
            'bpfcc-tools',
            'ca-certificates',
            'curl',
            'emacs-nox',
            'iftop',
            'nfs-common',
            'python3-pip',
            'python3-setuptools',
            'software-properties-common',
            'unzip',
            'virtualenv'
        ]

      - name: Add Docker GPG apt Key
        apt_key:
          url: https://download.docker.com/linux/ubuntu/gpg
          state: present

      - name: Add Docker Repository
        apt_repository:
          repo: deb https://download.docker.com/linux/ubuntu bionic stable
          state: present

      - name: Update apt and install docker-ce
        apt: update_cache=yes name=docker-ce state=latest

      - name: Install cryptography
        pip:
          name: cryptography==2.8

      - name: Install Docker Module for Python
        pip:
          name: docker

      - name: Install Docker Module for Python
        pip:
          name: docker-compose

      become: yes
      tags: install_packages

    - block:
      - name: Pull Kraken image
        docker_image:
          name: "{{ kraken_docker_image }}"
          source: pull
      become: yes
      tags: pull_docker_image

    - block:
      - name: Add nvidia-container-runtime apt signing key
        apt_key:
          url: https://nvidia.github.io/nvidia-container-runtime/gpgkey
          state: present
          validate_certs: no

      - name: Add apt repository
        get_url:
          url: https://nvidia.github.io/nvidia-container-runtime/{{ ansible_distribution|lower }}{{ ansible_distribution_version }}/nvidia-container-runtime.list
          dest: /etc/apt/sources.list.d/nvidia-container-runtime.list

      - name: Update repositories cache and install nvidia-container-runtime
        apt:
          name: nvidia-container-runtime
          update_cache: yes
      become: yes
      tags: install_nvidia_container_runtime
      when: install_nvidia_container_runtime is defined
