Merge branch 'erp5-component' into erp5
[slapos.git] / software / apache-frontend / templates / apache-custom-slave-list.cfg.in
1 {% if software_type == slap_software_type -%}
2
3 {% set cached_server_dict = {} -%}
4 {% set part_list = [] -%}
5 {% set cache_access = "http://%s:%s" % (local_ipv4, cache_port) -%}
6 {% set generic_instance_parameter_dict = {'cache_access': cache_access,} -%}
7 {% set slave_log_dict = {} -%}
8 {% if extra_slave_instance_list -%}
9 {%   set slave_instance_information_list = [] -%}
10 {%   set slave_instance_list = slave_instance_list + json_module.loads(extra_slave_instance_list) -%}
11 {% endif -%}
12 [jinja2-template-base]
13 recipe = slapos.recipe.template:jinja2
14 rendered = {{ apache_configuration_directory }}/${:filename}
15 extra-context =
16 context =
17     key eggs_directory buildout:eggs-directory
18     key develop_eggs_directory buildout:develop-eggs-directory
19     ${:extra-context}
20
21 {% do logrotate_dict.pop('recipe') %}
22 [logrotate]
23 {% for key, value in logrotate_dict.iteritems() -%}
24 {{ key }} = {{ value }}
25 {% endfor %}
26
27 # Loop trhought slave list to set up slaves
28 {% for slave_instance in slave_instance_list -%}
29 {%   set slave_reference = slave_instance.get('slave_reference') -%}
30 {%   set slave_section_title = 'dynamic-template-slave-instance-%s' % slave_reference -%}
31 {%   set slave_parameter_dict = generic_instance_parameter_dict.copy() -%}
32 {%   do part_list.append(slave_section_title) -%}
33
34
35 {%   set slave_directory_section = slave_reference + "-directory" -%}
36 {%   set slave_log_folder = logrotate_dict.get('backup') + '/' + slave_reference + "-logs" -%}
37 [{{slave_directory_section}}]
38 recipe = slapos.cookbook:mkdirectory
39 log-folder = {{slave_log_folder}}
40
41 # Set Up log files
42 {%   do slave_parameter_dict.__setitem__('access_log', '/'.join([apache_log_directory, '%s_access_log' % slave_reference])) -%}
43 {%   do slave_parameter_dict.__setitem__('error_log', '/'.join([apache_log_directory, '%s_error_log' % slave_reference])) -%}
44
45 # Set slave logrotate entry
46 {%   set slave_logrotate_section = slave_reference + "-logs" -%}
47 {%   do part_list.append(slave_logrotate_section) -%}
48 [{{slave_logrotate_section}}]
49 <= logrotate
50 recipe = slapos.cookbook:logrotate.d
51 name = ${:_buildout_section_name_}
52 log = {{slave_parameter_dict.get('access_log')}} {{slave_parameter_dict.get('error_log')}}
53 backup = {{ '${' + slave_directory_section + ':log-folder}' }}
54 frequency = daily
55 rotatep-num = 30
56 post = ${buildout:bin-directory}/killpidfromfile {{ apache_configuration.get('pid-file') }} SIGUSR1
57 sharedscripts = true
58 notifempty = true
59 create = true
60
61 # integrate current logs inside
62 {%   set slave_ln_section = slave_reference + "-ln" -%}
63 {%   do part_list.append(slave_ln_section) -%}
64 [{{slave_ln_section}}]
65 recipe = plone.recipe.command
66 stop-on-error = false
67 command = ln -s {{slave_parameter_dict.get('error_log')}} {{ '${' + slave_directory_section + ':log-folder}' }}/apache-error.log && ln -s {{slave_parameter_dict.get('access_log')}} {{ '${' + slave_directory_section + ':log-folder}' }}/apache-access.log
68
69 # Set password for slave
70 {%   set slave_password_section = slave_reference + "-password" -%}
71 [{{slave_password_section}}]
72 recipe = slapos.cookbook:generate.password
73 storage-path = {{apache_configuration_directory}}/.{{slave_reference}}.passwd
74 bytes = 8
75
76 # Set up htaccess file for slave
77 {%   set slave_htaccess_section = slave_reference + '-htaccess' %}
78 {%   do part_list.append(slave_htaccess_section) -%}
79 [{{slave_htaccess_section}}]
80 recipe = plone.recipe.command
81 stop-on-error = true
82 htaccess-path = {{apache_configuration_directory}}/.{{slave_reference}}.htaccess
83 command = {{frontend_configuration.get('apache-directory')}}/bin/htpasswd -cb ${:htaccess-path} {{ slave_reference }} {{ '${' + slave_password_section + ':passwd}' }}
84
85 # Add slave log directory to the slave log access dict
86 {%   do slave_log_dict.__setitem__(slave_reference, slave_log_folder) %}
87
88 # Set up apache configuration file for slave
89 [{{ slave_section_title }}]
90 < = jinja2-template-base
91 template = {{ template_slave_configuration }}
92 filename = {{ '%s.conf' % slave_reference }}
93 extra-context =
94     key apache_custom_https {{ 'slave-instance-%s-configuration:apache_custom_https' % slave_reference }}
95     key apache_custom_http {{ 'slave-instance-%s-configuration:apache_custom_http' % slave_reference }}
96     raw https_port {{ https_port }}
97     raw http_port {{ http_port }}
98 {{ '\n' }}
99
100 # Set ssl certificates for each slave
101 {%   for cert_name in ('ssl_key', 'ssl_crt', 'ssl_ca_crt', 'ssl_csr')-%}
102 {%     if cert_name in slave_instance -%}
103 {%       set cert_title = '%s-%s' % (slave_reference, cert_name.replace('ssl_', '')) -%}
104 {%       set cert_file = '/'.join([custom_ssl_directory, cert_title.replace('-','.')]) -%}
105 {%       do part_list.append(cert_title) -%}
106 {%       do slave_parameter_dict.__setitem__(cert_name, cert_file) -%}
107 # Store certificates on fs
108 [{{ cert_title }}]
109 < = jinja2-template-base
110 template = {{ empty_template }}
111 rendered = {{ cert_file }}
112 extra-context =
113     key content {{ cert_title + '-config:value' }}
114 # Store certificate in config
115 [{{ cert_title + '-config' }}]
116 value = {{ dumps(slave_instance.get(cert_name)) }}
117 {%     endif -%}
118 {%   endfor -%}
119
120 # Set apache configuration value for slave
121 [{{ ('slave-instance-%s-configuration' % slave_reference) }}]
122 {% set apache_custom_http = ((slave_instance.get('apache_custom_http', '')) % slave_parameter_dict) -%}
123 {% set apache_custom_https = ((slave_instance.get('apache_custom_https', '')) % slave_parameter_dict) -%}
124 apache_custom_http = {{ dumps(apache_custom_http) }}
125 apache_custom_https = {{ dumps(apache_custom_https) }}
126 {{ '\n' }}
127
128 # The slave use cache
129 {%   if 'enable_cache' in slave_instance and 'url' in slave_instance and 'domain' in slave_instance -%}
130 {%     do cached_server_dict.__setitem__(slave_instance.get('domain'), slave_instance.get('url')) -%}
131 {%   endif -%}
132
133 # Publish slave information
134 {%   set slave_log_access_url = 'https://' + slave_reference + ':${'+ slave_password_section +':passwd}@[' + frontend_configuration.get('apache-ipv6') + ']:' + frontend_configuration.get('apache-https-port') + '/' + slave_reference.lower() + '/' %}
135 {%   if not extra_slave_instance_list -%}
136 {%     set publish_section_title = 'publish-%s-connection-information' % slave_instance.get('slave_reference') -%}
137 {%     do part_list.append(publish_section_title) -%}
138 [{{ publish_section_title }}]
139 recipe = slapos.cookbook:publish
140 public-ipv4 = {{ public_ipv4 }}
141 log-access = {{ slave_log_access_url }}
142 -slave-reference = {{ slave_instance.get('slave_reference') }}
143 {%   else -%}
144 {%     do slave_instance_information_list.append({'slave-reference':slave_instance.get('slave_reference'), 'public-ipv4':public_ipv4, 'log-access': slave_log_access_url}) -%}
145 {%   endif -%}
146 {% endfor -%}
147
148 [slave-log-directories]
149 {% for key, value in slave_log_dict.iteritems() -%}
150 {{ key }} = {{ value }}
151 {% endfor %}
152
153 # Define log access
154 {% set log_access_section = "apache-log-access" %}
155 {% do part_list.append(log_access_section) -%}
156 [{{log_access_section}}]
157 < = jinja2-template-base
158 template = {{frontend_configuration.get('template-log-access')}}
159 rendered = {{frontend_configuration.get('log-access-configuration')}}
160 extra-context =
161     section slave_log_directory slave-log-directories
162     raw apache_log_directory {{apache_log_directory}}
163     raw apache_configuration_directory {{apache_configuration_directory}}
164
165 # Publish information for the instance
166 {% set publish_section_title = 'publish-apache-information' -%}
167 {% do part_list.append(publish_section_title) -%}
168 [{{ publish_section_title }}]
169 recipe = slapos.cookbook:publish
170 public-ipv4 = {{ public_ipv4 }}
171 private-ipv4 = {{ local_ipv4 }}
172 {% if extra_slave_instance_list -%}
173 slave-instance-information-list = {{ json_module.dumps(slave_instance_information_list) }}
174 {% endif -%}
175
176 {% do part_list.append('cached-rewrite-rules') -%}
177 [cached-rewrite-rules]
178 < = jinja2-template-base
179 template = {{ template_rewrite_cached }}
180 rendered = {{ rewrite_cached_configuration }}
181 extra-context =
182     import json_module json
183     key server_dict rewrite-rules:rules
184
185 [rewrite-rules]
186 rules = {{ dumps(cached_server_dict) }}
187
188 [buildout]
189 parts +=
190 {% for part in part_list -%}
191 {{ '    %s' % part }}
192 {% endfor -%}
193
194 eggs-directory = {{ eggs_directory }}
195 develop-eggs-directory = {{ develop_eggs_directory }}
196 offline = true
197 cache-access = {{ cache_access }}
198
199 {% endif -%}