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
150 source_directory = "#{basedir}/src/nominatim"
151 build_directory = "#{basedir}/src/build"
152 project_directory = "#{basedir}/planet-project"
153 bin_directory = "#{basedir}/bin"
154 cfg_directory = "#{basedir}/etc"
155 ui_directory = "#{basedir}/ui"
156 qa_bin_directory = "#{basedir}/src/Nominatim-Data-Analyser"
157 qa_data_directory = "#{basedir}/qa-data"
159 [basedir, "#{basedir}/src", cfg_directory, bin_directory, build_directory, project_directory].each do |path|
168 directory "#{bin_directory}/maintenance" do
174 if node[:nominatim][:flatnode_file]
175 directory File.dirname(node[:nominatim][:flatnode_file]) do
180 # Normally syncing via chef is a bad idea because syncing might involve
181 # an update of database functions which should not be done while an update
182 # is ongoing. Therefore we sync in between update cycles. There is an
183 # exception for slaves: they get DB function updates from the master, so
184 # only the source code needs to be updated, which chef may do.
185 git source_directory do
186 action node[:nominatim][:state] == "slave" ? :sync : :checkout
187 repository node[:nominatim][:repository]
188 revision node[:nominatim][:revision]
189 enable_submodules true
192 not_if { node[:nominatim][:state] != "slave" && File.exist?("#{source_directory}/README.md") }
193 notifies :run, "execute[compile_nominatim]"
196 remote_file "#{source_directory}/data/country_osm_grid.sql.gz" do
197 action :create_if_missing
198 source "https://www.nominatim.org/data/country_grid.sql.gz"
204 execute "compile_nominatim" do
208 command "cmake #{source_directory} && make"
209 notifies :run, "execute[install_nominatim]"
212 execute "install_nominatim" do
215 command "make install"
220 template "#{project_directory}/.env" do
221 source "nominatim.env.erb"
225 variables :base_url => node[:nominatim][:state] == "off" ? node[:fqdn] : "nominatim.openstreetmap.org",
226 :dbname => node[:nominatim][:dbname],
227 :flatnode_file => node[:nominatim][:flatnode_file],
228 :log_file => "#{node[:nominatim][:logdir]}/query.log",
229 :tokenizer => node[:nominatim][:config][:tokenizer],
230 :forward_dependencies => node[:nominatim][:config][:forward_dependencies]
233 %w[wikimedia-importance.sql.gz gb_postcodes.csv.gz us_postcodes.csv.gz].each do |fname|
234 remote_file "#{project_directory}/#{fname}" do
236 source "https://www.nominatim.org/data/#{fname}"
243 # Webserver + frontend
245 %w[user_agent referrer email generic].each do |name|
246 file "#{cfg_directory}/nginx_blocked_#{name}.conf" do
247 action :create_if_missing
254 node[:nominatim][:fpm_pools].each do |name, data|
258 pm_max_children data[:max_children]
260 pm_min_spare_servers 10
261 pm_max_spare_servers 20
262 pm_max_requests 10000
263 prometheus_port data[:prometheus_port]
267 ssl_certificate node[:fqdn] do
268 domains [node[:fqdn],
269 "nominatim.openstreetmap.org",
271 "nominatim.openstreetmap.com",
272 "nominatim.openstreetmap.net",
273 "nominatim.openstreetmaps.org",
274 "nominatim.openmaps.org",
275 "nominatim.qgis.org"]
276 notifies :reload, "service[nginx]"
279 include_recipe "nginx"
281 nginx_site "default" do
285 frontends = search(:node, "recipes:web\\:\\:frontend").sort_by(&:name)
287 nginx_site "nominatim" do
289 directory project_directory
290 variables :pools => node[:nominatim][:fpm_pools],
291 :frontends => frontends,
292 :confdir => "#{basedir}/etc",
293 :ui_directory => ui_directory
296 template "/etc/logrotate.d/nginx" do
297 source "logrotate.nginx.erb"
306 nominatim-update-source
307 nominatim-update-refresh-db
308 nominatim-update-data
309 nominatim-daily-maintenance].each do |fname|
310 template "#{bin_directory}/#{fname}" do
311 source "#{fname}.erb"
315 variables :bindir => bin_directory,
316 :srcdir => source_directory,
317 :builddir => build_directory,
318 :projectdir => project_directory,
319 :qabindir => qa_bin_directory,
320 :qadatadir => qa_data_directory
324 systemd_service "nominatim-update" do
325 description "Update the Nominatim database"
326 exec_start "#{bin_directory}/nominatim-update"
328 standard_output "append:#{node[:nominatim][:logdir]}/update.log"
329 standard_error "inherit"
330 working_directory project_directory
333 systemd_service "nominatim-update-maintenance-trigger" do
334 description "Trigger daily maintenance tasks for Nominatim DB"
335 exec_start "ln -sf #{bin_directory}/nominatim-daily-maintenance #{bin_directory}/maintenance/"
339 systemd_timer "nominatim-update-maintenance-trigger" do
340 action node[:nominatim][:state] != "off" ? :create : :delete
341 description "Schedule daily maintenance tasks for Nominatim DB"
342 on_calendar "*-*-* 02:03:00 UTC"
345 service "nominatim-update-maintenance-trigger" do
346 action node[:nominatim][:state] != "off" ? :enable : :disable
353 repository node[:nominatim][:ui_repository]
354 revision node[:nominatim][:ui_revision]
359 template "#{ui_directory}/dist/theme/config.theme.js" do
360 source "ui-config.js.erb"
368 if node[:nominatim][:enable_qa_tiles]
369 package "python3-geojson"
371 git qa_bin_directory do
372 repository node[:nominatim][:qa_repository]
373 revision node[:nominatim][:qa_revision]
374 enable_submodules true
377 notifies :run, "execute[compile_qa]"
380 execute "compile_qa" do
383 cwd "#{qa_bin_directory}/clustering-vt"
387 directory qa_data_directory do
394 template "#{qa_bin_directory}/analyser/config/config.yaml" do
395 source "qa_config.erb"
399 variables :outputdir => "#{qa_data_directory}/new"
402 ssl_certificate "qa-tile.nominatim.openstreetmap.org" do
403 domains ["qa-tile.nominatim.openstreetmap.org"]
404 notifies :reload, "service[nginx]"
407 nginx_site "qa-tiles.nominatim" do
408 template "nginx-qa-tiles.erb"
409 directory build_directory
410 variables :qa_data_directory => qa_data_directory
417 cron_d "nominatim-clean-db" do
418 action node[:nominatim][:state] == "master" ? :create : :delete
422 command "#{bin_directory}/clean-db-nominatim"
426 if node[:nominatim][:state] == "master"
427 postgresql_user "replication" do
428 cluster node[:nominatim][:dbcluster]
429 password data_bag_item("nominatim", "passwords")["replication"]
433 directory node[:rsyncd][:modules][:archive][:path] do
439 template "#{bin_directory}/clean-db-nominatim" do
440 source "clean-db-nominatim.erb"
444 variables :archive_dir => node[:rsyncd][:modules][:archive][:path],
445 :update_stop_file => "#{basedir}/status/updates_disabled",
446 :streaming_clients => search(:node, "nominatim_state:slave").map { |slave| slave[:fqdn] }.join(" ")
452 cron_d "nominatim-backup" do
453 action (node[:nominatim][:enable_backup] && node[:nominatim][:state] != "off") ? :create : :delete
458 command "#{bin_directory}/backup-nominatim"
462 cron_d "nominatim-vacuum-db" do
463 action node[:nominatim][:state] != "off" ? :create : :delete
467 command "#{bin_directory}/vacuum-db-nominatim"
471 %w[backup-nominatim vacuum-db-nominatim].each do |fname|
472 template "#{bin_directory}/#{fname}" do
473 source "#{fname}.erb"
477 variables :db => node[:nominatim][:dbname]
483 template "/etc/logrotate.d/nominatim" do
484 source "logrotate.nominatim.erb"
492 munin_plugin_conf "nominatim" do
494 variables :db => node[:nominatim][:dbname],
495 :querylog => "#{node[:nominatim][:logdir]}/query.log"
498 munin_plugin "nominatim_importlag" do
499 target "#{source_directory}/munin/nominatim_importlag"
502 munin_plugin "nominatim_query_speed" do
503 target "#{source_directory}/munin/nominatim_query_speed_querylog"
506 munin_plugin "nominatim_requests" do
507 target "#{source_directory}/munin/nominatim_requests_querylog"
510 prometheus_exporter "nominatim" do
514 "--nominatim.query-log=#{node[:nominatim][:logdir]}/query.log",
515 "--nominatim.database-name=#{node[:nominatim][:dbname]}"
519 include_recipe "fail2ban"
521 frontend_addresses = frontends.collect { |f| f.ipaddresses(:role => :external) }
523 fail2ban_jail "nominatim_limit_req" do
524 filter "nginx-limit-req"
525 logpath "#{node[:nominatim][:logdir]}/nominatim.openstreetmap.org-error.log"
528 ignoreips frontend_addresses.flatten.sort