# vim: set ft=cfg:

{% import 'parts' as parts %}
{% import 'replicated' as replicated with context %}

{% set backup_amount = slapparameter_dict.pop('resilient-clone-number', "1")|int + 1 -%}

[buildout]
eggs-directory = {{ eggs_directory }}
develop-eggs-directory = {{ develop_eggs_directory }}
offline = true

# += because we need to take up parts (like instance-custom, slapmonitor etc) from the profile we extended
parts +=
  {{ parts.replicate("kvm", backup_amount) }}
  publish-connection-informations
  kvm-frontend-url-promise
  kvm-backend-url-promise

{{ replicated.replicate("kvm", backup_amount, "kvm-export", "kvm-import", slapparameter_dict=slapparameter_dict) }}

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

# Bubble down the parameters of the requested instance to the user
[request-kvm]
# Note: += doesn't work.
return =
# Resilient related parameters
  url ssh-public-key ssh-url notification-id ip
# KVM related parameters
# XXX: return ALL parameters (like nat rules), through jinja
  backend-url url ip

[publish-connection-information]
recipe = slapos.cookbook:publish
backend-url = ${request-kvm:connection-backend-url}
url = ${request-kvm:connection-url}
ipv6 = ${request-kvm:connection-ip}

[kvm-frontend-url-promise]
# Check that url parameter is complete
recipe = collective.recipe.template
input = inline:#!/bin/sh
  URL="${request-kvm:connection-url}"
  if [[ ! "$URL" == https://* ]]; then
    exit 1
  fi
output = ${resilient-directory:promise}/kvm-frontend-url
mode = 700

[kvm-backend-url-promise]
# Check that backend url is reachable
recipe = slapos.cookbook:check_url_available
path = ${directory:promises}/frontend_promise
url = ${publish-connection-informations:url}
dash_path = /bin/sh
curl_path = {{ curl_executable_location }}
