{% set part_list = [] %}
{% set slave_information_dict = {} %}

# regroup slave information from all frontends
{% for frontend, slave_list_raw in slave_information.iteritems() %}
{%   if slave_list_raw %}
{%     set slave_list = json_module.loads(slave_list_raw) %}
{%   else %}
{%    set slave_list = [] %}
{%   endif %}
{%   for slave_dict in slave_list %}
{%     set slave_reference = slave_dict.pop('slave-reference') %}
{%     set log_access_url = slave_dict.pop('log-access', '') %}
{%     set current_slave_dict = slave_information_dict.get(slave_reference, {}) %}
{%     do current_slave_dict.update(slave_dict) %}
{%     set log_access_list = current_slave_dict.get('log-access-urls', []) %}
{%     do log_access_list.append( frontend + ': ' + log_access_url) %}
{%     do current_slave_dict.__setitem__(
            'log-access-urls',
            log_access_list
            ) %}
{%     do current_slave_dict.__setitem__(
            'replication_number',
            current_slave_dict.get('replication_number', 0) + 1
            ) %}
{%     do slave_information_dict.__setitem__(slave_reference, current_slave_dict) %}
{%   endfor %}
{% endfor %}

{% for slave_reference, rejected_info_list in rejected_slave_information['rejected-slave-dict'].iteritems() %}
{%   if slave_reference not in slave_information_dict %}
{%     do slave_information_dict.__setitem__(slave_reference, {}) %}
{%   endif %}
{%   do slave_information_dict[slave_reference].__setitem__('request-error-list', json_module.dumps(rejected_info_list)) %}
{% endfor %}

{% for slave_reference, warning_info_list in warning_slave_information['warning-slave-dict'].iteritems() %}
{%   if slave_reference not in slave_information_dict %}
{%     do slave_information_dict.__setitem__(slave_reference, {}) %}
{%   endif %}
{%   do slave_information_dict[slave_reference].__setitem__('warning-list', json_module.dumps(warning_info_list)) %}
{% endfor %}

{% for slave_reference, kedifa_dict in json_module.loads(slave_kedifa_information).iteritems() %}
{%   if slave_reference not in rejected_slave_information['rejected-slave-dict'] %}
{%     if slave_reference not in slave_information_dict %}
{%       do slave_information_dict.__setitem__(slave_reference, {}) %}
{%     endif %}
{%     do slave_information_dict[slave_reference].__setitem__('key-generate-auth-url', kedifa_dict['key-generate-auth-url']) %}
{%     do slave_information_dict[slave_reference].__setitem__('key-upload-url', kedifa_dict['key-upload-url']) %}
{%     do slave_information_dict[slave_reference].__setitem__('kedifa-caucase-url', kedifa_dict['kedifa-caucase-url']) %}
{%   endif %}
{% endfor %}

# Publish information for each slave
{% set active_slave_instance_list = json_module.loads(active_slave_instance_dict['active-slave-instance-list']) %}
{% for slave_reference, slave_information in slave_information_dict.iteritems() %}
{#   Filter out destroyed, so not existing anymore, slaves #}
{#   Note: This functionality is not yet covered by tests, please modify with care #}
{%   if slave_reference in active_slave_instance_list %}
{%     set publish_section_title = 'publish-%s' % slave_reference %}
{%     do part_list.append(publish_section_title) %}
[{{ publish_section_title }}]
recipe = slapos.cookbook:publish
-slave-reference = {{ slave_reference }}
{%     set log_access_url = slave_information.pop('log-access-urls', None) %}
{%     if log_access_url %}
{# sort_keys are important in order to avoid shuffling parameters on each run #}
log-access-url = {{ dumps(json_module.dumps(log_access_url, sort_keys=True)) }}
{%     endif %}
{%     for key, value in slave_information.iteritems() %}
{{ key }} = {{ dumps(value) }}
{%     endfor %}
{%   endif %}
{% endfor %}

[buildout]
extends = {{ profile_common }}
parts =
{% for part in part_list %}
{{ '  %s' % part }}
{% endfor %}
