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
145 python3-sqlalchemy-ext
156 source_directory = "#{basedir}/src/nominatim"
157 build_directory = "#{basedir}/src/build"
158 project_directory = "#{basedir}/planet-project"
159 bin_directory = "#{basedir}/bin"
160 cfg_directory = "#{basedir}/etc"
161 ui_directory = "#{basedir}/ui"
162 qa_bin_directory = "#{basedir}/src/Nominatim-Data-Analyser"
163 qa_data_directory = "#{basedir}/qa-data"
165 [basedir, "#{basedir}/src", cfg_directory, bin_directory, build_directory, project_directory].each do |path|
174 directory "#{bin_directory}/maintenance" do
180 if node[:nominatim][:flatnode_file]
181 directory File.dirname(node[:nominatim][:flatnode_file]) do
186 remote_directory "#{project_directory}/website" do
191 files_owner "nominatim"
192 files_group "nominatim"
197 # Normally syncing via chef is a bad idea because syncing might involve
198 # an update of database functions which should not be done while an update
199 # is ongoing. Therefore we sync in between update cycles. There is an
200 # exception for slaves: they get DB function updates from the master, so
201 # only the source code needs to be updated, which chef may do.
202 git source_directory do
203 action node[:nominatim][:state] == "slave" ? :sync : :checkout
204 repository node[:nominatim][:repository]
205 revision node[:nominatim][:revision]
206 enable_submodules true
209 not_if { node[:nominatim][:state] != "slave" && File.exist?("#{source_directory}/README.md") }
210 notifies :run, "execute[compile_nominatim]"
213 remote_file "#{source_directory}/data/country_osm_grid.sql.gz" do
214 action :create_if_missing
215 source "https://nominatim.org/data/country_grid.sql.gz"
221 execute "compile_nominatim" do
225 command "cmake -D WITH_LUAJIT=ON #{source_directory} && make"
226 notifies :run, "execute[install_nominatim]"
229 execute "install_nominatim" do
232 command "make install"
237 template "#{project_directory}/.env" do
238 source "nominatim.env.erb"
242 variables :base_url => node[:nominatim][:state] == "off" ? node[:fqdn] : "nominatim.openstreetmap.org",
243 :dbname => node[:nominatim][:dbname],
244 :flatnode_file => node[:nominatim][:flatnode_file],
245 :log_file => "#{node[:nominatim][:logdir]}/query.log",
246 :tokenizer => node[:nominatim][:config][:tokenizer],
247 :forward_dependencies => node[:nominatim][:config][:forward_dependencies]
250 remote_file "#{project_directory}/wikimedia-importance.sql.gz" do
251 action :create_if_missing
252 source "https://nominatim.org/data/wikimedia-importance.sql.gz"
258 %w[gb_postcodes.csv.gz us_postcodes.csv.gz].each do |fname|
259 remote_file "#{project_directory}/#{fname}" do
261 source "https://nominatim.org/data/#{fname}"
268 # Webserver + frontend
270 %w[user_agent referrer email generic].each do |name|
271 file "#{cfg_directory}/nginx_blocked_#{name}.conf" do
272 action :create_if_missing
279 node[:nominatim][:fpm_pools].each do |name, data|
283 pm_max_children data[:max_children]
285 pm_min_spare_servers 10
286 pm_max_spare_servers 20
287 pm_max_requests 10000
288 prometheus_port data[:prometheus_port]
292 ssl_certificate node[:fqdn] do
293 domains [node[:fqdn],
294 "nominatim.openstreetmap.org",
296 "nominatim.openstreetmap.com",
297 "nominatim.openstreetmap.net",
298 "nominatim.openstreetmaps.org",
299 "nominatim.openmaps.org",
300 "nominatim.qgis.org"]
301 notifies :reload, "service[nginx]"
304 include_recipe "nginx"
306 nginx_site "default" do
310 frontends = search(:node, "recipes:web\\:\\:frontend").sort_by(&:name)
312 nginx_site "nominatim" do
314 directory project_directory
315 variables :pools => node[:nominatim][:fpm_pools],
316 :frontends => frontends,
317 :confdir => "#{basedir}/etc",
318 :ui_directory => ui_directory
321 template "/etc/logrotate.d/nginx" do
322 source "logrotate.nginx.erb"
331 nominatim-update-source
332 nominatim-update-refresh-db
333 nominatim-update-data
334 nominatim-daily-maintenance].each do |fname|
335 template "#{bin_directory}/#{fname}" do
336 source "#{fname}.erb"
340 variables :bindir => bin_directory,
341 :srcdir => source_directory,
342 :builddir => build_directory,
343 :projectdir => project_directory,
344 :qabindir => qa_bin_directory,
345 :qadatadir => qa_data_directory
349 systemd_service "nominatim-update" do
350 description "Update the Nominatim database"
351 exec_start "#{bin_directory}/nominatim-update"
353 standard_output "append:#{node[:nominatim][:logdir]}/update.log"
354 standard_error "inherit"
355 working_directory project_directory
358 systemd_service "nominatim-update-maintenance-trigger" do
359 description "Trigger daily maintenance tasks for Nominatim DB"
360 exec_start "ln -sf #{bin_directory}/nominatim-daily-maintenance #{bin_directory}/maintenance/"
364 systemd_timer "nominatim-update-maintenance-trigger" do
365 action node[:nominatim][:state] != "off" ? :create : :delete
366 description "Schedule daily maintenance tasks for Nominatim DB"
367 on_calendar "*-*-* 02:03:00 UTC"
370 service "nominatim-update-maintenance-trigger" do
371 action node[:nominatim][:state] != "off" ? :enable : :disable
378 repository node[:nominatim][:ui_repository]
379 revision node[:nominatim][:ui_revision]
384 template "#{ui_directory}/dist/theme/config.theme.js" do
385 source "ui-config.js.erb"
393 if node[:nominatim][:enable_qa_tiles]
394 package "python3-geojson"
396 git qa_bin_directory do
397 repository node[:nominatim][:qa_repository]
398 revision node[:nominatim][:qa_revision]
399 enable_submodules true
402 notifies :run, "execute[compile_qa]"
405 execute "compile_qa" do
408 cwd "#{qa_bin_directory}/clustering-vt"
412 directory qa_data_directory do
419 template "#{qa_bin_directory}/analyser/config/config.yaml" do
420 source "qa_config.erb"
424 variables :outputdir => "#{qa_data_directory}/new"
427 ssl_certificate "qa-tile.nominatim.openstreetmap.org" do
428 domains ["qa-tile.nominatim.openstreetmap.org"]
429 notifies :reload, "service[nginx]"
432 nginx_site "qa-tiles.nominatim" do
433 template "nginx-qa-tiles.erb"
434 directory build_directory
435 variables :qa_data_directory => qa_data_directory
442 cron_d "nominatim-clean-db" do
443 action node[:nominatim][:state] == "master" ? :create : :delete
447 command "#{bin_directory}/clean-db-nominatim"
451 if node[:nominatim][:state] == "master"
452 postgresql_user "replication" do
453 cluster node[:nominatim][:dbcluster]
454 password data_bag_item("nominatim", "passwords")["replication"]
458 directory node[:rsyncd][:modules][:archive][:path] do
464 template "#{bin_directory}/clean-db-nominatim" do
465 source "clean-db-nominatim.erb"
469 variables :archive_dir => node[:rsyncd][:modules][:archive][:path],
470 :update_stop_file => "#{basedir}/status/updates_disabled",
471 :streaming_clients => search(:node, "nominatim_state:slave").map { |slave| slave[:fqdn] }.join(" ")
477 cron_d "nominatim-backup" do
478 action (node[:nominatim][:enable_backup] && node[:nominatim][:state] != "off") ? :create : :delete
483 command "#{bin_directory}/backup-nominatim"
487 cron_d "nominatim-vacuum-db" do
488 action node[:nominatim][:state] != "off" ? :create : :delete
492 command "#{bin_directory}/vacuum-db-nominatim"
496 %w[backup-nominatim vacuum-db-nominatim].each do |fname|
497 template "#{bin_directory}/#{fname}" do
498 source "#{fname}.erb"
502 variables :db => node[:nominatim][:dbname]
508 template "/etc/logrotate.d/nominatim" do
509 source "logrotate.nominatim.erb"
517 munin_plugin_conf "nominatim" do
519 variables :db => node[:nominatim][:dbname],
520 :querylog => "#{node[:nominatim][:logdir]}/query.log"
523 munin_plugin "nominatim_importlag" do
524 target "#{source_directory}/munin/nominatim_importlag"
527 munin_plugin "nominatim_query_speed" do
528 target "#{source_directory}/munin/nominatim_query_speed_querylog"
531 munin_plugin "nominatim_requests" do
532 target "#{source_directory}/munin/nominatim_requests_querylog"
535 prometheus_exporter "nominatim" do
538 restrict_address_families "AF_UNIX"
540 "--nominatim.query-log=#{node[:nominatim][:logdir]}/query.log",
541 "--nominatim.database-name=#{node[:nominatim][:dbname]}"
545 include_recipe "fail2ban"
547 frontend_addresses = frontends.collect { |f| f.ipaddresses(:role => :external) }
549 fail2ban_jail "nominatim_limit_req" do
550 filter "nginx-limit-req"
551 logpath "#{node[:nominatim][:logdir]}/nominatim.openstreetmap.org-error.log"
554 ignoreips frontend_addresses.flatten.sort