- name: create upgrade prepare scripts
  include: create-prepare-scripts.yaml
  tags: create_ffu_prepare_scripts

- name: create fast forward upgrade scripts
  include: create-ffu-scripts.yaml
  tags: create_ffu_scripts
  loop_control:
    loop_var: current_release
  with_items: "{{ ffu_undercloud_releases }}"
  when: ffu_noop|bool or ffu_undercloud_upgrade|bool or ffu_overcloud_upgrade|bool

- block:
    - name: ffu undercloud upgrade
      include: undercloud-upgrade.yml
      loop_control:
        loop_var: current_release
      with_items: "{{ ffu_undercloud_releases }}"

    - name: apply post ffu undercloud upgrade workarounds
      command: "{{working_dir}}/post_ffu_undercloud_upgrade_workarounds.sh"
      when: ffu_upgrade_workarounds|bool
  when: ffu_undercloud_upgrade|bool

- block:
    - include: "{{ item }}"
      with_items:
        - '../common/l3_agent_connectivity_check_start_script.yml'
        - '../common/l3_agent_failover_check_pre_script.yml'
      tags: ffu_update_stack_outputs

    - name: apply pre ffu update stack outputs workarounds
      command: "{{working_dir}}/pre_ffu_update_stack_outputs_workarounds.sh"
      when: ffu_upgrade_workarounds|bool
      tags: ffu_update_stack_outputs

    - include: ffu_update_stack_outputs.yaml
      tags: ffu_update_stack_outputs

    - include: "{{ item }}"
      with_items:
        - '../common/l3_agent_connectivity_check_stop_script.yml'
        - '../common/l3_agent_failover_check_post_script.yml'
      tags: ffu_update_stack_outputs

    - name: apply pre ffu download config workarounds
      command: "{{working_dir}}/pre_ffu_download_config_workarounds.sh"
      when: ffu_upgrade_workarounds|bool
      tags: ffu_download_config

    - include: ffu_download_config.yaml
      tags: ffu_download_config

    - name: generate overcloud ansible static yaml inventory file
      shell: |
        source {{ undercloud_rc }}
        /usr/bin/tripleo-ansible-inventory \
            --plan {{ overcloud_stack_name }} \
            {% if overcloud_ssh_user != '' %} --ansible_ssh_user {{ overcloud_ssh_user }} {% endif %} \
            --static-yaml-inventory {{ working_dir }}/tripleo-ansible-inventory-static.yaml

    - name: Create FFU ugrade playbooks scripts
      tags: ffu_upgrade_playbooks_scripts
      template:
        src: fast-forward-upgrade/ffu_upgrade_playbook.sh.j2
        dest: "{{working_dir}}/{{ item }}.sh"
        mode: 0775
      with_items:
         - 'ffu_upgrade_playbook_script'

    - name: apply pre fast forward upgrade playbook workarounds
      command: "{{working_dir}}/pre_ffu_upgrade_playbook_workarounds.sh"
      when: ffu_upgrade_workarounds|bool
      tags: ffu_upgrade_playbook

    - name: Ensure ansible-pacemaker module is present in CI.
      yum: name=ansible-pacemaker state=latest
      when: tripleo_ci|default(false)|bool
      tags: ffu_upgrade_playbook
      become: true

    - include: ../common/l3_agent_connectivity_check_start_script.yml
      tags: ffu_upgrade_playbook

    - name: Run FFU fast_forward_upgrade_playbook.yaml
      command: "{{working_dir}}/ffu_upgrade_playbook_script.sh"
      tags: ffu_upgrade_playbook

    - include: ../common/l3_agent_connectivity_check_stop_script.yml
      tags: ffu_upgrade_playbook

    ## we can either upgrade all overcloud nodes at one or per role/node
    - name: upgrade all overcloud nodes at once
      include: overcloud_bulk.yaml
      tags: ffu_upgrade_overcloud_bulk
      when: ffu_bulk|bool

    - name: upgrade overcloud nodes per roles
      include: overcloud_role.yaml
      tags: ffu_upgrade_overcloud_role
      when: not ffu_bulk|bool

    - name: apply pre fast forward upgrade converge workarounds
      command: "{{working_dir}}/pre_ffu_upgrade_converge_workarounds.sh"
      when: ffu_upgrade_workarounds|bool
      tags: ffu_upgrade_converge

    - include: ffu_upgrade_converge.yaml
      tags: ffu_upgrade_converge

    - block:
      - name: apply pre ffu ceph upgrade workarounds
        command: "{{working_dir}}/pre_ffu_upgrade_ceph_workarounds.sh"
        when: ffu_upgrade_workarounds|bool
        tags: ffu_upgrade_ceph

      - include: ../common/l3_agent_connectivity_check_start_script.yml
        tags: ffu_upgrade_ceph

      - include: ffu_upgrade_ceph.yaml
        tags: ffu_upgrade_ceph

      - include: "{{ item }}"
        with_items:
          - '../common/l3_agent_connectivity_check_stop_script.yml'
          - '../common/l3_agent_failover_check_post_script.yml'
        tags: ffu_upgrade_ceph
      when: ceph_ansible|succeeded

  when: ffu_overcloud_upgrade|bool
