---
- name: create upgrade prepare scripts
  include_tasks: create-prepare-scripts.yaml
  args:
    apply:
      tags:
        - create_ffu_prepare_scripts
  tags: create_ffu_prepare_scripts

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

- block:
    - name: ffu undercloud upgrade
      include_tasks: undercloud-upgrade.yml
      loop_control:
        loop_var: current_release
      loop: "{{ 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:
    - 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

    - name: start L3 verification scripts
      include_tasks: "{{ item }}"
      args:
        apply:
          tags: ffu_overcloud_prepare
      loop:
        - '../common/l3_agent_connectivity_check_start_script.yml'
        - '../common/l3_agent_failover_check_pre_script.yml'
      tags: ffu_overcloud_prepare

    - name: apply pre ffu overcloud prepare workarounds
      command: "{{ working_dir }}/pre_ffu_overcloud_prepare_workarounds.sh"
      when: ffu_upgrade_workarounds|bool
      tags: ffu_overcloud_prepare

    - name: convert nic templates
      import_tasks: ../common/convert_nic_templates.yaml
      tags: ffu_overcloud_prepare

    - name: set upgrade_prepare_extra_params for ffu prepare with custom repos
      set_fact:
        upgrade_prepare_extra_params: ["{{ working_dir }}/ffu_repos.yaml"]
      when: "'tripleo-repos' not in ffu_overcloud_repo_type"

    - name: set upgrade_prepare_extra_params for ffu prepare with deployed server
      set_fact:
        upgrade_prepare_extra_params: '{{ upgrade_prepare_extra_params + ["{{ tht_directory }}/environments/updates/update-from-deployed-server-newton.yaml"] }}'
      when: deployed_server|default(false)|bool

    - name: run overcloud upgrade prepare
      import_tasks: ../upgrade/overcloud_upgrade_prepare.yml
      tags: ffu_overcloud_prepare

    - name: stop L3 verification scripts
      include_tasks: "{{ item }}"
      args:
        apply:
          tags: ffu_overcloud_prepare
      loop:
        - '../common/l3_agent_connectivity_check_stop_script.yml'
        - '../common/l3_agent_failover_check_post_script.yml'
      tags: ffu_overcloud_prepare

    - name: apply pre fast forward overcloud upgrade run workarounds
      command: "{{ working_dir }}/pre_ffu_overcloud_run_workarounds.sh"
      when: ffu_upgrade_workarounds|bool
      tags: ffu_overcloud_run

    - name: start L3 connectivity script
      import_tasks: ../common/l3_agent_connectivity_check_start_script.yml
      tags: ffu_overcloud_run

    - name: run overcloud fast forward upgrade
      command: "{{ working_dir }}/overcloud_upgrade_run.sh"
      tags: ffu_overcloud_run

    - name: stop L3 connectivity script
      import_tasks: ../common/l3_agent_connectivity_check_stop_script.yml
      tags: ffu_overcloud_run

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

    - name: upgrade overcloud nodes per roles
      include_tasks: overcloud_role.yaml
      args:
        apply:
          tags: ffu_overcloud_upgrade_role
      tags: ffu_overcloud_upgrade_role
      when: not ffu_bulk|bool

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

        - name: run FFU ceph osd nodes upgrade scripts
          command: "{{ working_dir }}/overcloud_upgrade_CephStorage.sh"
          tags: ffu_overcloud_ceph

        - name: start L3 connectivity script
          import_tasks: ../common/l3_agent_connectivity_check_start_script.yml
          tags: ffu_overcloud_ceph

        - name: import ceph ugrade tasks
          import_tasks: ../upgrade/ceph_upgrade_run.yml
          tags: ffu_overcloud_ceph

        - name: stop L3 verification scripts
          include_tasks: "{{ item }}"
          args:
            apply:
              tags: ffu_overcloud_ceph
          loop:
            - '../common/l3_agent_connectivity_check_stop_script.yml'
            - '../common/l3_agent_failover_check_post_script.yml'
          tags: ffu_overcloud_ceph

    - name: apply pre ffu overcloud converge workarounds
      command: "{{ working_dir }}/pre_ffu_overcloud_converge_workarounds.sh"
      when: ffu_upgrade_workarounds|bool
      tags: ffu_overcloud_converge

    - name: import overcloud upgrade converge tasks
      import_tasks: ../upgrade/overcloud_upgrade_converge.yml
      tags: ffu_overcloud_converge

    - name: run post upgrade actions
      import_tasks: ../common/nova_actions_check.yml
      tags: ffu_overcloud_post

  when: ffu_overcloud_upgrade|bool
