---
- block:
  - name: get nvidia libs
    find:
      paths: "/usr/lib/x86_64-linux-gnu"
      patterns: "libnv*,libvdpau_nvidia*,libcuda.*"
      file_type: any
    register: nvidia_libs

  - name: create docker compose directory
    file:
      name: "/etc/docker-compose/xserver"
      state: directory

  - name: copy run_with_openGL script
    copy:
      src: "run_with_openGL.sh"
      dest: "/etc/docker-compose/xserver/"
      owner: root
      group: root
      mode: u=rwx,g=rx,o=rx

  - name: copy xserver service file
    template:
      src: "templates/docker-compose.yml.template"
      dest: "/etc/docker-compose/xserver/docker-compose.yml"
      owner: root
      group: root
      mode: u=rw,g=r,o=r
    notify: restart xserver

  - name: copy xserver upstart file
    copy:
      src: "files/zoox-xserver-idler.conf"
      dest: "/etc/init/zoox-xserver-idler.conf"
      owner: root
      group: root
      mode: u=rw,g=r,o=r
    notify: restart xserver
    when: ansible_distribution_version == '14.04'

  - name: Install xserver service file
    copy:
      src: "files/zoox-xserver-idler.service"
      dest: "/etc/systemd/system/zoox-xserver-idler.service"
      owner: root
      group: root
      mode: u=rw,g=r,o=r
    notify: restart xserver
    when: ansible_distribution_version == '18.04'

  - name: run xserver
    service:
      name: zoox-xserver-idler
      state: started
      enabled: yes

  when: gpus is defined and gpus > 0 and ec2_private_dns_name is not defined
  become: yes
