5 # Copyright:: 2015, OpenStreetMap Foundation
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
11 # https://www.apache.org/licenses/LICENSE-2.0
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.
20 include_recipe "accounts"
21 include_recipe "munin"
22 include_recipe "php::fpm"
23 include_recipe "prometheus"
25 basedir = data_bag_item("accounts", "nominatim")["home"]
26 email_errors = data_bag_item("accounts", "lonvia")["email"]
35 directory node[:nominatim][:logdir] do
42 file "#{node[:nominatim][:logdir]}/query.log" do
43 action :create_if_missing
49 file "#{node[:nominatim][:logdir]}/update.log" do
50 action :create_if_missing
58 include_recipe "postgresql"
60 postgresql_version = node[:nominatim][:dbcluster].split("/").first
61 postgis_version = node[:nominatim][:postgis]
63 package "postgresql-#{postgresql_version}-postgis-#{postgis_version}"
65 node[:nominatim][:dbadmins].each do |user|
66 postgresql_user user do
67 cluster node[:nominatim][:dbcluster]
69 only_if { node[:nominatim][:state] != "slave" }
73 postgresql_user "nominatim" do
74 cluster node[:nominatim][:dbcluster]
76 only_if { node[:nominatim][:state] != "slave" }
79 postgresql_user "www-data" do
80 cluster node[:nominatim][:dbcluster]
81 only_if { node[:nominatim][:state] != "slave" }
84 postgresql_munin "nominatim" do
85 cluster node[:nominatim][:dbcluster]
86 database node[:nominatim][:dbname]
89 directory "#{basedir}/tablespaces" do
95 # NOTE: tablespaces must be exactly in the same location on each
96 # Nominatim instance when replication is in use. Therefore
97 # use symlinks to canonical directory locations.
98 node[:nominatim][:tablespaces].each do |name, location|
106 link "#{basedir}/tablespaces/#{name}" do
110 postgresql_tablespace name do
111 cluster node[:nominatim][:dbcluster]
112 location "#{basedir}/tablespaces/#{name}"
116 if node[:nominatim][:state] == "master"
117 postgresql_user "replication" do
118 cluster node[:nominatim][:dbcluster]
119 password data_bag_item("nominatim", "passwords")["replication"]
123 directory node[:rsyncd][:modules][:archive][:path] do
129 template "/usr/local/bin/clean-db-nominatim" do
130 source "clean-db-nominatim.erb"
134 variables :archive_dir => node[:rsyncd][:modules][:archive][:path],
135 :update_stop_file => "#{basedir}/status/updates_disabled",
136 :streaming_clients => search(:node, "nominatim_state:slave").map { |slave| slave[:fqdn] }.join(" ")
150 libboost-filesystem-dev
174 source_directory = "#{basedir}/nominatim"
175 build_directory = "#{basedir}/bin"
176 ui_directory = "#{basedir}/ui"
177 qa_bin_directory = "#{basedir}/Nominatim-Data-Analyser"
178 qa_data_directory = "#{basedir}/qa-data"
180 directory build_directory do
187 # Normally syncing via chef is a bad idea because syncing might involve
188 # an update of database functions which should not be done while an update
189 # is ongoing. Therefore we sync in between update cycles. There is an
190 # exception for slaves: they get DB function updates from the master, so
191 # only the source code needs to be updated, which chef may do.
192 git source_directory do
193 action node[:nominatim][:state] == "slave" ? :sync : :checkout
194 repository node[:nominatim][:repository]
195 revision node[:nominatim][:revision]
196 enable_submodules true
199 not_if { node[:nominatim][:state] != "slave" && File.exist?("#{source_directory}/README.md") }
200 notifies :run, "execute[compile_nominatim]"
203 remote_file "#{source_directory}/data/country_osm_grid.sql.gz" do
204 action :create_if_missing
205 source "https://www.nominatim.org/data/country_grid.sql.gz"
211 execute "compile_nominatim" do
215 command "cmake #{source_directory} && make"
218 link "/usr/local/bin/nominatim" do
219 to "#{build_directory}/nominatim"
222 template "#{source_directory}/.git/hooks/post-merge" do
223 source "git-post-merge-hook.erb"
227 variables :srcdir => source_directory,
228 :builddir => build_directory,
229 :dbname => node[:nominatim][:dbname]
232 template "#{build_directory}/.env" do
233 source "nominatim.env.erb"
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 :tokenizer => node[:nominatim][:config][:tokenizer]
246 repository node[:nominatim][:ui_repository]
247 revision node[:nominatim][:ui_revision]
252 template "#{ui_directory}/dist/theme/config.theme.js" do
253 source "ui-config.js.erb"
259 if node[:nominatim][:flatnode_file]
260 directory File.dirname(node[:nominatim][:flatnode_file]) do
265 template "/etc/logrotate.d/nominatim" do
266 source "logrotate.nominatim.erb"
273 "wikimedia-importance.sql.gz",
274 "gb_postcodes.csv.gz",
275 "us_postcodes.csv.gz"
278 external_data.each do |fname|
279 remote_file "#{build_directory}/#{fname}" do
281 source "https://www.nominatim.org/data/#{fname}"
288 if node[:nominatim][:state] == "off"
289 cron_d "nominatim-backup" do
293 cron_d "nominatim-vacuum-db" do
297 cron_d "nominatim-clean-db" do
301 systemd_timer "nominatim-update-maintenance-trigger" do
305 cron_d "nominatim-backup" do
306 action node[:nominatim][:enable_backup] ? :create : :delete
311 command "/usr/local/bin/backup-nominatim"
315 cron_d "nominatim-vacuum-db" do
319 command "/usr/local/bin/vacuum-db-nominatim"
323 cron_d "nominatim-clean-db" do
324 action node[:nominatim][:state] == "master" ? :create : :delete
328 command "/usr/local/bin/clean-db-nominatim"
332 systemd_service "nominatim-update-maintenance-trigger" do
333 description "Trigger maintenance tasks for Nominatim DB"
334 exec_start "touch #{basedir}/status/update_maintenance"
338 systemd_timer "nominatim-update-maintenance-trigger" do
340 description "Schedule maintenance tasks for Nominatim DB"
341 on_calendar "*-*-* 02:03:00 UTC"
344 service "nominatim-update-maintenance-trigger" do
349 template "#{source_directory}/utils/nominatim-update" do
354 variables :bindir => build_directory,
355 :srcdir => source_directory,
356 :logfile => "#{node[:nominatim][:logdir]}/update.log",
357 :branch => node[:nominatim][:revision],
358 :update_stop_file => "#{basedir}/status/updates_disabled",
359 :update_maintenance_trigger => "#{basedir}/status/update_maintenance",
360 :qabindir => qa_bin_directory,
361 :qadatadir => qa_data_directory
364 template "/etc/init.d/nominatim-update" do
365 source "updater.init.erb"
369 variables :source_directory => source_directory
372 %w[backup-nominatim vacuum-db-nominatim].each do |fname|
373 template "/usr/local/bin/#{fname}" do
374 source "#{fname}.erb"
378 variables :db => node[:nominatim][:dbname]
382 ## webserver frontend
384 directory "#{basedir}/etc" do
390 %w[user_agent referrer email generic].each do |name|
391 file "#{basedir}/etc/nginx_blocked_#{name}.conf" do
392 action :create_if_missing
399 node[:nominatim][:fpm_pools].each do |name, data|
403 pm_max_children data[:max_children]
405 pm_min_spare_servers 10
406 pm_max_spare_servers 20
407 pm_max_requests 10000
408 prometheus_port data[:prometheus_port]
412 ssl_certificate node[:fqdn] do
413 domains [node[:fqdn],
414 "nominatim.openstreetmap.org",
416 "nominatim.openstreetmap.com",
417 "nominatim.openstreetmap.net",
418 "nominatim.openstreetmaps.org",
419 "nominatim.openmaps.org",
420 "nominatim.qgis.org"]
421 notifies :reload, "service[nginx]"
424 include_recipe "nginx"
426 nginx_site "default" do
430 frontends = search(:node, "recipes:web\\:\\:frontend").sort_by(&:name)
432 nginx_site "nominatim" do
434 directory build_directory
435 variables :pools => node[:nominatim][:fpm_pools],
436 :frontends => frontends,
437 :confdir => "#{basedir}/etc",
438 :ui_directory => ui_directory
441 template "/etc/logrotate.d/nginx" do
442 source "logrotate.nginx.erb"
448 munin_plugin_conf "nominatim" do
450 variables :db => node[:nominatim][:dbname],
451 :querylog => "#{node[:nominatim][:logdir]}/query.log"
454 munin_plugin "nominatim_importlag" do
455 target "#{source_directory}/munin/nominatim_importlag"
458 munin_plugin "nominatim_query_speed" do
459 target "#{source_directory}/munin/nominatim_query_speed_querylog"
462 munin_plugin "nominatim_requests" do
463 target "#{source_directory}/munin/nominatim_requests_querylog"
466 package "ruby-webrick"
468 prometheus_exporter "nominatim" do
472 "--nominatim.query-log=#{node[:nominatim][:logdir]}/query.log",
473 "--nominatim.database-name=#{node[:nominatim][:dbname]}"
477 directory "#{basedir}/status" do
483 include_recipe "fail2ban"
485 frontend_addresses = frontends.collect { |f| f.ipaddresses(:role => :external) }
487 fail2ban_jail "nominatim_limit_req" do
488 filter "nginx-limit-req"
489 logpath "#{node[:nominatim][:logdir]}/nominatim.openstreetmap.org-error.log"
492 ignoreips frontend_addresses.flatten.sort
495 ### QA tile generation
497 if node[:nominatim][:enable_qa_tiles]
498 package "python3-geojson"
500 git qa_bin_directory do
501 repository node[:nominatim][:qa_repository]
502 revision node[:nominatim][:qa_revision]
503 enable_submodules true
506 notifies :run, "execute[compile_qa]"
509 execute "compile_qa" do
512 cwd "#{qa_bin_directory}/clustering-vt"
516 directory qa_data_directory do
523 template "#{qa_bin_directory}/analyser/config/config.yaml" do
524 source "qa_config.erb"
528 variables :outputdir => "#{qa_data_directory}/new"
531 ssl_certificate "qa-tile.nominatim.openstreetmap.org" do
532 domains ["qa-tile.nominatim.openstreetmap.org"]
533 notifies :reload, "service[nginx]"
536 nginx_site "qa-tiles.nominatim" do
537 template "nginx-qa-tiles.erb"
538 directory build_directory
539 variables :qa_data_directory => qa_data_directory