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
152 python3-sqlalchemy-ext
163 if node[:nominatim][:api_flavour] == "php"
168 elsif node[:nominatim][:api_flavour] == "python"
170 python_virtualenv python_directory do
171 interpreter "/usr/bin/python3"
174 python_package "SQLAlchemy" do
175 python_virtualenv python_directory
179 python_package "PyICU" do
180 python_virtualenv python_directory
184 python_package "psycopg[binary]" do
185 python_virtualenv python_directory
189 python_package "psycopg2-binary" do
190 python_virtualenv python_directory
194 python_package "python-dotenv" 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 -D WITH_LUAJIT=ON #{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]
315 remote_file "#{project_directory}/wikimedia-importance.sql.gz" do
316 action :create_if_missing
317 source "https://nominatim.org/data/wikimedia-importance.sql.gz"
323 %w[gb_postcodes.csv.gz us_postcodes.csv.gz].each do |fname|
324 remote_file "#{project_directory}/#{fname}" do
326 source "https://nominatim.org/data/#{fname}"
333 # Webserver + frontend
335 %w[user_agent referrer email generic].each do |name|
336 file "#{cfg_directory}/nginx_blocked_#{name}.conf" do
337 action :create_if_missing
344 if node[:nominatim][:api_flavour] == "php"
345 node[:nominatim][:fpm_pools].each do |name, data|
349 pm_max_children data[:max_children]
351 pm_min_spare_servers 10
352 pm_max_spare_servers 20
353 pm_max_requests 10000
354 prometheus_port data[:prometheus_port]
357 elsif node[:nominatim][:api_flavour] == "python"
358 systemd_service "nominatim" do
359 description "Nominatim running as a gunicorn application"
362 working_directory project_directory
363 standard_output "append:#{node[:nominatim][:logdir]}/gunicorn.log"
364 standard_error "inherit"
365 exec_start "#{python_directory}/bin/gunicorn -b unix:/run/gunicorn-nominatim.openstreetmap.org.sock -w #{node[:nominatim][:api_workers]} -k uvicorn.workers.UvicornWorker nominatim.server.falcon.server:run_wsgi"
366 exec_reload "/bin/kill -s HUP $MAINPID"
367 environment :PYTHONPATH => "/usr/local/lib/nominatim/lib-python/"
371 requires "nominatim.socket"
372 after "network.target"
375 systemd_socket "nominatim" do
376 description "Gunicorn socket for Nominatim"
377 listen_stream "/run/gunicorn-nominatim.openstreetmap.org.sock"
378 socket_user "www-data"
382 ssl_certificate node[:fqdn] do
383 domains [node[:fqdn],
384 "nominatim.openstreetmap.org",
386 "nominatim.openstreetmap.com",
387 "nominatim.openstreetmap.net",
388 "nominatim.openstreetmaps.org",
389 "nominatim.openmaps.org",
390 "nominatim.qgis.org"]
391 notifies :reload, "service[nginx]"
394 include_recipe "nginx"
396 nginx_site "default" do
400 frontends = search(:node, "recipes:web\\:\\:frontend").sort_by(&:name)
402 nginx_site "nominatim" do
404 directory project_directory
405 variables :pools => node[:nominatim][:fpm_pools],
406 :frontends => frontends,
407 :confdir => "#{basedir}/etc",
408 :ui_directory => ui_directory
411 template "/etc/logrotate.d/nginx" do
412 source "logrotate.nginx.erb"
421 nominatim-update-source
422 nominatim-update-refresh-db
423 nominatim-update-data
424 nominatim-daily-maintenance].each do |fname|
425 template "#{bin_directory}/#{fname}" do
426 source "#{fname}.erb"
430 variables :bindir => bin_directory,
431 :srcdir => source_directory,
432 :builddir => build_directory,
433 :projectdir => project_directory,
434 :qabindir => qa_bin_directory,
435 :qadatadir => qa_data_directory
439 systemd_service "nominatim-update" do
440 description "Update the Nominatim database"
441 exec_start "#{bin_directory}/nominatim-update"
443 standard_output "append:#{node[:nominatim][:logdir]}/update.log"
444 standard_error "inherit"
445 working_directory project_directory
448 systemd_service "nominatim-update-maintenance-trigger" do
449 description "Trigger daily maintenance tasks for Nominatim DB"
450 exec_start "ln -sf #{bin_directory}/nominatim-daily-maintenance #{bin_directory}/maintenance/"
454 systemd_timer "nominatim-update-maintenance-trigger" do
455 action node[:nominatim][:state] != "off" ? :create : :delete
456 description "Schedule daily maintenance tasks for Nominatim DB"
457 on_calendar "*-*-* 02:03:00 UTC"
460 service "nominatim-update-maintenance-trigger" do
461 action node[:nominatim][:state] != "off" ? :enable : :disable
468 repository node[:nominatim][:ui_repository]
469 revision node[:nominatim][:ui_revision]
474 template "#{ui_directory}/dist/theme/config.theme.js" do
475 source "ui-config.js.erb"
483 if node[:nominatim][:enable_qa_tiles]
484 package "python3-geojson"
486 git qa_bin_directory do
487 repository node[:nominatim][:qa_repository]
488 revision node[:nominatim][:qa_revision]
489 enable_submodules true
492 notifies :run, "execute[compile_qa]"
495 execute "compile_qa" do
498 cwd "#{qa_bin_directory}/clustering-vt"
502 directory qa_data_directory do
509 template "#{qa_bin_directory}/analyser/config/config.yaml" do
510 source "qa_config.erb"
514 variables :outputdir => "#{qa_data_directory}/new"
517 ssl_certificate "qa-tile.nominatim.openstreetmap.org" do
518 domains ["qa-tile.nominatim.openstreetmap.org"]
519 notifies :reload, "service[nginx]"
522 nginx_site "qa-tiles.nominatim" do
523 template "nginx-qa-tiles.erb"
524 directory build_directory
525 variables :qa_data_directory => qa_data_directory
532 cron_d "nominatim-clean-db" do
533 action node[:nominatim][:state] == "master" ? :create : :delete
537 command "#{bin_directory}/clean-db-nominatim"
541 if node[:nominatim][:state] == "master"
542 postgresql_user "replication" do
543 cluster node[:nominatim][:dbcluster]
544 password data_bag_item("nominatim", "passwords")["replication"]
548 directory node[:rsyncd][:modules][:archive][:path] do
554 template "#{bin_directory}/clean-db-nominatim" do
555 source "clean-db-nominatim.erb"
559 variables :archive_dir => node[:rsyncd][:modules][:archive][:path],
560 :update_stop_file => "#{basedir}/status/updates_disabled",
561 :streaming_clients => search(:node, "nominatim_state:slave").map { |slave| slave[:fqdn] }.join(" ")
567 cron_d "nominatim-backup" do
568 action (node[:nominatim][:enable_backup] && node[:nominatim][:state] != "off") ? :create : :delete
573 command "#{bin_directory}/backup-nominatim"
577 cron_d "nominatim-vacuum-db" do
578 action node[:nominatim][:state] != "off" ? :create : :delete
582 command "#{bin_directory}/vacuum-db-nominatim"
586 %w[backup-nominatim vacuum-db-nominatim].each do |fname|
587 template "#{bin_directory}/#{fname}" do
588 source "#{fname}.erb"
592 variables :db => node[:nominatim][:dbname]
598 template "/etc/logrotate.d/nominatim" do
599 source "logrotate.nominatim.erb"
607 munin_plugin_conf "nominatim" do
609 variables :db => node[:nominatim][:dbname],
610 :querylog => "#{node[:nominatim][:logdir]}/query.log"
613 munin_plugin "nominatim_importlag" do
614 target "#{source_directory}/munin/nominatim_importlag"
617 munin_plugin "nominatim_query_speed" do
618 target "#{source_directory}/munin/nominatim_query_speed_querylog"
621 munin_plugin "nominatim_requests" do
622 target "#{source_directory}/munin/nominatim_requests_querylog"
625 prometheus_exporter "nominatim" do
628 restrict_address_families "AF_UNIX"
630 "--nominatim.query-log=#{node[:nominatim][:logdir]}/query.log",
631 "--nominatim.database-name=#{node[:nominatim][:dbname]}"
635 include_recipe "fail2ban"
637 frontend_addresses = frontends.collect { |f| f.ipaddresses(:role => :external) }
639 fail2ban_jail "nominatim_limit_req" do
640 filter "nginx-limit-req"
641 logpath "#{node[:nominatim][:logdir]}/nominatim.openstreetmap.org-error.log"
644 ignoreips frontend_addresses.flatten.sort