---
# This role is for vehicle images. The role for developer machines is scripts/ansible/roles/nvidia/.

- block:

  # Following deps are installed when you run apt-get install nvidia-375 libcuda-375
  - name: Install Nvidia driver dependencies
    apt:
      name:
        - "dkms"
        - "lib32gcc1"
        - "libc6-i386"
        - "libvdpau1"
        - "bbswitch-dkms"
        - "screen-resolution-extra"
      force: yes
      install_recommends: no
      cache_valid_time: "{{ apt_cache_valid_time }}"

  - name: Prepare installer block
    block:
      - name: Download nvidia driver
        get_url:
          url: "{{ nvidia_download_url }}"
          dest: /opt
          mode: 755
          checksum: "sha256:{{ nvidia_SHA256 }}"

      - name: Extract nvidia driver
        command: "./{{ nvidia_package_version }}.run --extract-only --silent"
        args:
          chdir: "/opt"

      - name: Patch dkms conf
        patch:
          src: 001-ignore-preempt-rt.patch
          dest: "/opt/{{ nvidia_package_version }}/kernel/dkms.conf"

      - name: Overwrite nvidia installer
        get_url:
          url: "{{ nvidia_installer_download_url }}"
          dest: /opt/{{ nvidia_package_version }}/nvidia-installer
          mode: 755
          checksum: "sha256:{{ nvidia_installer_SHA256 }}"
          force: yes

      - name: Install nvidia driver
        command: "/opt/{{ nvidia_package_version }}/nvidia-installer --silent --no-x-check --dkms -k {{ zoox_kernel_version }} -chroot"

    always:
      - name: Clean nvidia install files
        file:
          path: "{{ item }}"
          state: absent
        with_items:
          - /opt/{{ nvidia_package_version }}.run
          - /opt/{{ nvidia_package_version }}

  - name: Install Nvidia related utilities
    apt:
      name:
        - "nvidia-prime"
      install_recommends: no
      cache_valid_time: "{{ apt_cache_valid_time }}"

  become: yes
