instance-balancer.cfg.in 14.5 KB
Newer Older
1
{% import "caucase" as caucase with context %}
2 3
{% set part_list = [] -%}
{% macro section(name) %}{% do part_list.append(name) %}{{ name }}{% endmacro -%}
4
{% set ssl_parameter_dict = slapparameter_dict['ssl'] -%}
5
{% set frontend_caucase_url_list = ssl_parameter_dict.get('frontend-caucase-url-list', []) -%}
6 7 8 9 10
{#
XXX: This template only supports exactly one IPv4 and (if ipv6 is used) one IPv6
per partition. No more (undefined result), no less (IndexError).
-#}
{% set ipv4 = (ipv4_set | list)[0] -%}
11 12 13 14 15
{% set apache_ip_list = [ipv4] -%}
{% if ipv6_set -%}
{%   set ipv6 = (ipv6_set | list)[0] -%}
{%   do apache_ip_list.append('[' ~ ipv6 ~ ']') -%}
{% endif -%}
16

17
[jinja2-template-base]
18
recipe = slapos.recipe.template:jinja2
19 20
mode = 644

21 22 23 24 25 26
{{ caucase.updater(
     prefix='caucase-updater',
     buildout_bin_directory=parameter_dict['bin-directory'],
     updater_path='${directory:services-on-watch}/caucase-updater',
     url=ssl_parameter_dict['caucase-url'],
     data_dir='${directory:srv}/caucase-updater',
27
     crt_path='${apache-conf-ssl:caucase-cert}',
28 29
     ca_path='${directory:srv}/caucase-updater/ca.crt',
     crl_path='${directory:srv}/caucase-updater/crl.pem',
30
     key_path='${apache-conf-ssl:caucase-key}',
31 32 33 34 35 36
     on_renew='${apache-graceful:output}',
     max_sleep=ssl_parameter_dict.get('max-crl-update-delay', 1.0),
     template_csr_pem=ssl_parameter_dict.get('csr'),
     openssl=parameter_dict['openssl'] ~ '/bin/openssl',
)}}
{% do section('caucase-updater') -%}
37
{% do section('caucase-updater-promise') -%}
38

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
{% set frontend_caucase_url_hash_list = [] -%}
{% for frontend_caucase_url in frontend_caucase_url_list -%}
{%   set hash = hashlib.md5(frontend_caucase_url).hexdigest() -%}
{%   do frontend_caucase_url_hash_list.append(hash) -%}
{%   set data_dir = '${directory:srv}/client-cert-ca/%s' % hash -%}
{{   caucase.updater(
       prefix='caucase-updater-%s' % hash,
       buildout_bin_directory=parameter_dict['bin-directory'],
       updater_path='${directory:services-on-watch}/caucase-updater-%s' % hash,
       url=frontend_caucase_url,
       data_dir=data_dir,
       ca_path='%s/ca.crt' % data_dir,
       crl_path='%s/crl.pem' % data_dir,
       on_renew='${caucase-updater-housekeeper:output}; ${apache-graceful:output}',
       max_sleep=ssl_parameter_dict.get('max-crl-update-delay', 1.0),
       openssl=parameter_dict['openssl'] ~ '/bin/openssl',
     )}}
{%   do section('caucase-updater-%s' % hash) -%}
{% endfor -%}

{% if frontend_caucase_url_hash_list -%}
[caucase-updater-housekeeper]
recipe = collective.recipe.template
output = ${directory:bin}/caucase-updater-housekeeper
mode = 700
input =
  inline:
  #!${buildout:executable}
  import glob
  import os
  import subprocess
  hash_list = {{ repr(frontend_caucase_url_hash_list) }}
  crt_list = ['%s.crt' % e for e in hash_list]
  crl_list = ['%s.crl' % e for e in hash_list]
  for path in glob.glob('${apache-conf-ssl:ca-cert-dir}/*.crt'):
    if os.path.basename(path) not in crt_list:
      os.unlink(path)
  for path in glob.glob('${apache-conf-ssl:crl-dir}/*.crl'):
    if os.path.basename(path) not in crl_list:
      os.unlink(path)
  for hash in hash_list:
    crt = '${directory:srv}/client-cert-ca/%s/ca.crt' % hash
    crt_link = '${apache-conf-ssl:ca-cert-dir}/%s.crt' % hash
    crl = '${directory:srv}/client-cert-ca/%s/crl.pem' % hash
    crl_link = '${apache-conf-ssl:crl-dir}/%s.crl' % hash
    if os.path.isfile(crt) and not os.path.islink(crt_link):
      os.symlink(crt, crt_link)
    if os.path.isfile(crl) and not os.path.islink(crl_link):
      os.symlink(crl, crl_link)
  subprocess.check_call(['{{ parameter_dict["openssl"] }}/bin/c_rehash', '${apache-conf-ssl:ca-cert-dir}'])
  subprocess.check_call(['{{ parameter_dict["openssl"] }}/bin/c_rehash', '${apache-conf-ssl:crl-dir}'])

[caucase-updater-housekeeper-run]
recipe = plone.recipe.command
command = ${caucase-updater-housekeeper:output}
update-command = ${:command}
{% endif -%}

97 98
{% set haproxy_dict = {} -%}
{% set apache_dict = {} -%}
99 100
{% set zope_virtualhost_monster_backend_dict = {} %}
{% set test_runner_url_dict = {} %} {# family_name => list of apache URLs #}
101
{% set next_port = itertools.count(slapparameter_dict['tcpv4-port']).next -%}
102 103
{% for family_name, parameter_id_list in sorted(
  slapparameter_dict['zope-family-dict'].iteritems()) -%}
104
{%   set zope_family_address_list = [] -%}
105
{%   set ssl_authentication = slapparameter_dict['ssl-authentication-dict'].get(family_name, False) -%}
106
{%   set has_webdav = [] -%}
107 108
{%   for parameter_id in parameter_id_list -%}
{%     set zope_address_list = slapparameter_dict[parameter_id] -%}
109 110 111 112
{%     for zope_address, maxconn, webdav in zope_address_list -%}
{%       if webdav -%}
{%         do has_webdav.append(None) %}
{%       endif -%}
113
{%       set zope_effective_address = zope_address -%}
114
{%       do zope_family_address_list.append((zope_effective_address, maxconn, webdav)) -%}
115
{%     endfor -%}
116

117 118 119
{#     # Generate entries with rewrite rule for test runnners #}
{%     set test_runner_address_list = slapparameter_dict.get(parameter_id ~ '-test-runner-address-list', []) %}
{%     if test_runner_address_list -%}
120 121 122
{%       set test_runner_backend_mapping = {} %}
{%       set test_runner_apache_url_list = [] %}
{%       set test_runner_external_port = next_port() %}
123
{%       for i, (test_runner_internal_ip, test_runner_internal_port) in enumerate(test_runner_address_list) %}
124 125 126 127 128 129 130 131 132 133
{%         do test_runner_backend_mapping.__setitem__(
                'unit_test_' ~ i,
                'http://' ~ test_runner_internal_ip ~ ':' ~ test_runner_internal_port ) %}
{%         do test_runner_apache_url_list.append(
                'https://' ~ ipv4 ~ ':' ~ test_runner_external_port ~ '/unit_test_' ~ i ~ '/' ) %}
{%       endfor %}
{%       do zope_virtualhost_monster_backend_dict.__setitem__(
              (ipv4, test_runner_external_port),
              ( ssl_authentication, test_runner_backend_mapping ) ) -%}
{%       do test_runner_url_dict.__setitem__(family_name, test_runner_apache_url_list) -%}
134
{%     endif -%}
135
{%   endfor -%}
136

137 138 139 140
{# Make rendering fail artificially if any family has no known backend.
 # This is useful as haproxy's hot-reconfiguration mechanism is
 # supervisord-incompatible.
 # As jinja2 postpones KeyError until place-holder value is actually used,
141
 # do a no-op getitem.
142
-#}
143
{%   do zope_family_address_list[0][0] -%}
144
{%   set haproxy_port = next_port() -%}
145
{%   set backend_path = slapparameter_dict['backend-path-dict'][family_name] -%}
146
{%   do haproxy_dict.__setitem__(family_name, (haproxy_port, zope_family_address_list)) -%}
147 148 149 150 151 152 153
{%   if has_webdav -%}
{%     set internal_scheme = 'http' -%}{# mod_rewrite does not recognise webdav scheme -#}
{%     set external_scheme = 'webdavs' -%}
{%   else %}
{%     set internal_scheme = 'http' -%}
{%     set external_scheme = 'https' -%}
{%   endif -%}
154
{%   do apache_dict.__setitem__(family_name, (next_port(), external_scheme, internal_scheme ~ '://' ~ ipv4 ~ ':' ~ haproxy_port ~ backend_path, slapparameter_dict['ssl-authentication-dict'].get(family_name, False))) -%}
155 156
{% endfor -%}

157
[haproxy-cfg-parameter-dict]
158
socket-path = ${directory:run}/haproxy.sock
159
server-check-path = {{ dumps(slapparameter_dict['haproxy-server-check-path']) }}
160
backend-dict = {{ dumps(haproxy_dict) }}
161 162 163
ip = {{ ipv4 }}

[haproxy-cfg]
164
< = jinja2-template-base
165 166 167
template = {{ parameter_dict['template-haproxy-cfg'] }}
rendered = ${directory:etc}/haproxy.cfg
context = section parameter_dict haproxy-cfg-parameter-dict
168
extensions = jinja2.ext.do
169 170 171 172 173

[{{ section('haproxy') }}]
recipe = slapos.cookbook:wrapper
wrapper-path = ${directory:services}/haproxy
command-line = "{{ parameter_dict['haproxy'] }}/sbin/haproxy" -f "${haproxy-cfg:rendered}"
174
hash-files = ${haproxy-cfg:rendered}
175

176 177 178
[apache-conf-ssl]
cert = ${directory:apache-conf}/apache.crt
key = ${directory:apache-conf}/apache.pem
179 180 181
# XXX caucase certificate is not supported by caddy for now
caucase-cert = ${directory:apache-conf}/apache-caucase.crt
caucase-key = ${directory:apache-conf}/apache-caucase.pem
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
{% if frontend_caucase_url_list -%}
depends = ${caucase-updater-housekeeper-run:recipe}
ca-cert-dir = ${directory:apache-ca-cert-dir}
crl-dir = ${directory:apache-crl-dir}
{%- endif %}

[simplefile]
< = jinja2-template-base
template = inline:{{ '{{ content }}' }}

{% macro simplefile(section_name, file_path, content, mode='') -%}
{%   set content_section_name = section_name ~ '-content' -%}
[{{  content_section_name }}]
content = {{ dumps(content) }}

[{{  section(section_name) }}]
< = simplefile
rendered = {{ file_path }}
context = key content {{content_section_name}}:content
mode = {{ mode }}
{%- endmacro %}
203

204 205 206 207 208 209 210 211 212 213 214 215 216
[apache-ssl]
{% if ssl_parameter_dict.get('key') -%}
key = ${apache-ssl-key:rendered}
cert = ${apache-ssl-cert:rendered}
{{ simplefile('apache-ssl-key', '${apache-conf-ssl:key}', ssl_parameter_dict['key']) }}
{{ simplefile('apache-ssl-cert', '${apache-conf-ssl:cert}', ssl_parameter_dict['cert']) }}
{% else %}
recipe = plone.recipe.command
command = "{{ parameter_dict['openssl'] }}/bin/openssl" req -newkey rsa -batch -new -x509 -days 3650 -nodes -keyout "${:key}" -out "${:cert}"
key = ${apache-conf-ssl:key}
cert = ${apache-conf-ssl:cert}
{%- endif %}

217
[apache-conf-parameter-dict]
218
backend-list = {{ dumps(apache_dict.values()) }}
219
zope-virtualhost-monster-backend-dict = {{ dumps(zope_virtualhost_monster_backend_dict) }}
220
ip-list = {{ dumps(apache_ip_list) }}
221
pid-file = ${directory:run}/apache.pid
222
log-dir = ${directory:log}
223 224
error-log = ${directory:log}/apache-error.log
access-log = ${directory:log}/apache-access.log
225 226 227
# Apache 2.4's default value (60 seconds) can be a bit too short
timeout = 300
# Basic SSL server configuration
228 229
cert = ${apache-ssl:cert}
key = ${apache-ssl:key}
230 231
cipher =
ssl-session-cache = ${directory:log}/apache-ssl-session-cache
232
{% if frontend_caucase_url_list -%}
233
# Client x509 auth
234 235 236
ca-cert-dir = ${apache-conf-ssl:ca-cert-dir}
crl-dir = ${apache-conf-ssl:crl-dir}
{%- endif %}
237 238

[apache-conf]
239
< = jinja2-template-base
240 241 242 243 244 245 246 247
template = {{ parameter_dict['template-apache-conf'] }}
rendered = ${directory:apache-conf}/apache.conf
context = section parameter_dict apache-conf-parameter-dict

[{{ section('apache') }}]
recipe = slapos.cookbook:wrapper
wrapper-path = ${directory:services}/apache
command-line = "{{ parameter_dict['apache'] }}/bin/httpd" -f "${apache-conf:rendered}" -DFOREGROUND
248 249 250 251 252 253
wait-for-files =
  ${apache-conf-ssl:cert}
  ${apache-conf-ssl:key}

[apache-graceful]
recipe = collective.recipe.template
254 255
output = ${directory:bin}/apache-httpd-graceful
mode = 700
256 257 258 259
input = inline:
  #!/bin/sh
  kill -USR1 "$(cat '${apache-conf-parameter-dict:pid-file}')"

260
[{{ section('apache-promise') }}]
261
<= monitor-promise-base
262
# Check any apache port in ipv4, expect other ports and ipv6 to behave consistently
263 264 265 266
module = check_port_listening
name = apache.py
config-hostname = {{ ipv4 }}
config-port = {{ apache_dict.values()[0][0] }}
267

268
[{{ section('publish') }}]
269
recipe = slapos.cookbook:publish.serialised
270
{% for family_name, (apache_port, scheme, _, _) in apache_dict.items() -%}
271 272
{{   family_name ~ '-v6' }} = {% if ipv6_set %}{{ scheme ~ '://[' ~ ipv6 ~ ']:' ~ apache_port }}{% endif %}
{{   family_name }} = {{ scheme ~ '://' ~ ipv4 ~ ':' ~ apache_port }}
273
{% endfor -%}
274 275 276 277
{% for family_name, test_runner_url_list in test_runner_url_dict.items() -%}
{{    family_name ~ '-test-runner-url-list' }} = {{ dumps(test_runner_url_list) }}
{% endfor -%}

278
monitor-base-url = ${monitor-publish-parameters:monitor-base-url}
279

280
[{{ section('logrotate-apache') }}]
281
< = logrotate-entry-base
282
name = apache
283
log = ${apache-conf-parameter-dict:error-log} ${apache-conf-parameter-dict:access-log}
284
post = test ! -s ${apache-conf-parameter-dict:pid-file} || {{ parameter_dict['bin-directory'] }}/slapos-kill --pidfile ${apache-conf-parameter-dict:pid-file} -s USR1
285 286 287 288

[directory]
recipe = slapos.cookbook:mkdirectory
apache-conf = ${:etc}/apache
289 290 291 292
{% if frontend_caucase_url_list -%}
apache-ca-cert-dir = ${:apache-conf}/ssl.crt
apache-crl-dir = ${:apache-conf}/ssl.crl
{% endif -%}
293 294 295
bin = ${buildout:directory}/bin
etc = ${buildout:directory}/etc
services = ${:etc}/run
296
services-on-watch = ${:etc}/service
297 298 299
var = ${buildout:directory}/var
run = ${:var}/run
log = ${:var}/log
300
srv = ${buildout:directory}/srv
301 302
apachedex = ${monitor-directory:private}/apachedex

303 304 305
[{{ section('resiliency-exclude-file') }}]
# Generate rdiff exclude file in case of resiliency
< = jinja2-template-base
306
template = {{ 'inline:{{ "${directory:log}/**\\n" }}' }}
307 308
rendered = ${directory:srv}/exporter.exclude

309
[{{ section('monitor-generate-apachedex-report') }}]
310 311 312 313 314 315 316 317 318
recipe = slapos.cookbook:cron.d
cron-entries = ${cron:cron-entries}
name = generate-apachedex-report
# The goal is to be executed before logrotate log rotation.
# Here, logrotate-entry-base:frequency = daily, so we run at 23 o'clock every day.
frequency = 0 23 * * *
command = ${monitor-generate-apachedex-report-wrapper:wrapper-path}

[monitor-generate-apachedex-report-wrapper]
319
recipe = slapos.cookbook:wrapper
320
wrapper-path = ${directory:bin}/${:command}
321
command-line = "{{ parameter_dict['run-apachedex-location'] }}" "{{ parameter_dict['apachedex-location'] }}" "${directory:apachedex}" ${monitor-publish-parameters:monitor-base-url}/private/apachedex --apache-log-list "${apachedex-parameters:apache-log-list}" --configuration "${apachedex-parameters:configuration}"
322
command = generate-apachedex-report
323 324 325 326 327

[apachedex-parameters]
# XXX - Sample log file with curent date: apache_access.log-%(date)s.gz
# which will be equivalent to apache_access.log-20150112.gz if the date is 2015-01-12
apache-log-list = ${apache-conf-parameter-dict:access-log}
328
configuration = {{ slapparameter_dict['apachedex-configuration'] }}
329
promise-threshold = {{ slapparameter_dict['apachedex-promise-threshold'] }}
330 331

[{{ section('monitor-promise-apachedex-result') }}]
332 333 334 335
<= monitor-promise-base
module = check_command_execute
name = check-apachedex-result.py
config-command = "{{ parameter_dict['promise-check-apachedex-result'] }}" --apachedex_path "${directory:apachedex}" --status_file ${monitor-directory:private}/apachedex.report.json --threshold "${apachedex-parameters:promise-threshold}"
336

337
[{{ section('promise-check-computer-memory') }}]
338 339 340 341
<= monitor-promise-base
module = check_command_execute
name = check-computer-memory.py
config-command = "{{ parameter_dict["check-computer-memory-binary"] }}" -db ${monitor-instance-parameter:collector-db} --threshold "{{ slapparameter_dict["computer-memory-percent-threshold"] }}" --unit percent
342

343 344
[monitor-instance-parameter]
monitor-httpd-ipv6 = {{ (ipv6_set | list)[0] }}
345
monitor-httpd-port = {{ next_port() }}
346
monitor-title = {{ slapparameter_dict['name'] }}
347
password = {{ slapparameter_dict['monitor-passwd'] }}
348

349
[buildout]
350
extends =
351
  {{ template_monitor }}
352 353
parts +=
  {{ part_list | join('\n  ') }}