#!/bin/env bash
#
# Run major upgrade converge step on overcloud nodes
#
set -euo pipefail
{% if l3_agent_connectivity_check|bool %}
if [[ -e {{ working_dir }}/l3_agent_start_ping.sh ]]; then
    source {{ overcloud_rc }}
    bash {{ working_dir }}/l3_agent_start_ping.sh
fi
{% endif %}

{% if l3_agent_failover_check|bool %}
if [[ -e {{ working_dir }}/l3_agent_failover_pre.sh ]]; then
    source {{ overcloud_rc }}
    bash {{ working_dir }}/l3_agent_failover_pre.sh
fi
{% endif %}

{% if fip_http_check|bool %}
source {{ overcloud_rc }}
kill -9 $( lsof -t {{ working_dir }}/fip_http_check_start.sh ) || :
bash {{ working_dir }}/fip_http_check_start.sh &
{% endif %}


source {{ undercloud_rc }}
echo "[$(date)] Running {% if ffu_overcloud_upgrade %} ffwd-upgrade {% else %} major {% endif %}upgrade converge step"
openstack overcloud {% if ffu_overcloud_upgrade %} ffwd-upgrade {% else %} upgrade {% endif %}converge \
    {% if ffu_overcloud_upgrade -%}
        --yes \
    {% endif -%}
    {% if overcloud_ssh_user != '' -%}
    --overcloud-ssh-user {{ overcloud_ssh_user }} \
    {% endif -%}
    {% if tripleo_upgrade_debug|bool -%}
    --debug \
    {% endif -%}
    {% if tripleo_upgrade_validations_non_fatal|bool -%}
    --validation-errors-nonfatal \
    {% endif -%}
    {% if tripleo_upgrade_converge_plan_only|bool -%}
    --update-plan-only \
    {% endif -%}
    --templates {{ tht_directory }} \
    --stack {{ overcloud_stack_name }} \
    {% if splitstack_deployment|bool -%}
    --disable-validations \
    --deployed-server \
    {% endif -%}
    {% if network_data_file|length >0 -%}
    -n {{ network_data_file }} \
    {% endif -%}
    {% if initial_env_file is defined and initial_env_file.stdout_lines|length > 0 -%}
    {% for _env in initial_env_file.stdout_lines|difference(old_img|default([])) -%}
        -e {{ _env }} \
    {% endfor -%}
    {% endif -%}
    {% if ffu_overcloud_upgrade and deployment_with_ha_upstream -%}
    -e {{ tht_directory }}/environments/docker.yaml \
    -e {{ tht_directory }}/environments/docker-ha.yaml \
    {% endif -%}
    {% if cli_converted_options|default(false)|bool -%}
    -e {{working_dir}}/cli_opts_params.yaml \
    {% endif -%}
    {% if ffu_overcloud_upgrade -%}
    -e {{ working_dir }}/{{ container_registry_file }} \
    {% if ceph_osd_enabled -%}
    -e {{working_dir}}/ceph-ansible-env.yaml \
    {% endif -%}
    {% endif -%}
    {% if deployment_with_ovs|default(true)|bool -%}
    -e {{ tht_directory }}/environments/services/neutron-ovs.yaml \
    {% endif -%}
    -e {{ working_dir }}/{{ uc_containers_prepare_file }} \
    {% if (not ffu_overcloud_upgrade or roles_data != default_roles_data) %}--roles-file {{ roles_data }}{% endif %} 2>&1


echo "[$(date)] Finished {% if ffu_overcloud_upgrade %} ffwd-upgrade {% else %} major {% endif %}upgrade converge step"
{% if l3_agent_connectivity_check|bool %}
if [[ -e {{ working_dir }}/l3_agent_stop_ping.sh ]]; then
    source {{ overcloud_rc }}
    bash {{ working_dir }}/l3_agent_stop_ping.sh
fi
{% endif %}

{% if l3_agent_failover_check|bool %}
if [[ -e {{ working_dir }}/l3_agent_failover_post.sh ]]; then
    source {{ overcloud_rc }}
    bash {{ working_dir }}/l3_agent_failover_post.sh
fi
{% endif %}

{% if fip_http_check|bool %}
source {{ overcloud_rc }}
kill -9 $( lsof -t {{ working_dir }}/fip_http_check_start.sh )
bash {{ working_dir }}/fip_http_check_stop.sh
{% endif %}

{% if launch_sanity_workload|bool %}
bash {{ working_dir }}/workload_launch.sh 'sanity'
{% endif %}
