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"
116 python_virtualenv python_directory do
117 interpreter "/usr/bin/python3"
120 # These are updated during the database update.
121 python_package "nominatim-db" do
122 python_virtualenv python_directory
123 extra_index_url node[:nominatim][:pip_index]
126 python_package "nominatim-api" do
127 python_virtualenv python_directory
128 extra_index_url node[:nominatim][:pip_index]
131 remote_directory "#{project_directory}/static-website" do
136 files_owner "nominatim"
137 files_group "nominatim"
142 template "#{project_directory}/.env" do
143 source "nominatim.env.erb"
147 variables :base_url => node[:nominatim][:state] == "off" ? node[:fqdn] : "nominatim.openstreetmap.org",
148 :dbname => node[:nominatim][:dbname],
149 :flatnode_file => node[:nominatim][:flatnode_file],
150 :log_file => "#{node[:nominatim][:logdir]}/query.log",
151 :pool_size => node[:nominatim][:api_pool_size],
152 :query_timeout => node[:nominatim][:api_query_timeout],
153 :request_timeout => node[:nominatim][:api_request_timeout]
156 remote_file "#{project_directory}/secondary_importance.sql.gz" do
157 action :create_if_missing
158 source "https://nominatim.org/data/wikimedia-secondary-importance.sql.gz"
164 remote_file "#{project_directory}/wikimedia-importance.csv.gz" do
165 action :create_if_missing
166 source "https://nominatim.org/data/wikimedia-importance.csv.gz"
172 %w[gb_postcodes.csv.gz us_postcodes.csv.gz].each do |fname|
173 remote_file "#{project_directory}/#{fname}" do
175 source "https://nominatim.org/data/#{fname}"
182 # Webserver + frontend
184 %w[user_agent referrer email generic].each do |name|
185 file "#{cfg_directory}/nginx_blocked_#{name}.conf" do
186 action :create_if_missing
193 systemd_service "nominatim" do
194 description "Nominatim running as a gunicorn application"
197 working_directory project_directory
198 standard_output "append:#{node[:nominatim][:logdir]}/gunicorn.log"
199 standard_error "inherit"
200 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()'"
201 exec_reload "/bin/kill -s HUP $MAINPID"
205 requires "nominatim.socket"
206 after "network.target"
209 systemd_socket "nominatim" do
210 description "Gunicorn socket for Nominatim"
211 listen_stream "/run/gunicorn-nominatim.openstreetmap.org.sock"
212 socket_user "www-data"
215 ssl_certificate node[:fqdn] do
216 domains [node[:fqdn],
217 "nominatim.openstreetmap.org",
219 "nominatim.openstreetmap.com",
220 "nominatim.openstreetmap.net",
221 "nominatim.openstreetmaps.org",
222 "nominatim.openmaps.org",
223 "nominatim.qgis.org"]
224 notifies :reload, "service[nginx]"
227 nginx_site "default" do
231 frontends = search(:node, "recipes:web\\:\\:frontend").sort_by(&:name)
233 nginx_site "nominatim" do
235 directory project_directory
236 variables :pools => node[:nominatim][:fpm_pools],
237 :frontends => frontends,
238 :confdir => "#{basedir}/etc",
239 :ui_directory => ui_directory
242 template "/etc/logrotate.d/nginx" do
243 source "logrotate.nginx.erb"
249 ### Import, update and maintenance scripts
252 nominatim-update-data
253 nominatim-update-refresh-db
254 nominatim-daily-maintenance].each do |fname|
255 template "#{bin_directory}/#{fname}" do
256 source "#{fname}.erb"
260 variables :bindir => bin_directory,
261 :projectdir => project_directory,
262 :venvprefix => "#{python_directory}/bin/",
263 :qadatadir => qa_data_directory
267 systemd_service "nominatim-update" do
268 description "Update the Nominatim database"
269 exec_start "#{bin_directory}/nominatim-update"
271 standard_output "journal"
272 standard_error "inherit"
273 working_directory project_directory
276 systemd_service "nominatim-update-maintenance-trigger" do
277 description "Trigger daily maintenance tasks for Nominatim DB"
278 exec_start "ln -sf #{bin_directory}/nominatim-daily-maintenance #{bin_directory}/maintenance/"
282 systemd_timer "nominatim-update-maintenance-trigger" do
283 action node[:nominatim][:state] != "off" ? :create : :delete
284 description "Schedule daily maintenance tasks for Nominatim DB"
285 on_calendar "*-*-* 02:03:00 UTC"
288 service "nominatim-update-maintenance-trigger" do
289 action node[:nominatim][:state] != "off" ? :enable : :disable
296 repository node[:nominatim][:ui_repository]
297 revision node[:nominatim][:ui_revision]
302 template "#{ui_directory}/dist/theme/config.theme.js" do
303 source "ui-config.js.erb"
311 if node[:nominatim][:enable_qa_tiles]
312 python_package "nominatim-data-analyser" do
313 python_virtualenv python_directory
314 extra_index_url node[:nominatim][:pip_index]
317 directory qa_data_directory do
324 template "#{project_directory}/qa-config.yaml" do
325 source "qa_config.erb"
329 variables :outputdir => "#{qa_data_directory}/new"
332 ssl_certificate "qa-tile.nominatim.openstreetmap.org" do
333 domains ["qa-tile.nominatim.openstreetmap.org"]
334 notifies :reload, "service[nginx]"
337 nginx_site "qa-tiles.nominatim" do
338 template "nginx-qa-tiles.erb"
339 directory qa_data_directory
340 variables :qa_data_directory => qa_data_directory
344 ## Logging and monitoring
346 template "/etc/logrotate.d/nominatim" do
347 source "logrotate.nominatim.erb"
353 prometheus_exporter "nominatim" do
356 restrict_address_families "AF_UNIX"
358 "--nominatim.query-log=#{node[:nominatim][:logdir]}/query.log",
359 "--nominatim.database-name=#{node[:nominatim][:dbname]}"
363 ################################# END OF DEBIAN #############################
367 if node[:nominatim][:api_flavour] == "php"
368 include_recipe "php::fpm"
371 basedir = data_bag_item("accounts", "nominatim")["home"]
372 email_errors = data_bag_item("accounts", "lonvia")["email"]
381 ## Log directory setup
383 directory node[:nominatim][:logdir] do
390 file "#{node[:nominatim][:logdir]}/query.log" do
391 action :create_if_missing
397 file "#{node[:nominatim][:logdir]}/update.log" do
398 action :create_if_missing
406 include_recipe "postgresql"
408 postgresql_version = node[:nominatim][:dbcluster].split("/").first
409 postgis_version = node[:nominatim][:postgis]
411 package "postgresql-#{postgresql_version}-postgis-#{postgis_version}"
413 node[:nominatim][:dbadmins].each do |user|
414 postgresql_user user do
415 cluster node[:nominatim][:dbcluster]
417 only_if { node[:nominatim][:state] != "slave" }
421 postgresql_user "nominatim" do
422 cluster node[:nominatim][:dbcluster]
424 only_if { node[:nominatim][:state] != "slave" }
427 postgresql_user "www-data" do
428 cluster node[:nominatim][:dbcluster]
429 only_if { node[:nominatim][:state] != "slave" }
432 directory "#{basedir}/tablespaces" do
438 # NOTE: tablespaces must be exactly in the same location on each
439 # Nominatim instance when replication is in use. Therefore
440 # use symlinks to canonical directory locations.
441 node[:nominatim][:tablespaces].each do |name, location|
442 directory location do
449 link "#{basedir}/tablespaces/#{name}" do
453 postgresql_tablespace name do
454 cluster node[:nominatim][:dbcluster]
455 location "#{basedir}/tablespaces/#{name}"
462 include_recipe "python"
464 python_directory = "#{basedir}/venv"
472 libboost-filesystem-dev
491 python3-sqlalchemy-ext
502 if node[:nominatim][:api_flavour] == "php"
507 elsif node[:nominatim][:api_flavour] == "python"
509 python_virtualenv python_directory do
510 interpreter "/usr/bin/python3"
513 python_package "SQLAlchemy" do
514 python_virtualenv python_directory
518 python_package "PyICU" do
519 python_virtualenv python_directory
523 python_package "psycopg[binary]" do
524 python_virtualenv python_directory
528 python_package "psycopg2-binary" do
529 python_virtualenv python_directory
533 python_package "python-dotenv" do
534 python_virtualenv python_directory
538 python_package "pygments" do
539 python_virtualenv python_directory
543 python_package "PyYAML" do
544 python_virtualenv python_directory
548 python_package "falcon" do
549 python_virtualenv python_directory
553 python_package "uvicorn" do
554 python_virtualenv python_directory
558 python_package "gunicorn" do
559 python_virtualenv python_directory
563 python_package "jinja2" do
564 python_virtualenv python_directory
568 python_package "datrie" do
569 python_virtualenv python_directory
573 python_package "psutil" do
574 python_virtualenv python_directory
578 python_package "osmium" do
579 python_virtualenv python_directory
584 source_directory = "#{basedir}/src/nominatim"
585 build_directory = "#{basedir}/src/build"
586 project_directory = "#{basedir}/planet-project"
587 bin_directory = "#{basedir}/bin"
588 cfg_directory = "#{basedir}/etc"
589 ui_directory = "#{basedir}/ui"
590 qa_bin_directory = "#{basedir}/src/Nominatim-Data-Analyser"
591 qa_data_directory = "#{basedir}/qa-data"
593 [basedir, "#{basedir}/src", cfg_directory, bin_directory, build_directory, project_directory].each do |path|
602 directory "#{bin_directory}/maintenance" do
608 if node[:nominatim][:flatnode_file]
609 directory File.dirname(node[:nominatim][:flatnode_file]) do
614 remote_directory "#{project_directory}/static-website" do
619 files_owner "nominatim"
620 files_group "nominatim"
625 # Normally syncing via chef is a bad idea because syncing might involve
626 # an update of database functions which should not be done while an update
627 # is ongoing. Therefore we sync in between update cycles. There is an
628 # exception for slaves: they get DB function updates from the master, so
629 # only the source code needs to be updated, which chef may do.
630 git source_directory do
631 action node[:nominatim][:state] == "slave" ? :sync : :checkout
632 repository node[:nominatim][:repository]
633 revision node[:nominatim][:revision]
634 enable_submodules true
637 not_if { node[:nominatim][:state] != "slave" && File.exist?("#{source_directory}/README.md") }
638 notifies :run, "execute[compile_nominatim]"
641 remote_file "#{source_directory}/data/country_osm_grid.sql.gz" do
642 action :create_if_missing
643 source "https://nominatim.org/data/country_grid.sql.gz"
649 execute "compile_nominatim" do
653 command "cmake #{source_directory} && make"
654 notifies :run, "execute[install_nominatim]"
657 execute "install_nominatim" do
660 command "make install"
665 template "#{project_directory}/.env" do
666 source "nominatim.env.erb"
670 variables :base_url => node[:nominatim][:state] == "off" ? node[:fqdn] : "nominatim.openstreetmap.org",
671 :dbname => node[:nominatim][:dbname],
672 :flatnode_file => node[:nominatim][:flatnode_file],
673 :log_file => "#{node[:nominatim][:logdir]}/query.log",
674 :tokenizer => node[:nominatim][:config][:tokenizer],
675 :forward_dependencies => node[:nominatim][:config][:forward_dependencies],
676 :pool_size => node[:nominatim][:api_pool_size],
677 :query_timeout => node[:nominatim][:api_query_timeout],
678 :request_timeout => node[:nominatim][:api_request_timeout]
681 remote_file "#{project_directory}/secondary_importance.sql.gz" do
682 action :create_if_missing
683 source "https://nominatim.org/data/wikimedia-secondary-importance.sql.gz"
689 remote_file "#{project_directory}/wikimedia-importance.sql.gz" do
690 action :create_if_missing
691 source "https://nominatim.org/data/wikimedia-importance.sql.gz"
697 %w[gb_postcodes.csv.gz us_postcodes.csv.gz].each do |fname|
698 remote_file "#{project_directory}/#{fname}" do
700 source "https://nominatim.org/data/#{fname}"
707 # Webserver + frontend
709 %w[user_agent referrer email generic].each do |name|
710 file "#{cfg_directory}/nginx_blocked_#{name}.conf" do
711 action :create_if_missing
718 if node[:nominatim][:api_flavour] == "php"
719 node[:nominatim][:fpm_pools].each do |name, data|
723 pm_max_children data[:max_children]
725 pm_min_spare_servers 10
726 pm_max_spare_servers 20
727 pm_max_requests 10000
728 prometheus_port data[:prometheus_port]
731 elsif node[:nominatim][:api_flavour] == "python"
732 systemd_service "nominatim" do
733 description "Nominatim running as a gunicorn application"
736 working_directory project_directory
737 standard_output "append:#{node[:nominatim][:logdir]}/gunicorn.log"
738 standard_error "inherit"
739 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"
740 exec_reload "/bin/kill -s HUP $MAINPID"
741 environment :PYTHONPATH => "/usr/local/lib/nominatim/lib-python/"
745 requires "nominatim.socket"
746 after "network.target"
749 systemd_socket "nominatim" do
750 description "Gunicorn socket for Nominatim"
751 listen_stream "/run/gunicorn-nominatim.openstreetmap.org.sock"
752 socket_user "www-data"
756 ssl_certificate node[:fqdn] do
757 domains [node[:fqdn],
758 "nominatim.openstreetmap.org",
760 "nominatim.openstreetmap.com",
761 "nominatim.openstreetmap.net",
762 "nominatim.openstreetmaps.org",
763 "nominatim.openmaps.org",
764 "nominatim.qgis.org"]
765 notifies :reload, "service[nginx]"
768 include_recipe "nginx"
770 nginx_site "default" do
774 frontends = search(:node, "recipes:web\\:\\:frontend").sort_by(&:name)
776 nginx_site "nominatim" do
778 directory project_directory
779 variables :pools => node[:nominatim][:fpm_pools],
780 :frontends => frontends,
781 :confdir => "#{basedir}/etc",
782 :ui_directory => ui_directory
785 template "/etc/logrotate.d/nginx" do
786 source "logrotate.nginx.erb"
795 nominatim-update-source
796 nominatim-update-refresh-db
797 nominatim-update-data
798 nominatim-daily-maintenance].each do |fname|
799 template "#{bin_directory}/#{fname}" do
800 source "#{fname}.erb"
804 variables :bindir => bin_directory,
805 :srcdir => source_directory,
806 :builddir => build_directory,
807 :projectdir => project_directory,
808 :qabindir => qa_bin_directory,
809 :qadatadir => qa_data_directory
813 systemd_service "nominatim-update" do
814 description "Update the Nominatim database"
815 exec_start "#{bin_directory}/nominatim-update"
817 standard_output "append:#{node[:nominatim][:logdir]}/update.log"
818 standard_error "inherit"
819 working_directory project_directory
822 systemd_service "nominatim-update-maintenance-trigger" do
823 description "Trigger daily maintenance tasks for Nominatim DB"
824 exec_start "ln -sf #{bin_directory}/nominatim-daily-maintenance #{bin_directory}/maintenance/"
828 systemd_timer "nominatim-update-maintenance-trigger" do
829 action node[:nominatim][:state] != "off" ? :create : :delete
830 description "Schedule daily maintenance tasks for Nominatim DB"
831 on_calendar "*-*-* 02:03:00 UTC"
834 service "nominatim-update-maintenance-trigger" do
835 action node[:nominatim][:state] != "off" ? :enable : :disable
842 repository node[:nominatim][:ui_repository]
843 revision node[:nominatim][:ui_revision]
848 template "#{ui_directory}/dist/theme/config.theme.js" do
849 source "ui-config.js.erb"
857 if node[:nominatim][:enable_qa_tiles]
858 package "python3-geojson"
860 git qa_bin_directory do
861 repository node[:nominatim][:qa_repository]
862 revision node[:nominatim][:qa_revision]
863 enable_submodules true
866 notifies :run, "execute[compile_qa]"
869 execute "compile_qa" do
872 cwd "#{qa_bin_directory}/clustering-vt"
876 directory qa_data_directory do
883 template "#{qa_bin_directory}/analyser/config/config.yaml" do
884 source "qa_config.erb"
888 variables :outputdir => "#{qa_data_directory}/new"
891 ssl_certificate "qa-tile.nominatim.openstreetmap.org" do
892 domains ["qa-tile.nominatim.openstreetmap.org"]
893 notifies :reload, "service[nginx]"
896 nginx_site "qa-tiles.nominatim" do
897 template "nginx-qa-tiles.erb"
898 directory build_directory
899 variables :qa_data_directory => qa_data_directory
906 cron_d "nominatim-clean-db" do
907 action node[:nominatim][:state] == "master" ? :create : :delete
911 command "#{bin_directory}/clean-db-nominatim"
915 if node[:nominatim][:state] == "master"
916 postgresql_user "replication" do
917 cluster node[:nominatim][:dbcluster]
918 password data_bag_item("nominatim", "passwords")["replication"]
922 directory node[:rsyncd][:modules][:archive][:path] do
928 template "#{bin_directory}/clean-db-nominatim" do
929 source "clean-db-nominatim.erb"
933 variables :archive_dir => node[:rsyncd][:modules][:archive][:path],
934 :update_stop_file => "#{basedir}/status/updates_disabled",
935 :streaming_clients => search(:node, "nominatim_state:slave").map { |slave| slave[:fqdn] }.join(" ")
941 cron_d "nominatim-backup" do
942 action (node[:nominatim][:enable_backup] && node[:nominatim][:state] != "off") ? :create : :delete
947 command "#{bin_directory}/backup-nominatim"
951 cron_d "nominatim-vacuum-db" do
952 action node[:nominatim][:state] != "off" ? :create : :delete
956 command "#{bin_directory}/vacuum-db-nominatim"
960 %w[backup-nominatim vacuum-db-nominatim].each do |fname|
961 template "#{bin_directory}/#{fname}" do
962 source "#{fname}.erb"
966 variables :db => node[:nominatim][:dbname]
972 template "/etc/logrotate.d/nominatim" do
973 source "logrotate.nominatim.erb"
980 prometheus_exporter "nominatim" do
983 restrict_address_families "AF_UNIX"
985 "--nominatim.query-log=#{node[:nominatim][:logdir]}/query.log",
986 "--nominatim.database-name=#{node[:nominatim][:dbname]}"
990 include_recipe "fail2ban"
991 end # platform?('debian')
993 frontend_addresses = frontends.collect { |f| f.ipaddresses(:role => :external) }
995 fail2ban_jail "nominatim_limit_req" do
996 filter "nginx-limit-req"
997 logpath "#{node[:nominatim][:logdir]}/nominatim.openstreetmap.org-error.log"
1000 ignoreips frontend_addresses.flatten.sort