{% set publish_dict = {} -%}
{% set part_list = [] -%}
{% set monitor_base_url_dict = {} -%}
{% set mariadb_dict = {} -%}
{% set ip = (ipv6_set | list)[0] -%}
{% set ipv4 = (ipv4_set | list)[0] -%}
{% set cluster_list = [] -%}
{% set tag_list = ["gtidstrict", "bind", "pkg", "innodb", "noquerycache", "slow",
  "pfs", "linux", "readonly", "diskmonitor", "sqlerror", "compressbinlog", "bm4ci",
  "mroonga", "utctime", "readcommitted", "nohandshake", "ssl"] -%}
{% set frontend_parameter_dict = slapparameter_dict.get('slave-frontend', {}) -%}
{% set count = namespace(value=2) %}
{% set caucase_bind = '[' ~ ip ~ ']:8890' -%}
{% set caucase_url = 'http://' ~ caucase_bind -%}
{% set csrid_list = [] -%}
{% set default_parameter_dict = {"cluster1": {"db-prefered-master": "",
  "database-amount": 2, "proxysql-user": "external",
  "logical-backup-cron": "0 21 * * *", "physical-backup-cron": "0 1 * * *"}} -%}
{% set cluster_name_list = slapparameter_dict.get('repman-cluster-dict', default_parameter_dict).keys() -%}
{% set database_slave_dict = {} -%}
{% set db_name_dict = {} -%}
{% set default_slave_cluster_id = list(cluster_name_list)[0] -%}

{% macro password(name) -%}
[{{ name }}-password]
recipe = slapos.cookbook:generate.password
bytes = 12

{% endmacro -%}

[directory]
recipe = slapos.cookbook:mkdirectory
home = ${buildout:directory}
etc = ${:home}/etc
var = ${:home}/var
run = ${:var}/run
scripts = ${:etc}/run
service = ${:etc}/service
controller = ${:etc}/controller
promise = ${:etc}/promise
log = ${:var}/log
data = ${:var}/lib
nginx-prefix = ${:var}/nginx
tmp = ${:home}/tmp
backup-caucased = ${:srv}/backup-caucased
caucased = ${:srv}/caucased
ssl = ${:etc}/ssl

[proxysql-directory]
recipe = slapos.cookbook:mkdirectory
home = ${buildout:directory}/proxy
config = ${directory:etc}/proxysql
ssl = ${:config}/default-ssl
{% for cluster in cluster_name_list -%}
{{ cluster }} = ${:home}/{{ cluster }}
{% do database_slave_dict.__setitem__(cluster, []) -%}
{% do db_name_dict.__setitem__(cluster, []) -%}
{% endfor -%}

{% import "supervisord_lib" as supervisord_lib with context %}
{% set proxysql_controller = "proxysql-ctl" -%}
{{ supervisord_lib.supervisord(proxysql_controller, buildout_bin_directory, supervisord_conf, use_service_hash=False) }}

[proxysql-controller-update]
recipe = slapos.cookbook:wrapper
command-line = ${proxysql-ctl-bin:wrapper-path} update
wrapper-path = ${directory:scripts}/proxysql-ctl-update
{% do part_list.append("supervisord-proxysql-ctl") -%}
{% do part_list.append("proxysql-controller-update") -%}

[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}
config-caucase-url = {{ caucase_url }}

[download-proxy-config]
recipe = slapos.recipe.template:jinja2
inline =
  #!{{ bash_bin }}
  NAME=$1
  HOST=$2
  PORT=$3
  CONFIG=$4
  if [ -z "$CONFIG" ]; then
    CONFIG="${proxysql-directory:config}/proxysql.cnf"
  fi
  ETCDIR=`dirname $CONFIG`
  mkdir -p ${repman:config-tmp}/proxies
  mkdir -p $ETCDIR/proxysql
  ln -sf ${proxysql-directory:ssl} $ETCDIR/proxysql/ssl
  cd ${repman:config-tmp}/proxies
  {{ curl_bin }} -o proxies-$NAME.tar.gz ${nginx-parameter:repman-url}/api/clusters/$NAME/servers/$HOST/$PORT/config
  tar -xzf proxies-$NAME.tar.gz
  cp etc/proxysql/proxysql.cnf $CONFIG
