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 template "#{source_directory}/.git/hooks/post-merge" do
219 source "git-post-merge-hook.erb"
223 variables :srcdir => source_directory,
224 :builddir => build_directory,
225 :dbname => node[:nominatim][:dbname]
228 template "#{build_directory}/.env" do
229 source "nominatim.env.erb"
233 variables :base_url => node[:nominatim][:state] == "off" ? node[:fqdn] : "nominatim.openstreetmap.org",
234 :dbname => node[:nominatim][:dbname],
235 :flatnode_file => node[:nominatim][:flatnode_file],
236 :log_file => "#{node[:nominatim][:logdir]}/query.log",
237 :tokenizer => node[:nominatim][:config][:tokenizer]
242 repository node[:nominatim][:ui_repository]
243 revision node[:nominatim][:ui_revision]
248 template "#{ui_directory}/dist/theme/config.theme.js" do
249 source "ui-config.js.erb"
255 if node[:nominatim][:flatnode_file]
256 directory File.dirname(node[:nominatim][:flatnode_file]) do
261 template "/etc/logrotate.d/nominatim" do
262 source "logrotate.nominatim.erb"
269 "wikimedia-importance.sql.gz",
270 "gb_postcodes.csv.gz",
271 "us_postcodes.csv.gz"
274 external_data.each do |fname|
275 remote_file "#{build_directory}/#{fname}" do
277 source "https://www.nominatim.org/data/#{fname}"
284 if node[:nominatim][:state] == "off"
285 cron_d "nominatim-backup" do
289 cron_d "nominatim-vacuum-db" do
293 cron_d "nominatim-clean-db" do
297 systemd_timer "nominatim-update-maintenance-trigger" do
301 cron_d "nominatim-backup" do
302 action node[:nominatim][:enable_backup] ? :create : :delete
307 command "/usr/local/bin/backup-nominatim"
311 cron_d "nominatim-vacuum-db" do
315 command "/usr/local/bin/vacuum-db-nominatim"
319 cron_d "nominatim-clean-db" do
320 action node[:nominatim][:state] == "master" ? :create : :delete
324 command "/usr/local/bin/clean-db-nominatim"
328 systemd_service "nominatim-update-maintenance-trigger" do
329 description "Trigger maintenance tasks for Nominatim DB"
330 exec_start "touch #{basedir}/status/update_maintenance"
334 systemd_timer "nominatim-update-maintenance-trigger" do
336 description "Schedule maintenance tasks for Nominatim DB"
337 on_calendar "*-*-* 02:03:00 UTC"
340 service "nominatim-update-maintenance-trigger" do
345 template "#{source_directory}/utils/nominatim-update" do
350 variables :bindir => build_directory,
351 :srcdir => source_directory,
352 :logfile => "#{node[:nominatim][:logdir]}/update.log",
353 :branch => node[:nominatim][:revision],
354 :update_stop_file => "#{basedir}/status/updates_disabled",
355 :update_maintenance_trigger => "#{basedir}/status/update_maintenance",
356 :qabindir => qa_bin_directory,
357 :qadatadir => qa_data_directory
360 template "/etc/init.d/nominatim-update" do
361 source "updater.init.erb"
365 variables :source_directory => source_directory
368 %w[backup-nominatim vacuum-db-nominatim].each do |fname|
369 template "/usr/local/bin/#{fname}" do
370 source "#{fname}.erb"
374 variables :db => node[:nominatim][:dbname]
378 ## webserver frontend
380 directory "#{basedir}/etc" do
386 %w[user_agent referrer email generic].each do |name|
387 file "#{basedir}/etc/nginx_blocked_#{name}.conf" do
388 action :create_if_missing
395 node[:nominatim][:fpm_pools].each do |name, data|
399 pm_max_children data[:max_children]
401 pm_min_spare_servers 10
402 pm_max_spare_servers 20
403 pm_max_requests 10000
404 prometheus_port data[:prometheus_port]
408 ssl_certificate node[:fqdn] do
409 domains [node[:fqdn],
410 "nominatim.openstreetmap.org",
412 "nominatim.openstreetmap.com",
413 "nominatim.openstreetmap.net",
414 "nominatim.openstreetmaps.org",
415 "nominatim.openmaps.org",
416 "nominatim.qgis.org"]
417 notifies :reload, "service[nginx]"
420 include_recipe "nginx"
422 nginx_site "default" do
426 frontends = search(:node, "recipes:web\\:\\:frontend").sort_by(&:name)
428 nginx_site "nominatim" do
430 directory build_directory
431 variables :pools => node[:nominatim][:fpm_pools],
432 :frontends => frontends,
433 :confdir => "#{basedir}/etc",
434 :ui_directory => ui_directory
437 template "/etc/logrotate.d/nginx" do
438 source "logrotate.nginx.erb"
444 munin_plugin_conf "nominatim" do
446 variables :db => node[:nominatim][:dbname],
447 :querylog => "#{node[:nominatim][:logdir]}/query.log"
450 munin_plugin "nominatim_importlag" do
451 target "#{source_directory}/munin/nominatim_importlag"
454 munin_plugin "nominatim_query_speed" do
455 target "#{source_directory}/munin/nominatim_query_speed_querylog"
458 munin_plugin "nominatim_requests" do
459 target "#{source_directory}/munin/nominatim_requests_querylog"
462 prometheus_exporter "nominatim" do
466 "--nominatim.query-log=#{node[:nominatim][:logdir]}/query.log",
467 "--nominatim.database-name=#{node[:nominatim][:dbname]}"
471 directory "#{basedir}/status" do
477 include_recipe "fail2ban"
479 frontend_addresses = frontends.collect { |f| f.ipaddresses(:role => :external) }
481 fail2ban_jail "nominatim_limit_req" do
482 filter "nginx-limit-req"
483 logpath "#{node[:nominatim][:logdir]}/nominatim.openstreetmap.org-error.log"
486 ignoreips frontend_addresses.flatten.sort
489 ### QA tile generation
491 if node[:nominatim][:enable_qa_tiles]
492 package "python3-geojson"
494 git qa_bin_directory do
495 repository node[:nominatim][:qa_repository]
496 revision node[:nominatim][:qa_revision]
497 enable_submodules true
500 notifies :run, "execute[compile_qa]"
503 execute "compile_qa" do
506 cwd "#{qa_bin_directory}/clustering-vt"
510 directory qa_data_directory do
517 template "#{qa_bin_directory}/analyser/config/config.yaml" do
518 source "qa_config.erb"
522 variables :outputdir => "#{qa_data_directory}/new"
525 ssl_certificate "qa-tile.nominatim.openstreetmap.org" do
526 domains ["qa-tile.nominatim.openstreetmap.org"]
527 notifies :reload, "service[nginx]"
530 nginx_site "qa-tiles.nominatim" do
531 template "nginx-qa-tiles.erb"
532 directory build_directory
533 variables :qa_data_directory => qa_data_directory