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}"
131 libboost-filesystem-dev
148 python3-sqlalchemy-ext
157 if node[:nominatim][:api_flavour] == "php"
162 elsif node[:nominatim][:api_flavour] == "python"
170 source_directory = "#{basedir}/src/nominatim"
171 build_directory = "#{basedir}/src/build"
172 project_directory = "#{basedir}/planet-project"
173 bin_directory = "#{basedir}/bin"
174 cfg_directory = "#{basedir}/etc"
175 ui_directory = "#{basedir}/ui"
176 qa_bin_directory = "#{basedir}/src/Nominatim-Data-Analyser"
177 qa_data_directory = "#{basedir}/qa-data"
179 [basedir, "#{basedir}/src", cfg_directory, bin_directory, build_directory, project_directory].each do |path|
188 directory "#{bin_directory}/maintenance" do
194 if node[:nominatim][:flatnode_file]
195 directory File.dirname(node[:nominatim][:flatnode_file]) do
200 remote_directory "#{project_directory}/static-website" do
205 files_owner "nominatim"
206 files_group "nominatim"
211 # Normally syncing via chef is a bad idea because syncing might involve
212 # an update of database functions which should not be done while an update
213 # is ongoing. Therefore we sync in between update cycles. There is an
214 # exception for slaves: they get DB function updates from the master, so
215 # only the source code needs to be updated, which chef may do.
216 git source_directory do
217 action node[:nominatim][:state] == "slave" ? :sync : :checkout
218 repository node[:nominatim][:repository]
219 revision node[:nominatim][:revision]
220 enable_submodules true
223 not_if { node[:nominatim][:state] != "slave" && File.exist?("#{source_directory}/README.md") }
224 notifies :run, "execute[compile_nominatim]"
227 remote_file "#{source_directory}/data/country_osm_grid.sql.gz" do
228 action :create_if_missing
229 source "https://nominatim.org/data/country_grid.sql.gz"
235 execute "compile_nominatim" do
239 command "cmake -D WITH_LUAJIT=ON #{source_directory} && make"
240 notifies :run, "execute[install_nominatim]"
243 execute "install_nominatim" do
246 command "make install"
251 template "#{project_directory}/.env" do
252 source "nominatim.env.erb"
256 variables :base_url => node[:nominatim][:state] == "off" ? node[:fqdn] : "nominatim.openstreetmap.org",
257 :dbname => node[:nominatim][:dbname],
258 :flatnode_file => node[:nominatim][:flatnode_file],
259 :log_file => "#{node[:nominatim][:logdir]}/query.log",
260 :tokenizer => node[:nominatim][:config][:tokenizer],
261 :forward_dependencies => node[:nominatim][:config][:forward_dependencies],
262 :pool_size => node[:nominatim][:api_pool_size]
265 remote_file "#{project_directory}/wikimedia-importance.sql.gz" do
266 action :create_if_missing
267 source "https://nominatim.org/data/wikimedia-importance.sql.gz"
273 %w[gb_postcodes.csv.gz us_postcodes.csv.gz].each do |fname|
274 remote_file "#{project_directory}/#{fname}" do
276 source "https://nominatim.org/data/#{fname}"
283 # Webserver + frontend
285 %w[user_agent referrer email generic].each do |name|
286 file "#{cfg_directory}/nginx_blocked_#{name}.conf" do
287 action :create_if_missing
294 if node[:nominatim][:api_flavour] == "php"
295 node[:nominatim][:fpm_pools].each do |name, data|
299 pm_max_children data[:max_children]
301 pm_min_spare_servers 10
302 pm_max_spare_servers 20
303 pm_max_requests 10000
304 prometheus_port data[:prometheus_port]
307 elsif node[:nominatim][:api_flavour] == "python"
308 systemd_service "nominatim" do
309 description "Nominatim running as a gunicorn application"
312 working_directory project_directory
313 standard_output "append:#{node[:nominatim][:logdir]}/gunicorn.log"
314 standard_error "inherit"
315 exec_start "/usr/bin/gunicorn -b unix:/run/gunicorn-nominatim.openstreetmap.org.sock -w #{node[:nominatim][:api_workers]} -k uvicorn.workers.UvicornWorker nominatim.server.starlette.server:run_wsgi"
316 exec_reload "/bin/kill -s HUP $MAINPID"
317 environment :PYTHONPATH => "/usr/local/lib/nominatim/lib-python/"
321 requires "nominatim.socket"
322 after "network.target"
325 systemd_socket "nominatim" do
326 description "Gunicorn socket for Nominatim"
327 listen_stream "/run/gunicorn-nominatim.openstreetmap.org.sock"
328 socket_user "www-data"
332 ssl_certificate node[:fqdn] do
333 domains [node[:fqdn],
334 "nominatim.openstreetmap.org",
336 "nominatim.openstreetmap.com",
337 "nominatim.openstreetmap.net",
338 "nominatim.openstreetmaps.org",
339 "nominatim.openmaps.org",
340 "nominatim.qgis.org"]
341 notifies :reload, "service[nginx]"
344 include_recipe "nginx"
346 nginx_site "default" do
350 frontends = search(:node, "recipes:web\\:\\:frontend").sort_by(&:name)
352 nginx_site "nominatim" do
354 directory project_directory
355 variables :pools => node[:nominatim][:fpm_pools],
356 :frontends => frontends,
357 :confdir => "#{basedir}/etc",
358 :ui_directory => ui_directory
361 template "/etc/logrotate.d/nginx" do
362 source "logrotate.nginx.erb"
371 nominatim-update-source
372 nominatim-update-refresh-db
373 nominatim-update-data
374 nominatim-daily-maintenance].each do |fname|
375 template "#{bin_directory}/#{fname}" do
376 source "#{fname}.erb"
380 variables :bindir => bin_directory,
381 :srcdir => source_directory,
382 :builddir => build_directory,
383 :projectdir => project_directory,
384 :qabindir => qa_bin_directory,
385 :qadatadir => qa_data_directory
389 systemd_service "nominatim-update" do
390 description "Update the Nominatim database"
391 exec_start "#{bin_directory}/nominatim-update"
393 standard_output "append:#{node[:nominatim][:logdir]}/update.log"
394 standard_error "inherit"
395 working_directory project_directory
398 systemd_service "nominatim-update-maintenance-trigger" do
399 description "Trigger daily maintenance tasks for Nominatim DB"
400 exec_start "ln -sf #{bin_directory}/nominatim-daily-maintenance #{bin_directory}/maintenance/"
404 systemd_timer "nominatim-update-maintenance-trigger" do
405 action node[:nominatim][:state] != "off" ? :create : :delete
406 description "Schedule daily maintenance tasks for Nominatim DB"
407 on_calendar "*-*-* 02:03:00 UTC"
410 service "nominatim-update-maintenance-trigger" do
411 action node[:nominatim][:state] != "off" ? :enable : :disable
418 repository node[:nominatim][:ui_repository]
419 revision node[:nominatim][:ui_revision]
424 template "#{ui_directory}/dist/theme/config.theme.js" do
425 source "ui-config.js.erb"
433 if node[:nominatim][:enable_qa_tiles]
434 package "python3-geojson"
436 git qa_bin_directory do
437 repository node[:nominatim][:qa_repository]
438 revision node[:nominatim][:qa_revision]
439 enable_submodules true
442 notifies :run, "execute[compile_qa]"
445 execute "compile_qa" do
448 cwd "#{qa_bin_directory}/clustering-vt"
452 directory qa_data_directory do
459 template "#{qa_bin_directory}/analyser/config/config.yaml" do
460 source "qa_config.erb"
464 variables :outputdir => "#{qa_data_directory}/new"
467 ssl_certificate "qa-tile.nominatim.openstreetmap.org" do
468 domains ["qa-tile.nominatim.openstreetmap.org"]
469 notifies :reload, "service[nginx]"
472 nginx_site "qa-tiles.nominatim" do
473 template "nginx-qa-tiles.erb"
474 directory build_directory
475 variables :qa_data_directory => qa_data_directory
482 cron_d "nominatim-clean-db" do
483 action node[:nominatim][:state] == "master" ? :create : :delete
487 command "#{bin_directory}/clean-db-nominatim"
491 if node[:nominatim][:state] == "master"
492 postgresql_user "replication" do
493 cluster node[:nominatim][:dbcluster]
494 password data_bag_item("nominatim", "passwords")["replication"]
498 directory node[:rsyncd][:modules][:archive][:path] do
504 template "#{bin_directory}/clean-db-nominatim" do
505 source "clean-db-nominatim.erb"
509 variables :archive_dir => node[:rsyncd][:modules][:archive][:path],
510 :update_stop_file => "#{basedir}/status/updates_disabled",
511 :streaming_clients => search(:node, "nominatim_state:slave").map { |slave| slave[:fqdn] }.join(" ")
517 cron_d "nominatim-backup" do
518 action (node[:nominatim][:enable_backup] && node[:nominatim][:state] != "off") ? :create : :delete
523 command "#{bin_directory}/backup-nominatim"
527 cron_d "nominatim-vacuum-db" do
528 action node[:nominatim][:state] != "off" ? :create : :delete
532 command "#{bin_directory}/vacuum-db-nominatim"
536 %w[backup-nominatim vacuum-db-nominatim].each do |fname|
537 template "#{bin_directory}/#{fname}" do
538 source "#{fname}.erb"
542 variables :db => node[:nominatim][:dbname]
548 template "/etc/logrotate.d/nominatim" do
549 source "logrotate.nominatim.erb"
557 munin_plugin_conf "nominatim" do
559 variables :db => node[:nominatim][:dbname],
560 :querylog => "#{node[:nominatim][:logdir]}/query.log"
563 munin_plugin "nominatim_importlag" do
564 target "#{source_directory}/munin/nominatim_importlag"
567 munin_plugin "nominatim_query_speed" do
568 target "#{source_directory}/munin/nominatim_query_speed_querylog"
571 munin_plugin "nominatim_requests" do
572 target "#{source_directory}/munin/nominatim_requests_querylog"
575 prometheus_exporter "nominatim" do
578 restrict_address_families "AF_UNIX"
580 "--nominatim.query-log=#{node[:nominatim][:logdir]}/query.log",
581 "--nominatim.database-name=#{node[:nominatim][:dbname]}"
585 include_recipe "fail2ban"
587 frontend_addresses = frontends.collect { |f| f.ipaddresses(:role => :external) }
589 fail2ban_jail "nominatim_limit_req" do
590 filter "nginx-limit-req"
591 logpath "#{node[:nominatim][:logdir]}/nominatim.openstreetmap.org-error.log"
594 ignoreips frontend_addresses.flatten.sort