#!/usr/bin/env ansible-playbook
---
# This is a special playbook to upgrade a server, be careful!
- hosts: server,test_vm
  tasks:
    - name: Upgrade
      apt:
        upgrade: dist
        update_cache: true
      register: apt_result
      retries: 3
      until: apt_result is succeeded

    - name: Clean unwanted olderstuff
      apt:
        autoremove: true
        purge: true
      register: apt_result
      retries: 3
      until: apt_result is succeeded

- hosts: owncloud-srv.adm.crans.org
  become_user: www-data
  become: true
  vars:
    # Owncloud command line interface
    occ_bin: '/var/www/owncloud/occ'
  tasks:
    - name: Upgrade owncloud
      command: "{{ occ_bin }} upgrade"
      register: upgrade_owncloud
      failed_when:
        # occ return code is 3 when ownCloud is already latest version
        - upgrade_owncloud.rc != 0
        - upgrade_owncloud.rc != 3
      changed_when:
        - upgrade_owncloud.rc != 3

    - name: Upgrade owncloud output
      debug:
        msg:
          - "stdout: {{ upgrade_owncloud.stdout_lines }}"
          - "stderr: {{ upgrade_owncloud.stderr_lines }}"
      when: not ansible_check_mode

    - name: Disable maintenance mode
      command: "{{ occ_bin }} maintenance:mode --off"
      when:
        - not ansible_check_mode
        # Maintenance mode has not been enabled.
        - upgrade_owncloud.rc != 3
