#!/usr/bin/env ansible-playbook
---

- name: MIGRATE TO DEBIAN 10
  hosts: all
  tasks:

    - name: check / space
      shell:
        cmd: '[ $(df --output="avail" / | tail -n 1) -gt 4000000 ]'

    - name: check /boot space
      shell:
        cmd: '[ $(df --output="avail" /boot | tail -n 1) -gt 300000 ]'

    - name: dist-upgrade current ubuntu
      apt:
        force_apt_get: true
        install_recommends: false
        update_cache: true
        dpkg_options: force-confnew
        upgrade: dist
      register: apt_status
      retries: 60
      until: apt_status is success or ('Failed to lock apt for exclusive operation' not in apt_status.msg and '/var/lib/dpkg/lock' not in apt_status.msg)

    - name: autoremove current ubuntu
      apt:
        force_apt_get: true
        install_recommends: false
        autoclean: true
        autoremove: true
      register: apt_status
      retries: 60
      until: apt_status is success or ('Failed to lock apt for exclusive operation' not in apt_status.msg and '/var/lib/dpkg/lock' not in apt_status.msg)

    - name: list ubicast packages
      shell:
        cmd: |
          rm -f /root/ubicast-installed;
          for pkg in 'ubicast-mediaserver' 'ubicast-mediaserver-runtime' 'ubicast-monitor' 'ubicast-monitor-runtime' 'ubicast-skyreach' 'ubicast-skyreach-runtime' 'celerity-server' 'celerity-workers'; do
            dpkg -s "$pkg" >/dev/null 2>&1 && echo -n "$pkg " | tee -a '/root/ubicast-installed';
            echo '';
          done

    - name: dump mediaserver database
      shell:
        cmd: /usr/bin/mscontroller.py dump

    - name: dump skyreach database
      shell:
        cmd: /home/skyreach/htdocs/skyreach_site/scripts/control.sh dump

    - name: stop services
      loop:
        - nginx
        - msmonitor
        - mediaserver
        - skyreach
      systemd:
        name: "{{ item }}"
        state: stopped

    - name: add debian keys
      loop:
        - https://ftp-master.debian.org/keys/archive-key-10.asc
        - https://ftp-master.debian.org/keys/archive-key-10-security.asc
      apt_key:
        url: "{{ item }}"

    - name: disable skyreach repository
      shell:
        cmd: mv -f /etc/apt/sources.list.d/skyreach.list /etc/apt/sources.list.d/skyreach.list.migrate

    - name: update sources list
      copy:
        dest: /etc/apt/sources.list
        content: |
          deb http://ftp.debian.org/debian buster main contrib non-free
          deb http://ftp.debian.org/debian buster-updates main contrib non-free
          deb http://security.debian.org buster/updates main contrib non-free

    - name: install debian keyring
      apt:
        force_apt_get: true
        install_recommends: false
        update_cache: true
        name: debian-archive-keyring
      register: apt_status
      retries: 60
      until: apt_status is success or ('Failed to lock apt for exclusive operation' not in apt_status.msg and '/var/lib/dpkg/lock' not in apt_status.msg)

    - name: upgrade to debian
      apt:
        force_apt_get: true
        install_recommends: false
        update_cache: true
        dpkg_options: force-confnew
        upgrade: dist
      register: apt_status
      retries: 60
      until: apt_status is success or ('Failed to lock apt for exclusive operation' not in apt_status.msg and '/var/lib/dpkg/lock' not in apt_status.msg)

    - name: autoremove debian
      apt:
        force_apt_get: true
        install_recommends: false
        autoclean: true
        autoremove: true
      register: apt_status
      retries: 60
      until: apt_status is success or ('Failed to lock apt for exclusive operation' not in apt_status.msg and '/var/lib/dpkg/lock' not in apt_status.msg)

    - name: install apt-show-version
      apt:
        force_apt_get: true
        install_recommends: false
        name: apt-show-version
      register: apt_status
      retries: 60
      until: apt_status is success or ('Failed to lock apt for exclusive operation' not in apt_status.msg and '/var/lib/dpkg/lock' not in apt_status.msg)

    - name: install debian version of packages
      shell:
        cmd: "apt-get install $(apt-show-versions | grep -P 'newer than version in archive' | awk -F: '{print $1\"/buster\"}')"

    - name: upgrade
      apt:
        force_apt_get: true
        install_recommends: false
        update_cache: true
        dpkg_options: force-confnew
        upgrade: dist
      register: apt_status
      retries: 60
      until: apt_status is success or ('Failed to lock apt for exclusive operation' not in apt_status.msg and '/var/lib/dpkg/lock' not in apt_status.msg)

    - name: autoremove
      apt:
        force_apt_get: true
        install_recommends: false
        autoclean: true
        autoremove: true
      register: apt_status
      retries: 60
      until: apt_status is success or ('Failed to lock apt for exclusive operation' not in apt_status.msg and '/var/lib/dpkg/lock' not in apt_status.msg)

...
