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}/wikimedia-importance.sql.gz" do
318 action :create_if_missing
319 source "https://nominatim.org/data/wikimedia-importance.sql.gz"
325 %w[gb_postcodes.csv.gz us_postcodes.csv.gz].each do |fname|
326 remote_file "#{project_directory}/#{fname}" do
328 source "https://nominatim.org/data/#{fname}"
335 # Webserver + frontend
337 %w[user_agent referrer email generic].each do |name|
338 file "#{cfg_directory}/nginx_blocked_#{name}.conf" do
339 action :create_if_missing
346 if node[:nominatim][:api_flavour] == "php"
347 node[:nominatim][:fpm_pools].each do |name, data|
351 pm_max_children data[:max_children]
353 pm_min_spare_servers 10
354 pm_max_spare_servers 20
355 pm_max_requests 10000
356 prometheus_port data[:prometheus_port]
359 elsif node[:nominatim][:api_flavour] == "python"
360 systemd_service "nominatim" do
361 description "Nominatim running as a gunicorn application"
364 working_directory project_directory
365 standard_output "append:#{node[:nominatim][:logdir]}/gunicorn.log"
366 standard_error "inherit"
367 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"
368 exec_reload "/bin/kill -s HUP $MAINPID"
369 environment :PYTHONPATH => "/usr/local/lib/nominatim/lib-python/"
373 requires "nominatim.socket"
374 after "network.target"
377 systemd_socket "nominatim" do
378 description "Gunicorn socket for Nominatim"
379 listen_stream "/run/gunicorn-nominatim.openstreetmap.org.sock"
380 socket_user "www-data"
384 ssl_certificate node[:fqdn] do
385 domains [node[:fqdn],
386 "nominatim.openstreetmap.org",
388 "nominatim.openstreetmap.com",
389 "nominatim.openstreetmap.net",
390 "nominatim.openstreetmaps.org",
391 "nominatim.openmaps.org",
392 "nominatim.qgis.org"]
393 notifies :reload, "service[nginx]"
396 include_recipe "nginx"
398 nginx_site "default" do
402 frontends = search(:node, "recipes:web\\:\\:frontend").sort_by(&:name)
404 nginx_site "nominatim" do
406 directory project_directory
407 variables :pools => node[:nominatim][:fpm_pools],
408 :frontends => frontends,
409 :confdir => "#{basedir}/etc",
410 :ui_directory => ui_directory
413 template "/etc/logrotate.d/nginx" do
414 source "logrotate.nginx.erb"
423 nominatim-update-source
424 nominatim-update-refresh-db
425 nominatim-update-data
426 nominatim-daily-maintenance].each do |fname|
427 template "#{bin_directory}/#{fname}" do
428 source "#{fname}.erb"
432 variables :bindir => bin_directory,
433 :srcdir => source_directory,
434 :builddir => build_directory,
435 :projectdir => project_directory,
436 :qabindir => qa_bin_directory,
437 :qadatadir => qa_data_directory
441 systemd_service "nominatim-update" do
442 description "Update the Nominatim database"
443 exec_start "#{bin_directory}/nominatim-update"
445 standard_output "append:#{node[:nominatim][:logdir]}/update.log"
446 standard_error "inherit"
447 working_directory project_directory
450 systemd_service "nominatim-update-maintenance-trigger" do
451 description "Trigger daily maintenance tasks for Nominatim DB"
452 exec_start "ln -sf #{bin_directory}/nominatim-daily-maintenance #{bin_directory}/maintenance/"
456 systemd_timer "nominatim-update-maintenance-trigger" do
457 action node[:nominatim][:state] != "off" ? :create : :delete
458 description "Schedule daily maintenance tasks for Nominatim DB"
459 on_calendar "*-*-* 02:03:00 UTC"
462 service "nominatim-update-maintenance-trigger" do
463 action node[:nominatim][:state] != "off" ? :enable : :disable
470 repository node[:nominatim][:ui_repository]
471 revision node[:nominatim][:ui_revision]
476 template "#{ui_directory}/dist/theme/config.theme.js" do
477 source "ui-config.js.erb"
485 if node[:nominatim][:enable_qa_tiles]
486 package "python3-geojson"
488 git qa_bin_directory do
489 repository node[:nominatim][:qa_repository]
490 revision node[:nominatim][:qa_revision]
491 enable_submodules true
494 notifies :run, "execute[compile_qa]"
497 execute "compile_qa" do
500 cwd "#{qa_bin_directory}/clustering-vt"
504 directory qa_data_directory do
511 template "#{qa_bin_directory}/analyser/config/config.yaml" do
512 source "qa_config.erb"
516 variables :outputdir => "#{qa_data_directory}/new"
519 ssl_certificate "qa-tile.nominatim.openstreetmap.org" do
520 domains ["qa-tile.nominatim.openstreetmap.org"]
521 notifies :reload, "service[nginx]"
524 nginx_site "qa-tiles.nominatim" do
525 template "nginx-qa-tiles.erb"
526 directory build_directory
527 variables :qa_data_directory => qa_data_directory
534 cron_d "nominatim-clean-db" do
535 action node[:nominatim][:state] == "master" ? :create : :delete
539 command "#{bin_directory}/clean-db-nominatim"
543 if node[:nominatim][:state] == "master"
544 postgresql_user "replication" do
545 cluster node[:nominatim][:dbcluster]
546 password data_bag_item("nominatim", "passwords")["replication"]
550 directory node[:rsyncd][:modules][:archive][:path] do
556 template "#{bin_directory}/clean-db-nominatim" do
557 source "clean-db-nominatim.erb"
561 variables :archive_dir => node[:rsyncd][:modules][:archive][:path],
562 :update_stop_file => "#{basedir}/status/updates_disabled",
563 :streaming_clients => search(:node, "nominatim_state:slave").map { |slave| slave[:fqdn] }.join(" ")
569 cron_d "nominatim-backup" do
570 action (node[:nominatim][:enable_backup] && node[:nominatim][:state] != "off") ? :create : :delete
575 command "#{bin_directory}/backup-nominatim"
579 cron_d "nominatim-vacuum-db" do
580 action node[:nominatim][:state] != "off" ? :create : :delete
584 command "#{bin_directory}/vacuum-db-nominatim"
588 %w[backup-nominatim vacuum-db-nominatim].each do |fname|
589 template "#{bin_directory}/#{fname}" do
590 source "#{fname}.erb"
594 variables :db => node[:nominatim][:dbname]
600 template "/etc/logrotate.d/nominatim" do
601 source "logrotate.nominatim.erb"
608 prometheus_exporter "nominatim" do
611 restrict_address_families "AF_UNIX"
613 "--nominatim.query-log=#{node[:nominatim][:logdir]}/query.log",
614 "--nominatim.database-name=#{node[:nominatim][:dbname]}"
618 include_recipe "fail2ban"
620 frontend_addresses = frontends.collect { |f| f.ipaddresses(:role => :external) }
622 fail2ban_jail "nominatim_limit_req" do
623 filter "nginx-limit-req"
624 logpath "#{node[:nominatim][:logdir]}/nominatim.openstreetmap.org-error.log"
627 ignoreips frontend_addresses.flatten.sort