---

  # Debian 9/10: install latest kernel stable updates provided by the distribution.
  # - good for security
  # - needed to keep in sync, with nxd-fuse-dkms which through dkms installs
  #   latest linux-headers-amd64. If those headers won't match installed
  #   kernel, nxd-fuse will be skipped to compile, fail to load and FUSE won't
  #   be working at all:
  #   https://lab.nexedi.com/nexedi/slapos.package/merge_requests/132#note_120438
  #
  #   NOTE we have to explicitly install/upgrade kernel headers here as well
  #   because once they are installed via nxd-fuse-dkms -> dkms -> linux-headers-amd64,
  #   they won't be upgraded, and if linux-image-amd64 gets upgraded, but headers are
  #   not, nxd-fuse-dkms will be skipped to recompile for upgraded kernel.
  - name: Debian 9/10 - Install latest stable updates for distribution kernel
    apt:
      name:
      - linux-image-amd64
      - linux-headers-amd64
      state: latest
      update_cache: yes
    when: ansible_distribution == "Debian" and
          (ansible_distribution_major_version == "9" or ansible_distribution_major_version == "10")
    notify: [ 'Mark to reboot' ]

  - name: Install Debian jessie-backports repository
    apt_repository: repo='deb http://ftp.debian.org/debian jessie-backports main' state=present update_cache=yes
    when: ansible_distribution == "Debian" and ansible_distribution_major_version == "8"

  - name: Install kernel 4.9 on debian 8.0
    apt: name=linux-image-4.9.0-0.bpo.9-amd64  state=latest default_release=jessie-backports update_cache=yes
    when: ansible_distribution == "Debian" and ansible_distribution_major_version == "8"
    notify: [ 'Mark to reboot' ]

  - name: Install Debian jessie repository on debian 7
    lineinfile:
      dest: '/etc/apt/sources.list'
      line: "{{item.line}}"
      state: present
    with_items:
      - {line: "deb http://deb.debian.org/debian jessie main"}
      - {line: "deb-src http://deb.debian.org/debian jessie main"}
      - {line: "deb http://security.debian.org/ jessie/updates main contrib non-free"}
      - {line: "deb-src http://security.debian.org/ jessie/updates main contrib non-free"}
    when: ansible_distribution == "Debian" and ansible_distribution_major_version == "7"

  - name: Dist upgrade debian 7 to debian 8
    apt: upgrade=dist update_cache=yes
    when: ansible_distribution == "Debian" and ansible_distribution_major_version == "7"
    notify: [ 'Mark to reboot' ]


  # install nxd-fuse on old Debian and Ubuntu
  - name: Install nxd-fuse
    apt: name=nxd-fuse-dkms state=latest update_cache=yes
    when: (ansible_distribution == "Debian" and ansible_distribution_major_version == "10") or
          (ansible_distribution == "Debian" and ansible_distribution_major_version == "9")  or
          (ansible_distribution == "Ubuntu" and ansible_distribution_version == "16.04")
    notify: [ 'Mark to reboot' ]


  - name: create trigger reboot script
    copy: src=slapos-trigger-reboot.sh dest=/usr/local/bin/slapos-trigger-reboot.sh mode=700

  - name: Stat AUTOREBOOT
    stat: path=/opt/upgrader/AUTOREBOOT
    register: autoreboot

  - name: Setup auto-reboot cron
    cron: name="Launch Reboot when REBOOT is Required" minute="*/30" job='/usr/local/bin/slapos-trigger-reboot.sh'
    when: autoreboot.stat.exists == True
