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 ## Log directory setup
37 directory node[:nominatim][:logdir] do
44 file "#{node[:nominatim][:logdir]}/query.log" do
45 action :create_if_missing
51 file "#{node[:nominatim][:logdir]}/update.log" do
52 action :create_if_missing
60 include_recipe "postgresql"
62 postgresql_version = node[:nominatim][:dbcluster].split("/").first
63 postgis_version = node[:nominatim][:postgis]
65 package "postgresql-#{postgresql_version}-postgis-#{postgis_version}"
67 node[:nominatim][:dbadmins].each do |user|
68 postgresql_user user do
69 cluster node[:nominatim][:dbcluster]
71 only_if { node[:nominatim][:state] != "slave" }
75 postgresql_user "nominatim" do
76 cluster node[:nominatim][:dbcluster]
78 only_if { node[:nominatim][:state] != "slave" }
81 postgresql_user "www-data" do
82 cluster node[:nominatim][:dbcluster]
83 only_if { node[:nominatim][:state] != "slave" }
86 postgresql_munin "nominatim" do
87 cluster node[:nominatim][:dbcluster]
88 database node[:nominatim][:dbname]
91 directory "#{basedir}/tablespaces" do
97 # NOTE: tablespaces must be exactly in the same location on each
98 # Nominatim instance when replication is in use. Therefore
99 # use symlinks to canonical directory locations.
100 node[:nominatim][:tablespaces].each do |name, location|
101 directory location do
108 link "#{basedir}/tablespaces/#{name}" do
112 postgresql_tablespace name do
113 cluster node[:nominatim][:dbcluster]
114 location "#{basedir}/tablespaces/#{name}"
128 libboost-filesystem-dev
152 source_directory = "#{basedir}/src/nominatim"
153 build_directory = "#{basedir}/src/build"
154 project_directory = "#{basedir}/planet-project"
155 bin_directory = "#{basedir}/bin"
156 cfg_directory = "#{basedir}/etc"
157 ui_directory = "#{basedir}/ui"
158 qa_bin_directory = "#{basedir}/src/Nominatim-Data-Analyser"
159 qa_data_directory = "#{basedir}/qa-data"
161 [basedir, "#{basedir}/src", cfg_directory, bin_directory, build_directory, project_directory].each do |path|
170 directory "#{bin_directory}/maintenance" do
176 if node[:nominatim][:flatnode_file]
177 directory File.dirname(node[:nominatim][:flatnode_file]) do
182 # Normally syncing via chef is a bad idea because syncing might involve
183 # an update of database functions which should not be done while an update
184 # is ongoing. Therefore we sync in between update cycles. There is an
185 # exception for slaves: they get DB function updates from the master, so
186 # only the source code needs to be updated, which chef may do.
187 git source_directory do
188 action node[:nominatim][:state] == "slave" ? :sync : :checkout
189 repository node[:nominatim][:repository]
190 revision node[:nominatim][:revision]
191 enable_submodules true
194 not_if { node[:nominatim][:state] != "slave" && File.exist?("#{source_directory}/README.md") }
195 notifies :run, "execute[compile_nominatim]"
198 remote_file "#{source_directory}/data/country_osm_grid.sql.gz" do
199 action :create_if_missing
200 source "https://www.nominatim.org/data/country_grid.sql.gz"
206 execute "compile_nominatim" do
210 command "cmake #{source_directory} && make"
211 notifies :run, "execute[install_nominatim]"
214 execute "install_nominatim" do
217 command "make install"
222 template "#{project_directory}/.env" do
223 source "nominatim.env.erb"
227 variables :base_url => node[:nominatim][:state] == "off" ? node[:fqdn] : "nominatim.openstreetmap.org",
228 :dbname => node[:nominatim][:dbname],
229 :flatnode_file => node[:nominatim][:flatnode_file],
230 :log_file => "#{node[:nominatim][:logdir]}/query.log",
231 :tokenizer => node[:nominatim][:config][:tokenizer],
232 :forward_dependencies => node[:nominatim][:config][:forward_dependencies]
235 remote_file "#{project_directory}/wikimedia-importance.sql.gz" do
236 action :create_if_missing
237 source "https://www.nominatim.org/data/wikimedia-importance.sql.gz"
243 %w[gb_postcodes.csv.gz us_postcodes.csv.gz].each do |fname|
244 remote_file "#{project_directory}/#{fname}" do
246 source "https://www.nominatim.org/data/#{fname}"
253 # Webserver + frontend
255 %w[user_agent referrer email generic].each do |name|
256 file "#{cfg_directory}/nginx_blocked_#{name}.conf" do
257 action :create_if_missing
264 node[:nominatim][:fpm_pools].each do |name, data|
268 pm_max_children data[:max_children]
270 pm_min_spare_servers 10
271 pm_max_spare_servers 20
272 pm_max_requests 10000
273 prometheus_port data[:prometheus_port]
277 ssl_certificate node[:fqdn] do
278 domains [node[:fqdn],
279 "nominatim.openstreetmap.org",
281 "nominatim.openstreetmap.com",
282 "nominatim.openstreetmap.net",
283 "nominatim.openstreetmaps.org",
284 "nominatim.openmaps.org",
285 "nominatim.qgis.org"]
286 notifies :reload, "service[nginx]"
289 include_recipe "nginx"
291 nginx_site "default" do
295 frontends = search(:node, "recipes:web\\:\\:frontend").sort_by(&:name)
297 nginx_site "nominatim" do
299 directory project_directory
300 variables :pools => node[:nominatim][:fpm_pools],
301 :frontends => frontends,
302 :confdir => "#{basedir}/etc",
303 :ui_directory => ui_directory
306 template "/etc/logrotate.d/nginx" do
307 source "logrotate.nginx.erb"
316 nominatim-update-source
317 nominatim-update-refresh-db
318 nominatim-update-data
319 nominatim-daily-maintenance].each do |fname|
320 template "#{bin_directory}/#{fname}" do
321 source "#{fname}.erb"
325 variables :bindir => bin_directory,
326 :srcdir => source_directory,
327 :builddir => build_directory,
328 :projectdir => project_directory,
329 :qabindir => qa_bin_directory,
330 :qadatadir => qa_data_directory
334 systemd_service "nominatim-update" do
335 description "Update the Nominatim database"
336 exec_start "#{bin_directory}/nominatim-update"
338 standard_output "append:#{node[:nominatim][:logdir]}/update.log"
339 standard_error "inherit"
340 working_directory project_directory
343 systemd_service "nominatim-update-maintenance-trigger" do
344 description "Trigger daily maintenance tasks for Nominatim DB"
345 exec_start "ln -sf #{bin_directory}/nominatim-daily-maintenance #{bin_directory}/maintenance/"
349 systemd_timer "nominatim-update-maintenance-trigger" do
350 action node[:nominatim][:state] != "off" ? :create : :delete
351 description "Schedule daily maintenance tasks for Nominatim DB"
352 on_calendar "*-*-* 02:03:00 UTC"
355 service "nominatim-update-maintenance-trigger" do
356 action node[:nominatim][:state] != "off" ? :enable : :disable
363 repository node[:nominatim][:ui_repository]
364 revision node[:nominatim][:ui_revision]
369 template "#{ui_directory}/dist/theme/config.theme.js" do
370 source "ui-config.js.erb"
378 if node[:nominatim][:enable_qa_tiles]
379 package "python3-geojson"
381 git qa_bin_directory do
382 repository node[:nominatim][:qa_repository]
383 revision node[:nominatim][:qa_revision]
384 enable_submodules true
387 notifies :run, "execute[compile_qa]"
390 execute "compile_qa" do
393 cwd "#{qa_bin_directory}/clustering-vt"
397 directory qa_data_directory do
404 template "#{qa_bin_directory}/analyser/config/config.yaml" do
405 source "qa_config.erb"
409 variables :outputdir => "#{qa_data_directory}/new"
412 ssl_certificate "qa-tile.nominatim.openstreetmap.org" do
413 domains ["qa-tile.nominatim.openstreetmap.org"]
414 notifies :reload, "service[nginx]"
417 nginx_site "qa-tiles.nominatim" do
418 template "nginx-qa-tiles.erb"
419 directory build_directory
420 variables :qa_data_directory => qa_data_directory
427 cron_d "nominatim-clean-db" do
428 action node[:nominatim][:state] == "master" ? :create : :delete
432 command "#{bin_directory}/clean-db-nominatim"
436 if node[:nominatim][:state] == "master"
437 postgresql_user "replication" do
438 cluster node[:nominatim][:dbcluster]
439 password data_bag_item("nominatim", "passwords")["replication"]
443 directory node[:rsyncd][:modules][:archive][:path] do
449 template "#{bin_directory}/clean-db-nominatim" do
450 source "clean-db-nominatim.erb"
454 variables :archive_dir => node[:rsyncd][:modules][:archive][:path],
455 :update_stop_file => "#{basedir}/status/updates_disabled",
456 :streaming_clients => search(:node, "nominatim_state:slave").map { |slave| slave[:fqdn] }.join(" ")
462 cron_d "nominatim-backup" do
463 action (node[:nominatim][:enable_backup] && node[:nominatim][:state] != "off") ? :create : :delete
468 command "#{bin_directory}/backup-nominatim"
472 cron_d "nominatim-vacuum-db" do
473 action node[:nominatim][:state] != "off" ? :create : :delete
477 command "#{bin_directory}/vacuum-db-nominatim"
481 %w[backup-nominatim vacuum-db-nominatim].each do |fname|
482 template "#{bin_directory}/#{fname}" do
483 source "#{fname}.erb"
487 variables :db => node[:nominatim][:dbname]
493 template "/etc/logrotate.d/nominatim" do
494 source "logrotate.nominatim.erb"
502 munin_plugin_conf "nominatim" do
504 variables :db => node[:nominatim][:dbname],
505 :querylog => "#{node[:nominatim][:logdir]}/query.log"
508 munin_plugin "nominatim_importlag" do
509 target "#{source_directory}/munin/nominatim_importlag"
512 munin_plugin "nominatim_query_speed" do
513 target "#{source_directory}/munin/nominatim_query_speed_querylog"
516 munin_plugin "nominatim_requests" do
517 target "#{source_directory}/munin/nominatim_requests_querylog"
520 prometheus_exporter "nominatim" do
523 restrict_address_families "AF_UNIX"
525 "--nominatim.query-log=#{node[:nominatim][:logdir]}/query.log",
526 "--nominatim.database-name=#{node[:nominatim][:dbname]}"
530 include_recipe "fail2ban"
532 frontend_addresses = frontends.collect { |f| f.ipaddresses(:role => :external) }
534 fail2ban_jail "nominatim_limit_req" do
535 filter "nginx-limit-req"
536 logpath "#{node[:nominatim][:logdir]}/nominatim.openstreetmap.org-error.log"
539 ignoreips frontend_addresses.flatten.sort