import json 

software_url = '{{ software_release_url }}'
# Choose a SlapOS Node
# If you are deploying SlapOS Master with Webrunner, then computer_id is 'slaprunner'
computer_id = 'local_computer'

supply(software_url, computer_id)

parameter_dict = {
  "timezone": "UTC",
  "site-id": "erp5",
  "bt5": "{{ bt5_list }}",
  "zope-partition-dict": {
    "admin": {
      "family": "admin",
      "thread-amount": 4,
      "port-base": 2220,
      "instance-count": 1
    },
    "activities-node": {
      "family": "activities",
      "thread-amount": 4,
      "instance-count": 1,
      "timerserver-interval": 1,
      "port-base": 2230
    },
    "distribution-node": {
      "family": "distribution",
      "thread-amount": 1,
      "instance-count": 1,
      "port-base": 2210,
      "timerserver-interval": 1
    },
    "web-node": {
      "family": "web",
      "thread-amount": 2,
      "instance-count": 1,
      "port-base": 2240
    },
    "service-slapos": {
      "family": "service",
      "thread-amount": 2,
      "instance-count": 1,
      "port-base": 2250,
      "ssl-authentication": True,
      "backend-path": "/%(site-id)s/portal_slap"
    }
  }
}

ca_path = '/etc/slapos-ssl' 
parameter_dict["shared-certificate-authority-path"] = ca_path 

# Choose a title
title = "instance-of-{{ playbook_name }}"

erp5_instance = request(software_url,
  title,
  filter_kw={'computer_guid': computer_id},
  software_type='default',
  partition_parameter_kw={
   '_': json.dumps(parameter_dict, sort_keys=True, indent=2),
  }
).getConnectionParameterDict()

if '_' in erp5_instance:
  backend_dict = json.loads(erp5_instance['_'])
  if 'family-admin-v6' in backend_dict:
    backend_url = str(backend_dict['family-admin-v6'])
    open('{{ backend_url_path }}', 'w').write(backend_url)
