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 "pygments" do
201 python_virtualenv python_directory
205 python_package "PyYAML" do
206 python_virtualenv python_directory
210 python_package "falcon" do
211 python_virtualenv python_directory
215 python_package "uvicorn" do
216 python_virtualenv python_directory
220 python_package "gunicorn" do
221 python_virtualenv python_directory
226 source_directory = "#{basedir}/src/nominatim"
227 build_directory = "#{basedir}/src/build"
228 project_directory = "#{basedir}/planet-project"
229 bin_directory = "#{basedir}/bin"
230 cfg_directory = "#{basedir}/etc"
231 ui_directory = "#{basedir}/ui"
232 qa_bin_directory = "#{basedir}/src/Nominatim-Data-Analyser"
233 qa_data_directory = "#{basedir}/qa-data"
235 [basedir, "#{basedir}/src", cfg_directory, bin_directory, build_directory, project_directory].each do |path|
244 directory "#{bin_directory}/maintenance" do
250 if node[:nominatim][:flatnode_file]
251 directory File.dirname(node[:nominatim][:flatnode_file]) do
256 remote_directory "#{project_directory}/static-website" do
261 files_owner "nominatim"
262 files_group "nominatim"
267 # Normally syncing via chef is a bad idea because syncing might involve
268 # an update of database functions which should not be done while an update
269 # is ongoing. Therefore we sync in between update cycles. There is an
270 # exception for slaves: they get DB function updates from the master, so
271 # only the source code needs to be updated, which chef may do.
272 git source_directory do
273 action node[:nominatim][:state] == "slave" ? :sync : :checkout
274 repository node[:nominatim][:repository]
275 revision node[:nominatim][:revision]
276 enable_submodules true
279 not_if { node[:nominatim][:state] != "slave" && File.exist?("#{source_directory}/README.md") }
280 notifies :run, "execute[compile_nominatim]"
283 remote_file "#{source_directory}/data/country_osm_grid.sql.gz" do
284 action :create_if_missing
285 source "https://nominatim.org/data/country_grid.sql.gz"
291 execute "compile_nominatim" do
295 command "cmake -D WITH_LUAJIT=ON #{source_directory} && make"
296 notifies :run, "execute[install_nominatim]"
299 execute "install_nominatim" do
302 command "make install"
307 template "#{project_directory}/.env" do
308 source "nominatim.env.erb"
312 variables :base_url => node[:nominatim][:state] == "off" ? node[:fqdn] : "nominatim.openstreetmap.org",
313 :dbname => node[:nominatim][:dbname],
314 :flatnode_file => node[:nominatim][:flatnode_file],
315 :log_file => "#{node[:nominatim][:logdir]}/query.log",
316 :tokenizer => node[:nominatim][:config][:tokenizer],
317 :forward_dependencies => node[:nominatim][:config][:forward_dependencies],
318 :pool_size => node[:nominatim][:api_pool_size],
319 :query_timeout => node[:nominatim][:api_query_timeout],
320 :request_timeout => node[:nominatim][:api_request_timeout]
323 remote_file "#{project_directory}/wikimedia-importance.sql.gz" do
324 action :create_if_missing
325 source "https://nominatim.org/data/wikimedia-importance.sql.gz"
331 %w[gb_postcodes.csv.gz us_postcodes.csv.gz].each do |fname|
332 remote_file "#{project_directory}/#{fname}" do
334 source "https://nominatim.org/data/#{fname}"
341 # Webserver + frontend
343 %w[user_agent referrer email generic].each do |name|
344 file "#{cfg_directory}/nginx_blocked_#{name}.conf" do
345 action :create_if_missing
352 if node[:nominatim][:api_flavour] == "php"
353 node[:nominatim][:fpm_pools].each do |name, data|
357 pm_max_children data[:max_children]
359 pm_min_spare_servers 10
360 pm_max_spare_servers 20
361 pm_max_requests 10000
362 prometheus_port data[:prometheus_port]
365 elsif node[:nominatim][:api_flavour] == "python"
366 systemd_service "nominatim" do
367 description "Nominatim running as a gunicorn application"
370 working_directory project_directory
371 standard_output "append:#{node[:nominatim][:logdir]}/gunicorn.log"
372 standard_error "inherit"
373 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"
374 exec_reload "/bin/kill -s HUP $MAINPID"
375 environment :PYTHONPATH => "/usr/local/lib/nominatim/lib-python/"
379 requires "nominatim.socket"
380 after "network.target"
383 systemd_socket "nominatim" do
384 description "Gunicorn socket for Nominatim"
385 listen_stream "/run/gunicorn-nominatim.openstreetmap.org.sock"
386 socket_user "www-data"
390 ssl_certificate node[:fqdn] do
391 domains [node[:fqdn],
392 "nominatim.openstreetmap.org",
394 "nominatim.openstreetmap.com",
395 "nominatim.openstreetmap.net",
396 "nominatim.openstreetmaps.org",
397 "nominatim.openmaps.org",
398 "nominatim.qgis.org"]
399 notifies :reload, "service[nginx]"
402 include_recipe "nginx"
404 nginx_site "default" do
408 frontends = search(:node, "recipes:web\\:\\:frontend").sort_by(&:name)
410 nginx_site "nominatim" do
412 directory project_directory
413 variables :pools => node[:nominatim][:fpm_pools],
414 :frontends => frontends,
415 :confdir => "#{basedir}/etc",
416 :ui_directory => ui_directory
419 template "/etc/logrotate.d/nginx" do
420 source "logrotate.nginx.erb"
429 nominatim-update-source
430 nominatim-update-refresh-db
431 nominatim-update-data
432 nominatim-daily-maintenance].each do |fname|
433 template "#{bin_directory}/#{fname}" do
434 source "#{fname}.erb"
438 variables :bindir => bin_directory,
439 :srcdir => source_directory,
440 :builddir => build_directory,
441 :projectdir => project_directory,
442 :qabindir => qa_bin_directory,
443 :qadatadir => qa_data_directory
447 systemd_service "nominatim-update" do
448 description "Update the Nominatim database"
449 exec_start "#{bin_directory}/nominatim-update"
451 standard_output "append:#{node[:nominatim][:logdir]}/update.log"
452 standard_error "inherit"
453 working_directory project_directory
456 systemd_service "nominatim-update-maintenance-trigger" do
457 description "Trigger daily maintenance tasks for Nominatim DB"
458 exec_start "ln -sf #{bin_directory}/nominatim-daily-maintenance #{bin_directory}/maintenance/"
462 systemd_timer "nominatim-update-maintenance-trigger" do
463 action node[:nominatim][:state] != "off" ? :create : :delete
464 description "Schedule daily maintenance tasks for Nominatim DB"
465 on_calendar "*-*-* 02:03:00 UTC"
468 service "nominatim-update-maintenance-trigger" do
469 action node[:nominatim][:state] != "off" ? :enable : :disable
476 repository node[:nominatim][:ui_repository]
477 revision node[:nominatim][:ui_revision]
482 template "#{ui_directory}/dist/theme/config.theme.js" do
483 source "ui-config.js.erb"
491 if node[:nominatim][:enable_qa_tiles]
492 package "python3-geojson"
494 git qa_bin_directory do
495 repository node[:nominatim][:qa_repository]
496 revision node[:nominatim][:qa_revision]
497 enable_submodules true
500 notifies :run, "execute[compile_qa]"
503 execute "compile_qa" do
506 cwd "#{qa_bin_directory}/clustering-vt"
510 directory qa_data_directory do
517 template "#{qa_bin_directory}/analyser/config/config.yaml" do
518 source "qa_config.erb"
522 variables :outputdir => "#{qa_data_directory}/new"
525 ssl_certificate "qa-tile.nominatim.openstreetmap.org" do
526 domains ["qa-tile.nominatim.openstreetmap.org"]
527 notifies :reload, "service[nginx]"
530 nginx_site "qa-tiles.nominatim" do
531 template "nginx-qa-tiles.erb"
532 directory build_directory
533 variables :qa_data_directory => qa_data_directory
540 cron_d "nominatim-clean-db" do
541 action node[:nominatim][:state] == "master" ? :create : :delete
545 command "#{bin_directory}/clean-db-nominatim"
549 if node[:nominatim][:state] == "master"
550 postgresql_user "replication" do
551 cluster node[:nominatim][:dbcluster]
552 password data_bag_item("nominatim", "passwords")["replication"]
556 directory node[:rsyncd][:modules][:archive][:path] do
562 template "#{bin_directory}/clean-db-nominatim" do
563 source "clean-db-nominatim.erb"
567 variables :archive_dir => node[:rsyncd][:modules][:archive][:path],
568 :update_stop_file => "#{basedir}/status/updates_disabled",
569 :streaming_clients => search(:node, "nominatim_state:slave").map { |slave| slave[:fqdn] }.join(" ")
575 cron_d "nominatim-backup" do
576 action (node[:nominatim][:enable_backup] && node[:nominatim][:state] != "off") ? :create : :delete
581 command "#{bin_directory}/backup-nominatim"
585 cron_d "nominatim-vacuum-db" do
586 action node[:nominatim][:state] != "off" ? :create : :delete
590 command "#{bin_directory}/vacuum-db-nominatim"
594 %w[backup-nominatim vacuum-db-nominatim].each do |fname|
595 template "#{bin_directory}/#{fname}" do
596 source "#{fname}.erb"
600 variables :db => node[:nominatim][:dbname]
606 template "/etc/logrotate.d/nominatim" do
607 source "logrotate.nominatim.erb"
615 munin_plugin_conf "nominatim" do
617 variables :db => node[:nominatim][:dbname],
618 :querylog => "#{node[:nominatim][:logdir]}/query.log"
621 munin_plugin "nominatim_importlag" do
622 target "#{source_directory}/munin/nominatim_importlag"
625 munin_plugin "nominatim_query_speed" do
626 target "#{source_directory}/munin/nominatim_query_speed_querylog"
629 munin_plugin "nominatim_requests" do
630 target "#{source_directory}/munin/nominatim_requests_querylog"
633 prometheus_exporter "nominatim" do
636 restrict_address_families "AF_UNIX"
638 "--nominatim.query-log=#{node[:nominatim][:logdir]}/query.log",
639 "--nominatim.database-name=#{node[:nominatim][:dbname]}"
643 include_recipe "fail2ban"
645 frontend_addresses = frontends.collect { |f| f.ipaddresses(:role => :external) }
647 fail2ban_jail "nominatim_limit_req" do
648 filter "nginx-limit-req"
649 logpath "#{node[:nominatim][:logdir]}/nominatim.openstreetmap.org-error.log"
652 ignoreips frontend_addresses.flatten.sort