---
- name: Backup etcd
  hosts: oo_etcd_hosts_to_backup
  roles:
  - role: openshift_facts
  - role: etcd_common
    r_etcd_common_action: backup
    r_etcd_common_backup_tag: etcd_backup_tag
    r_etcd_common_etcd_runtime: "{{ openshift.common.etcd_runtime }}"
    r_etcd_common_embedded_etcd: "{{ groups.oo_etcd_to_config | default([]) | length == 0 }}"
    r_etcd_common_backup_sufix_name: "{{ lookup('pipe', 'date +%Y%m%d%H%M%S') }}"

- name: Gate on etcd backup
  hosts: localhost
  connection: local
  become: no
  tasks:
  - set_fact:
      etcd_backup_completed: "{{ hostvars
                                 | oo_select_keys(groups.oo_etcd_hosts_to_backup)
                                 | oo_collect('inventory_hostname', {'r_etcd_common_backup_complete': true}) }}"
  - set_fact:
      etcd_backup_failed: "{{ groups.oo_etcd_hosts_to_backup | difference(etcd_backup_completed) }}"
  - fail:
      msg: "Upgrade cannot continue. The following hosts did not complete etcd backup: {{ etcd_backup_failed | join(',') }}"
    when: etcd_backup_failed | length > 0
