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 "php::fpm"
23 include_recipe "prometheus"
25 basedir = data_bag_item("accounts", "nominatim")["home"]
26 email_errors = data_bag_item("accounts", "lonvia")["email"]
35 ## Log directory setup
37 directory node[:nominatim][:logdir] do
44 file "#{node[:nominatim][:logdir]}/query.log" do
45 action :create_if_missing
51 file "#{node[:nominatim][:logdir]}/update.log" do
52 action :create_if_missing
60 include_recipe "postgresql"
62 postgresql_version = node[:nominatim][:dbcluster].split("/").first
63 postgis_version = node[:nominatim][:postgis]
65 package "postgresql-#{postgresql_version}-postgis-#{postgis_version}"
67 node[:nominatim][:dbadmins].each do |user|
68 postgresql_user user do
69 cluster node[:nominatim][:dbcluster]
71 only_if { node[:nominatim][:state] != "slave" }
75 postgresql_user "nominatim" do
76 cluster node[:nominatim][:dbcluster]
78 only_if { node[:nominatim][:state] != "slave" }
81 postgresql_user "www-data" do
82 cluster node[:nominatim][:dbcluster]
83 only_if { node[:nominatim][:state] != "slave" }
86 postgresql_munin "nominatim" do
87 cluster node[:nominatim][:dbcluster]
88 database node[:nominatim][:dbname]
91 directory "#{basedir}/tablespaces" do
97 # NOTE: tablespaces must be exactly in the same location on each
98 # Nominatim instance when replication is in use. Therefore
99 # use symlinks to canonical directory locations.
100 node[:nominatim][:tablespaces].each do |name, location|
101 directory location do
108 link "#{basedir}/tablespaces/#{name}" do
112 postgresql_tablespace name do
113 cluster node[:nominatim][:dbcluster]
114 location "#{basedir}/tablespaces/#{name}"
128 libboost-filesystem-dev
145 python3-sqlalchemy-ext
156 source_directory = "#{basedir}/src/nominatim"
157 build_directory = "#{basedir}/src/build"
158 project_directory = "#{basedir}/planet-project"
159 bin_directory = "#{basedir}/bin"
160 cfg_directory = "#{basedir}/etc"
161 ui_directory = "#{basedir}/ui"
162 qa_bin_directory = "#{basedir}/src/Nominatim-Data-Analyser"
163 qa_data_directory = "#{basedir}/qa-data"
165 [basedir, "#{basedir}/src", cfg_directory, bin_directory, build_directory, project_directory].each do |path|
174 directory "#{bin_directory}/maintenance" do
180 if node[:nominatim][:flatnode_file]
181 directory File.dirname(node[:nominatim][:flatnode_file]) do
186 # Normally syncing via chef is a bad idea because syncing might involve
187 # an update of database functions which should not be done while an update
188 # is ongoing. Therefore we sync in between update cycles. There is an
189 # exception for slaves: they get DB function updates from the master, so
190 # only the source code needs to be updated, which chef may do.
191 git source_directory do
192 action node[:nominatim][:state] == "slave" ? :sync : :checkout
193 repository node[:nominatim][:repository]
194 revision node[:nominatim][:revision]
195 enable_submodules true
198 not_if { node[:nominatim][:state] != "slave" && File.exist?("#{source_directory}/README.md") }
199 notifies :run, "execute[compile_nominatim]"
202 remote_file "#{source_directory}/data/country_osm_grid.sql.gz" do
203 action :create_if_missing
204 source "https://nominatim.org/data/country_grid.sql.gz"
210 execute "compile_nominatim" do
214 command "cmake -D WITH_LUAJIT=ON #{source_directory} && make"
215 notifies :run, "execute[install_nominatim]"
218 execute "install_nominatim" do
221 command "make install"
226 template "#{project_directory}/.env" do
227 source "nominatim.env.erb"
231 variables :base_url => node[:nominatim][:state] == "off" ? node[:fqdn] : "nominatim.openstreetmap.org",
232 :dbname => node[:nominatim][:dbname],
233 :flatnode_file => node[:nominatim][:flatnode_file],
234 :log_file => "#{node[:nominatim][:logdir]}/query.log",
235 :tokenizer => node[:nominatim][:config][:tokenizer],
236 :forward_dependencies => node[:nominatim][:config][:forward_dependencies]
239 remote_file "#{project_directory}/wikimedia-importance.sql.gz" do
240 action :create_if_missing
241 source "https://nominatim.org/data/wikimedia-importance.sql.gz"
247 %w[gb_postcodes.csv.gz us_postcodes.csv.gz].each do |fname|
248 remote_file "#{project_directory}/#{fname}" do
250 source "https://nominatim.org/data/#{fname}"
257 # Webserver + frontend
259 %w[user_agent referrer email generic].each do |name|
260 file "#{cfg_directory}/nginx_blocked_#{name}.conf" do
261 action :create_if_missing
268 node[:nominatim][:fpm_pools].each do |name, data|
272 pm_max_children data[:max_children]
274 pm_min_spare_servers 10
275 pm_max_spare_servers 20
276 pm_max_requests 10000
277 prometheus_port data[:prometheus_port]
281 ssl_certificate node[:fqdn] do
282 domains [node[:fqdn],
283 "nominatim.openstreetmap.org",
285 "nominatim.openstreetmap.com",
286 "nominatim.openstreetmap.net",
287 "nominatim.openstreetmaps.org",
288 "nominatim.openmaps.org",
289 "nominatim.qgis.org"]
290 notifies :reload, "service[nginx]"
293 include_recipe "nginx"
295 nginx_site "default" do
299 frontends = search(:node, "recipes:web\\:\\:frontend").sort_by(&:name)
301 nginx_site "nominatim" do
303 directory project_directory
304 variables :pools => node[:nominatim][:fpm_pools],
305 :frontends => frontends,
306 :confdir => "#{basedir}/etc",
307 :ui_directory => ui_directory
310 template "/etc/logrotate.d/nginx" do
311 source "logrotate.nginx.erb"
320 nominatim-update-source
321 nominatim-update-refresh-db
322 nominatim-update-data
323 nominatim-daily-maintenance].each do |fname|
324 template "#{bin_directory}/#{fname}" do
325 source "#{fname}.erb"
329 variables :bindir => bin_directory,
330 :srcdir => source_directory,
331 :builddir => build_directory,
332 :projectdir => project_directory,
333 :qabindir => qa_bin_directory,
334 :qadatadir => qa_data_directory
338 systemd_service "nominatim-update" do
339 description "Update the Nominatim database"
340 exec_start "#{bin_directory}/nominatim-update"
342 standard_output "append:#{node[:nominatim][:logdir]}/update.log"
343 standard_error "inherit"
344 working_directory project_directory
347 systemd_service "nominatim-update-maintenance-trigger" do
348 description "Trigger daily maintenance tasks for Nominatim DB"
349 exec_start "ln -sf #{bin_directory}/nominatim-daily-maintenance #{bin_directory}/maintenance/"
353 systemd_timer "nominatim-update-maintenance-trigger" do
354 action node[:nominatim][:state] != "off" ? :create : :delete
355 description "Schedule daily maintenance tasks for Nominatim DB"
356 on_calendar "*-*-* 02:03:00 UTC"
359 service "nominatim-update-maintenance-trigger" do
360 action node[:nominatim][:state] != "off" ? :enable : :disable
367 repository node[:nominatim][:ui_repository]
368 revision node[:nominatim][:ui_revision]
373 template "#{ui_directory}/dist/theme/config.theme.js" do
374 source "ui-config.js.erb"
382 if node[:nominatim][:enable_qa_tiles]
383 package "python3-geojson"
385 git qa_bin_directory do
386 repository node[:nominatim][:qa_repository]
387 revision node[:nominatim][:qa_revision]
388 enable_submodules true
391 notifies :run, "execute[compile_qa]"
394 execute "compile_qa" do
397 cwd "#{qa_bin_directory}/clustering-vt"
401 directory qa_data_directory do
408 template "#{qa_bin_directory}/analyser/config/config.yaml" do
409 source "qa_config.erb"
413 variables :outputdir => "#{qa_data_directory}/new"
416 ssl_certificate "qa-tile.nominatim.openstreetmap.org" do
417 domains ["qa-tile.nominatim.openstreetmap.org"]
418 notifies :reload, "service[nginx]"
421 nginx_site "qa-tiles.nominatim" do
422 template "nginx-qa-tiles.erb"
423 directory build_directory
424 variables :qa_data_directory => qa_data_directory
431 cron_d "nominatim-clean-db" do
432 action node[:nominatim][:state] == "master" ? :create : :delete
436 command "#{bin_directory}/clean-db-nominatim"
440 if node[:nominatim][:state] == "master"
441 postgresql_user "replication" do
442 cluster node[:nominatim][:dbcluster]
443 password data_bag_item("nominatim", "passwords")["replication"]
447 directory node[:rsyncd][:modules][:archive][:path] do
453 template "#{bin_directory}/clean-db-nominatim" do
454 source "clean-db-nominatim.erb"
458 variables :archive_dir => node[:rsyncd][:modules][:archive][:path],
459 :update_stop_file => "#{basedir}/status/updates_disabled",
460 :streaming_clients => search(:node, "nominatim_state:slave").map { |slave| slave[:fqdn] }.join(" ")
466 cron_d "nominatim-backup" do
467 action (node[:nominatim][:enable_backup] && node[:nominatim][:state] != "off") ? :create : :delete
472 command "#{bin_directory}/backup-nominatim"
476 cron_d "nominatim-vacuum-db" do
477 action node[:nominatim][:state] != "off" ? :create : :delete
481 command "#{bin_directory}/vacuum-db-nominatim"
485 %w[backup-nominatim vacuum-db-nominatim].each do |fname|
486 template "#{bin_directory}/#{fname}" do
487 source "#{fname}.erb"
491 variables :db => node[:nominatim][:dbname]
497 template "/etc/logrotate.d/nominatim" do
498 source "logrotate.nominatim.erb"
506 munin_plugin_conf "nominatim" do
508 variables :db => node[:nominatim][:dbname],
509 :querylog => "#{node[:nominatim][:logdir]}/query.log"
512 munin_plugin "nominatim_importlag" do
513 target "#{source_directory}/munin/nominatim_importlag"
516 munin_plugin "nominatim_query_speed" do
517 target "#{source_directory}/munin/nominatim_query_speed_querylog"
520 munin_plugin "nominatim_requests" do
521 target "#{source_directory}/munin/nominatim_requests_querylog"
524 prometheus_exporter "nominatim" do
527 restrict_address_families "AF_UNIX"
529 "--nominatim.query-log=#{node[:nominatim][:logdir]}/query.log",
530 "--nominatim.database-name=#{node[:nominatim][:dbname]}"
534 include_recipe "fail2ban"
536 frontend_addresses = frontends.collect { |f| f.ipaddresses(:role => :external) }
538 fail2ban_jail "nominatim_limit_req" do
539 filter "nginx-limit-req"
540 logpath "#{node[:nominatim][:logdir]}/nominatim.openstreetmap.org-error.log"
543 ignoreips frontend_addresses.flatten.sort