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 platform?("debian")
24 include_recipe "postgresql"
25 include_recipe "python"
26 include_recipe "nginx"
28 include_recipe "fail2ban"
30 basedir = data_bag_item("accounts", "nominatim")["home"]
31 project_directory = "#{basedir}/planet-project"
32 bin_directory = "#{basedir}/bin"
33 cfg_directory = "#{basedir}/etc"
34 ui_directory = "#{basedir}/ui"
35 qa_bin_directory = "#{basedir}/Nominatim-Data-Analyser"
36 qa_data_directory = "#{basedir}/qa-data"
45 [basedir, bin_directory, cfg_directory, project_directory, ui_directory].each do |path|
53 if node[:nominatim][:flatnode_file]
54 directory File.dirname(node[:nominatim][:flatnode_file]) do
59 directory "#{bin_directory}/maintenance" do
65 ## Log directory setup
67 directory node[:nominatim][:logdir] do
74 file "#{node[:nominatim][:logdir]}/query.log" do
75 action :create_if_missing
83 postgresql_version = node[:nominatim][:dbcluster].split("/").first
84 postgis_version = node[:nominatim][:postgis]
86 package "postgresql-#{postgresql_version}-postgis-#{postgis_version}"
88 node[:nominatim][:dbadmins].each do |user|
89 postgresql_user user do
90 cluster node[:nominatim][:dbcluster]
95 postgresql_user "nominatim" do
96 cluster node[:nominatim][:dbcluster]
100 postgresql_user "www-data" do
101 cluster node[:nominatim][:dbcluster]
106 python_directory = "#{basedir}/venv"
115 python_virtualenv python_directory do
116 interpreter "/usr/bin/python3"
119 # These are updated during the database update.
120 python_package "nominatim-db" do
121 python_virtualenv python_directory
122 extra_index_url node[:nominatim][:pip_index]
125 python_package "nominatim-api" do
126 python_virtualenv python_directory
127 extra_index_url node[:nominatim][:pip_index]
130 remote_directory "#{project_directory}/static-website" do
135 files_owner "nominatim"
136 files_group "nominatim"
141 template "#{project_directory}/.env" do
142 source "nominatim.env.erb"
146 variables :base_url => node[:nominatim][:state] == "off" ? node[:fqdn] : "nominatim.openstreetmap.org",
147 :dbname => node[:nominatim][:dbname],
148 :flatnode_file => node[:nominatim][:flatnode_file],
149 :log_file => "#{node[:nominatim][:logdir]}/query.log",
150 :pool_size => node[:nominatim][:api_pool_size],
151 :query_timeout => node[:nominatim][:api_query_timeout],
152 :request_timeout => node[:nominatim][:api_request_timeout]
155 remote_file "#{project_directory}/secondary_importance.sql.gz" do
156 action :create_if_missing
157 source "https://nominatim.org/data/wikimedia-secondary-importance.sql.gz"
163 remote_file "#{project_directory}/wikimedia-importance.csv.gz" do
164 action :create_if_missing
165 source "https://nominatim.org/data/wikimedia-importance.csv.gz"
171 %w[gb_postcodes.csv.gz us_postcodes.csv.gz].each do |fname|
172 remote_file "#{project_directory}/#{fname}" do
174 source "https://nominatim.org/data/#{fname}"
181 # Webserver + frontend
183 %w[user_agent referrer email generic].each do |name|
184 file "#{cfg_directory}/nginx_blocked_#{name}.conf" do
185 action :create_if_missing
192 systemd_service "nominatim" do
193 description "Nominatim running as a gunicorn application"
196 working_directory project_directory
197 standard_output "append:#{node[:nominatim][:logdir]}/gunicorn.log"
198 standard_error "inherit"
199 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()'"
200 exec_reload "/bin/kill -s HUP $MAINPID"
204 requires "nominatim.socket"
205 after "network.target"
208 systemd_socket "nominatim" do
209 description "Gunicorn socket for Nominatim"
210 listen_stream "/run/gunicorn-nominatim.openstreetmap.org.sock"
211 socket_user "www-data"
214 ssl_certificate node[:fqdn] do
215 domains [node[:fqdn],
216 "nominatim.openstreetmap.org",
218 "nominatim.openstreetmap.com",
219 "nominatim.openstreetmap.net",
220 "nominatim.openstreetmaps.org",
221 "nominatim.openmaps.org",
222 "nominatim.qgis.org"]
223 notifies :reload, "service[nginx]"
226 nginx_site "default" do
230 frontends = search(:node, "recipes:web\\:\\:frontend").sort_by(&:name)
232 nginx_site "nominatim" do
234 directory project_directory
235 variables :pools => node[:nominatim][:fpm_pools],
236 :frontends => frontends,
237 :confdir => "#{basedir}/etc",
238 :ui_directory => ui_directory
241 template "/etc/logrotate.d/nginx" do
242 source "logrotate.nginx.erb"
248 ### Import, update and maintenance scripts
251 nominatim-update-data
252 nominatim-update-refresh-db
253 nominatim-daily-maintenance].each do |fname|
254 template "#{bin_directory}/#{fname}" do
255 source "#{fname}.erb"
259 variables :bindir => bin_directory,
260 :projectdir => project_directory,
261 :venvprefix => "#{python_directory}/bin/",
262 :qadatadir => qa_data_directory
266 systemd_service "nominatim-update" do
267 description "Update the Nominatim database"
268 exec_start "#{bin_directory}/nominatim-update"
270 standard_output "journal"
271 standard_error "inherit"
272 working_directory project_directory
275 systemd_service "nominatim-update-maintenance-trigger" do
276 description "Trigger daily maintenance tasks for Nominatim DB"
277 exec_start "ln -sf #{bin_directory}/nominatim-daily-maintenance #{bin_directory}/maintenance/"
281 systemd_timer "nominatim-update-maintenance-trigger" do
282 action node[:nominatim][:state] != "off" ? :create : :delete
283 description "Schedule daily maintenance tasks for Nominatim DB"
284 on_calendar "*-*-* 02:03:00 UTC"
287 service "nominatim-update-maintenance-trigger" do
288 action node[:nominatim][:state] != "off" ? :enable : :disable
295 repository node[:nominatim][:ui_repository]
296 revision node[:nominatim][:ui_revision]
301 template "#{ui_directory}/dist/theme/config.theme.js" do
302 source "ui-config.js.erb"
310 if node[:nominatim][:enable_qa_tiles]
311 python_package "nominatim-data-analyser" do
312 python_virtualenv python_directory
313 extra_index_url node[:nominatim][:pip_index]
316 directory qa_data_directory do
323 template "#{project_directory}/qa-config.yaml" do
324 source "qa_config.erb"
328 variables :outputdir => "#{qa_data_directory}/new"
331 ssl_certificate "qa-tile.nominatim.openstreetmap.org" do
332 domains ["qa-tile.nominatim.openstreetmap.org"]
333 notifies :reload, "service[nginx]"
336 nginx_site "qa-tiles.nominatim" do
337 template "nginx-qa-tiles.erb"
338 directory qa_data_directory
339 variables :qa_data_directory => qa_data_directory
343 ## Logging and monitoring
345 template "/etc/logrotate.d/nominatim" do
346 source "logrotate.nominatim.erb"
352 prometheus_exporter "nominatim" do
355 restrict_address_families "AF_UNIX"
357 "--nominatim.query-log=#{node[:nominatim][:logdir]}/query.log",
358 "--nominatim.database-name=#{node[:nominatim][:dbname]}"
362 ################################# END OF DEBIAN #############################
366 if node[:nominatim][:api_flavour] == "php"
367 include_recipe "php::fpm"
370 basedir = data_bag_item("accounts", "nominatim")["home"]
371 email_errors = data_bag_item("accounts", "lonvia")["email"]
380 ## Log directory setup
382 directory node[:nominatim][:logdir] do
389 file "#{node[:nominatim][:logdir]}/query.log" do
390 action :create_if_missing
396 file "#{node[:nominatim][:logdir]}/update.log" do
397 action :create_if_missing
405 include_recipe "postgresql"
407 postgresql_version = node[:nominatim][:dbcluster].split("/").first
408 postgis_version = node[:nominatim][:postgis]
410 package "postgresql-#{postgresql_version}-postgis-#{postgis_version}"
412 node[:nominatim][:dbadmins].each do |user|
413 postgresql_user user do
414 cluster node[:nominatim][:dbcluster]
416 only_if { node[:nominatim][:state] != "slave" }
420 postgresql_user "nominatim" do
421 cluster node[:nominatim][:dbcluster]
423 only_if { node[:nominatim][:state] != "slave" }
426 postgresql_user "www-data" do
427 cluster node[:nominatim][:dbcluster]
428 only_if { node[:nominatim][:state] != "slave" }
431 directory "#{basedir}/tablespaces" do
437 # NOTE: tablespaces must be exactly in the same location on each
438 # Nominatim instance when replication is in use. Therefore
439 # use symlinks to canonical directory locations.
440 node[:nominatim][:tablespaces].each do |name, location|
441 directory location do
448 link "#{basedir}/tablespaces/#{name}" do
452 postgresql_tablespace name do
453 cluster node[:nominatim][:dbcluster]
454 location "#{basedir}/tablespaces/#{name}"
461 include_recipe "python"
463 python_directory = "#{basedir}/venv"
471 libboost-filesystem-dev
490 python3-sqlalchemy-ext
501 if node[:nominatim][:api_flavour] == "php"
506 elsif node[:nominatim][:api_flavour] == "python"
508 python_virtualenv python_directory do
509 interpreter "/usr/bin/python3"
512 python_package "SQLAlchemy" do
513 python_virtualenv python_directory
517 python_package "PyICU" do
518 python_virtualenv python_directory
522 python_package "psycopg[binary]" do
523 python_virtualenv python_directory
527 python_package "psycopg2-binary" do
528 python_virtualenv python_directory
532 python_package "python-dotenv" do
533 python_virtualenv python_directory
537 python_package "pygments" do
538 python_virtualenv python_directory
542 python_package "PyYAML" do
543 python_virtualenv python_directory
547 python_package "falcon" do
548 python_virtualenv python_directory
552 python_package "uvicorn" do
553 python_virtualenv python_directory
557 python_package "gunicorn" do
558 python_virtualenv python_directory
562 python_package "jinja2" do
563 python_virtualenv python_directory
567 python_package "datrie" do
568 python_virtualenv python_directory
572 python_package "psutil" do
573 python_virtualenv python_directory
577 python_package "osmium" do
578 python_virtualenv python_directory
583 source_directory = "#{basedir}/src/nominatim"
584 build_directory = "#{basedir}/src/build"
585 project_directory = "#{basedir}/planet-project"
586 bin_directory = "#{basedir}/bin"
587 cfg_directory = "#{basedir}/etc"
588 ui_directory = "#{basedir}/ui"
589 qa_bin_directory = "#{basedir}/src/Nominatim-Data-Analyser"
590 qa_data_directory = "#{basedir}/qa-data"
592 [basedir, "#{basedir}/src", cfg_directory, bin_directory, build_directory, project_directory].each do |path|
601 directory "#{bin_directory}/maintenance" do
607 if node[:nominatim][:flatnode_file]
608 directory File.dirname(node[:nominatim][:flatnode_file]) do
613 remote_directory "#{project_directory}/static-website" do
618 files_owner "nominatim"
619 files_group "nominatim"
624 # Normally syncing via chef is a bad idea because syncing might involve
625 # an update of database functions which should not be done while an update
626 # is ongoing. Therefore we sync in between update cycles. There is an
627 # exception for slaves: they get DB function updates from the master, so
628 # only the source code needs to be updated, which chef may do.
629 git source_directory do
630 action node[:nominatim][:state] == "slave" ? :sync : :checkout
631 repository node[:nominatim][:repository]
632 revision node[:nominatim][:revision]
633 enable_submodules true
636 not_if { node[:nominatim][:state] != "slave" && File.exist?("#{source_directory}/README.md") }
637 notifies :run, "execute[compile_nominatim]"
640 remote_file "#{source_directory}/data/country_osm_grid.sql.gz" do
641 action :create_if_missing
642 source "https://nominatim.org/data/country_grid.sql.gz"
648 execute "compile_nominatim" do
652 command "cmake #{source_directory} && make"
653 notifies :run, "execute[install_nominatim]"
656 execute "install_nominatim" do
659 command "make install"
664 template "#{project_directory}/.env" do
665 source "nominatim.env.erb"
669 variables :base_url => node[:nominatim][:state] == "off" ? node[:fqdn] : "nominatim.openstreetmap.org",
670 :dbname => node[:nominatim][:dbname],
671 :flatnode_file => node[:nominatim][:flatnode_file],
672 :log_file => "#{node[:nominatim][:logdir]}/query.log",
673 :tokenizer => node[:nominatim][:config][:tokenizer],
674 :forward_dependencies => node[:nominatim][:config][:forward_dependencies],
675 :pool_size => node[:nominatim][:api_pool_size],
676 :query_timeout => node[:nominatim][:api_query_timeout],
677 :request_timeout => node[:nominatim][:api_request_timeout]
680 remote_file "#{project_directory}/secondary_importance.sql.gz" do
681 action :create_if_missing
682 source "https://nominatim.org/data/wikimedia-secondary-importance.sql.gz"
688 remote_file "#{project_directory}/wikimedia-importance.sql.gz" do
689 action :create_if_missing
690 source "https://nominatim.org/data/wikimedia-importance.sql.gz"
696 %w[gb_postcodes.csv.gz us_postcodes.csv.gz].each do |fname|
697 remote_file "#{project_directory}/#{fname}" do
699 source "https://nominatim.org/data/#{fname}"
706 # Webserver + frontend
708 %w[user_agent referrer email generic].each do |name|
709 file "#{cfg_directory}/nginx_blocked_#{name}.conf" do
710 action :create_if_missing
717 if node[:nominatim][:api_flavour] == "php"
718 node[:nominatim][:fpm_pools].each do |name, data|
722 pm_max_children data[:max_children]
724 pm_min_spare_servers 10
725 pm_max_spare_servers 20
726 pm_max_requests 10000
727 prometheus_port data[:prometheus_port]
730 elsif node[:nominatim][:api_flavour] == "python"
731 systemd_service "nominatim" do
732 description "Nominatim running as a gunicorn application"
735 working_directory project_directory
736 standard_output "append:#{node[:nominatim][:logdir]}/gunicorn.log"
737 standard_error "inherit"
738 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"
739 exec_reload "/bin/kill -s HUP $MAINPID"
740 environment :PYTHONPATH => "/usr/local/lib/nominatim/lib-python/"
744 requires "nominatim.socket"
745 after "network.target"
748 systemd_socket "nominatim" do
749 description "Gunicorn socket for Nominatim"
750 listen_stream "/run/gunicorn-nominatim.openstreetmap.org.sock"
751 socket_user "www-data"
755 ssl_certificate node[:fqdn] do
756 domains [node[:fqdn],
757 "nominatim.openstreetmap.org",
759 "nominatim.openstreetmap.com",
760 "nominatim.openstreetmap.net",
761 "nominatim.openstreetmaps.org",
762 "nominatim.openmaps.org",
763 "nominatim.qgis.org"]
764 notifies :reload, "service[nginx]"
767 include_recipe "nginx"
769 nginx_site "default" do
773 frontends = search(:node, "recipes:web\\:\\:frontend").sort_by(&:name)
775 nginx_site "nominatim" do
777 directory project_directory
778 variables :pools => node[:nominatim][:fpm_pools],
779 :frontends => frontends,
780 :confdir => "#{basedir}/etc",
781 :ui_directory => ui_directory
784 template "/etc/logrotate.d/nginx" do
785 source "logrotate.nginx.erb"
794 nominatim-update-source
795 nominatim-update-refresh-db
796 nominatim-update-data
797 nominatim-daily-maintenance].each do |fname|
798 template "#{bin_directory}/#{fname}" do
799 source "#{fname}.erb"
803 variables :bindir => bin_directory,
804 :srcdir => source_directory,
805 :builddir => build_directory,
806 :projectdir => project_directory,
807 :qabindir => qa_bin_directory,
808 :qadatadir => qa_data_directory
812 systemd_service "nominatim-update" do
813 description "Update the Nominatim database"
814 exec_start "#{bin_directory}/nominatim-update"
816 standard_output "append:#{node[:nominatim][:logdir]}/update.log"
817 standard_error "inherit"
818 working_directory project_directory
821 systemd_service "nominatim-update-maintenance-trigger" do
822 description "Trigger daily maintenance tasks for Nominatim DB"
823 exec_start "ln -sf #{bin_directory}/nominatim-daily-maintenance #{bin_directory}/maintenance/"
827 systemd_timer "nominatim-update-maintenance-trigger" do
828 action node[:nominatim][:state] != "off" ? :create : :delete
829 description "Schedule daily maintenance tasks for Nominatim DB"
830 on_calendar "*-*-* 02:03:00 UTC"
833 service "nominatim-update-maintenance-trigger" do
834 action node[:nominatim][:state] != "off" ? :enable : :disable
841 repository node[:nominatim][:ui_repository]
842 revision node[:nominatim][:ui_revision]
847 template "#{ui_directory}/dist/theme/config.theme.js" do
848 source "ui-config.js.erb"
856 if node[:nominatim][:enable_qa_tiles]
857 package "python3-geojson"
859 git qa_bin_directory do
860 repository node[:nominatim][:qa_repository]
861 revision node[:nominatim][:qa_revision]
862 enable_submodules true
865 notifies :run, "execute[compile_qa]"
868 execute "compile_qa" do
871 cwd "#{qa_bin_directory}/clustering-vt"
875 directory qa_data_directory do
882 template "#{qa_bin_directory}/analyser/config/config.yaml" do
883 source "qa_config.erb"
887 variables :outputdir => "#{qa_data_directory}/new"
890 ssl_certificate "qa-tile.nominatim.openstreetmap.org" do
891 domains ["qa-tile.nominatim.openstreetmap.org"]
892 notifies :reload, "service[nginx]"
895 nginx_site "qa-tiles.nominatim" do
896 template "nginx-qa-tiles.erb"
897 directory build_directory
898 variables :qa_data_directory => qa_data_directory
905 cron_d "nominatim-clean-db" do
906 action node[:nominatim][:state] == "master" ? :create : :delete
910 command "#{bin_directory}/clean-db-nominatim"
914 if node[:nominatim][:state] == "master"
915 postgresql_user "replication" do
916 cluster node[:nominatim][:dbcluster]
917 password data_bag_item("nominatim", "passwords")["replication"]
921 directory node[:rsyncd][:modules][:archive][:path] do
927 template "#{bin_directory}/clean-db-nominatim" do
928 source "clean-db-nominatim.erb"
932 variables :archive_dir => node[:rsyncd][:modules][:archive][:path],
933 :update_stop_file => "#{basedir}/status/updates_disabled",
934 :streaming_clients => search(:node, "nominatim_state:slave").map { |slave| slave[:fqdn] }.join(" ")
940 cron_d "nominatim-backup" do
941 action (node[:nominatim][:enable_backup] && node[:nominatim][:state] != "off") ? :create : :delete
946 command "#{bin_directory}/backup-nominatim"
950 cron_d "nominatim-vacuum-db" do
951 action node[:nominatim][:state] != "off" ? :create : :delete
955 command "#{bin_directory}/vacuum-db-nominatim"
959 %w[backup-nominatim vacuum-db-nominatim].each do |fname|
960 template "#{bin_directory}/#{fname}" do
961 source "#{fname}.erb"
965 variables :db => node[:nominatim][:dbname]
971 template "/etc/logrotate.d/nominatim" do
972 source "logrotate.nominatim.erb"
979 prometheus_exporter "nominatim" do
982 restrict_address_families "AF_UNIX"
984 "--nominatim.query-log=#{node[:nominatim][:logdir]}/query.log",
985 "--nominatim.database-name=#{node[:nominatim][:dbname]}"
989 include_recipe "fail2ban"
990 end # platform?('debian')
992 frontend_addresses = frontends.collect { |f| f.ipaddresses(:role => :external) }
994 fail2ban_jail "nominatim_limit_req" do
995 filter "nginx-limit-req"
996 logpath "#{node[:nominatim][:logdir]}/nominatim.openstreetmap.org-error.log"
999 ignoreips frontend_addresses.flatten.sort