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
235 source_directory = "#{basedir}/src/nominatim"
236 build_directory = "#{basedir}/src/build"
237 project_directory = "#{basedir}/planet-project"
238 bin_directory = "#{basedir}/bin"
239 cfg_directory = "#{basedir}/etc"
240 ui_directory = "#{basedir}/ui"
241 qa_bin_directory = "#{basedir}/src/Nominatim-Data-Analyser"
242 qa_data_directory = "#{basedir}/qa-data"
244 [basedir, "#{basedir}/src", cfg_directory, bin_directory, build_directory, project_directory].each do |path|
253 directory "#{bin_directory}/maintenance" do
259 if node[:nominatim][:flatnode_file]
260 directory File.dirname(node[:nominatim][:flatnode_file]) do
265 remote_directory "#{project_directory}/static-website" do
270 files_owner "nominatim"
271 files_group "nominatim"
276 # Normally syncing via chef is a bad idea because syncing might involve
277 # an update of database functions which should not be done while an update
278 # is ongoing. Therefore we sync in between update cycles. There is an
279 # exception for slaves: they get DB function updates from the master, so
280 # only the source code needs to be updated, which chef may do.
281 git source_directory do
282 action node[:nominatim][:state] == "slave" ? :sync : :checkout
283 repository node[:nominatim][:repository]
284 revision node[:nominatim][:revision]
285 enable_submodules true
288 not_if { node[:nominatim][:state] != "slave" && File.exist?("#{source_directory}/README.md") }
289 notifies :run, "execute[compile_nominatim]"
292 remote_file "#{source_directory}/data/country_osm_grid.sql.gz" do
293 action :create_if_missing
294 source "https://nominatim.org/data/country_grid.sql.gz"
300 execute "compile_nominatim" do
304 command "cmake #{source_directory} && make"
305 notifies :run, "execute[install_nominatim]"
308 execute "install_nominatim" do
311 command "make install"
316 template "#{project_directory}/.env" do
317 source "nominatim.env.erb"
321 variables :base_url => node[:nominatim][:state] == "off" ? node[:fqdn] : "nominatim.openstreetmap.org",
322 :dbname => node[:nominatim][:dbname],
323 :flatnode_file => node[:nominatim][:flatnode_file],
324 :log_file => "#{node[:nominatim][:logdir]}/query.log",
325 :tokenizer => node[:nominatim][:config][:tokenizer],
326 :forward_dependencies => node[:nominatim][:config][:forward_dependencies],
327 :pool_size => node[:nominatim][:api_pool_size],
328 :query_timeout => node[:nominatim][:api_query_timeout],
329 :request_timeout => node[:nominatim][:api_request_timeout]
332 remote_file "#{project_directory}/secondary_importance.sql.gz" do
333 action :create_if_missing
334 source "https://nominatim.org/data/wikimedia-secondary-importance.sql.gz"
340 remote_file "#{project_directory}/wikimedia-importance.sql.gz" do
341 action :create_if_missing
342 source "https://nominatim.org/data/wikimedia-importance.sql.gz"
348 %w[gb_postcodes.csv.gz us_postcodes.csv.gz].each do |fname|
349 remote_file "#{project_directory}/#{fname}" do
351 source "https://nominatim.org/data/#{fname}"
358 # Webserver + frontend
360 %w[user_agent referrer email generic].each do |name|
361 file "#{cfg_directory}/nginx_blocked_#{name}.conf" do
362 action :create_if_missing
369 if node[:nominatim][:api_flavour] == "php"
370 node[:nominatim][:fpm_pools].each do |name, data|
374 pm_max_children data[:max_children]
376 pm_min_spare_servers 10
377 pm_max_spare_servers 20
378 pm_max_requests 10000
379 prometheus_port data[:prometheus_port]
382 elsif node[:nominatim][:api_flavour] == "python"
383 systemd_service "nominatim" do
384 description "Nominatim running as a gunicorn application"
387 working_directory project_directory
388 standard_output "append:#{node[:nominatim][:logdir]}/gunicorn.log"
389 standard_error "inherit"
390 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"
391 exec_reload "/bin/kill -s HUP $MAINPID"
392 environment :PYTHONPATH => "/usr/local/lib/nominatim/lib-python/"
396 requires "nominatim.socket"
397 after "network.target"
400 systemd_socket "nominatim" do
401 description "Gunicorn socket for Nominatim"
402 listen_stream "/run/gunicorn-nominatim.openstreetmap.org.sock"
403 socket_user "www-data"
407 ssl_certificate node[:fqdn] do
408 domains [node[:fqdn],
409 "nominatim.openstreetmap.org",
411 "nominatim.openstreetmap.com",
412 "nominatim.openstreetmap.net",
413 "nominatim.openstreetmaps.org",
414 "nominatim.openmaps.org",
415 "nominatim.qgis.org"]
416 notifies :reload, "service[nginx]"
419 include_recipe "nginx"
421 nginx_site "default" do
425 frontends = search(:node, "recipes:web\\:\\:frontend").sort_by(&:name)
427 nginx_site "nominatim" do
429 directory project_directory
430 variables :pools => node[:nominatim][:fpm_pools],
431 :frontends => frontends,
432 :confdir => "#{basedir}/etc",
433 :ui_directory => ui_directory
436 template "/etc/logrotate.d/nginx" do
437 source "logrotate.nginx.erb"
446 nominatim-update-source
447 nominatim-update-refresh-db
448 nominatim-update-data
449 nominatim-daily-maintenance].each do |fname|
450 template "#{bin_directory}/#{fname}" do
451 source "#{fname}.erb"
455 variables :bindir => bin_directory,
456 :srcdir => source_directory,
457 :builddir => build_directory,
458 :projectdir => project_directory,
459 :qabindir => qa_bin_directory,
460 :qadatadir => qa_data_directory
464 systemd_service "nominatim-update" do
465 description "Update the Nominatim database"
466 exec_start "#{bin_directory}/nominatim-update"
468 standard_output "append:#{node[:nominatim][:logdir]}/update.log"
469 standard_error "inherit"
470 working_directory project_directory
473 systemd_service "nominatim-update-maintenance-trigger" do
474 description "Trigger daily maintenance tasks for Nominatim DB"
475 exec_start "ln -sf #{bin_directory}/nominatim-daily-maintenance #{bin_directory}/maintenance/"
479 systemd_timer "nominatim-update-maintenance-trigger" do
480 action node[:nominatim][:state] != "off" ? :create : :delete
481 description "Schedule daily maintenance tasks for Nominatim DB"
482 on_calendar "*-*-* 02:03:00 UTC"
485 service "nominatim-update-maintenance-trigger" do
486 action node[:nominatim][:state] != "off" ? :enable : :disable
493 repository node[:nominatim][:ui_repository]
494 revision node[:nominatim][:ui_revision]
499 template "#{ui_directory}/dist/theme/config.theme.js" do
500 source "ui-config.js.erb"
508 if node[:nominatim][:enable_qa_tiles]
509 package "python3-geojson"
511 git qa_bin_directory do
512 repository node[:nominatim][:qa_repository]
513 revision node[:nominatim][:qa_revision]
514 enable_submodules true
517 notifies :run, "execute[compile_qa]"
520 execute "compile_qa" do
523 cwd "#{qa_bin_directory}/clustering-vt"
527 directory qa_data_directory do
534 template "#{qa_bin_directory}/analyser/config/config.yaml" do
535 source "qa_config.erb"
539 variables :outputdir => "#{qa_data_directory}/new"
542 ssl_certificate "qa-tile.nominatim.openstreetmap.org" do
543 domains ["qa-tile.nominatim.openstreetmap.org"]
544 notifies :reload, "service[nginx]"
547 nginx_site "qa-tiles.nominatim" do
548 template "nginx-qa-tiles.erb"
549 directory build_directory
550 variables :qa_data_directory => qa_data_directory
557 cron_d "nominatim-clean-db" do
558 action node[:nominatim][:state] == "master" ? :create : :delete
562 command "#{bin_directory}/clean-db-nominatim"
566 if node[:nominatim][:state] == "master"
567 postgresql_user "replication" do
568 cluster node[:nominatim][:dbcluster]
569 password data_bag_item("nominatim", "passwords")["replication"]
573 directory node[:rsyncd][:modules][:archive][:path] do
579 template "#{bin_directory}/clean-db-nominatim" do
580 source "clean-db-nominatim.erb"
584 variables :archive_dir => node[:rsyncd][:modules][:archive][:path],
585 :update_stop_file => "#{basedir}/status/updates_disabled",
586 :streaming_clients => search(:node, "nominatim_state:slave").map { |slave| slave[:fqdn] }.join(" ")
592 cron_d "nominatim-backup" do
593 action (node[:nominatim][:enable_backup] && node[:nominatim][:state] != "off") ? :create : :delete
598 command "#{bin_directory}/backup-nominatim"
602 cron_d "nominatim-vacuum-db" do
603 action node[:nominatim][:state] != "off" ? :create : :delete
607 command "#{bin_directory}/vacuum-db-nominatim"
611 %w[backup-nominatim vacuum-db-nominatim].each do |fname|
612 template "#{bin_directory}/#{fname}" do
613 source "#{fname}.erb"
617 variables :db => node[:nominatim][:dbname]
623 template "/etc/logrotate.d/nominatim" do
624 source "logrotate.nominatim.erb"
631 prometheus_exporter "nominatim" do
634 restrict_address_families "AF_UNIX"
636 "--nominatim.query-log=#{node[:nominatim][:logdir]}/query.log",
637 "--nominatim.database-name=#{node[:nominatim][:dbname]}"
641 include_recipe "fail2ban"
643 frontend_addresses = frontends.collect { |f| f.ipaddresses(:role => :external) }
645 fail2ban_jail "nominatim_limit_req" do
646 filter "nginx-limit-req"
647 logpath "#{node[:nominatim][:logdir]}/nominatim.openstreetmap.org-error.log"
650 ignoreips frontend_addresses.flatten.sort