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 "prometheus"
24 if node[:nominatim][:api_flavour] == "php"
25 include_recipe "php::fpm"
28 basedir = data_bag_item("accounts", "nominatim")["home"]
29 email_errors = data_bag_item("accounts", "lonvia")["email"]
38 ## Log directory setup
40 directory node[:nominatim][:logdir] do
47 file "#{node[:nominatim][:logdir]}/query.log" do
48 action :create_if_missing
54 file "#{node[:nominatim][:logdir]}/update.log" do
55 action :create_if_missing
63 include_recipe "postgresql"
65 postgresql_version = node[:nominatim][:dbcluster].split("/").first
66 postgis_version = node[:nominatim][:postgis]
68 package "postgresql-#{postgresql_version}-postgis-#{postgis_version}"
70 node[:nominatim][:dbadmins].each do |user|
71 postgresql_user user do
72 cluster node[:nominatim][:dbcluster]
74 only_if { node[:nominatim][:state] != "slave" }
78 postgresql_user "nominatim" do
79 cluster node[:nominatim][:dbcluster]
81 only_if { node[:nominatim][:state] != "slave" }
84 postgresql_user "www-data" do
85 cluster node[:nominatim][:dbcluster]
86 only_if { node[:nominatim][:state] != "slave" }
89 postgresql_munin "nominatim" do
90 cluster node[:nominatim][:dbcluster]
91 database node[:nominatim][:dbname]
94 directory "#{basedir}/tablespaces" do
100 # NOTE: tablespaces must be exactly in the same location on each
101 # Nominatim instance when replication is in use. Therefore
102 # use symlinks to canonical directory locations.
103 node[:nominatim][:tablespaces].each do |name, location|
104 directory location do
111 link "#{basedir}/tablespaces/#{name}" do
115 postgresql_tablespace name do
116 cluster node[:nominatim][:dbcluster]
117 location "#{basedir}/tablespaces/#{name}"
124 include_recipe "python"
126 python_directory = "#{basedir}/venv"
134 libboost-filesystem-dev
153 python3-sqlalchemy-ext
164 if node[:nominatim][:api_flavour] == "php"
169 elsif node[:nominatim][:api_flavour] == "python"
171 python_virtualenv python_directory do
172 interpreter "/usr/bin/python3"
175 python_package "SQLAlchemy" do
176 python_virtualenv python_directory
180 python_package "PyICU" do
181 python_virtualenv python_directory
185 python_package "psycopg[binary]" do
186 python_virtualenv python_directory
190 python_package "psycopg2-binary" do
191 python_virtualenv python_directory
195 python_package "python-dotenv" do
196 python_virtualenv python_directory
200 python_package "PyYAML" do
201 python_virtualenv python_directory
205 python_package "falcon" do
206 python_virtualenv python_directory
210 python_package "uvicorn" do
211 python_virtualenv python_directory
215 python_package "gunicorn" do
216 python_virtualenv python_directory
221 source_directory = "#{basedir}/src/nominatim"
222 build_directory = "#{basedir}/src/build"
223 project_directory = "#{basedir}/planet-project"
224 bin_directory = "#{basedir}/bin"
225 cfg_directory = "#{basedir}/etc"
226 ui_directory = "#{basedir}/ui"
227 qa_bin_directory = "#{basedir}/src/Nominatim-Data-Analyser"
228 qa_data_directory = "#{basedir}/qa-data"
230 [basedir, "#{basedir}/src", cfg_directory, bin_directory, build_directory, project_directory].each do |path|
239 directory "#{bin_directory}/maintenance" do
245 if node[:nominatim][:flatnode_file]
246 directory File.dirname(node[:nominatim][:flatnode_file]) do
251 remote_directory "#{project_directory}/static-website" do
256 files_owner "nominatim"
257 files_group "nominatim"
262 # Normally syncing via chef is a bad idea because syncing might involve
263 # an update of database functions which should not be done while an update
264 # is ongoing. Therefore we sync in between update cycles. There is an
265 # exception for slaves: they get DB function updates from the master, so
266 # only the source code needs to be updated, which chef may do.
267 git source_directory do
268 action node[:nominatim][:state] == "slave" ? :sync : :checkout
269 repository node[:nominatim][:repository]
270 revision node[:nominatim][:revision]
271 enable_submodules true
274 not_if { node[:nominatim][:state] != "slave" && File.exist?("#{source_directory}/README.md") }
275 notifies :run, "execute[compile_nominatim]"
278 remote_file "#{source_directory}/data/country_osm_grid.sql.gz" do
279 action :create_if_missing
280 source "https://nominatim.org/data/country_grid.sql.gz"
286 execute "compile_nominatim" do
290 command "cmake -D WITH_LUAJIT=ON #{source_directory} && make"
291 notifies :run, "execute[install_nominatim]"
294 execute "install_nominatim" do
297 command "make install"
302 template "#{project_directory}/.env" do
303 source "nominatim.env.erb"
307 variables :base_url => node[:nominatim][:state] == "off" ? node[:fqdn] : "nominatim.openstreetmap.org",
308 :dbname => node[:nominatim][:dbname],
309 :flatnode_file => node[:nominatim][:flatnode_file],
310 :log_file => "#{node[:nominatim][:logdir]}/query.log",
311 :tokenizer => node[:nominatim][:config][:tokenizer],
312 :forward_dependencies => node[:nominatim][:config][:forward_dependencies],
313 :pool_size => node[:nominatim][:api_pool_size],
314 :query_timeout => node[:nominatim][:api_query_timeout],
315 :request_timeout => node[:nominatim][:api_request_timeout]
318 remote_file "#{project_directory}/wikimedia-importance.sql.gz" do
319 action :create_if_missing
320 source "https://nominatim.org/data/wikimedia-importance.sql.gz"
326 %w[gb_postcodes.csv.gz us_postcodes.csv.gz].each do |fname|
327 remote_file "#{project_directory}/#{fname}" do
329 source "https://nominatim.org/data/#{fname}"
336 # Webserver + frontend
338 %w[user_agent referrer email generic].each do |name|
339 file "#{cfg_directory}/nginx_blocked_#{name}.conf" do
340 action :create_if_missing
347 if node[:nominatim][:api_flavour] == "php"
348 node[:nominatim][:fpm_pools].each do |name, data|
352 pm_max_children data[:max_children]
354 pm_min_spare_servers 10
355 pm_max_spare_servers 20
356 pm_max_requests 10000
357 prometheus_port data[:prometheus_port]
360 elsif node[:nominatim][:api_flavour] == "python"
361 systemd_service "nominatim" do
362 description "Nominatim running as a gunicorn application"
365 working_directory project_directory
366 standard_output "append:#{node[:nominatim][:logdir]}/gunicorn.log"
367 standard_error "inherit"
368 exec_start "#{python_directory}/bin/gunicorn --max-requests 200000 -b unix:/run/gunicorn-nominatim.openstreetmap.org.sock -w #{node[:nominatim][:api_workers]} -k uvicorn.workers.UvicornWorker nominatim.server.falcon.server:run_wsgi"
369 exec_reload "/bin/kill -s HUP $MAINPID"
370 environment :PYTHONPATH => "/usr/local/lib/nominatim/lib-python/"
374 requires "nominatim.socket"
375 after "network.target"
378 systemd_socket "nominatim" do
379 description "Gunicorn socket for Nominatim"
380 listen_stream "/run/gunicorn-nominatim.openstreetmap.org.sock"
381 socket_user "www-data"
385 ssl_certificate node[:fqdn] do
386 domains [node[:fqdn],
387 "nominatim.openstreetmap.org",
389 "nominatim.openstreetmap.com",
390 "nominatim.openstreetmap.net",
391 "nominatim.openstreetmaps.org",
392 "nominatim.openmaps.org",
393 "nominatim.qgis.org"]
394 notifies :reload, "service[nginx]"
397 include_recipe "nginx"
399 nginx_site "default" do
403 frontends = search(:node, "recipes:web\\:\\:frontend").sort_by(&:name)
405 nginx_site "nominatim" do
407 directory project_directory
408 variables :pools => node[:nominatim][:fpm_pools],
409 :frontends => frontends,
410 :confdir => "#{basedir}/etc",
411 :ui_directory => ui_directory
414 template "/etc/logrotate.d/nginx" do
415 source "logrotate.nginx.erb"
424 nominatim-update-source
425 nominatim-update-refresh-db
426 nominatim-update-data
427 nominatim-daily-maintenance].each do |fname|
428 template "#{bin_directory}/#{fname}" do
429 source "#{fname}.erb"
433 variables :bindir => bin_directory,
434 :srcdir => source_directory,
435 :builddir => build_directory,
436 :projectdir => project_directory,
437 :qabindir => qa_bin_directory,
438 :qadatadir => qa_data_directory
442 systemd_service "nominatim-update" do
443 description "Update the Nominatim database"
444 exec_start "#{bin_directory}/nominatim-update"
446 standard_output "append:#{node[:nominatim][:logdir]}/update.log"
447 standard_error "inherit"
448 working_directory project_directory
451 systemd_service "nominatim-update-maintenance-trigger" do
452 description "Trigger daily maintenance tasks for Nominatim DB"
453 exec_start "ln -sf #{bin_directory}/nominatim-daily-maintenance #{bin_directory}/maintenance/"
457 systemd_timer "nominatim-update-maintenance-trigger" do
458 action node[:nominatim][:state] != "off" ? :create : :delete
459 description "Schedule daily maintenance tasks for Nominatim DB"
460 on_calendar "*-*-* 02:03:00 UTC"
463 service "nominatim-update-maintenance-trigger" do
464 action node[:nominatim][:state] != "off" ? :enable : :disable
471 repository node[:nominatim][:ui_repository]
472 revision node[:nominatim][:ui_revision]
477 template "#{ui_directory}/dist/theme/config.theme.js" do
478 source "ui-config.js.erb"
486 if node[:nominatim][:enable_qa_tiles]
487 package "python3-geojson"
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 ssl_certificate "qa-tile.nominatim.openstreetmap.org" do
521 domains ["qa-tile.nominatim.openstreetmap.org"]
522 notifies :reload, "service[nginx]"
525 nginx_site "qa-tiles.nominatim" do
526 template "nginx-qa-tiles.erb"
527 directory build_directory
528 variables :qa_data_directory => qa_data_directory
535 cron_d "nominatim-clean-db" do
536 action node[:nominatim][:state] == "master" ? :create : :delete
540 command "#{bin_directory}/clean-db-nominatim"
544 if node[:nominatim][:state] == "master"
545 postgresql_user "replication" do
546 cluster node[:nominatim][:dbcluster]
547 password data_bag_item("nominatim", "passwords")["replication"]
551 directory node[:rsyncd][:modules][:archive][:path] do
557 template "#{bin_directory}/clean-db-nominatim" do
558 source "clean-db-nominatim.erb"
562 variables :archive_dir => node[:rsyncd][:modules][:archive][:path],
563 :update_stop_file => "#{basedir}/status/updates_disabled",
564 :streaming_clients => search(:node, "nominatim_state:slave").map { |slave| slave[:fqdn] }.join(" ")
570 cron_d "nominatim-backup" do
571 action (node[:nominatim][:enable_backup] && node[:nominatim][:state] != "off") ? :create : :delete
576 command "#{bin_directory}/backup-nominatim"
580 cron_d "nominatim-vacuum-db" do
581 action node[:nominatim][:state] != "off" ? :create : :delete
585 command "#{bin_directory}/vacuum-db-nominatim"
589 %w[backup-nominatim vacuum-db-nominatim].each do |fname|
590 template "#{bin_directory}/#{fname}" do
591 source "#{fname}.erb"
595 variables :db => node[:nominatim][:dbname]
601 template "/etc/logrotate.d/nominatim" do
602 source "logrotate.nominatim.erb"
610 munin_plugin_conf "nominatim" do
612 variables :db => node[:nominatim][:dbname],
613 :querylog => "#{node[:nominatim][:logdir]}/query.log"
616 munin_plugin "nominatim_importlag" do
617 target "#{source_directory}/munin/nominatim_importlag"
620 munin_plugin "nominatim_query_speed" do
621 target "#{source_directory}/munin/nominatim_query_speed_querylog"
624 munin_plugin "nominatim_requests" do
625 target "#{source_directory}/munin/nominatim_requests_querylog"
628 prometheus_exporter "nominatim" do
631 restrict_address_families "AF_UNIX"
633 "--nominatim.query-log=#{node[:nominatim][:logdir]}/query.log",
634 "--nominatim.database-name=#{node[:nominatim][:dbname]}"
638 include_recipe "fail2ban"
640 frontend_addresses = frontends.collect { |f| f.ipaddresses(:role => :external) }
642 fail2ban_jail "nominatim_limit_req" do
643 filter "nginx-limit-req"
644 logpath "#{node[:nominatim][:logdir]}/nominatim.openstreetmap.org-error.log"
647 ignoreips frontend_addresses.flatten.sort