---
- name: generate roles list from inventory file
  import_tasks: ../common/load_roles_from_inventory.yaml

- name: create FFU role ugrades scripts
  template:
    src: fast-forward-upgrade/overcloud_upgrade_role.sh.j2
    dest: "{{ working_dir }}/overcloud_upgrade_{{ item }}.sh"
    mode: 0775
  tags: ffu_overcloud_upgrade_controller
  loop: "{{ oc_roles|default([]) }}"

- name: start L3 connectivity check
  import_tasks: ../common/l3_agent_connectivity_check_start_script.yml
  tags: ffu_overcloud_upgrade_controller

- name: pre controller role upgrade steps workarounds
  command: "{{ working_dir }}/pre_ffu_overcloud_upgrade_workarounds.sh"
  when: ffu_upgrade_workarounds|bool
  tags: ffu_overcloud_upgrade_controller

- name: run FFU control plane ugrade scripts
  command: "{{ working_dir }}/overcloud_upgrade_{{ item }}.sh"
  tags: ffu_overcloud_upgrade_controller
  loop: "{{ oc_roles|difference(['Compute','ComputeAlt','CephStorage']) }}"

- name: run L3 validation
  include_tasks: "{{ item }}"
  loop:
    - '../common/l3_agent_connectivity_check_stop_script.yml'
    - '../common/l3_agent_failover_check_post_script.yml'
  tags: ffu_overcloud_upgrade_controller

- name: start L3 connectivity check
  import_tasks: ../common/l3_agent_connectivity_check_start_script.yml
  tags: ffu_overcloud_upgrade_compute

- name: run FFU compute ugrade scripts
  command: "{{ working_dir }}/overcloud_upgrade_{{ item }}.sh"
  tags: ffu_overcloud_upgrade_compute
  loop: "{{ oc_roles|intersect(['Compute','ComputeAlt']) }}"
  when:
    - not ffu_computes_rolling|bool

- name: validate L3 connectivity result
  import_tasks: ../common/l3_agent_connectivity_check_stop_script.yml
  tags: ffu_overcloud_upgrade_compute

- block:
    - name: register compute nodes from tripleo ansible inventory
      shell: |
        awk -F':' '/^[^ \n]*compute-/ {print $1}' {{ working_dir }}/tripleo-ansible-inventory.yaml
      register: tripleo_compute_nodes
      tags: ffu_overcloud_upgrade_compute

    - name: Create compute nodes FFU ugrade playbooks scripts
      template:
        src: fast-forward-upgrade/overcloud_upgrade_nodes.sh.j2
        dest: "{{ working_dir }}/overcloud_upgrade_{{ item }}.sh"
        mode: 0775
      tags: ffu_overcloud_upgrade_compute
      loop: '{{ tripleo_compute_nodes.stdout_lines }}'

    - name: start L3 connectivity check
      import_tasks: ../common/l3_agent_connectivity_check_start_script.yml
      tags: ffu_overcloud_upgrade_compute

    - name: Rolling upgrade compute nodes
      command: "{{ working_dir }}/overcloud_upgrade_{{ item }}.sh"
      tags: ffu_overcloud_upgrade_compute
      loop: '{{ tripleo_compute_nodes.stdout_lines }}'

    - name: validate L3 connectivity result
      import_tasks: ../common/l3_agent_connectivity_check_stop_script.yml
      tags: ffu_overcloud_upgrade_compute
  when: ffu_computes_rolling|bool
