---
  - name: Remove playbook completed file
    file: path=/etc/opt/vm-bootstrap-completed state=absent

  - file: path=/opt/upgrader state=directory mode=0755

  - stat: path=/opt/upgrader/current-upgrade-signature
    register: current_upgrade_signature

  - file: path=/opt/upgrader/ state=directory mode=0755

  - file: path=/opt/upgrader/playbook/ state=absent

  - file: path=/opt/upgrader/archive.tar.gz state=absent
  
  - file: path=/opt/upgrader/playbook state=directory mode=0755

  - name: Download the latest playbook 
    shell: slapcache-download --destination=/opt/upgrader/archive.tar.gz

  - name: Save new archive MD5
    stat:
      path=/opt/upgrader/archive.tar.gz
      get_md5=yes
    register: archive

  - name: Update playbook md5
    shell: echo {{ archive.stat.md5 }} > /opt/upgrader/next-upgrade-signature

  - name: Stat Current Upgrade Signature
    file: path=/opt/upgrader/current-upgrade-signature state=touch mode="0666"
    when: current_upgrade_signature.stat.exists == False

  - name: Copy slapos.playbook
    unarchive: src=/opt/upgrader/archive.tar.gz dest=/opt/slapos.playbook
    when: (upgrade_after|int > lookup('file', '/opt/upgrader/last-upgrade')|int ) and (lookup('file', '/opt/upgrader/next-upgrade-signature') != lookup('file', '/opt/upgrader/current-upgrade-signature'))

  - name: Upgrade Current Upgrade Signature
    shell: echo /opt/upgrader/next-upgrade-signature > /opt/upgrader/current-upgrade-signature && echo {{ ansible_date_time.epoch }} > /opt/upgrader/last-upgrade
    when: (upgrade_after|int > lookup('file', '/opt/upgrader/last-upgrade')|int )