output = ${directory:bin}/update-proxysql-config

{% set slave_information_list = [] -%}
{% for instance_dict in slave_instance_list -%}
{% set slave_dict = {
  'name': 'db_%s' % instance_dict['slave_reference'].replace('-', '_').lower(),
  'user': instance_dict['slave_reference'].replace('_', '').replace('-', '').lower(),
  'password': instance_dict.get('db_password', '${' ~ instance_dict['slave_reference'] ~ '-password:passwd}'),
  'slave_reference': instance_dict['slave_reference'],
  'charset': instance_dict.get('db_charset', ''),
  'cluster_id': instance_dict.get('cluster_id', default_slave_cluster_id),
  'require_ssl': True
  } -%}
{% if not slave_dict['cluster_id'] in cluster_name_list -%}
{% do slave_dict.__setitem__('cluster_id', default_slave_cluster_id) -%}
{% endif -%}
{% do slave_information_list.append(slave_dict) -%}
{% do database_slave_dict[slave_dict['cluster_id']].append(slave_dict) -%}
{% do db_name_dict[slave_dict['cluster_id']].append([slave_dict['name'], slave_dict['user']]) -%}
{{ password(instance_dict['slave_reference']) }}
{% endfor %}
# set each database dict which will be used to publish slave information
[database-slave-information]
{% for slave_dict in slave_information_list -%}
{{   slave_dict['name'] }} = !py!{{ slave_dict }}
{% endfor %}

{% do mariadb_dict.__setitem__('computer-memory-percent-threshold', 80) -%}
{% for name, parameter_dict in slapparameter_dict.get('repman-cluster-dict', default_parameter_dict).items() -%}

{% do mariadb_dict.__setitem__('innodb-file-per-table', parameter_dict.get('innodb-file-per-table', 1)) -%}
{% do mariadb_dict.__setitem__('use-ipv6', parameter_dict.get('use-ipv6', True)) -%}
{% set db_list = db_name_dict[name] -%}

# Request mariadb instances
{% set db_amount = parameter_dict.get('database-amount', 2) -%}
{% if db_amount < 2 -%}
{% set db_amount = 2 -%}
{% endif -%}

{% set count.value = count.value + db_amount %}
{% set mariadb_path_list = [] -%}
{% set mariadb_server_list = [] -%}
{% set receiver_port_list = [] -%}
{% for i in range(0, db_amount) -%}
{% do mariadb_dict.__setitem__('tcp-port', 2099 + (i * 100)) -%}
{% set section = 'request-mariadb-' ~ i ~ '-' ~ name -%}
{% set dbname = 'Mariadb-' ~ i %}
{% set instance_name = dbname ~ '-' ~ name-%}

[{{ section }}]
<= request-common
software-type = mariadb
name = {{ instance_name }}
sla-computer_guid = {{ dumps(parameter_dict.get('-sla-' ~ i ~'-computer_guid', '')) }}
{% for key, value in mariadb_dict.items() -%}
config-{{ key }} = {{ dumps(value) }}
{% endfor -%}
config-monitor-passwd = ${publish-early:monitor-password}
config-root-password = ${publish-early:db-root-password}
config-repman-user = ${repman-parameter:username}
config-heartbeat-user = ${repman-parameter:heartbeat-user}
config-repman-passwd =  ${repman-parameter:password}
config-repman-url = ${nginx-parameter:backend-url}
config-repman-secure-url = ${nginx-parameter:backend-ssl-url}
config-cluster = {{ name }}
config-name = {{ instance_name }}
config-database-list = !py!{{ database_slave_dict[name] }}
config-database-name = {{ dumps(db_list) }}
config-require-ssl = {{ dumps(slapparameter_dict.get('require-ssl', False)) }}
return =
  database-host
  receiver-port
  monitor-base-url
  partition-path
  csr-id

