{%- set parameter_dict = dict(default_parameter_dict, **parameter_dict) -%}
{%- set additional_frontend = parameter_dict['additional-frontend-guid'] -%}

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

parts +=
  monitor-base
  $${:theia-parts}
  $${:theia-environment-parts}


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


# Change frontend name to avoid conflicts
[remote-frontend]
name = Import {{ parameter_dict['frontend-name'] }}

{% if additional_frontend -%}
[remote-additional-frontend]
name = Import {{ parameter_dict['additional-frontend-name'] }}
{%- endif %}


# Change standalone socket path to avoid collisions
[slapos-standalone-config]
abstract-socket-path = $${directory:home}/standalone-import-ready


# Disable frontend request forwarding in the 'import' instance
[slapos-standalone-script]
forward-frontend-requests = disabled


# Change port ranges to avoid race conditions on port allocation
[frontend-instance-port]
minimum = 3200
maximum = 3300

[theia-service-port]
minimum = 3700
maximum = 3800

[slapos-standalone-port]
minimum = 4200
maximum = 4300


# Always disable autoprocessing in the import instance
[slapos-autorun]
autorun = stopped


# Change the gravatar favicon seed
[favicon.ico]
seed = Import {{ root_title }}


# The resilient stack calls post-notification-run:output followed by
# importer:wrapper when the instance is notified that the backup files
# have just been pushed to it. All it expects is the path of a script
# in post-notification-run:output and in importer:wrapper.

[post-notification-run]
# Do nothing because the backup signature will
# be verified by the import script itself
recipe = slapos.recipe.build
output = $${directory:bin}/post-notification-run-script
location = $${:output}
install =
  import os
  os.symlink("/bin/true", location)

[importer]
wrapper = $${theia-import-script:output}

[theia-import-script]
recipe = slapos.recipe.template:jinja2
output = $${directory:bin}/theia-import-script
exitcode-file = $${directory:srv}/import-exitcode-file
error-file = $${directory:srv}/import-errormessage-file
context =
  raw python ${software-info:python-for-resiliency}
  raw theia_import ${software-info:theia-import}
  raw bash ${software-info:bash}
  raw rsync ${software-info:rsync}
  raw sqlite3 ${software-info:sqlite3}
  raw slapos ${software-info:slapos}
  raw slapos_node_software_log $${directory:runner}/var/log/slapos-node-software.log
  raw slapos_node_instance_log $${directory:runner}/var/log/slapos-node-instance.log
  raw supervisorctl ${software-info:supervisorctl}
  raw supervisord_conf $${directory:runner}/etc/supervisord.conf
  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 }}
  . $${common-environment:output}
  . $${slapos-standalone-activate:output}
  {{ python }} {{ theia_import }} \
  --rsync {{ rsync }} \
  --sqlite3 {{ sqlite3 }} \
  --slapos {{ slapos }} \
  --srlog {{ slapos_node_software_log }} \
  --cplog {{ slapos_node_instance_log }} \
  --supervisorctl {{ supervisorctl }} \
  --supervisordconf {{ supervisord_conf }} \
  --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 import script has run
# successfully and recently (at most 2 days ago).
[promises]
import-promises =
  $${import-promise:name}

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

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

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


# Resilient connection parameters of import instance are published
# through the resilient stack.
# Extend resilient parameters with normal theia connection parameters
[resilient-publish-connection-parameter]
<= publish-connection-parameter