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
220 source_directory = "#{basedir}/src/nominatim"
221 build_directory = "#{basedir}/src/build"
222 project_directory = "#{basedir}/planet-project"
223 bin_directory = "#{basedir}/bin"
224 cfg_directory = "#{basedir}/etc"
225 ui_directory = "#{basedir}/ui"
226 qa_bin_directory = "#{basedir}/src/Nominatim-Data-Analyser"
227 qa_data_directory = "#{basedir}/qa-data"
229 [basedir, "#{basedir}/src", cfg_directory, bin_directory, build_directory, project_directory].each do |path|
238 directory "#{bin_directory}/maintenance" do
244 if node[:nominatim][:flatnode_file]
245 directory File.dirname(node[:nominatim][:flatnode_file]) do
250 remote_directory "#{project_directory}/static-website" do
255 files_owner "nominatim"
256 files_group "nominatim"
261 # Normally syncing via chef is a bad idea because syncing might involve
262 # an update of database functions which should not be done while an update
263 # is ongoing. Therefore we sync in between update cycles. There is an
264 # exception for slaves: they get DB function updates from the master, so
265 # only the source code needs to be updated, which chef may do.
266 git source_directory do
267 action node[:nominatim][:state] == "slave" ? :sync : :checkout
268 repository node[:nominatim][:repository]
269 revision node[:nominatim][:revision]
270 enable_submodules true
273 not_if { node[:nominatim][:state] != "slave" && File.exist?("#{source_directory}/README.md") }
274 notifies :run, "execute[compile_nominatim]"
277 remote_file "#{source_directory}/data/country_osm_grid.sql.gz" do
278 action :create_if_missing
279 source "https://nominatim.org/data/country_grid.sql.gz"
285 execute "compile_nominatim" do
289 command "cmake #{source_directory} && make"
290 notifies :run, "execute[install_nominatim]"
293 execute "install_nominatim" do
296 command "make install"
301 template "#{project_directory}/.env" do
302 source "nominatim.env.erb"
306 variables :base_url => node[:nominatim][:state] == "off" ? node[:fqdn] : "nominatim.openstreetmap.org",
307 :dbname => node[:nominatim][:dbname],
308 :flatnode_file => node[:nominatim][:flatnode_file],
309 :log_file => "#{node[:nominatim][:logdir]}/query.log",
310 :tokenizer => node[:nominatim][:config][:tokenizer],
311 :forward_dependencies => node[:nominatim][:config][:forward_dependencies],
312 :pool_size => node[:nominatim][:api_pool_size],
313 :query_timeout => node[:nominatim][:api_query_timeout],
314 :request_timeout => node[:nominatim][:api_request_timeout]
317 remote_file "#{project_directory}/secondary_importance.sql.gz" do
318 action :create_if_missing
319 source "https://nominatim.org/data/wikimedia-secondary-importance.sql.gz"
325 remote_file "#{project_directory}/wikimedia-importance.sql.gz" do
326 action :create_if_missing
327 source "https://nominatim.org/data/wikimedia-importance.sql.gz"
333 %w[gb_postcodes.csv.gz us_postcodes.csv.gz].each do |fname|
334 remote_file "#{project_directory}/#{fname}" do
336 source "https://nominatim.org/data/#{fname}"
343 # Webserver + frontend
345 %w[user_agent referrer email generic].each do |name|
346 file "#{cfg_directory}/nginx_blocked_#{name}.conf" do
347 action :create_if_missing
354 if node[:nominatim][:api_flavour] == "php"
355 node[:nominatim][:fpm_pools].each do |name, data|
359 pm_max_children data[:max_children]
361 pm_min_spare_servers 10
362 pm_max_spare_servers 20
363 pm_max_requests 10000
364 prometheus_port data[:prometheus_port]
367 elsif node[:nominatim][:api_flavour] == "python"
368 systemd_service "nominatim" do
369 description "Nominatim running as a gunicorn application"
372 working_directory project_directory
373 standard_output "append:#{node[:nominatim][:logdir]}/gunicorn.log"
374 standard_error "inherit"
375 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"
376 exec_reload "/bin/kill -s HUP $MAINPID"
377 environment :PYTHONPATH => "/usr/local/lib/nominatim/lib-python/"
381 requires "nominatim.socket"
382 after "network.target"
385 systemd_socket "nominatim" do
386 description "Gunicorn socket for Nominatim"
387 listen_stream "/run/gunicorn-nominatim.openstreetmap.org.sock"
388 socket_user "www-data"
392 ssl_certificate node[:fqdn] do
393 domains [node[:fqdn],
394 "nominatim.openstreetmap.org",
396 "nominatim.openstreetmap.com",
397 "nominatim.openstreetmap.net",
398 "nominatim.openstreetmaps.org",
399 "nominatim.openmaps.org",
400 "nominatim.qgis.org"]
401 notifies :reload, "service[nginx]"
404 include_recipe "nginx"
406 nginx_site "default" do
410 frontends = search(:node, "recipes:web\\:\\:frontend").sort_by(&:name)
412 nginx_site "nominatim" do
414 directory project_directory
415 variables :pools => node[:nominatim][:fpm_pools],
416 :frontends => frontends,
417 :confdir => "#{basedir}/etc",
418 :ui_directory => ui_directory
421 template "/etc/logrotate.d/nginx" do
422 source "logrotate.nginx.erb"
431 nominatim-update-source
432 nominatim-update-refresh-db
433 nominatim-update-data
434 nominatim-daily-maintenance].each do |fname|
435 template "#{bin_directory}/#{fname}" do
436 source "#{fname}.erb"
440 variables :bindir => bin_directory,
441 :srcdir => source_directory,
442 :builddir => build_directory,
443 :projectdir => project_directory,
444 :qabindir => qa_bin_directory,
445 :qadatadir => qa_data_directory
449 systemd_service "nominatim-update" do
450 description "Update the Nominatim database"
451 exec_start "#{bin_directory}/nominatim-update"
453 standard_output "append:#{node[:nominatim][:logdir]}/update.log"
454 standard_error "inherit"
455 working_directory project_directory
458 systemd_service "nominatim-update-maintenance-trigger" do
459 description "Trigger daily maintenance tasks for Nominatim DB"
460 exec_start "ln -sf #{bin_directory}/nominatim-daily-maintenance #{bin_directory}/maintenance/"
464 systemd_timer "nominatim-update-maintenance-trigger" do
465 action node[:nominatim][:state] != "off" ? :create : :delete
466 description "Schedule daily maintenance tasks for Nominatim DB"
467 on_calendar "*-*-* 02:03:00 UTC"
470 service "nominatim-update-maintenance-trigger" do
471 action node[:nominatim][:state] != "off" ? :enable : :disable
478 repository node[:nominatim][:ui_repository]
479 revision node[:nominatim][:ui_revision]
484 template "#{ui_directory}/dist/theme/config.theme.js" do
485 source "ui-config.js.erb"
493 if node[:nominatim][:enable_qa_tiles]
494 package "python3-geojson"
496 git qa_bin_directory do
497 repository node[:nominatim][:qa_repository]
498 revision node[:nominatim][:qa_revision]
499 enable_submodules true
502 notifies :run, "execute[compile_qa]"
505 execute "compile_qa" do
508 cwd "#{qa_bin_directory}/clustering-vt"
512 directory qa_data_directory do
519 template "#{qa_bin_directory}/analyser/config/config.yaml" do
520 source "qa_config.erb"
524 variables :outputdir => "#{qa_data_directory}/new"
527 ssl_certificate "qa-tile.nominatim.openstreetmap.org" do
528 domains ["qa-tile.nominatim.openstreetmap.org"]
529 notifies :reload, "service[nginx]"
532 nginx_site "qa-tiles.nominatim" do
533 template "nginx-qa-tiles.erb"
534 directory build_directory
535 variables :qa_data_directory => qa_data_directory
542 cron_d "nominatim-clean-db" do
543 action node[:nominatim][:state] == "master" ? :create : :delete
547 command "#{bin_directory}/clean-db-nominatim"
551 if node[:nominatim][:state] == "master"
552 postgresql_user "replication" do
553 cluster node[:nominatim][:dbcluster]
554 password data_bag_item("nominatim", "passwords")["replication"]
558 directory node[:rsyncd][:modules][:archive][:path] do
564 template "#{bin_directory}/clean-db-nominatim" do
565 source "clean-db-nominatim.erb"
569 variables :archive_dir => node[:rsyncd][:modules][:archive][:path],
570 :update_stop_file => "#{basedir}/status/updates_disabled",
571 :streaming_clients => search(:node, "nominatim_state:slave").map { |slave| slave[:fqdn] }.join(" ")
577 cron_d "nominatim-backup" do
578 action (node[:nominatim][:enable_backup] && node[:nominatim][:state] != "off") ? :create : :delete
583 command "#{bin_directory}/backup-nominatim"
587 cron_d "nominatim-vacuum-db" do
588 action node[:nominatim][:state] != "off" ? :create : :delete
592 command "#{bin_directory}/vacuum-db-nominatim"
596 %w[backup-nominatim vacuum-db-nominatim].each do |fname|
597 template "#{bin_directory}/#{fname}" do
598 source "#{fname}.erb"
602 variables :db => node[:nominatim][:dbname]
608 template "/etc/logrotate.d/nominatim" do
609 source "logrotate.nominatim.erb"
616 prometheus_exporter "nominatim" do
619 restrict_address_families "AF_UNIX"
621 "--nominatim.query-log=#{node[:nominatim][:logdir]}/query.log",
622 "--nominatim.database-name=#{node[:nominatim][:dbname]}"
626 include_recipe "fail2ban"
628 frontend_addresses = frontends.collect { |f| f.ipaddresses(:role => :external) }
630 fail2ban_jail "nominatim_limit_req" do
631 filter "nginx-limit-req"
632 logpath "#{node[:nominatim][:logdir]}/nominatim.openstreetmap.org-error.log"
635 ignoreips frontend_addresses.flatten.sort