{% do part_list.append(section) -%}
{% do mariadb_server_list.append('${' ~ section ~ ':connection-database-host}') -%}
{% do receiver_port_list.append('${' ~ section ~ ':connection-receiver-port}') -%}
{% do mariadb_path_list.append('${' ~ section ~ ':connection-partition-path}') -%}
{% do monitor_base_url_dict.__setitem__(instance_name, '${' ~ section ~ ':connection-monitor-base-url}') -%}
{% do csrid_list.append('${' ~ section ~ ':connection-csr-id}') -%}

{% endfor -%}

# Manage Replication Manager clusters
[{{name}}-admin-port]
recipe = slapos.cookbook:free_port
ip = {{ ipv4 }}
minimum = 6032
maximum = 6132

[{{name}}-port]
recipe = slapos.cookbook:free_port
ip = {{ ipv4 }}
minimum = 7032
maximum = 7132

{% set prefered_master = parameter_dict.get("db-prefered-master") -%}
[{{ name ~ '-cluster-parameter' }}]
{% for key, value in parameter_dict.items() -%}
{{ key }} = {{ value }}
{% endfor -%}
name = {{ name }}
proxysql-user = {{ parameter_dict.get("proxysql-user", "external") }}
proxy-port = {{ '${' ~ name ~ '-port:port}' }}
proxy-admin-port = {{ '${' ~ name ~ '-admin-port:port}' }}
db-user = repman
db-password = ${publish-early:db-root-password}
db-list = {{ mariadb_server_list | join(',') }}

autorejoin = {{ parameter_dict.get("autorejoin", True) }}
autoseed = {{ parameter_dict.get("autoseed", True) }}
heartbeat-user = ${repman-parameter:heartbeat-user}
heartbeat-password = ${publish-early:db-root-password}
partition-list = {{ mariadb_path_list | join(',') }}
{% if prefered_master -%}
db-prefered-master = {{ prefered_master }}
{% else -%}
# First database is the prefered master
db-prefered-master = {{ mariadb_server_list[0] }}
{% endif -%}
proxysql-servers = {{ ipv4 }}
proxysql-servers-ipv6 = [{{ ip }}]
password = ${repman-parameter:password}
proxysql-partition = {{ '${proxysql-directory:' ~ name ~ '}' }}
receiver-port-list = {{ receiver_port_list | join(',') }}
enabled-tags = {{ parameter_dict.get("tag-list", tag_list) | join(',') }}
proxy-tags = {{ parameter_dict.get("proxy-tags", ["pkg", "masterslave", "linux", "noreadwritesplit", "ssl"]) | join(',') }}
logical-backup-cron = {{ parameter_dict.get("logical-backup-cron", "0 22 * * *") }}
physical-backup-cron = {{ parameter_dict.get("physical-backup-cron", "0 0 * * *") }}
proxy-cpu-cores = {{ parameter_dict.get("proxy-cpu-cores", 2) }}
proxy-memory = {{ parameter_dict.get("proxy-memory", 1) }}
db-cpu-cores = {{ parameter_dict.get("db-cpu-cores", 2) }}
db-disk-iops = {{ parameter_dict.get("db-disk-iops", 300) }}
db-memory = {{ parameter_dict.get("db-memory", 256) }}
db-memory-shared-pct = {{ parameter_dict.get("db-memory-shared-pct", ["threads:16", "innodb:60", "myisam:10", "aria:10", "rocksdb:1", "tokudb:1", "s3:1", "archive:1", "querycache:0"]) | join(',') }}
db-memory-threaded-pct = {{ parameter_dict.get("db-memory-threaded-pct", ["tmp:70", "join:20", "sort:10"]) | join(',') }}
# failover
failover-mode = {{ parameter_dict.get('failover-mode', 'manual') }}
failover-limit = {{ parameter_dict.get('failover-limit', 5) }}
failover-falsepositive-heartbeat = {{ parameter_dict.get('failover-falsepositive-heartbeat', True) }}
failover-falsepositive-heartbeat-timeout = {{ parameter_dict.get('failover-falsepositive-heartbeat-timeout', 3) }}
failover-falsepositive-ping-counter = {{ parameter_dict.get('failover-falsepositive-ping-counter', 5) }}
failover-max-slave-delay = {{ parameter_dict.get('failover-max-slave-delay', 30) }}
failover-readonly-state = {{ parameter_dict.get('failover-readonly-state', True) }}
failover-restart-unsafe = {{ parameter_dict.get('failover-restart-unsafe', False) }}
failover-time-limit = {{ parameter_dict.get('failover-time-limit', 0) }}
#switchover
switchover-at-equal-gtid = {{ parameter_dict.get('switchover-at-equal-gtid', False) }}
switchover-slave-wait-catch = {{ parameter_dict.get('switchover-slave-wait-catch', True) }}
switchover-wait-kill = {{ parameter_dict.get('switchover-wait-kill', 5000) }}
switchover-wait-trx = {{ parameter_dict.get('switchover-wait-trx', 10) }}
switchover-wait-write-query = {{ parameter_dict.get('switchover-wait-write-query', 10) }}

