#!/bin/bash
#
# Apply upgrade workarounds
set -euo pipefail

{% if 'pre_ffu_undercloud_upgrade_workarounds' in item %}
{% for bugs in pre_ffu_undercloud_upgrade_workarounds|default([]) %}
{% for key, value in bugs.items() %}
echo {{ key }}
{% if value.patch %}
curl -4 https://review.openstack.org/changes/{{ value.id }}/revisions/current/patch?download | \
    base64 -d | \
    sudo patch  -d {{ value.basedir }} -p1

{% else %}
{{ value.command }}

{% endif %}
{% endfor %}
{% endfor %}

{% elif 'post_ffu_undercloud_upgrade_workarounds' in item %}
{% for bugs in post_ffu_undercloud_upgrade_workarounds|default([]) %}
{% for key, value in bugs.items() %}
echo {{ key }}
{% if value.patch %}
curl -4 https://review.openstack.org/changes/{{ value.id }}/revisions/current/patch?download | \
    base64 -d | \
    sudo patch  -d {{ value.basedir }} -p1

{% else %}
{{ value.command }}

{% endif %}
{% endfor %}
{% endfor %}

{% elif 'pre_ffu_update_stack_outputs_workarounds' in item %}
{% for bugs in pre_ffu_update_stack_outputs_workarounds|default([]) %}
{% for key, value in bugs.items() %}
echo {{ key }}
{% if value.patch %}
curl -4 https://review.openstack.org/changes/{{ value.id }}/revisions/current/patch?download | \
    base64 -d | \
    sudo patch  -d {{ value.basedir }} -p1

{% else %}
{{ value.command }}

{% endif %}
{% endfor %}
{% endfor %}

{% elif 'pre_ffu_download_config_workarounds' in item %}
{% for bugs in pre_ffu_download_config_workarounds|default([]) %}
{% for key, value in bugs.items() %}
echo {{ key }}
{% if value.patch %}
curl -4 https://review.openstack.org/changes/{{ value.id }}/revisions/current/patch?download | \
    base64 -d | \
    sudo patch  -d {{ value.basedir }} -p1

{% else %}
{{ value.command }}

{% endif %}
{% endfor %}
{% endfor %}

{% elif 'pre_ffu_upgrade_playbook_workarounds' in item %}
{% for bugs in pre_ffu_upgrade_playbook_workarounds|default([]) %}
{% for key, value in bugs.items() %}
echo {{ key }}
{% if value.patch %}
curl -4 https://review.openstack.org/changes/{{ value.id }}/revisions/current/patch?download | \
     base64 -d | \
     sudo patch  -d {{ value.basedir }} -p1

{% else %}
{{ value.command }}

{% endif %}
{% endfor %}
{% endfor %}

{% elif 'pre_ffu_deploy_steps_playbook_workarounds' in item %}
{% for bugs in pre_ffu_deploy_steps_playbook_workarounds|default([]) %}
{% for key, value in bugs.items() %}
echo {{ key }}
{% if value.patch %}
curl -4 https://review.openstack.org/changes/{{ value.id }}/revisions/current/patch?download | \
    base64 -d | \
    sudo patch  -d {{ value.basedir }} -p1

{% else %}
{{ value.command }}

{% endif %}
{% endfor %}
{% endfor %}

{% endif %}
