{%- set parameter_dict = dict(default_parameter_dict, **parameter_dict) -%}

[buildout]
extends = {{ theia_instance_cfg }}
          {{ pbsready_export_cfg }}

parts +=
  monitor-base
  $${:theia-parts}
  $${:theia-environment-parts}
  resilient-publish-connection-parameter


# The resilient stack makes the 'resilient' instance
# request the 'export' instance with a 'namebase' parameter.
# The export template then expects to receive it in
# slap-parameter:namebase
[slap-parameter]
namebase = {{ parameter_dict['namebase'] }}


# The resilient export stack periodically calls exporter:wrapper
# and then notifies the pull-backup instance that data is ready
# to be pulled from the export instance.
# All it expects is that a script be available in exporter:wrapper.
[exporter]
wrapper = $${theia-export-script:output}

[theia-export-script]
recipe = slapos.recipe.template:jinja2
output = $${directory:bin}/theia-export-script
exitcode-file = $${directory:srv}/export-exitcode-file
error-file = $${directory:srv}/export-errormessage-file
context =
  raw python ${software-info:python-for-resiliency}
  raw theia_export ${software-info:theia-export}
  raw bash ${software-info:bash}
  raw rsync ${software-info:rsync}
  raw sqlite3 ${software-info:sqlite3}
  raw root_path $${buildout:directory}
  raw backup_path $${directory:backup}
  raw slapos_cfg $${directory:runner}/etc/slapos.cfg
  raw project_path $${directory:project}
  raw public_path $${directory:frontend-static-public}
  key exitfile :exitcode-file
  key errorfile :error-file
{%- raw %}
inline =
  #!{{ bash }}
  {{ python }} {{ theia_export }} \
  --rsync {{ rsync }} \
  --sqlite3 {{ sqlite3 }} \
  --root {{ root_path }} \
  --backup {{ backup_path }} \
  --cfg {{ slapos_cfg }} \
  --dirs {{ project_path }} \
  --dirs {{ public_path }} \
  --exitfile {{ exitfile }} \
  --errorfile {{ errorfile }}
{%- endraw %}


# Add a promise to check that the export script has run
# successfully and recently (at most 2 days ago).
[promises]
export-promises =
  $${export-promise:name}

[export-promise]
<= monitor-promise-base
promise = check_command_execute
name = resiliency-export-promise.py
config-command = $${export-promise-script:output}

[initial-export-exitcode-file]
recipe = slapos.recipe.template:jinja2
output = $${theia-export-script:exitcode-file}
inline = 0
once = $${:output}

[export-promise-script]
recipe = slapos.recipe.template:jinja2
output = $${directory:bin}/export-promise-script
exitcode-file = $${initial-export-exitcode-file:output}
context =
  key exitcodefile :exitcode-file
  key errorfile theia-export-script:error-file
{%- raw %}
inline =
  #!/bin/sh
  if [ -z $(find {{ repr(exitcodefile) }} -mtime -2) ]
  then
    echo "ERROR export script last ran on " $(date -r {{ repr(exitcodefile) }})
    exit 1
  elif [ "$(cat {{ repr(exitcodefile) }})" = 0 ]
  then
    echo "OK export script last ran on " $(date -r {{ repr(exitcodefile) }})
    exit 0
  else
    echo "ERROR export script failed on " $(date -r {{ repr(exitcodefile) }})
    cat {{ repr(errorfile) }}
    exit 1
  fi
{%- endraw %}


# Extend resilient parameters with normal theia connection parameters
[resilient-publish-connection-parameter]
<= publish-connection-parameter