[{{ 'config-' ~ name }}]
recipe = slapos.recipe.template:jinja2
url = {{ config_cluster_toml_in }}
output = ${repman:clusters}/config-{{ name }}.toml
extra-context =
context =
  section parameter_dict {{ name ~ '-cluster-parameter' }}

# Donwnload mariadb configuration from repman
[config-proxysql-{{ name }}]
recipe = plone.recipe.command
# if Repman is not started, cannot download config from server
stop-on-error = false
config = ${proxysql-directory:home}/{{ name }}/etc/proxysql.cnf
data = ${repman:proxy-data}/{{ name }}
command =
  mkdir -p ${:data} &&
  ${download-proxy-config:output} {{ name }} {{ ipv4 }} {{ '${' ~ name ~ '-cluster-parameter:proxy-admin-port}' }} ${:config}
update-command = ${:command}

[proxysql-{{ name }}-wrapper]
recipe = slapos.cookbook:wrapper
command-line =
  {{ proxysql_location }}/bin/proxysql -f
  -c ${config-proxysql-{{ name }}:config}
  -D ${config-proxysql-{{ name }}:data}
  --reload
#  -S /tmp/proxysql_admin.sock
wrapper-path = ${directory:controller}/proxysql-{{ name }}
wait-for-files =
  ${repman:bootstrap}/{{ name }}_bootstrapped
  ${config-proxysql-{{ name }}:config}
depends =
  {{ '${proxysql-' ~ name ~ '-admin-promise:recipe}' }}
  {{ '${proxysql-' ~ name ~ '-promise:recipe}' }}
  {{ '${proxysql-' ~ name ~ '-ipv6-promise:recipe}' }}

[proxysql-{{ name }}-admin-promise]
<= monitor-promise-base
promise = check_socket_listening
name = proxysql-{{ name }}-admin-port-listening.py
config-host =  {{ ipv4 }}
config-port = {{ '${' ~ name ~ '-cluster-parameter:proxy-admin-port}' }}

[proxysql-{{ name }}-promise]
<= monitor-promise-base
promise = check_socket_listening
name = proxysql-{{ name }}-port-listening.py
config-host =  {{ ipv4 }}
config-port = {{ '${' ~ name ~ '-cluster-parameter:proxy-port}' }}

[proxysql-{{ name }}-ipv6-promise]
<= monitor-promise-base
promise = check_socket_listening
name = proxysql-{{ name }}-ipv6-port-listening.py
config-host =  {{ ip }}
config-port = {{ '${' ~ name ~ '-cluster-parameter:proxy-port}' }}

{% set service_name = "proxysql-" ~ name -%}
{% set proxysql_dict = {"name": service_name, "command": "${" ~ service_name ~ "-wrapper:wrapper-path}",
  "stopwaitsecs": 60, "startretries": 10, "autorestart": False, "environment": [],
  "stdout_logfile": "${repman:proxies-log}/" ~ service_name ~ ".log",
  "stderr_logfile": "${repman:proxies-log}/" ~ service_name ~ ".log" } %}

