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 postgresql_user "nominatim" do
68 cluster node[:nominatim][:dbcluster]
70 only_if { node[:nominatim][:state] != "slave" }
73 postgresql_user "www-data" do
74 cluster node[:nominatim][:dbcluster]
75 only_if { node[:nominatim][:state] != "slave" }
78 postgresql_munin "nominatim" do
79 cluster node[:nominatim][:dbcluster]
80 database node[:nominatim][:dbname]
83 directory "#{basedir}/tablespaces" do
89 # NOTE: tablespaces must be exactly in the same location on each
90 # Nominatim instance when replication is in use. Therefore
91 # use symlinks to canonical directory locations.
92 node[:nominatim][:tablespaces].each do |name, location|
100 link "#{basedir}/tablespaces/#{name}" do
104 postgresql_tablespace name do
105 cluster node[:nominatim][:dbcluster]
106 location "#{basedir}/tablespaces/#{name}"
120 libboost-filesystem-dev
142 source_directory = "#{basedir}/src/nominatim"
143 build_directory = "#{basedir}/src/build"
144 project_directory = "#{basedir}/planet-project"
145 bin_directory = "#{basedir}/bin"
146 cfg_directory = "#{basedir}/etc"
147 ui_directory = "#{basedir}/ui"
148 qa_bin_directory = "#{basedir}/src/Nominatim-Data-Analyser"
149 qa_data_directory = "#{basedir}/qa-data"
151 [basedir, "#{basedir}/src", cfg_directory, bin_directory, build_directory, project_directory].each do |path|
160 directory "#{bin_directory}/maintenance" do
166 if node[:nominatim][:flatnode_file]
167 directory File.dirname(node[:nominatim][:flatnode_file]) do
172 # Normally syncing via chef is a bad idea because syncing might involve
173 # an update of database functions which should not be done while an update
174 # is ongoing. Therefore we sync in between update cycles. There is an
175 # exception for slaves: they get DB function updates from the master, so
176 # only the source code needs to be updated, which chef may do.
177 git source_directory do
178 action node[:nominatim][:state] == "slave" ? :sync : :checkout
179 repository node[:nominatim][:repository]
180 revision node[:nominatim][:revision]
181 enable_submodules true
184 not_if { node[:nominatim][:state] != "slave" && File.exist?("#{source_directory}/README.md") }
185 notifies :run, "execute[compile_nominatim]"
188 remote_file "#{source_directory}/data/country_osm_grid.sql.gz" do
189 action :create_if_missing
190 source "https://www.nominatim.org/data/country_grid.sql.gz"
196 execute "compile_nominatim" do
200 command "cmake #{source_directory} && make"
201 notifies :run, "execute[install_nominatim]"
204 execute "install_nominatim" do
207 command "make install"
212 template "#{project_directory}/.env" do
213 source "nominatim.env.erb"
217 variables :base_url => node[:nominatim][:state] == "off" ? node[:fqdn] : "nominatim.openstreetmap.org",
218 :dbname => node[:nominatim][:dbname],
219 :flatnode_file => node[:nominatim][:flatnode_file],
220 :log_file => "#{node[:nominatim][:logdir]}/query.log",
221 :tokenizer => node[:nominatim][:config][:tokenizer]
224 #%w[wikimedia-importance.sql.gz gb_postcodes.csv.gz us_postcodes.csv.gz].each do |fname|
225 # remote_file "#{project_directory}/#{fname}" do
227 # source "https://www.nominatim.org/data/#{fname}"
234 # Webserver + frontend
236 %w[user_agent referrer email generic].each do |name|
237 file "#{cfg_directory}/nginx_blocked_#{name}.conf" do
238 action :create_if_missing
245 node[:nominatim][:fpm_pools].each do |name, data|
249 pm_max_children data[:max_children]
251 pm_min_spare_servers 10
252 pm_max_spare_servers 20
253 pm_max_requests 10000
254 prometheus_port data[:prometheus_port]
258 ssl_certificate node[:fqdn] do
259 domains [node[:fqdn],
260 "nominatim.openstreetmap.org",
262 "nominatim.openstreetmap.com",
263 "nominatim.openstreetmap.net",
264 "nominatim.openstreetmaps.org",
265 "nominatim.openmaps.org",
266 "nominatim.qgis.org"]
267 notifies :reload, "service[nginx]"
270 include_recipe "nginx"
272 nginx_site "default" do
276 frontends = search(:node, "recipes:web\\:\\:frontend").sort_by(&:name)
278 nginx_site "nominatim" do
280 directory build_directory
281 variables :pools => node[:nominatim][:fpm_pools],
282 :frontends => frontends,
283 :confdir => "#{basedir}/etc",
284 :ui_directory => ui_directory
287 template "/etc/logrotate.d/nginx" do
288 source "logrotate.nginx.erb"
297 nominatim-update-source
298 nominatim-update-refresh-db
299 nominatim-update-data
300 nominatim-daily-maintenance].each do |fname|
301 template "#{bin_directory}/#{fname}" do
302 source "#{fname}.erb"
306 variables :bindir => bin_directory,
307 :srcdir => source_directory,
308 :builddir => build_directory,
309 :projectdir => project_directory,
310 :qabindir => qa_bin_directory,
311 :qadatadir => qa_data_directory
315 systemd_service "nominatim-update" do
316 description "Update the Nominatim database"
317 exec_start "#{bin_directory}/nominatim-update"
319 standard_output "append:#{node[:nominatim][:logdir]}/update.log"
320 standard_error "inherit"
321 working_directory project_directory
324 systemd_service "nominatim-update-maintenance-trigger" do
325 description "Trigger daily maintenance tasks for Nominatim DB"
326 exec_start "ln -sf #{bin_directory}/nominatim-daily-maintenance #{bin_directory}/maintenance/"
330 systemd_timer "nominatim-update-maintenance-trigger" do
331 action node[:nominatim][:state] != "off" ? :create : :delete
332 description "Schedule daily maintenance tasks for Nominatim DB"
333 on_calendar "*-*-* 02:03:00 UTC"
336 service "nominatim-update-maintenance-trigger" do
337 action node[:nominatim][:state] != "off" ? :enable : :disable
344 repository node[:nominatim][:ui_repository]
345 revision node[:nominatim][:ui_revision]
350 template "#{ui_directory}/dist/theme/config.theme.js" do
351 source "ui-config.js.erb"
359 if node[:nominatim][:enable_qa_tiles]
360 package "python3-geojson"
362 git qa_bin_directory do
363 repository node[:nominatim][:qa_repository]
364 revision node[:nominatim][:qa_revision]
365 enable_submodules true
368 notifies :run, "execute[compile_qa]"
371 execute "compile_qa" do
374 cwd "#{qa_bin_directory}/clustering-vt"
378 directory qa_data_directory do
385 template "#{qa_bin_directory}/analyser/config/config.yaml" do
386 source "qa_config.erb"
390 variables :outputdir => "#{qa_data_directory}/new"
393 ssl_certificate "qa-tile.nominatim.openstreetmap.org" do
394 domains ["qa-tile.nominatim.openstreetmap.org"]
395 notifies :reload, "service[nginx]"
398 nginx_site "qa-tiles.nominatim" do
399 template "nginx-qa-tiles.erb"
400 directory build_directory
401 variables :qa_data_directory => qa_data_directory
408 cron_d "nominatim-clean-db" do
409 action node[:nominatim][:state] == "master" ? :create : :delete
413 command "#{bin_directory}/clean-db-nominatim"
417 if node[:nominatim][:state] == "master"
418 postgresql_user "replication" do
419 cluster node[:nominatim][:dbcluster]
420 password data_bag_item("nominatim", "passwords")["replication"]
424 directory node[:rsyncd][:modules][:archive][:path] do
430 template "#{bin_directory}/clean-db-nominatim" do
431 source "clean-db-nominatim.erb"
435 variables :archive_dir => node[:rsyncd][:modules][:archive][:path],
436 :update_stop_file => "#{basedir}/status/updates_disabled",
437 :streaming_clients => search(:node, "nominatim_state:slave").map { |slave| slave[:fqdn] }.join(" ")
443 cron_d "nominatim-backup" do
444 action (node[:nominatim][:enable_backup] && node[:nominatim][:state] != "off") ? :create : :delete
449 command "#{bin_directory}/backup-nominatim"
453 cron_d "nominatim-vacuum-db" do
454 action node[:nominatim][:state] != "off" ? :create : :delete
458 command "#{bin_directory}/vacuum-db-nominatim"
462 %w[backup-nominatim vacuum-db-nominatim].each do |fname|
463 template "#{bin_directory}/#{fname}" do
464 source "#{fname}.erb"
468 variables :db => node[:nominatim][:dbname]
474 template "/etc/logrotate.d/nominatim" do
475 source "logrotate.nominatim.erb"
483 munin_plugin_conf "nominatim" do
485 variables :db => node[:nominatim][:dbname],
486 :querylog => "#{node[:nominatim][:logdir]}/query.log"
489 munin_plugin "nominatim_importlag" do
490 target "#{source_directory}/munin/nominatim_importlag"
493 munin_plugin "nominatim_query_speed" do
494 target "#{source_directory}/munin/nominatim_query_speed_querylog"
497 munin_plugin "nominatim_requests" do
498 target "#{source_directory}/munin/nominatim_requests_querylog"
501 prometheus_exporter "nominatim" do
505 "--nominatim.query-log=#{node[:nominatim][:logdir]}/query.log",
506 "--nominatim.database-name=#{node[:nominatim][:dbname]}"
510 include_recipe "fail2ban"
512 frontend_addresses = frontends.collect { |f| f.ipaddresses(:role => :external) }
514 fail2ban_jail "nominatim_limit_req" do
515 filter "nginx-limit-req"
516 logpath "#{node[:nominatim][:logdir]}/nominatim.openstreetmap.org-error.log"
519 ignoreips frontend_addresses.flatten.sort