]> git.openstreetmap.org Git - chef.git/blob - cookbooks/nominatim/recipes/default.rb
nominatim: switch to new postcode file format
[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   python3-icu
175   php-pgsql
176   php-intl
177   php-symfony-dotenv
178 ]
179
180 source_directory = "#{basedir}/nominatim"
181 build_directory = "#{basedir}/bin"
182 ui_directory = "#{basedir}/ui"
183
184 directory build_directory do
185   owner "nominatim"
186   group "nominatim"
187   mode "755"
188   recursive true
189 end
190
191 # Normally syncing via chef is a bad idea because syncing might involve
192 # an update of database functions which should not be done while an update
193 # is ongoing. Therefore we sync in between update cycles. There is an
194 # exception for slaves: they get DB function updates from the master, so
195 # only the source code needs to be updated, which chef may do.
196 git source_directory do
197   action node[:nominatim][:state] == "slave" ? :sync : :checkout
198   repository node[:nominatim][:repository]
199   revision node[:nominatim][:revision]
200   enable_submodules true
201   user "nominatim"
202   group "nominatim"
203   not_if { node[:nominatim][:state] != "slave" && File.exist?("#{source_directory}/README.md") }
204   notifies :run, "execute[compile_nominatim]"
205 end
206
207 remote_file "#{source_directory}/data/country_osm_grid.sql.gz" do
208   action :create_if_missing
209   source "https://www.nominatim.org/data/country_grid.sql.gz"
210   owner "nominatim"
211   group "nominatim"
212   mode "644"
213 end
214
215 execute "compile_nominatim" do
216   action :nothing
217   user "nominatim"
218   cwd build_directory
219   command "cmake #{source_directory} && make"
220 end
221
222 template "#{source_directory}/.git/hooks/post-merge" do
223   source "git-post-merge-hook.erb"
224   owner "nominatim"
225   group "nominatim"
226   mode "755"
227   variables :srcdir => source_directory,
228             :builddir => build_directory,
229             :dbname => node[:nominatim][:dbname]
230 end
231
232 template "#{build_directory}/.env" do
233   source "nominatim.env.erb"
234   owner "nominatim"
235   group "nominatim"
236   mode "664"
237   variables :base_url => node[:nominatim][:state] == "off" ? node[:fqdn] : "nominatim.openstreetmap.org",
238             :dbname => node[:nominatim][:dbname],
239             :flatnode_file => node[:nominatim][:flatnode_file],
240             :log_file => "#{node[:nominatim][:logdir]}/query.log"
241 end
242
243 git ui_directory do
244   action :sync
245   repository node[:nominatim][:ui_repository]
246   revision node[:nominatim][:ui_revision]
247   user "nominatim"
248   group "nominatim"
249 end
250
251 template "#{ui_directory}/dist/theme/config.theme.js" do
252   source "ui-config.js.erb"
253   owner "nominatim"
254   group "nominatim"
255   mode "664"
256 end
257
258 if node[:nominatim][:flatnode_file]
259   directory File.dirname(node[:nominatim][:flatnode_file]) do
260     recursive true
261   end
262 end
263
264 template "/etc/logrotate.d/nominatim" do
265   source "logrotate.nominatim.erb"
266   owner "root"
267   group "root"
268   mode "644"
269 end
270
271 external_data = [
272   "wikimedia-importance.sql.gz",
273   "gb_postcodes.csv.gz",
274   "us_postcodes.csv.gz"
275 ]
276
277 external_data.each do |fname|
278   remote_file "#{build_directory}/#{fname}" do
279     action :create_if_missing
280     source "https://www.nominatim.org/data/#{fname}"
281     owner "nominatim"
282     group "nominatim"
283     mode "644"
284   end
285 end
286
287 if node[:nominatim][:state] == "off"
288   cron_d "nominatim-backup" do
289     action :delete
290   end
291
292   cron_d "nominatim-vacuum-db" do
293     action :delete
294   end
295
296   cron_d "nominatim-clean-db" do
297     action :delete
298   end
299
300   cron_d "nominatim-update-maintenance-trigger" do
301     action :delete
302   end
303 else
304   cron_d "nominatim-backup" do
305     action node[:nominatim][:enable_backup] ? :create : :delete
306     minute "0"
307     hour "3"
308     day "1"
309     user "nominatim"
310     command "/usr/local/bin/backup-nominatim"
311     mailto email_errors
312   end
313
314   cron_d "nominatim-vacuum-db" do
315     minute "20"
316     hour "0"
317     user "postgres"
318     command "/usr/local/bin/vacuum-db-nominatim"
319     mailto email_errors
320   end
321
322   cron_d "nominatim-clean-db" do
323     action node[:nominatim][:state] == "master" ? :create : :delete
324     minute "5"
325     hour "*/4"
326     user "postgres"
327     command "/usr/local/bin/clean-db-nominatim"
328     mailto email_errors
329   end
330
331   cron_d "nominatim-update-maintenance-trigger" do
332     minute "18"
333     hour "1"
334     user "nominatim"
335     command "touch #{basedir}/status/update_maintenance"
336     mailto email_errors
337   end
338 end
339
340 template "#{source_directory}/utils/nominatim-update" do
341   source "updater.erb"
342   user "nominatim"
343   group "nominatim"
344   mode "755"
345   variables :bindir => build_directory,
346             :srcdir => source_directory,
347             :logfile => "#{node[:nominatim][:logdir]}/update.log",
348             :branch => node[:nominatim][:revision],
349             :update_stop_file => "#{basedir}/status/updates_disabled",
350             :update_maintenance_trigger => "#{basedir}/status/update_maintenance"
351 end
352
353 template "/etc/init.d/nominatim-update" do
354   source "updater.init.erb"
355   user "nominatim"
356   group "nominatim"
357   mode "755"
358   variables :source_directory => source_directory
359 end
360
361 %w[backup-nominatim vacuum-db-nominatim].each do |fname|
362   template "/usr/local/bin/#{fname}" do
363     source "#{fname}.erb"
364     owner "root"
365     group "root"
366     mode "755"
367     variables :db => node[:nominatim][:dbname]
368   end
369 end
370
371 ## webserver frontend
372
373 directory "#{basedir}/etc" do
374   owner "nominatim"
375   group "adm"
376   mode "775"
377 end
378
379 %w[user_agent referrer email generic].each do |name|
380   file "#{basedir}/etc/nginx_blocked_#{name}.conf" do
381     action :create_if_missing
382     owner "nominatim"
383     group "adm"
384     mode "664"
385   end
386 end
387
388 node[:nominatim][:fpm_pools].each do |name, data|
389   php_fpm name do
390     port data[:port]
391     pm data[:pm]
392     pm_max_children data[:max_children]
393     pm_start_servers 20
394     pm_min_spare_servers 10
395     pm_max_spare_servers 20
396     pm_max_requests 10000
397     prometheus_port data[:prometheus_port]
398   end
399 end
400
401 ssl_certificate node[:fqdn] do
402   domains [node[:fqdn],
403            "nominatim.openstreetmap.org",
404            "nominatim.osm.org",
405            "nominatim.openstreetmap.com",
406            "nominatim.openstreetmap.net",
407            "nominatim.openstreetmaps.org",
408            "nominatim.openmaps.org",
409            "nominatim.qgis.org"]
410   notifies :reload, "service[nginx]"
411 end
412
413 package "apache2" do
414   action :remove
415 end
416
417 include_recipe "nginx"
418
419 nginx_site "default" do
420   action [:delete]
421 end
422
423 frontends = search(:node, "recipes:web\\:\\:frontend").sort_by(&:name)
424
425 nginx_site "nominatim" do
426   template "nginx.erb"
427   directory build_directory
428   variables :pools => node[:nominatim][:fpm_pools],
429             :frontends => frontends,
430             :confdir => "#{basedir}/etc",
431             :ui_directory => ui_directory
432 end
433
434 template "/etc/logrotate.d/nginx" do
435   source "logrotate.nginx.erb"
436   owner "root"
437   group "root"
438   mode "644"
439 end
440
441 munin_plugin_conf "nominatim" do
442   template "munin.erb"
443   variables :db => node[:nominatim][:dbname],
444             :querylog => "#{node[:nominatim][:logdir]}/query.log"
445 end
446
447 munin_plugin "nominatim_importlag" do
448   target "#{source_directory}/munin/nominatim_importlag"
449 end
450
451 munin_plugin "nominatim_query_speed" do
452   target "#{source_directory}/munin/nominatim_query_speed_querylog"
453 end
454
455 munin_plugin "nominatim_requests" do
456   target "#{source_directory}/munin/nominatim_requests_querylog"
457 end
458
459 directory "#{basedir}/status" do
460   owner "nominatim"
461   group "postgres"
462   mode "775"
463 end
464
465 include_recipe "fail2ban"
466
467 frontend_addresses = frontends.collect { |f| f.ipaddresses(:role => :external) }
468
469 fail2ban_jail "nominatim_limit_req" do
470   filter "nginx-limit-req"
471   logpath "#{node[:nominatim][:logdir]}/nominatim.openstreetmap.org-error.log"
472   ports [80, 443]
473   maxretry 20
474   ignoreips frontend_addresses.flatten.sort
475 end