{{ supervisord_lib.supervisord_program(service_name, proxysql_dict) }}
{% do part_list.append("supervisord-" ~ service_name) %}

{% do part_list.append('config-' ~ name) -%}
{% do cluster_list.append("{'name': '" ~ name ~ "', 'host': '" ~ ipv4 ~ "', 'port': '${" ~ name ~ "-cluster-parameter:proxy-admin-port}'}") -%}


[{{ name}}-publish-slave-information]
recipe = slapos.cookbook:switch-softwaretype
default = {{ 'dynamic-' ~name ~ '-publish-slave-information:output' }}
RootSoftwareInstance = ${:default}

[dynamic-{{ name}}-publish-slave-information]
recipe = slapos.recipe.template:jinja2
output = ${buildout:directory}/database-{{ name}}-publish-slave-information.cfg
extensions = jinja2.ext.do
url = {{ template_publish_slave_information }}
context =
    import json_module          json
    key proxy_port              {{ name }}-cluster-parameter:proxy-port
    raw eggs_directory          {{ eggs_directory }}
    raw develop_eggs_directory  {{ develop_eggs_directory }}
    raw cluster                 {{ name }}
    raw ipv6                    {{ ip }}
    raw ipv4                    {{ ipv4 }}
    section slave_dict          database-slave-information

{% do part_list.append(name ~ '-publish-slave-information') -%}
{% endfor -%}

# deploy caucase
{% import "caucase" as caucase with context %}
{{ caucase.caucased(
    prefix='caucased',
    buildout_bin_directory=buildout_bin_directory,
    caucased_path='${directory:service}/caucased',
    backup_dir='${directory:backup-caucased}',
    data_dir='${directory:caucased}',
    netloc=caucase_bind,
    tmp='${directory:tmp}',
    service_auto_approve_count=count.value,
    user_auto_approve_count=1,
    key_len=2048,
)}}
{% do part_list.append('caucased') -%}
{% do part_list.append('caucased-promise') -%}
{% do publish_dict.__setitem__('caucase-http-url', caucase_url) -%}
{{ caucase.updater(
     prefix='caucase-updater',
     buildout_bin_directory=buildout_bin_directory,
     updater_path='${directory:services}/caucase-updater',
     url=caucase_url,
     data_dir='${directory:srv}/caucase-updater',
     crt_path='${directory:ssl}/repman-cert.crt',
     ca_path='${directory:srv}/caucase-updater/ca.crt',
     crl_path='${directory:srv}/caucase-updater/crl.pem',
     key_path='${directory:ssl}/repman-cert.key',
     on_renew=None,
     max_sleep=None,
     template_csr_pem=None,
     openssl=openssl_bin,
)}}
{% do part_list.append('caucase-updater') -%}
{% do part_list.append('caucase-updater-promise') -%}

#caucase user certificate
{{ caucase.updater(
     prefix='caucase-user-updater',
     buildout_bin_directory=buildout_bin_directory,
     updater_path='${directory:services}/caucase-user-updater',
     url=caucase_url,
     data_dir='${directory:srv}/caucase-user-updater',
     crt_path='${directory:ssl}/caucase.user.crt',
     ca_path='${directory:srv}/caucase-user-updater/ca.crt',
     crl_path='${directory:srv}/caucase-user-updater/crl.pem',
     key_path='${directory:ssl}/caucase.user.key',
     on_renew=None,
     max_sleep=None,
     template_csr_pem=None,
     openssl=openssl_bin,
     mode='user',
)}}
{% do part_list.append('caucase-user-updater') -%}
{% do part_list.append('caucase-user-updater-promise') -%}

