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
171 source_directory = "#{basedir}/nominatim"
172 build_directory = "#{basedir}/bin"
173 ui_directory = "#{basedir}/ui"
174 qa_bin_directory = "#{basedir}/Nominatim-Data-Analyser"
175 qa_data_directory = "#{basedir}/qa-data"
177 directory build_directory do
184 # Normally syncing via chef is a bad idea because syncing might involve
185 # an update of database functions which should not be done while an update
186 # is ongoing. Therefore we sync in between update cycles. There is an
187 # exception for slaves: they get DB function updates from the master, so
188 # only the source code needs to be updated, which chef may do.
189 git source_directory do
190 action node[:nominatim][:state] == "slave" ? :sync : :checkout
191 repository node[:nominatim][:repository]
192 revision node[:nominatim][:revision]
193 enable_submodules true
196 not_if { node[:nominatim][:state] != "slave" && File.exist?("#{source_directory}/README.md") }
197 notifies :run, "execute[compile_nominatim]"
200 remote_file "#{source_directory}/data/country_osm_grid.sql.gz" do
201 action :create_if_missing
202 source "https://www.nominatim.org/data/country_grid.sql.gz"
208 execute "compile_nominatim" do
212 command "cmake #{source_directory} && make"
215 template "#{source_directory}/.git/hooks/post-merge" do
216 source "git-post-merge-hook.erb"
220 variables :srcdir => source_directory,
221 :builddir => build_directory,
222 :dbname => node[:nominatim][:dbname]
225 template "#{build_directory}/.env" do
226 source "nominatim.env.erb"
230 variables :base_url => node[:nominatim][:state] == "off" ? node[:fqdn] : "nominatim.openstreetmap.org",
231 :dbname => node[:nominatim][:dbname],
232 :flatnode_file => node[:nominatim][:flatnode_file],
233 :log_file => "#{node[:nominatim][:logdir]}/query.log",
234 :tokenizer => node[:nominatim][:config][:tokenizer]
239 repository node[:nominatim][:ui_repository]
240 revision node[:nominatim][:ui_revision]
245 template "#{ui_directory}/dist/theme/config.theme.js" do
246 source "ui-config.js.erb"
252 if node[:nominatim][:flatnode_file]
253 directory File.dirname(node[:nominatim][:flatnode_file]) do
258 template "/etc/logrotate.d/nominatim" do
259 source "logrotate.nominatim.erb"
266 "wikimedia-importance.sql.gz",
267 "gb_postcodes.csv.gz",
268 "us_postcodes.csv.gz"
271 external_data.each do |fname|
272 remote_file "#{build_directory}/#{fname}" do
274 source "https://www.nominatim.org/data/#{fname}"
281 if node[:nominatim][:state] == "off"
282 cron_d "nominatim-backup" do
286 cron_d "nominatim-vacuum-db" do
290 cron_d "nominatim-clean-db" do
294 systemd_timer "nominatim-update-maintenance-trigger" do
298 cron_d "nominatim-backup" do
299 action node[:nominatim][:enable_backup] ? :create : :delete
304 command "/usr/local/bin/backup-nominatim"
308 cron_d "nominatim-vacuum-db" do
312 command "/usr/local/bin/vacuum-db-nominatim"
316 cron_d "nominatim-clean-db" do
317 action node[:nominatim][:state] == "master" ? :create : :delete
321 command "/usr/local/bin/clean-db-nominatim"
325 systemd_service "nominatim-update-maintenance-trigger" do
326 description "Trigger maintenance tasks for Nominatim DB"
327 exec_start "touch #{basedir}/status/update_maintenance"
331 systemd_timer "nominatim-update-maintenance-trigger" do
333 description "Schedule maintenance tasks for Nominatim DB"
334 on_calendar "*-*-* 02:03:00 UTC"
337 service "nominatim-update-maintenance-trigger" do
342 template "#{source_directory}/utils/nominatim-update" do
347 variables :bindir => build_directory,
348 :srcdir => source_directory,
349 :logfile => "#{node[:nominatim][:logdir]}/update.log",
350 :branch => node[:nominatim][:revision],
351 :update_stop_file => "#{basedir}/status/updates_disabled",
352 :update_maintenance_trigger => "#{basedir}/status/update_maintenance",
353 :qabindir => qa_bin_directory,
354 :qadatadir => qa_data_directory
357 template "/etc/init.d/nominatim-update" do
358 source "updater.init.erb"
362 variables :source_directory => source_directory
365 %w[backup-nominatim vacuum-db-nominatim].each do |fname|
366 template "/usr/local/bin/#{fname}" do
367 source "#{fname}.erb"
371 variables :db => node[:nominatim][:dbname]
375 ## webserver frontend
377 directory "#{basedir}/etc" do
383 %w[user_agent referrer email generic].each do |name|
384 file "#{basedir}/etc/nginx_blocked_#{name}.conf" do
385 action :create_if_missing
392 node[:nominatim][:fpm_pools].each do |name, data|
396 pm_max_children data[:max_children]
398 pm_min_spare_servers 10
399 pm_max_spare_servers 20
400 pm_max_requests 10000
401 prometheus_port data[:prometheus_port]
405 ssl_certificate node[:fqdn] do
406 domains [node[:fqdn],
407 "nominatim.openstreetmap.org",
409 "nominatim.openstreetmap.com",
410 "nominatim.openstreetmap.net",
411 "nominatim.openstreetmaps.org",
412 "nominatim.openmaps.org",
413 "nominatim.qgis.org"]
414 notifies :reload, "service[nginx]"
417 include_recipe "nginx"
419 nginx_site "default" do
423 frontends = search(:node, "recipes:web\\:\\:frontend").sort_by(&:name)
425 nginx_site "nominatim" do
427 directory build_directory
428 variables :pools => node[:nominatim][:fpm_pools],
429 :frontends => frontends,
430 :confdir => "#{basedir}/etc",
431 :ui_directory => ui_directory
434 template "/etc/logrotate.d/nginx" do
435 source "logrotate.nginx.erb"
441 munin_plugin_conf "nominatim" do
443 variables :db => node[:nominatim][:dbname],
444 :querylog => "#{node[:nominatim][:logdir]}/query.log"
447 munin_plugin "nominatim_importlag" do
448 target "#{source_directory}/munin/nominatim_importlag"
451 munin_plugin "nominatim_query_speed" do
452 target "#{source_directory}/munin/nominatim_query_speed_querylog"
455 munin_plugin "nominatim_requests" do
456 target "#{source_directory}/munin/nominatim_requests_querylog"
459 prometheus_exporter "nominatim" do
463 "--nominatim.query-log=#{node[:nominatim][:logdir]}/query.log",
464 "--nominatim.database-name=#{node[:nominatim][:dbname]}"
468 directory "#{basedir}/status" do
474 include_recipe "fail2ban"
476 frontend_addresses = frontends.collect { |f| f.ipaddresses(:role => :external) }
478 fail2ban_jail "nominatim_limit_req" do
479 filter "nginx-limit-req"
480 logpath "#{node[:nominatim][:logdir]}/nominatim.openstreetmap.org-error.log"
483 ignoreips frontend_addresses.flatten.sort
486 ### QA tile generation
488 if node[:nominatim][:enable_qa_tiles]
489 git qa_bin_directory do
490 repository node[:nominatim][:qa_repository]
491 revision node[:nominatim][:qa_revision]
492 enable_submodules true
495 notifies :run, "execute[compile_qa]"
498 execute "compile_qa" do
501 cwd "#{qa_bin_directory}/clustering-vt"
505 directory qa_data_directory do
512 template "#{qa_bin_directory}/analyser/config/config.yaml" do
513 source "qa_config.erb"
517 variables :outputdir => "#{qa_data_directory}/new"
520 link "#{build_directory}/website/qa-data" do
521 to "#{qa_data_directory}/current"