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 "prometheus"
23 if node[:nominatim][:api_flavour] == "php"
24 include_recipe "php::fpm"
27 basedir = data_bag_item("accounts", "nominatim")["home"]
28 email_errors = data_bag_item("accounts", "lonvia")["email"]
37 ## Log directory setup
39 directory node[:nominatim][:logdir] do
46 file "#{node[:nominatim][:logdir]}/query.log" do
47 action :create_if_missing
53 file "#{node[:nominatim][:logdir]}/update.log" do
54 action :create_if_missing
62 include_recipe "postgresql"
64 postgresql_version = node[:nominatim][:dbcluster].split("/").first
65 postgis_version = node[:nominatim][:postgis]
67 package "postgresql-#{postgresql_version}-postgis-#{postgis_version}"
69 node[:nominatim][:dbadmins].each do |user|
70 postgresql_user user do
71 cluster node[:nominatim][:dbcluster]
73 only_if { node[:nominatim][:state] != "slave" }
77 postgresql_user "nominatim" do
78 cluster node[:nominatim][:dbcluster]
80 only_if { node[:nominatim][:state] != "slave" }
83 postgresql_user "www-data" do
84 cluster node[:nominatim][:dbcluster]
85 only_if { node[:nominatim][:state] != "slave" }
88 directory "#{basedir}/tablespaces" do
94 # NOTE: tablespaces must be exactly in the same location on each
95 # Nominatim instance when replication is in use. Therefore
96 # use symlinks to canonical directory locations.
97 node[:nominatim][:tablespaces].each do |name, location|
105 link "#{basedir}/tablespaces/#{name}" do
109 postgresql_tablespace name do
110 cluster node[:nominatim][:dbcluster]
111 location "#{basedir}/tablespaces/#{name}"
118 include_recipe "python"
120 python_directory = "#{basedir}/venv"
128 libboost-filesystem-dev
147 python3-sqlalchemy-ext
158 if node[:nominatim][:api_flavour] == "php"
163 elsif node[:nominatim][:api_flavour] == "python"
165 python_virtualenv python_directory do
166 interpreter "/usr/bin/python3"
169 python_package "SQLAlchemy" do
170 python_virtualenv python_directory
174 python_package "PyICU" do
175 python_virtualenv python_directory
179 python_package "psycopg[binary]" do
180 python_virtualenv python_directory
184 python_package "psycopg2-binary" do
185 python_virtualenv python_directory
189 python_package "python-dotenv" do
190 python_virtualenv python_directory
194 python_package "pygments" do
195 python_virtualenv python_directory
199 python_package "PyYAML" do
200 python_virtualenv python_directory
204 python_package "falcon" do
205 python_virtualenv python_directory
209 python_package "uvicorn" do
210 python_virtualenv python_directory
214 python_package "gunicorn" do
215 python_virtualenv python_directory
219 python_package "jinja2" do
220 python_virtualenv python_directory
224 python_package "datrie" do
225 python_virtualenv python_directory
229 python_package "psutil" do
230 python_virtualenv python_directory
234 python_package "osmium" do
235 python_virtualenv python_directory
240 source_directory = "#{basedir}/src/nominatim"
241 build_directory = "#{basedir}/src/build"
242 project_directory = "#{basedir}/planet-project"
243 bin_directory = "#{basedir}/bin"
244 cfg_directory = "#{basedir}/etc"
245 ui_directory = "#{basedir}/ui"
246 qa_bin_directory = "#{basedir}/src/Nominatim-Data-Analyser"
247 qa_data_directory = "#{basedir}/qa-data"
249 [basedir, "#{basedir}/src", cfg_directory, bin_directory, build_directory, project_directory].each do |path|
258 directory "#{bin_directory}/maintenance" do
264 if node[:nominatim][:flatnode_file]
265 directory File.dirname(node[:nominatim][:flatnode_file]) do
270 remote_directory "#{project_directory}/static-website" do
275 files_owner "nominatim"
276 files_group "nominatim"
281 # Normally syncing via chef is a bad idea because syncing might involve
282 # an update of database functions which should not be done while an update
283 # is ongoing. Therefore we sync in between update cycles. There is an
284 # exception for slaves: they get DB function updates from the master, so
285 # only the source code needs to be updated, which chef may do.
286 git source_directory do
287 action node[:nominatim][:state] == "slave" ? :sync : :checkout
288 repository node[:nominatim][:repository]
289 revision node[:nominatim][:revision]
290 enable_submodules true
293 not_if { node[:nominatim][:state] != "slave" && File.exist?("#{source_directory}/README.md") }
294 notifies :run, "execute[compile_nominatim]"
297 remote_file "#{source_directory}/data/country_osm_grid.sql.gz" do
298 action :create_if_missing
299 source "https://nominatim.org/data/country_grid.sql.gz"
305 execute "compile_nominatim" do
309 command "cmake #{source_directory} && make"
310 notifies :run, "execute[install_nominatim]"
313 execute "install_nominatim" do
316 command "make install"
321 template "#{project_directory}/.env" do
322 source "nominatim.env.erb"
326 variables :base_url => node[:nominatim][:state] == "off" ? node[:fqdn] : "nominatim.openstreetmap.org",
327 :dbname => node[:nominatim][:dbname],
328 :flatnode_file => node[:nominatim][:flatnode_file],
329 :log_file => "#{node[:nominatim][:logdir]}/query.log",
330 :tokenizer => node[:nominatim][:config][:tokenizer],
331 :forward_dependencies => node[:nominatim][:config][:forward_dependencies],
332 :pool_size => node[:nominatim][:api_pool_size],
333 :query_timeout => node[:nominatim][:api_query_timeout],
334 :request_timeout => node[:nominatim][:api_request_timeout]
337 remote_file "#{project_directory}/secondary_importance.sql.gz" do
338 action :create_if_missing
339 source "https://nominatim.org/data/wikimedia-secondary-importance.sql.gz"
345 remote_file "#{project_directory}/wikimedia-importance.sql.gz" do
346 action :create_if_missing
347 source "https://nominatim.org/data/wikimedia-importance.sql.gz"
353 %w[gb_postcodes.csv.gz us_postcodes.csv.gz].each do |fname|
354 remote_file "#{project_directory}/#{fname}" do
356 source "https://nominatim.org/data/#{fname}"
363 # Webserver + frontend
365 %w[user_agent referrer email generic].each do |name|
366 file "#{cfg_directory}/nginx_blocked_#{name}.conf" do
367 action :create_if_missing
374 if node[:nominatim][:api_flavour] == "php"
375 node[:nominatim][:fpm_pools].each do |name, data|
379 pm_max_children data[:max_children]
381 pm_min_spare_servers 10
382 pm_max_spare_servers 20
383 pm_max_requests 10000
384 prometheus_port data[:prometheus_port]
387 elsif node[:nominatim][:api_flavour] == "python"
388 systemd_service "nominatim" do
389 description "Nominatim running as a gunicorn application"
392 working_directory project_directory
393 standard_output "append:#{node[:nominatim][:logdir]}/gunicorn.log"
394 standard_error "inherit"
395 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_api.server.falcon.server:run_wsgi"
396 exec_reload "/bin/kill -s HUP $MAINPID"
397 environment :PYTHONPATH => "/usr/local/lib/nominatim/lib-python/"
401 requires "nominatim.socket"
402 after "network.target"
405 systemd_socket "nominatim" do
406 description "Gunicorn socket for Nominatim"
407 listen_stream "/run/gunicorn-nominatim.openstreetmap.org.sock"
408 socket_user "www-data"
412 ssl_certificate node[:fqdn] do
413 domains [node[:fqdn],
414 "nominatim.openstreetmap.org",
416 "nominatim.openstreetmap.com",
417 "nominatim.openstreetmap.net",
418 "nominatim.openstreetmaps.org",
419 "nominatim.openmaps.org",
420 "nominatim.qgis.org"]
421 notifies :reload, "service[nginx]"
424 include_recipe "nginx"
426 nginx_site "default" do
430 frontends = search(:node, "recipes:web\\:\\:frontend").sort_by(&:name)
432 nginx_site "nominatim" do
434 directory project_directory
435 variables :pools => node[:nominatim][:fpm_pools],
436 :frontends => frontends,
437 :confdir => "#{basedir}/etc",
438 :ui_directory => ui_directory
441 template "/etc/logrotate.d/nginx" do
442 source "logrotate.nginx.erb"
451 nominatim-update-source
452 nominatim-update-refresh-db
453 nominatim-update-data
454 nominatim-daily-maintenance].each do |fname|
455 template "#{bin_directory}/#{fname}" do
456 source "#{fname}.erb"
460 variables :bindir => bin_directory,
461 :srcdir => source_directory,
462 :builddir => build_directory,
463 :projectdir => project_directory,
464 :qabindir => qa_bin_directory,
465 :qadatadir => qa_data_directory
469 systemd_service "nominatim-update" do
470 description "Update the Nominatim database"
471 exec_start "#{bin_directory}/nominatim-update"
473 standard_output "append:#{node[:nominatim][:logdir]}/update.log"
474 standard_error "inherit"
475 working_directory project_directory
478 systemd_service "nominatim-update-maintenance-trigger" do
479 description "Trigger daily maintenance tasks for Nominatim DB"
480 exec_start "ln -sf #{bin_directory}/nominatim-daily-maintenance #{bin_directory}/maintenance/"
484 systemd_timer "nominatim-update-maintenance-trigger" do
485 action node[:nominatim][:state] != "off" ? :create : :delete
486 description "Schedule daily maintenance tasks for Nominatim DB"
487 on_calendar "*-*-* 02:03:00 UTC"
490 service "nominatim-update-maintenance-trigger" do
491 action node[:nominatim][:state] != "off" ? :enable : :disable
498 repository node[:nominatim][:ui_repository]
499 revision node[:nominatim][:ui_revision]
504 template "#{ui_directory}/dist/theme/config.theme.js" do
505 source "ui-config.js.erb"
513 if node[:nominatim][:enable_qa_tiles]
514 package "python3-geojson"
516 git qa_bin_directory do
517 repository node[:nominatim][:qa_repository]
518 revision node[:nominatim][:qa_revision]
519 enable_submodules true
522 notifies :run, "execute[compile_qa]"
525 execute "compile_qa" do
528 cwd "#{qa_bin_directory}/clustering-vt"
532 directory qa_data_directory do
539 template "#{qa_bin_directory}/analyser/config/config.yaml" do
540 source "qa_config.erb"
544 variables :outputdir => "#{qa_data_directory}/new"
547 ssl_certificate "qa-tile.nominatim.openstreetmap.org" do
548 domains ["qa-tile.nominatim.openstreetmap.org"]
549 notifies :reload, "service[nginx]"
552 nginx_site "qa-tiles.nominatim" do
553 template "nginx-qa-tiles.erb"
554 directory build_directory
555 variables :qa_data_directory => qa_data_directory
562 cron_d "nominatim-clean-db" do
563 action node[:nominatim][:state] == "master" ? :create : :delete
567 command "#{bin_directory}/clean-db-nominatim"
571 if node[:nominatim][:state] == "master"
572 postgresql_user "replication" do
573 cluster node[:nominatim][:dbcluster]
574 password data_bag_item("nominatim", "passwords")["replication"]
578 directory node[:rsyncd][:modules][:archive][:path] do
584 template "#{bin_directory}/clean-db-nominatim" do
585 source "clean-db-nominatim.erb"
589 variables :archive_dir => node[:rsyncd][:modules][:archive][:path],
590 :update_stop_file => "#{basedir}/status/updates_disabled",
591 :streaming_clients => search(:node, "nominatim_state:slave").map { |slave| slave[:fqdn] }.join(" ")
597 cron_d "nominatim-backup" do
598 action (node[:nominatim][:enable_backup] && node[:nominatim][:state] != "off") ? :create : :delete
603 command "#{bin_directory}/backup-nominatim"
607 cron_d "nominatim-vacuum-db" do
608 action node[:nominatim][:state] != "off" ? :create : :delete
612 command "#{bin_directory}/vacuum-db-nominatim"
616 %w[backup-nominatim vacuum-db-nominatim].each do |fname|
617 template "#{bin_directory}/#{fname}" do
618 source "#{fname}.erb"
622 variables :db => node[:nominatim][:dbname]
628 template "/etc/logrotate.d/nominatim" do
629 source "logrotate.nominatim.erb"
636 prometheus_exporter "nominatim" do
639 restrict_address_families "AF_UNIX"
641 "--nominatim.query-log=#{node[:nominatim][:logdir]}/query.log",
642 "--nominatim.database-name=#{node[:nominatim][:dbname]}"
646 include_recipe "fail2ban"
648 frontend_addresses = frontends.collect { |f| f.ipaddresses(:role => :external) }
650 fail2ban_jail "nominatim_limit_req" do
651 filter "nginx-limit-req"
652 logpath "#{node[:nominatim][:logdir]}/nominatim.openstreetmap.org-error.log"
655 ignoreips frontend_addresses.flatten.sort