#caucase proxysql certificate
{{ caucase.updater(
     prefix='caucase-proxysql-updater',
     buildout_bin_directory=buildout_bin_directory,
     updater_path='${directory:services}/caucase-proxysql-updater',
     url=caucase_url,
     data_dir='${proxysql-directory:ssl}',
     crt_path='${proxysql-directory:ssl}/client-cert.pem',
     ca_path='${proxysql-directory:ssl}/ca-cert.pem',
     crl_path='${proxysql-directory:ssl}/crl.pem',
     key_path='${proxysql-directory:ssl}/client-key.pem',
     on_renew=None,
     max_sleep=None,
     template_csr_pem=None,
     openssl=openssl_bin,
)}}
{% do part_list.append('caucase-proxysql-updater') -%}
{% do part_list.append('caucase-proxysql-updater-promise') -%}

[caucase-sign-csr]
recipe = slapos.recipe.template:jinja2
output = ${directory:bin}/caucase-sign
inline =
  #!/bin/sh
  cp ${directory:ssl}/caucase.user.key ${directory:ssl}/caucase-full.key
  cat ${directory:ssl}/caucase.user.crt >> ${directory:ssl}/caucase-full.key
  for csr_id in {{ csrid_list | join(' ') }}; do
    if [ "$csr_id" = "None" ] || [ -z "$csr_id"]; then
      continue
    fi
    {{ buildout_bin_directory }}/caucase --ca-url {{ caucase_url }} --user-key ${directory:ssl}/caucase-full.key --sign-csr $csr_id
  done

[cron-caucase-sign-csr]
recipe = slapos.cookbook:cron.d
cron-entries = ${cron:cron-entries}
name = caucase-sign-csr
frequency = * * * * *
command = ${caucase-sign-csr:output}
{% do part_list.append('cron-caucase-sign-csr') -%}

[slap-configuration]
recipe = slapos.cookbook:slapconfiguration
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}

[instance-parameter]
<= slap-configuration
# repman monitor seems to use a fixed port
repman-secure-port = 10005
repman-port = 10001
nginx-ssl-port  = 10006
nginx-port  = 10007


[repman]
recipe   = slapos.cookbook:mkdirectory
etc      = ${directory:etc}/repman
data-dir = ${directory:var}/lib
root-dir = ${directory:srv}/repman
clusters = ${:etc}/cluster.d
proxy-data = ${:data-dir}/proxy
config-tmp = ${directory:tmp}/config
bootstrap = ${:etc}/bootstrap
proxies-log = ${directory:log}/proxy

[nginx-parameter]
ipv6 = ${instance-parameter:ipv6-random}
port = ${instance-parameter:nginx-port}
ssl-port = ${instance-parameter:nginx-ssl-port}
ssl-certificate = ${directory:ssl}/repman-cert.crt
ca-certificate = ${directory:srv}/caucase-updater/ca.crt
ssl-key = ${directory:ssl}/repman-cert.key
pid-file = ${directory:run}/nginx.pid
access-log = ${directory:log}/nginx_access.log
error-log = ${directory:log}/nginx_error.log
repman-secure-url = https://${repman-parameter:ipv4}:${repman-parameter:secure-port}
repman-url = http://${repman-parameter:ipv4}:${repman-parameter:port}
config-file = ${directory:etc}/nginx.conf
backend-ssl-url = https://[${:ipv6}]:${:ssl-port}
backend-url = http://[${:ipv6}]:${:port}

[repman-password]
recipe = slapos.cookbook:generate.password
bytes = 12

[gen-root-password]
recipe = slapos.cookbook:generate.password
bytes = 12

[repman-parameter]
log = ${directory:log}/repman.log
http-root = ${repman:root-dir}/dashboard
share-dir = ${repman:root-dir}/share
secure-port = ${instance-parameter:repman-secure-port}
port = ${instance-parameter:repman-port}
ipv4 = ${instance-parameter:ipv4-random}
username = admin
heartbeat-user = heartbeat
password = ${publish-early:repman-password}
cluster-d = ${repman:clusters}
mysql-bin-dir = {{ mariadb_location }}/bin
mysqlbinlog-path = {{ mariadb_location }}/bin/mysqlbinlog
mysqlclient-path = {{ mariadb_location }}/bin/mysql
mysqldump-path = {{ mariadb_location }}/bin/mysqldump
haproxy-bin   = {{ haproxy_location }}/sbin/haproxy
sysbench-bin  = {{ sysbench_location }}/bin/sysbench
restic-bin    = {{ restic_bin_location }}

