{% if slap_software_type in software_type -%}
{% set part_list =  [] %}

# Create all needed directories
[directory]
recipe = slapos.cookbook:mkdirectory

bin = $${buildout:directory}/bin/
etc = $${buildout:directory}/etc/
srv = $${buildout:directory}/srv/
var = $${buildout:directory}/var/
template = $${buildout:directory}/template/

backup = $${:srv}/backup
log = $${:var}/log
run = $${:var}/run
service = $${:etc}/service
etc-run = $${:etc}/run
promise = $${:etc}/promise

logrotate-backup = $${:backup}/logrotate
logrotate-entries = $${:etc}/logrotate.d

cron-entries = $${:etc}/cron.d
crontabs = $${:etc}/crontabs
cronstamps = $${:etc}/cronstamps
ca-dir = $${:srv}/ssl

# Instance parameters
[instance-parameter]
# Fetches parameters defined in SlapOS Master for this instance.
# Always the same.
recipe = slapos.cookbook:slapconfiguration.serialised
computer = $${slap-connection:computer-id}
partition = $${slap-connection:partition-id}
url = $${slap-connection:server-url}
key = $${slap-connection:key-file}
cert = $${slap-connection:cert-file}


# Generic jinja section to extend
[jinja2-template-base]
recipe = slapos.recipe.template:jinja2
rendered = $${buildout:directory}/$${:filename}
extra-context =
context =
    import json_module json
    key eggs_directory buildout:eggs-directory
    key develop_eggs_directory buildout:develop-eggs-directory
    key slap_software_type instance-parameter:slap-software-type
    key slapparameter_dict instance-parameter:configuration
    section directory directory
    $${:extra-context}

####################
# PowerDNS sections
#
[pdns]
configuration = $${pdns-directory:configuration}/pdns.conf
local-ipv4 = $${instance-parameter:ipv4-random}
ipv6 = $${instance-parameter:ipv6-random}
port = 5353
socket-directory = $${pdns-directory:socket}
webserver-port = 8088

[geo]
ip-map-zonefile = ${iso-list:location}/${iso-list:filename}
geo-maps = $${pdns-directory:geo-maps}

[pdns-directory]
recipe = slapos.cookbook:mkdirectory
configuration = $${directory:etc}/pdns
geo-maps = $${:configuration}/geo-maps
socket = $${directory:run}/pdns-socket

[pdns-configuration-template]
< = jinja2-template-base
template = ${template-pdns-configuration:target}
rendered = $${pdns:configuration}
extra-context =
  section pdns pdns
  section geo geo

# Executables
[pdns-server]
recipe = slapos.cookbook:wrapper
command-line = ${powerdns:location}/sbin/pdns_server --config-dir=$${pdns-directory:configuration}
wrapper-path = $${directory:service}/pdns
hash-files = $${buildout:directory}/software_release/buildout.cfg

[pdns-reload]
recipe = slapos.cookbook:wrapper
command-line = ${powerdns:location}/bin/pdns_control reload --config-dir=$${pdns-directory:configuration}
wrapper-path = $${directory:etc-run}/pdns-reload

# Port redirection
[port-redirection]
<= jinja2-template-base
template = inline:
{%- raw %}
  [{"srcPort": 53, "destPort": {{ pdns['port'] }}}]
{% endraw -%}
rendered = $${buildout:directory}/.slapos-port-redirect
extra-context =
  section pdns pdns

# Promises
[pdns-promise-listen-port]
recipe = slapos.cookbook:check_port_listening
path = $${directory:promise}/pdns-port-listening
hostname = $${pdns:local-ipv4}
port = $${pdns:port}

[publish-connection-informations]
recipe = slapos.cookbook:publish
monitor-url = $${monitor-publish-parameters:monitor-url}
monitor-user = $${monitor-publish-parameters:monitor-user}
monitor-password = $${monitor-publish-parameters:monitor-password}

#####################
# Power DNS Slave configuration
#
{% set slave_instance_list = json_module.loads(slapparameter_dict.get('extra_slave_instance_list', '')) %}

# Iter through slave list to prepare configuration
{% for slave in slave_instance_list %}
{%   if 'record' in slave and 'origin' in slave and 'default' in slave %}
{%     set slave_reference = slave.get('slave_reference') %}
{%     set slave_section_name = 'map-configuration-%s' % slave_reference  %}
{%     do part_list.append(slave_section_name) %}
[{{ slave_section_name }}]
< = jinja2-template-base
template = ${template-cdn-conf:location}/${template-cdn-conf:filename}
rendered = $${geo:geo-maps}/{{ slave_reference }}
configuration = {{ json_module.dumps(slave) }}
extra-context =
  key json_cdn :configuration
{%   endif %}
{% endfor %}

####################
# Monitoring
[monitor-rewrite-rule]
powerdns = http://$${pdns:local-ipv4}:$${pdns:webserver-port}/

[buildout]
parts =
  pdns-configuration-template
  pdns-server
  pdns-reload
  port-redirection
  pdns-promise-listen-port
  monitor-base
  publish-connection-informations
{% for part in part_list %}
{{ '  %s' % part }}
{% endfor %}

extends = ${monitor-template:output}

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