- name: create a comptatible deployment scripts from oooq
  include: ../upgrade/use_oooq.yaml
  tags: use_oooq
  when: use_oooq|bool

- name: make a copy of the initial overcloud deploy script
  copy:
    remote_src: yes
    src: "{{ overcloud_deploy_script }}"
    dest: "{{ overcloud_deploy_script }}.orig.sh"

- name: Adjust ffu scenario file.
  replace:
    dest: "{{ overcloud_deploy_script }}"
    regexp: "{{composable_scenario}}"
    replace: "{{upgrade_composable_scenario}}"
  when:
    - tripleo_ci|bool
    - upgrade_composable_scenario != ""
    - composable_scenario != ""

- name: Convert Ceph parameters to ceph ansible
  include: ../common/convert_ceph_params.yaml

- name: create ffu upgrade workaround scripts
  template:
    src: fast-forward-upgrade/workarounds.sh.j2
    dest: "{{working_dir}}/{{ item }}.sh"
    mode: 0775
  with_items:
     - 'post_ffu_undercloud_upgrade_workarounds'
     - 'pre_ffu_update_stack_outputs_workarounds'
     - 'pre_ffu_download_config_workarounds'
     - 'pre_ffu_upgrade_playbook_workarounds'
     - 'pre_ffu_upgrade_steps_playbook_workarounds'
     - 'pre_ffu_deploy_steps_playbook_workarounds'
     - 'pre_ffu_upgrade_converge_workarounds'
     - 'pre_ffu_upgrade_ceph_workarounds'
  when: ffu_upgrade_workarounds

- name: register is_upstream variable
  shell: |
    rpm -q openstack-tripleo-heat-templates | grep '\.el[[:digit:]]\.'
  failed_when: false
  register: is_upstream

- name: Check if overcloud deployed with pacemaker
  shell: |
    source {{ undercloud_rc }}
    openstack stack output show overcloud EnabledServices | grep 'pacemaker'
  failed_when: false
  register: pacemaker_present

- name: set deployment_with_ha fact
  set_fact:
    deployment_with_ha: "{{ is_upstream.rc == 0 and pacemaker_present.rc == 0 }}"

- name: Create FFU custom repos environment
  template:
    src: fast-forward-upgrade/ffu_repos.yaml.j2
    dest: "{{working_dir}}/ffu_repos.yaml"
  when: "'tripleo-repos' not in ffu_overcloud_repo_type"

- name: create ffu overcloud upgrade scripts
  include: ../upgrade/step_upgrade.yml
  loop_control:
    loop_var: ugstage
  with_items:
    - step: "FFU update stack outputs"
      script: "{{ working_dir }}/ffu_update_stack_outputs.sh"
      environment_file:
        - "{% if upstream_container_images %}{{ tht_directory }}/environments/docker.yaml{% endif %}"
        - "{% if upstream_container_images and deployment_with_ha %}{{ tht_directory }}/environments/docker-ha.yaml{% endif %}"
        - "{{ working_dir}}/{{ container_registry_file }}"
        - "{{ tht_directory }}/environments/lifecycle/ffwd-upgrade-prepare.yaml"
        - "{% if 'tripleo-repos' not in ffu_overcloud_repo_type %}{{working_dir}}/ffu_repos.yaml{% endif %}"
        - "{% if deployed_server|default(false)|bool %}{{tht_directory}}/environments/updates/update-from-deployed-server-newton.yaml{% endif %}"

    - step: "FFU converge"
      script: "{{ working_dir }}/ffu_upgrade_converge_script.sh"
      environment_file:
        - "{% if upstream_container_images %}{{ tht_directory }}/environments/docker.yaml{% endif %}"
        - "{% if upstream_container_images and deployment_with_ha %}{{ tht_directory }}/environments/docker-ha.yaml{% endif %}"
        - "{{ working_dir}}/{{ container_registry_file }}"
        - "{% if deployed_server|default(false)|bool %}{{tht_directory}}/environments/updates/update-from-deployed-server-newton.yaml{% endif %}"
        - "{{ tht_directory }}/environments/lifecycle/ffwd-upgrade-converge.yaml"
      option:
        - "update-plan-only"

    - step: "FFU post deploy Ceph upgrade"
      script: "{{ working_dir }}/ffu_upgrade_ceph_script.sh"
      environment_file:
        - "{% if upstream_container_images %}{{ tht_directory }}/environments/docker.yaml{% endif %}"
        - "{% if upstream_container_images and deployment_with_ha %}{{ tht_directory }}/environments/docker-ha.yaml{% endif %}"
        - "{{ working_dir}}/{{ container_registry_file }}"
        - "{{ tht_directory }}/environments/updates/update-from-ceph-newton.yaml"
        - "{% if ceph_ansible|succeeded %}{{ working_dir }}/ceph-ansible-env.yaml{% endif %}"
        - "{% if deployed_server|default(false)|bool %}{{tht_directory}}/environments/updates/update-from-deployed-server-newton.yaml{% endif %}"

- include: ../common/ssh_config_skip_host.yml

- include: ../common/create_l3_agent_connectivity_check_script.yml

- include: ../common/create_l3_agent_failover_check_script.yml