mail-from = {{ slapparameter_dict.get("mail-from", "mrm@localhost") }}
mail-smtp-addr = {{ slapparameter_dict.get("mail-smtp-addr", "localhost") }}
mail-smtp-port = {{ slapparameter_dict.get("mail-smtp-port", "25") }}
mail-smtp-password = {{ slapparameter_dict.get("mail-smtp-password", "") }}
mail-smtp-user = {{ slapparameter_dict.get("mail-smtp-user", "") }}
mail-to = {{ slapparameter_dict.get("mail-to", "") }}

http-session-lifetime = {{ slapparameter_dict.get("http-session-lifetime", 86400) }}
http-refresh-interval = {{ slapparameter_dict.get("http-refresh-interval", 4) }}
ssl-cert = ${directory:ssl}/repman-cert.crt
ssl-key = ${directory:ssl}/repman-cert.key

[repman-config-folder]
recipe = plone.recipe.command
repman-location = {{ repman_src_location }}
command =
  cd ${:repman-location}
  {{ rsync_location }}/bin/rsync -av share ${repman:root-dir}/
  {{ rsync_location }}/bin/rsync -av  dashboard ${repman:root-dir}/

update-command = ${:command}

[replication-manager-reload]
recipe = slapos.recipe.template:jinja2
url = {{ template_repman_manager_sh }}
cluster-list = {{ dumps( list(slapparameter_dict.get('repman-cluster-dict', default_parameter_dict)) ) }}
context =
  section parameter_dict repman
  key username repman-parameter:username
  key password repman-parameter:password
  key secure_url nginx-parameter:backend-ssl-url
  key cluster_name_list :cluster-list
  raw jq_bin {{ jq_bin }}
  raw curl_bin {{ curl_bin }}
  raw bash_bin {{ bash_bin }}
output = ${directory:scripts}/repman-reload

[replication-manager]
recipe = slapos.cookbook:wrapper
command-line =
   {{ gowork_bin }}/replication-manager
   --monitoring-basedir=${repman:root-dir}
   --monitoring-sharedir=${repman-parameter:share-dir}
   --http-root=${repman-parameter:http-root}
   --monitoring-datadir=${repman:data-dir}
   --config=${repman-config.toml:output}
   --log-file=${repman-parameter:log}
   --memprofile=${directory:tmp}/repmgr.mprof
   monitor

wrapper-path = ${directory:service}/replication-manager
# setup repman instance folder
depends =
  ${repman-config-folder:recipe}
  ${replication-manager-reload:recipe}
  ${repman-listen-promise:recipe}
  ${repman-listen-ssl-promise:recipe}
wait-for-files =
  ${directory:ssl}/repman-cert.crt
  ${directory:ssl}/repman-cert.key

[repman-config.toml]
recipe = slapos.recipe.template:jinja2
url = {{ config_toml_in }}
output = ${repman:etc}/config.toml
extra-context =
context =
  section parameter_dict repman-parameter

[repman-listen-promise]
<= monitor-promise-base
promise = check_socket_listening
name = repman_service_listen.py
config-host = ${repman-parameter:ipv4}
config-port = ${repman-parameter:port}

[repman-listen-ssl-promise]
<= monitor-promise-base
promise = check_socket_listening
name = repman_service_ssl_listen.py
config-host = ${repman-parameter:ipv4}
config-port = ${repman-parameter:secure-port}

[nginx-conf]
recipe = slapos.recipe.template:jinja2
url = {{ nginx_conf_in }}
output = ${nginx-parameter:config-file}
context =
    section parameter_dict nginx-parameter

[nginx-launcher]
recipe = slapos.cookbook:wrapper
command-line =
  {{ nginx_bin }}
  -p ${directory:nginx-prefix}
  -c ${nginx-conf:output}
