{% set publish_dict = {} -%}
{% set part_list = [] -%}
{% set ipv6 = (ipv6 | list)[0] -%}
{% set monitor_base_url_dict = {} -%}

[directory]
recipe = slapos.cookbook:mkdirectory
etc = ${buildout:directory}/etc
plugins = ${:etc}/plugin

[request-common]
recipe = slapos.cookbook:request.serialised
software-url = ${slap-connection:software-release-url}
server-url = ${slap-connection:server-url}
key-file = ${slap-connection:key-file}
cert-file = ${slap-connection:cert-file}
computer-id = ${slap-connection:computer-id}
partition-id = ${slap-connection:partition-id}

[request-apache]
<= request-common
software-type = apache
name = apache-php
sla-computer_guid = {{ dumps(slapparameter_dict.get('apache-computer-guid', '')) }}
{% for key, value in slapparameter_dict.items() -%}
{%  if key.startswith('instance.') -%}
config-{{ key }} = {{ dumps(value) }}
{%  endif -%}
{% endfor -%}
config-monitor-passwd = ${monitor-instance-parameter:password}
config-database-list = ${request-mariadb:connection-database-list}
return =
  url
  backend-url
  monitor-base-url

{% do part_list.append('request-apache') -%}
{% do publish_dict.__setitem__('backend-url', '${request-apache:connection-backend-url}') -%}
{% do publish_dict.__setitem__('url', '${request-apache:connection-url}') -%}
{% do monitor_base_url_dict.__setitem__('apache', '${request-apache:connection-monitor-base-url}') -%}

[request-mariadb]
<= request-common
software-type = mariadb
name = Mariadb
sla-computer_guid = {{ dumps(slapparameter_dict.get('mariadb-computer-guid', '')) }}
config-database-list = {{ dumps([{'name': db_name, 'user': db_user, 'password': db_password }]) }}
config-test-database-amount = {{ dumps(0) }}
config-tcpv4-port = {{ dumps(2099) }}
config-max-slowqueries-threshold = {{ dumps(1000) }}
config-slowest-query-threshold =
config-computer-memory-percent-threshold = {{ dumps(80) }}
config-monitor-passwd = ${monitor-instance-parameter:password}
config-name = ${:name}
config-innodb-file-per-table = {{ dumps(slapparameter_dict.get('innodb-file-per-table', 1)) }}
return =
  database-list
  monitor-base-url

{% do part_list.append('request-mariadb') -%}
{% do publish_dict.__setitem__('mariadb-url-list', '${request-mariadb:connection-database-list}') -%}
{% do monitor_base_url_dict.__setitem__('mariadb', '${request-mariadb:connection-monitor-base-url}') -%}

[publish-early]
recipe = slapos.cookbook:publish-early
-init =
  monitor-password monitor-htpasswd:passwd

[monitor-instance-parameter]
monitor-httpd-port = 8060
cors-domains = {{ slapparameter_dict.get('monitor-cors-domains', 'monitor.app.officejs.com') }}
username = admin
password = ${publish-early:monitor-password}

[monitor-base-url-dict]
{% for key, value in monitor_base_url_dict.items() -%}
{{ key }} = {{ value }}
{% endfor %}

[monitor-conf-parameters]

[publish-connection-information]
<= monitor-publish
recipe = slapos.cookbook:publish
{% for name, value in publish_dict.items() -%}
{{   name }} = {{ value }}
{% endfor %}
{% do part_list.append('monitor-base') -%}

[buildout]
extends =
  {{ template_monitor }}

parts =
  publish-connection-information

# Complete parts with sections
  {{ part_list | join('\n  ') }}

eggs-directory = {{ eggs_directory }}
develop-eggs-directory = {{ develop_eggs_directory }}
offline = true
