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
153 source_directory = "#{basedir}/src/nominatim"
154 build_directory = "#{basedir}/src/build"
155 project_directory = "#{basedir}/planet-project"
156 bin_directory = "#{basedir}/bin"
157 cfg_directory = "#{basedir}/etc"
158 ui_directory = "#{basedir}/ui"
159 qa_bin_directory = "#{basedir}/src/Nominatim-Data-Analyser"
160 qa_data_directory = "#{basedir}/qa-data"
162 [basedir, "#{basedir}/src", cfg_directory, bin_directory, build_directory, project_directory].each do |path|
171 directory "#{bin_directory}/maintenance" do
177 if node[:nominatim][:flatnode_file]
178 directory File.dirname(node[:nominatim][:flatnode_file]) do
183 # Normally syncing via chef is a bad idea because syncing might involve
184 # an update of database functions which should not be done while an update
185 # is ongoing. Therefore we sync in between update cycles. There is an
186 # exception for slaves: they get DB function updates from the master, so
187 # only the source code needs to be updated, which chef may do.
188 git source_directory do
189 action node[:nominatim][:state] == "slave" ? :sync : :checkout
190 repository node[:nominatim][:repository]
191 revision node[:nominatim][:revision]
192 enable_submodules true
195 not_if { node[:nominatim][:state] != "slave" && File.exist?("#{source_directory}/README.md") }
196 notifies :run, "execute[compile_nominatim]"
199 remote_file "#{source_directory}/data/country_osm_grid.sql.gz" do
200 action :create_if_missing
201 source "https://nominatim.org/data/country_grid.sql.gz"
207 execute "compile_nominatim" do
211 command "cmake -D WITH_LUAJIT=ON #{source_directory} && make"
212 notifies :run, "execute[install_nominatim]"
215 execute "install_nominatim" do
218 command "make install"
223 template "#{project_directory}/.env" do
224 source "nominatim.env.erb"
228 variables :base_url => node[:nominatim][:state] == "off" ? node[:fqdn] : "nominatim.openstreetmap.org",
229 :dbname => node[:nominatim][:dbname],
230 :flatnode_file => node[:nominatim][:flatnode_file],
231 :log_file => "#{node[:nominatim][:logdir]}/query.log",
232 :tokenizer => node[:nominatim][:config][:tokenizer],
233 :forward_dependencies => node[:nominatim][:config][:forward_dependencies]
236 remote_file "#{project_directory}/wikimedia-importance.sql.gz" do
237 action :create_if_missing
238 source "https://nominatim.org/data/wikimedia-importance.sql.gz"
244 %w[gb_postcodes.csv.gz us_postcodes.csv.gz].each do |fname|
245 remote_file "#{project_directory}/#{fname}" do
247 source "https://nominatim.org/data/#{fname}"
254 # Webserver + frontend
256 %w[user_agent referrer email generic].each do |name|
257 file "#{cfg_directory}/nginx_blocked_#{name}.conf" do
258 action :create_if_missing
265 node[:nominatim][:fpm_pools].each do |name, data|
269 pm_max_children data[:max_children]
271 pm_min_spare_servers 10
272 pm_max_spare_servers 20
273 pm_max_requests 10000
274 prometheus_port data[:prometheus_port]
278 ssl_certificate node[:fqdn] do
279 domains [node[:fqdn],
280 "nominatim.openstreetmap.org",
282 "nominatim.openstreetmap.com",
283 "nominatim.openstreetmap.net",
284 "nominatim.openstreetmaps.org",
285 "nominatim.openmaps.org",
286 "nominatim.qgis.org"]
287 notifies :reload, "service[nginx]"
290 include_recipe "nginx"
292 nginx_site "default" do
296 frontends = search(:node, "recipes:web\\:\\:frontend").sort_by(&:name)
298 nginx_site "nominatim" do
300 directory project_directory
301 variables :pools => node[:nominatim][:fpm_pools],
302 :frontends => frontends,
303 :confdir => "#{basedir}/etc",
304 :ui_directory => ui_directory
307 template "/etc/logrotate.d/nginx" do
308 source "logrotate.nginx.erb"
317 nominatim-update-source
318 nominatim-update-refresh-db
319 nominatim-update-data
320 nominatim-daily-maintenance].each do |fname|
321 template "#{bin_directory}/#{fname}" do
322 source "#{fname}.erb"
326 variables :bindir => bin_directory,
327 :srcdir => source_directory,
328 :builddir => build_directory,
329 :projectdir => project_directory,
330 :qabindir => qa_bin_directory,
331 :qadatadir => qa_data_directory
335 systemd_service "nominatim-update" do
336 description "Update the Nominatim database"
337 exec_start "#{bin_directory}/nominatim-update"
339 standard_output "append:#{node[:nominatim][:logdir]}/update.log"
340 standard_error "inherit"
341 working_directory project_directory
344 systemd_service "nominatim-update-maintenance-trigger" do
345 description "Trigger daily maintenance tasks for Nominatim DB"
346 exec_start "ln -sf #{bin_directory}/nominatim-daily-maintenance #{bin_directory}/maintenance/"
350 systemd_timer "nominatim-update-maintenance-trigger" do
351 action node[:nominatim][:state] != "off" ? :create : :delete
352 description "Schedule daily maintenance tasks for Nominatim DB"
353 on_calendar "*-*-* 02:03:00 UTC"
356 service "nominatim-update-maintenance-trigger" do
357 action node[:nominatim][:state] != "off" ? :enable : :disable
364 repository node[:nominatim][:ui_repository]
365 revision node[:nominatim][:ui_revision]
370 template "#{ui_directory}/dist/theme/config.theme.js" do
371 source "ui-config.js.erb"
379 if node[:nominatim][:enable_qa_tiles]
380 package "python3-geojson"
382 git qa_bin_directory do
383 repository node[:nominatim][:qa_repository]
384 revision node[:nominatim][:qa_revision]
385 enable_submodules true
388 notifies :run, "execute[compile_qa]"
391 execute "compile_qa" do
394 cwd "#{qa_bin_directory}/clustering-vt"
398 directory qa_data_directory do
405 template "#{qa_bin_directory}/analyser/config/config.yaml" do
406 source "qa_config.erb"
410 variables :outputdir => "#{qa_data_directory}/new"
413 ssl_certificate "qa-tile.nominatim.openstreetmap.org" do
414 domains ["qa-tile.nominatim.openstreetmap.org"]
415 notifies :reload, "service[nginx]"
418 nginx_site "qa-tiles.nominatim" do
419 template "nginx-qa-tiles.erb"
420 directory build_directory
421 variables :qa_data_directory => qa_data_directory
428 cron_d "nominatim-clean-db" do
429 action node[:nominatim][:state] == "master" ? :create : :delete
433 command "#{bin_directory}/clean-db-nominatim"
437 if node[:nominatim][:state] == "master"
438 postgresql_user "replication" do
439 cluster node[:nominatim][:dbcluster]
440 password data_bag_item("nominatim", "passwords")["replication"]
444 directory node[:rsyncd][:modules][:archive][:path] do
450 template "#{bin_directory}/clean-db-nominatim" do
451 source "clean-db-nominatim.erb"
455 variables :archive_dir => node[:rsyncd][:modules][:archive][:path],
456 :update_stop_file => "#{basedir}/status/updates_disabled",
457 :streaming_clients => search(:node, "nominatim_state:slave").map { |slave| slave[:fqdn] }.join(" ")
463 cron_d "nominatim-backup" do
464 action (node[:nominatim][:enable_backup] && node[:nominatim][:state] != "off") ? :create : :delete
469 command "#{bin_directory}/backup-nominatim"
473 cron_d "nominatim-vacuum-db" do
474 action node[:nominatim][:state] != "off" ? :create : :delete
478 command "#{bin_directory}/vacuum-db-nominatim"
482 %w[backup-nominatim vacuum-db-nominatim].each do |fname|
483 template "#{bin_directory}/#{fname}" do
484 source "#{fname}.erb"
488 variables :db => node[:nominatim][:dbname]
494 template "/etc/logrotate.d/nominatim" do
495 source "logrotate.nominatim.erb"
503 munin_plugin_conf "nominatim" do
505 variables :db => node[:nominatim][:dbname],
506 :querylog => "#{node[:nominatim][:logdir]}/query.log"
509 munin_plugin "nominatim_importlag" do
510 target "#{source_directory}/munin/nominatim_importlag"
513 munin_plugin "nominatim_query_speed" do
514 target "#{source_directory}/munin/nominatim_query_speed_querylog"
517 munin_plugin "nominatim_requests" do
518 target "#{source_directory}/munin/nominatim_requests_querylog"
521 prometheus_exporter "nominatim" do
524 restrict_address_families "AF_UNIX"
526 "--nominatim.query-log=#{node[:nominatim][:logdir]}/query.log",
527 "--nominatim.database-name=#{node[:nominatim][:dbname]}"
531 include_recipe "fail2ban"
533 frontend_addresses = frontends.collect { |f| f.ipaddresses(:role => :external) }
535 fail2ban_jail "nominatim_limit_req" do
536 filter "nginx-limit-req"
537 logpath "#{node[:nominatim][:logdir]}/nominatim.openstreetmap.org-error.log"
540 ignoreips frontend_addresses.flatten.sort