]> git.openstreetmap.org Git - chef.git/blob - cookbooks/nominatim/recipes/default.rb
Switch MTA-STS policy to enforce mode
[chef.git] / cookbooks / nominatim / recipes / default.rb
1 #
2 # Cookbook:: nominatim
3 # Recipe:: base
4 #
5 # Copyright:: 2015, OpenStreetMap Foundation
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #     https://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19
20 basedir = data_bag_item("accounts", "nominatim")["home"]
21 email_errors = data_bag_item("accounts", "lonvia")["email"]
22
23 directory basedir do
24   owner "nominatim"
25   group "nominatim"
26   mode 0o755
27   recursive true
28 end
29
30 directory node[:nominatim][:logdir] do
31   owner "nominatim"
32   group "nominatim"
33   mode 0o755
34   recursive true
35 end
36
37 file "#{node[:nominatim][:logdir]}/query.log" do
38   action :create_if_missing
39   owner "www-data"
40   group "adm"
41   mode 0o664
42 end
43
44 file "#{node[:nominatim][:logdir]}/update.log" do
45   action :create_if_missing
46   owner "nominatim"
47   group "adm"
48   mode 0o664
49 end
50
51 ## Postgresql
52
53 include_recipe "postgresql"
54
55 postgresql_version = node[:nominatim][:dbcluster].split("/").first
56 postgis_version = node[:nominatim][:postgis]
57
58 package "postgresql-#{postgresql_version}-postgis-#{postgis_version}"
59
60 node[:nominatim][:dbadmins].each do |user|
61   postgresql_user user do
62     cluster node[:nominatim][:dbcluster]
63     superuser true
64     only_if { node[:nominatim][:state] != "slave" }
65   end
66 end
67
68 postgresql_user "nominatim" do
69   cluster node[:nominatim][:dbcluster]
70   superuser true
71   only_if { node[:nominatim][:state] != "slave" }
72 end
73
74 postgresql_user "www-data" do
75   cluster node[:nominatim][:dbcluster]
76   only_if { node[:nominatim][:state] != "slave" }
77 end
78
79 postgresql_munin "nominatim" do
80   cluster node[:nominatim][:dbcluster]
81   database node[:nominatim][:dbname]
82 end
83
84 directory "#{basedir}/tablespaces" do
85   owner "postgres"
86   group "postgres"
87   mode 0o700
88 end
89
90 # Note: tablespaces must be exactly in the same location on each
91 #       Nominatim instance when replication is in use. Therefore
92 #       use symlinks to canonical directory locations.
93 node[:nominatim][:tablespaces].each do |name, location|
94   directory location do
95     owner "postgres"
96     group "postgres"
97     mode 0o700
98     recursive true
99   end
100
101   link "#{basedir}/tablespaces/#{name}" do
102     to location
103   end
104
105   postgresql_tablespace name do
106     cluster node[:nominatim][:dbcluster]
107     location "#{basedir}/tablespaces/#{name}"
108   end
109 end
110
111 if node[:nominatim][:state] == "master"
112   postgresql_user "replication" do
113     cluster node[:nominatim][:dbcluster]
114     password data_bag_item("nominatim", "passwords")["replication"]
115     replication true
116   end
117
118   directory node[:rsyncd][:modules][:archive][:path] do
119     owner "postgres"
120     group "postgres"
121     mode 0o700
122   end
123
124   template "/usr/local/bin/clean-db-nominatim" do
125     source "clean-db-nominatim.erb"
126     owner "root"
127     group "root"
128     mode 0o755
129     variables :archive_dir => node[:rsyncd][:modules][:archive][:path],
130               :update_stop_file => "#{basedir}/status/updates_disabled",
131               :streaming_clients => search(:node, "nominatim_state:slave").map { |slave| slave[:fqdn] }.join(" ")
132   end
133 end
134
135 ## Nominatim backend
136
137 include_recipe "git"
138
139 package %w[
140   build-essential
141   cmake
142   g++
143   libboost-dev
144   libboost-system-dev
145   libboost-filesystem-dev
146   libexpat1-dev
147   zlib1g-dev
148   libxml2-dev
149   libbz2-dev
150   libpq-dev
151   libgeos++-dev
152   libproj-dev
153   python3-pyosmium
154   pyosmium
155 ]
156
157 source_directory = "#{basedir}/nominatim"
158 build_directory = "#{basedir}/bin"
159
160 directory build_directory do
161   owner "nominatim"
162   group "nominatim"
163   mode 0o755
164   recursive true
165 end
166
167 # Normally syncing via chef is a bad idea because syncing might involve
168 # an update of database functions which should not be done while an update
169 # is ongoing. Therefore we sync in between update cycles. There is an
170 # exception for slaves: they get DB function updates from the master, so
171 # only the source code needs to be updated, which chef may do.
172 git source_directory do
173   action node[:nominatim][:state] == "slave" ? :sync : :checkout
174   repository node[:nominatim][:repository]
175   revision node[:nominatim][:revision]
176   enable_submodules true
177   user "nominatim"
178   group "nominatim"
179   not_if { node[:nominatim][:state] != "slave" && File.exist?("#{source_directory}/README.md") }
180   notifies :run, "execute[compile_nominatim]", :immediately
181 end
182
183 execute "compile_nominatim" do
184   action :nothing
185   user "nominatim"
186   cwd build_directory
187   command "cmake #{source_directory} && make"
188 end
189
190 template "#{source_directory}/.git/hooks/post-merge" do
191   source "git-post-merge-hook.erb"
192   owner "nominatim"
193   group "nominatim"
194   mode 0o755
195   variables :srcdir => source_directory,
196             :builddir => build_directory,
197             :dbname => node[:nominatim][:dbname]
198 end
199
200 template "#{build_directory}/settings/local.php" do
201   source "settings.erb"
202   owner "nominatim"
203   group "nominatim"
204   mode 0o664
205   variables :base_url => node[:nominatim][:state] == "off" ? node[:fqdn] : "nominatim.openstreetmap.org",
206             :dbname => node[:nominatim][:dbname],
207             :flatnode_file => node[:nominatim][:flatnode_file],
208             :log_file => "#{node[:nominatim][:logdir]}/query.log"
209 end
210
211 if node[:nominatim][:flatnode_file]
212   directory File.dirname(node[:nominatim][:flatnode_file]) do
213     recursive true
214   end
215 end
216
217 template "/etc/logrotate.d/nominatim" do
218   source "logrotate.nominatim.erb"
219   owner "root"
220   group "root"
221   mode 0o644
222 end
223
224 external_data = [
225   "wikimedia-importance.sql.gz",
226   "gb_postcode_data.sql.gz"
227 ]
228
229 external_data.each do |fname|
230   remote_file "#{source_directory}/data/#{fname}" do
231     action :create_if_missing
232     source "https://www.nominatim.org/data/#{fname}"
233     owner "nominatim"
234     group "nominatim"
235     mode 0o644
236   end
237 end
238
239 remote_file "#{source_directory}/data/country_osm_grid.sql.gz" do
240   action :create_if_missing
241   source "https://www.nominatim.org/data/country_grid.sql.gz"
242   owner "nominatim"
243   group "nominatim"
244   mode 0o644
245 end
246
247 template "/etc/cron.d/nominatim" do
248   action node[:nominatim][:state] == "off" ? :delete : :create
249   source "nominatim.cron.erb"
250   owner "root"
251   group "root"
252   mode "0644"
253   variables :bin_directory => "#{source_directory}/utils",
254             :mailto => email_errors,
255             :update_maintenance_trigger => "#{basedir}/status/update_maintenance"
256 end
257
258 template "#{source_directory}/utils/nominatim-update" do
259   source "updater.erb"
260   user "nominatim"
261   group "nominatim"
262   mode 0o755
263   variables :bindir => build_directory,
264             :srcdir => source_directory,
265             :logfile => "#{node[:nominatim][:logdir]}/update.log",
266             :branch => node[:nominatim][:revision],
267             :update_stop_file => "#{basedir}/status/updates_disabled",
268             :update_maintenance_trigger => "#{basedir}/status/update_maintenance"
269 end
270
271 template "/etc/init.d/nominatim-update" do
272   source "updater.init.erb"
273   user "nominatim"
274   group "nominatim"
275   mode 0o755
276   variables :source_directory => source_directory
277 end
278
279 %w[backup-nominatim vacuum-db-nominatim].each do |fname|
280   template "/usr/local/bin/#{fname}" do
281     source "#{fname}.erb"
282     owner "root"
283     group "root"
284     mode 0o755
285     variables :db => node[:nominatim][:dbname]
286   end
287 end
288
289 ## webserver frontend
290
291 template "#{build_directory}/settings/ip_blocks.conf" do
292   action :create_if_missing
293   source "ipblocks.erb"
294   owner "nominatim"
295   group "nominatim"
296   mode 0o664
297 end
298
299 file "#{build_directory}/settings/apache_blocks.conf" do
300   action :create_if_missing
301   owner "nominatim"
302   group "nominatim"
303   mode 0o664
304 end
305
306 file "#{build_directory}/settings/ip_blocks.map" do
307   action :create_if_missing
308   owner "nominatim"
309   group "nominatim"
310   mode 0o664
311 end
312
313 include_recipe "apache"
314
315 package "php"
316 package "php-fpm"
317 package "php-pgsql"
318 package "php-intl"
319
320 apache_module "rewrite"
321 apache_module "proxy"
322 apache_module "proxy_fcgi"
323 apache_module "proxy_http"
324 apache_module "headers"
325
326 service "php7.2-fpm" do
327   action [:enable, :start]
328   supports :status => true, :restart => true, :reload => true
329 end
330
331 node[:nominatim][:fpm_pools].each do |name, data|
332   template "/etc/php/7.2/fpm/pool.d/#{name}.conf" do
333     source "fpm.conf.erb"
334     owner "root"
335     group "root"
336     mode 0o644
337     variables data.merge(:name => name)
338     notifies :reload, "service[php7.2-fpm]"
339   end
340 end
341
342 systemd_service "apache-nominatim" do
343   service "apache2"
344   dropin "nominatim"
345   tasks_max 12000
346   notifies :restart, "service[apache2]"
347 end
348
349 ssl_certificate "nominatim.openstreetmap.org" do
350   domains ["nominatim.openstreetmap.org",
351            "nominatim.osm.org",
352            "nominatim.openstreetmap.com",
353            "nominatim.openstreetmap.net",
354            "nominatim.openstreetmaps.org",
355            "nominatim.openmaps.org"]
356   notifies :reload, "service[apache2]"
357 end
358
359 apache_site "nominatim.openstreetmap.org" do
360   template "apache.erb"
361   directory build_directory
362   variables :pools => node[:nominatim][:fpm_pools]
363   only_if { node[:nominatim][:state] != "off" }
364 end
365
366 apache_site "default" do
367   action [:disable]
368 end
369
370 template "/etc/logrotate.d/apache2" do
371   source "logrotate.apache.erb"
372   owner "root"
373   group "root"
374   mode 0o644
375 end
376
377 include_recipe "fail2ban"
378
379 munin_plugin_conf "nominatim" do
380   template "munin.erb"
381   variables :db => node[:nominatim][:dbname],
382             :querylog => "#{node[:nominatim][:logdir]}/query.log"
383 end
384
385 munin_plugin "nominatim_importlag" do
386   target "#{source_directory}/munin/nominatim_importlag"
387 end
388
389 munin_plugin "nominatim_query_speed" do
390   target "#{source_directory}/munin/nominatim_query_speed_querylog"
391 end
392
393 munin_plugin "nominatim_requests" do
394   target "#{source_directory}/munin/nominatim_requests_querylog"
395 end
396
397 munin_plugin "nominatim_throttled_ips" do
398   target "#{source_directory}/munin/nominatim_throttled_ips"
399 end
400
401 directory "#{basedir}/status" do
402   owner "nominatim"
403   group "postgres"
404   mode 0o775
405 end