]> git.openstreetmap.org Git - chef.git/blob - cookbooks/nominatim/recipes/default.rb
Merge branch 'patch-2' of https://github.com/Tigerfell/chef into pr257
[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 include_recipe "accounts"
21 include_recipe "munin"
22 include_recipe "php::fpm"
23
24 basedir = data_bag_item("accounts", "nominatim")["home"]
25 email_errors = data_bag_item("accounts", "lonvia")["email"]
26
27 directory basedir do
28   owner "nominatim"
29   group "nominatim"
30   mode "755"
31   recursive true
32 end
33
34 directory node[:nominatim][:logdir] do
35   owner "nominatim"
36   group "nominatim"
37   mode "755"
38   recursive true
39 end
40
41 file "#{node[:nominatim][:logdir]}/query.log" do
42   action :create_if_missing
43   owner "www-data"
44   group "adm"
45   mode "664"
46 end
47
48 file "#{node[:nominatim][:logdir]}/update.log" do
49   action :create_if_missing
50   owner "nominatim"
51   group "adm"
52   mode "664"
53 end
54
55 # exception granted for a limited time so that they can set up their own server
56 firewall_rule "increase-limits-gnome-proxy" do
57   action :accept
58   family "inet"
59   source "net:8.43.85.23"
60   dest "fw"
61   proto "tcp:syn"
62   dest_ports "https"
63   rate_limit "s:10/sec:30"
64 end
65
66 ## Postgresql
67
68 include_recipe "postgresql"
69
70 postgresql_version = node[:nominatim][:dbcluster].split("/").first
71 postgis_version = node[:nominatim][:postgis]
72
73 package "postgresql-#{postgresql_version}-postgis-#{postgis_version}"
74
75 node[:nominatim][:dbadmins].each do |user|
76   postgresql_user user do
77     cluster node[:nominatim][:dbcluster]
78     superuser true
79     only_if { node[:nominatim][:state] != "slave" }
80   end
81 end
82
83 postgresql_user "nominatim" do
84   cluster node[:nominatim][:dbcluster]
85   superuser true
86   only_if { node[:nominatim][:state] != "slave" }
87 end
88
89 postgresql_user "www-data" do
90   cluster node[:nominatim][:dbcluster]
91   only_if { node[:nominatim][:state] != "slave" }
92 end
93
94 postgresql_munin "nominatim" do
95   cluster node[:nominatim][:dbcluster]
96   database node[:nominatim][:dbname]
97 end
98
99 directory "#{basedir}/tablespaces" do
100   owner "postgres"
101   group "postgres"
102   mode "700"
103 end
104
105 # NOTE: tablespaces must be exactly in the same location on each
106 #       Nominatim instance when replication is in use. Therefore
107 #       use symlinks to canonical directory locations.
108 node[:nominatim][:tablespaces].each do |name, location|
109   directory location do
110     owner "postgres"
111     group "postgres"
112     mode "700"
113     recursive true
114   end
115
116   link "#{basedir}/tablespaces/#{name}" do
117     to location
118   end
119
120   postgresql_tablespace name do
121     cluster node[:nominatim][:dbcluster]
122     location "#{basedir}/tablespaces/#{name}"
123   end
124 end
125
126 if node[:nominatim][:state] == "master"
127   postgresql_user "replication" do
128     cluster node[:nominatim][:dbcluster]
129     password data_bag_item("nominatim", "passwords")["replication"]
130     replication true
131   end
132
133   directory node[:rsyncd][:modules][:archive][:path] do
134     owner "postgres"
135     group "postgres"
136     mode "700"
137   end
138
139   template "/usr/local/bin/clean-db-nominatim" do
140     source "clean-db-nominatim.erb"
141     owner "root"
142     group "root"
143     mode "755"
144     variables :archive_dir => node[:rsyncd][:modules][:archive][:path],
145               :update_stop_file => "#{basedir}/status/updates_disabled",
146               :streaming_clients => search(:node, "nominatim_state:slave").map { |slave| slave[:fqdn] }.join(" ")
147   end
148 end
149
150 ## Nominatim backend
151
152 include_recipe "git"
153
154 package %w[
155   build-essential
156   cmake
157   g++
158   libboost-dev
159   libboost-system-dev
160   libboost-filesystem-dev
161   libexpat1-dev
162   zlib1g-dev
163   libxml2-dev
164   libbz2-dev
165   libpq-dev
166   libgeos++-dev
167   libproj-dev
168   python3-pyosmium
169   pyosmium
170   python3-psycopg2
171   python3-dotenv
172   python3-psutil
173   python3-jinja2
174   php-pgsql
175   php-intl
176   php-symfony-dotenv
177 ]
178
179 source_directory = "#{basedir}/nominatim"
180 build_directory = "#{basedir}/bin"
181 ui_directory = "#{basedir}/ui"
182
183 directory build_directory do
184   owner "nominatim"
185   group "nominatim"
186   mode "755"
187   recursive true
188 end
189
190 # Normally syncing via chef is a bad idea because syncing might involve
191 # an update of database functions which should not be done while an update
192 # is ongoing. Therefore we sync in between update cycles. There is an
193 # exception for slaves: they get DB function updates from the master, so
194 # only the source code needs to be updated, which chef may do.
195 git source_directory do
196   action node[:nominatim][:state] == "slave" ? :sync : :checkout
197   repository node[:nominatim][:repository]
198   revision node[:nominatim][:revision]
199   enable_submodules true
200   user "nominatim"
201   group "nominatim"
202   not_if { node[:nominatim][:state] != "slave" && File.exist?("#{source_directory}/README.md") }
203   notifies :run, "execute[compile_nominatim]"
204 end
205
206 remote_file "#{source_directory}/data/country_osm_grid.sql.gz" do
207   action :create_if_missing
208   source "https://www.nominatim.org/data/country_grid.sql.gz"
209   owner "nominatim"
210   group "nominatim"
211   mode "644"
212 end
213
214 execute "compile_nominatim" do
215   action :nothing
216   user "nominatim"
217   cwd build_directory
218   command "cmake #{source_directory} && make"
219 end
220
221 template "#{source_directory}/.git/hooks/post-merge" do
222   source "git-post-merge-hook.erb"
223   owner "nominatim"
224   group "nominatim"
225   mode "755"
226   variables :srcdir => source_directory,
227             :builddir => build_directory,
228             :dbname => node[:nominatim][:dbname]
229 end
230
231 template "#{build_directory}/.env" do
232   source "nominatim.env.erb"
233   owner "nominatim"
234   group "nominatim"
235   mode "664"
236   variables :base_url => node[:nominatim][:state] == "off" ? node[:fqdn] : "nominatim.openstreetmap.org",
237             :dbname => node[:nominatim][:dbname],
238             :flatnode_file => node[:nominatim][:flatnode_file],
239             :log_file => "#{node[:nominatim][:logdir]}/query.log"
240 end
241
242 git ui_directory do
243   action :sync
244   repository node[:nominatim][:ui_repository]
245   revision node[:nominatim][:ui_revision]
246   user "nominatim"
247   group "nominatim"
248 end
249
250 template "#{ui_directory}/dist/theme/config.theme.js" do
251   source "ui-config.js.erb"
252   owner "nominatim"
253   group "nominatim"
254   mode "664"
255 end
256
257 if node[:nominatim][:flatnode_file]
258   directory File.dirname(node[:nominatim][:flatnode_file]) do
259     recursive true
260   end
261 end
262
263 template "/etc/logrotate.d/nominatim" do
264   source "logrotate.nominatim.erb"
265   owner "root"
266   group "root"
267   mode "644"
268 end
269
270 external_data = [
271   "wikimedia-importance.sql.gz",
272   "gb_postcode_data.sql.gz",
273   "us_postcode_data.sql.gz"
274 ]
275
276 external_data.each do |fname|
277   remote_file "#{build_directory}/#{fname}" do
278     action :create_if_missing
279     source "https://www.nominatim.org/data/#{fname}"
280     owner "nominatim"
281     group "nominatim"
282     mode "644"
283   end
284 end
285
286 if node[:nominatim][:state] == "off"
287   cron_d "nominatim-backup" do
288     action :delete
289   end
290
291   cron_d "nominatim-vacuum-db" do
292     action :delete
293   end
294
295   cron_d "nominatim-clean-db" do
296     action :delete
297   end
298
299   cron_d "nominatim-update-maintenance-trigger" do
300     action :delete
301   end
302 else
303   cron_d "nominatim-backup" do
304     action node[:nominatim][:enable_backup] ? :create : :delete
305     minute "0"
306     hour "3"
307     day "1"
308     user "nominatim"
309     command "/usr/local/bin/backup-nominatim"
310     mailto email_errors
311   end
312
313   cron_d "nominatim-vacuum-db" do
314     minute "20"
315     hour "0"
316     user "postgres"
317     command "/usr/local/bin/vacuum-db-nominatim"
318     mailto email_errors
319   end
320
321   cron_d "nominatim-clean-db" do
322     action node[:nominatim][:state] == "master" ? :create : :delete
323     minute "5"
324     hour "*/4"
325     user "postgres"
326     command "/usr/local/bin/clean-db-nominatim"
327     mailto email_errors
328   end
329
330   cron_d "nominatim-update-maintenance-trigger" do
331     minute "18"
332     hour "1"
333     user "nominatim"
334     command "touch #{basedir}/status/update_maintenance"
335     mailto email_errors
336   end
337 end
338
339 template "#{source_directory}/utils/nominatim-update" do
340   source "updater.erb"
341   user "nominatim"
342   group "nominatim"
343   mode "755"
344   variables :bindir => build_directory,
345             :srcdir => source_directory,
346             :logfile => "#{node[:nominatim][:logdir]}/update.log",
347             :branch => node[:nominatim][:revision],
348             :update_stop_file => "#{basedir}/status/updates_disabled",
349             :update_maintenance_trigger => "#{basedir}/status/update_maintenance"
350 end
351
352 template "/etc/init.d/nominatim-update" do
353   source "updater.init.erb"
354   user "nominatim"
355   group "nominatim"
356   mode "755"
357   variables :source_directory => source_directory
358 end
359
360 %w[backup-nominatim vacuum-db-nominatim].each do |fname|
361   template "/usr/local/bin/#{fname}" do
362     source "#{fname}.erb"
363     owner "root"
364     group "root"
365     mode "755"
366     variables :db => node[:nominatim][:dbname]
367   end
368 end
369
370 ## webserver frontend
371
372 directory "#{basedir}/etc" do
373   owner "nominatim"
374   group "adm"
375   mode "775"
376 end
377
378 %w[user_agent referrer email generic].each do |name|
379   file "#{basedir}/etc/nginx_blocked_#{name}.conf" do
380     action :create_if_missing
381     owner "nominatim"
382     group "adm"
383     mode "664"
384   end
385 end
386
387 node[:nominatim][:fpm_pools].each do |name, data|
388   php_fpm name do
389     port data[:port]
390     pm data[:pm]
391     pm_max_children data[:max_children]
392     pm_start_servers 20
393     pm_min_spare_servers 10
394     pm_max_spare_servers 20
395     pm_max_requests 10000
396     prometheus_port data[:prometheus_port]
397   end
398 end
399
400 ssl_certificate node[:fqdn] do
401   domains [node[:fqdn],
402            "nominatim.openstreetmap.org",
403            "nominatim.osm.org",
404            "nominatim.openstreetmap.com",
405            "nominatim.openstreetmap.net",
406            "nominatim.openstreetmaps.org",
407            "nominatim.openmaps.org",
408            "nominatim.qgis.org"]
409   notifies :reload, "service[nginx]"
410 end
411
412 package "apache2" do
413   action :remove
414 end
415
416 include_recipe "nginx"
417
418 nginx_site "default" do
419   action [:delete]
420 end
421
422 frontends = search(:node, "recipes:web\\:\\:frontend").sort_by(&:name)
423
424 nginx_site "nominatim" do
425   template "nginx.erb"
426   directory build_directory
427   variables :pools => node[:nominatim][:fpm_pools],
428             :frontends => frontends,
429             :confdir => "#{basedir}/etc",
430             :ui_directory => ui_directory
431 end
432
433 template "/etc/logrotate.d/nginx" do
434   source "logrotate.nginx.erb"
435   owner "root"
436   group "root"
437   mode "644"
438 end
439
440 munin_plugin_conf "nominatim" do
441   template "munin.erb"
442   variables :db => node[:nominatim][:dbname],
443             :querylog => "#{node[:nominatim][:logdir]}/query.log"
444 end
445
446 munin_plugin "nominatim_importlag" do
447   target "#{source_directory}/munin/nominatim_importlag"
448 end
449
450 munin_plugin "nominatim_query_speed" do
451   target "#{source_directory}/munin/nominatim_query_speed_querylog"
452 end
453
454 munin_plugin "nominatim_requests" do
455   target "#{source_directory}/munin/nominatim_requests_querylog"
456 end
457
458 directory "#{basedir}/status" do
459   owner "nominatim"
460   group "postgres"
461   mode "775"
462 end
463
464 include_recipe "fail2ban"
465
466 frontend_addresses = frontends.collect { |f| f.ipaddresses(:role => :external) }
467
468 fail2ban_jail "nominatim_limit_req" do
469   filter "nginx-limit-req"
470   logpath "#{node[:nominatim][:logdir]}/nominatim.openstreetmap.org-error.log"
471   ports [80, 443]
472   maxretry 20
473   ignoreips frontend_addresses.flatten.sort
474 end