wrapper-path = ${directory:services}/nginx
wait-for-files =
  ${directory:ssl}/repman-cert.crt
  ${directory:srv}/caucase-updater/ca.crt
  ${directory:ssl}/repman-cert.key


[nginx-graceful-wrapper]
recipe = slapos.recipe.template
inline =
  #!{{ bash_bin }}
  kill -USR1 "$(cat ${nginx-parameter:pid-file})"
output = ${directory:scripts}/nginx-graceful

[logrotate-entry-nginx]
<= logrotate-entry-base
name = nginx
log = ${nginx-parameter:access-log} ${nginx-parameter:error-log}
post = kill -USR1 $(cat ${nginx-parameter:pid-file})

[publish-early]
recipe = slapos.cookbook:publish-early
-init =
  monitor-password monitor-htpasswd:passwd
  db-root-password gen-root-password:passwd
  repman-password repman-password:passwd
database-dict = {{ db_name_dict }}

[publish-connection-parameter]
<= monitor-publish
-extends = publish-early
recipe = slapos.cookbook:publish
backend-url = ${nginx-parameter:backend-ssl-url}
url = ${repman-frontend:connection-secure_access}
username = ${repman-parameter:username}
database-dict = {{ db_name_dict }}
{% for name, value in publish_dict.items() -%}
{{   name }} = {{ value }}
{% endfor %}


[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 %}

[repman-frontend]
<= slap-connection
recipe = slapos.cookbook:requestoptional
name = Replication Manager Frontend
# XXX We have hardcoded SR URL here.
software-url = {{ frontend_parameter_dict.get('frontend-software-url', 'http://git.erp5.org/gitweb/slapos.git/blob_plain/HEAD:/software/apache-frontend/software.cfg') }}
{% if frontend_parameter_dict.get('frontend-software-type', '') -%}
software-type ={{ frontend_parameter_dict['frontend-software-type'] }}
{% endif -%}
shared = true
config-url = ${nginx-parameter:backend-ssl-url}
config-domain = {{ frontend_parameter_dict.get('slave-domain', '') }}
return = domain secure_access

[repman-frontend-promise]
<= monitor-promise-base
promise = check_url_available
name = check_repman_frontend.py
config-url = https://${repman-frontend:connection-domain}

[repman-backend-promise]
<= monitor-promise-base
promise = check_url_available
name = check_repman_backend.py
config-url = ${nginx-parameter:backend-ssl-url}

[template-proxysql-need-stop-start]
recipe = slapos.recipe.template:jinja2
output = ${directory:bin}/proxysql_check_stop_start
url = {{ template_proxy_need_stopstart }}
cluster-list = !py![{{ cluster_list | join(', ') }}]
context =
  key proxysql_controller {{proxysql_controller}}-bin:wrapper-path
  key repman_url          nginx-parameter:backend-url
  key get_proxy_config    download-proxy-config:output
  key cluster_list        :cluster-list
  raw jq_bin              {{ jq_bin }}
  raw bash_bin           {{ bash_bin }}
  raw curl_bin            {{ curl_bin }}

[proxy-need-stop-start]
recipe = slapos.cookbook:cron.d
cron-entries = ${cron:cron-entries}
name = proxysql-need-stop-start
frequency = * * * * *
command = ${template-proxysql-need-stop-start:output}

#############################
#
# Deploy replication-manager instance
#
#############################
[slap_connection]
# Kept for backward compatibility
computer_id = ${slap-connection:computer-id}
partition_id = ${slap-connection:partition-id}
server_url = ${slap-connection:server-url}
software_release_url = ${slap-connection:software-release-url}
key_file = ${slap-connection:key-file}
cert_file = ${slap-connection:cert-file}

[buildout]
extends =
  {{ template_monitor }}
parts =
  replication-manager
  monitor-base
  logrotate-entry-nginx
  nginx-launcher
  publish-connection-parameter
  repman-frontend-promise
  repman-backend-promise
  proxy-need-stop-start

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

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