---
- name: Determine etcd version
  hosts: oo_etcd_hosts_to_upgrade
  tasks:
  - block:
    - name: Record RPM based etcd version
      command: rpm -qa --qf '%{version}' etcd\*
      args:
        warn: no
      register: etcd_rpm_version
      failed_when: false
      # AUDIT:changed_when: `false` because we are only inspecting
      # state, not manipulating anything
      changed_when: false
    - debug:
        msg: "Etcd rpm version {{ etcd_rpm_version.stdout }} detected"
    when:
    - not openshift.common.is_containerized | bool

  - block:
    - name: Record containerized etcd version (docker)
      command: docker exec etcd_container rpm -qa --qf '%{version}' etcd\*
      register: etcd_container_version_docker
      failed_when: false
      # AUDIT:changed_when: `false` because we are only inspecting
      # state, not manipulating anything
      changed_when: false
      when:
      - not openshift.common.is_etcd_system_container | bool

      # Given a register variables is set even if the whwen condition
      # is false, we need to set etcd_container_version separately
    - set_fact:
        etcd_container_version: "{{ etcd_container_version_docker.stdout }}"
      when:
      - not openshift.common.is_etcd_system_container | bool

    - name: Record containerized etcd version (runc)
      command: runc exec etcd_container rpm -qa --qf '%{version}' etcd\*
      register: etcd_container_version_runc
      failed_when: false
      # AUDIT:changed_when: `false` because we are only inspecting
      # state, not manipulating anything
      changed_when: false
      when:
      - openshift.common.is_etcd_system_container | bool

      # Given a register variables is set even if the whwen condition
      # is false, we need to set etcd_container_version separately
    - set_fact:
        etcd_container_version: "{{ etcd_container_version_runc.stdout }}"
      when:
      - openshift.common.is_etcd_system_container | bool

    - debug:
        msg: "Etcd containerized version {{ etcd_container_version }} detected"
    when:
    - openshift.common.is_containerized | bool

- include: upgrade_rpm_members.yml
  vars:
    etcd_upgrade_version: '2.1'

- include: upgrade_rpm_members.yml
  vars:
    etcd_upgrade_version: '2.2'

- include: upgrade_image_members.yml
  vars:
    etcd_upgrade_version: '2.2.5'

- include: upgrade_rpm_members.yml
  vars:
    etcd_upgrade_version: '2.3'

- include: upgrade_image_members.yml
  vars:
    etcd_upgrade_version: '2.3.7'

- include: upgrade_rpm_members.yml
  vars:
    etcd_upgrade_version: '3.0'

- include: upgrade_image_members.yml
  vars:
    etcd_upgrade_version: '3.0.15'

- include: upgrade_rpm_members.yml
  vars:
    etcd_upgrade_version: '3.1'

- include: upgrade_image_members.yml
  vars:
    etcd_upgrade_version: '3.1.3'

- name: Upgrade fedora to latest
  hosts: oo_etcd_hosts_to_upgrade
  serial: 1
  tasks:
  - include_role:
      name: etcd_upgrade
    when:
    - ansible_distribution == 'Fedora'
    - not openshift.common.is_containerized | bool

- name: Backup etcd
  include: backup.yml
  vars:
    etcd_backup_tag: "post-3.0-"
  when: openshift_etcd_backup | default(true) | bool
