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"
24 basedir = data_bag_item("accounts", "nominatim")["home"]
25 email_errors = data_bag_item("accounts", "lonvia")["email"]
34 directory node[:nominatim][:logdir] do
41 file "#{node[:nominatim][:logdir]}/query.log" do
42 action :create_if_missing
48 file "#{node[:nominatim][:logdir]}/update.log" do
49 action :create_if_missing
57 include_recipe "postgresql"
59 postgresql_version = node[:nominatim][:dbcluster].split("/").first
60 postgis_version = node[:nominatim][:postgis]
62 package "postgresql-#{postgresql_version}-postgis-#{postgis_version}"
64 node[:nominatim][:dbadmins].each do |user|
65 postgresql_user user do
66 cluster node[:nominatim][:dbcluster]
68 only_if { node[:nominatim][:state] != "slave" }
72 postgresql_user "nominatim" do
73 cluster node[:nominatim][:dbcluster]
75 only_if { node[:nominatim][:state] != "slave" }
78 postgresql_user "www-data" do
79 cluster node[:nominatim][:dbcluster]
80 only_if { node[:nominatim][:state] != "slave" }
83 postgresql_munin "nominatim" do
84 cluster node[:nominatim][:dbcluster]
85 database node[:nominatim][:dbname]
88 directory "#{basedir}/tablespaces" do
94 # NOTE: tablespaces must be exactly in the same location on each
95 # Nominatim instance when replication is in use. Therefore
96 # use symlinks to canonical directory locations.
97 node[:nominatim][:tablespaces].each do |name, location|
105 link "#{basedir}/tablespaces/#{name}" do
109 postgresql_tablespace name do
110 cluster node[:nominatim][:dbcluster]
111 location "#{basedir}/tablespaces/#{name}"
115 if node[:nominatim][:state] == "master"
116 postgresql_user "replication" do
117 cluster node[:nominatim][:dbcluster]
118 password data_bag_item("nominatim", "passwords")["replication"]
122 directory node[:rsyncd][:modules][:archive][:path] do
128 template "/usr/local/bin/clean-db-nominatim" do
129 source "clean-db-nominatim.erb"
133 variables :archive_dir => node[:rsyncd][:modules][:archive][:path],
134 :update_stop_file => "#{basedir}/status/updates_disabled",
135 :streaming_clients => search(:node, "nominatim_state:slave").map { |slave| slave[:fqdn] }.join(" ")
149 libboost-filesystem-dev
170 source_directory = "#{basedir}/nominatim"
171 build_directory = "#{basedir}/bin"
172 ui_directory = "#{basedir}/ui"
173 qa_bin_directory = "#{basedir}/Nominatim-Data-Analyser"
174 qa_data_directory = "#{basedir}/qa-data"
176 directory build_directory do
183 # Normally syncing via chef is a bad idea because syncing might involve
184 # an update of database functions which should not be done while an update
185 # is ongoing. Therefore we sync in between update cycles. There is an
186 # exception for slaves: they get DB function updates from the master, so
187 # only the source code needs to be updated, which chef may do.
188 git source_directory do
189 action node[:nominatim][:state] == "slave" ? :sync : :checkout
190 repository node[:nominatim][:repository]
191 revision node[:nominatim][:revision]
192 enable_submodules true
195 not_if { node[:nominatim][:state] != "slave" && File.exist?("#{source_directory}/README.md") }
196 notifies :run, "execute[compile_nominatim]"
199 remote_file "#{source_directory}/data/country_osm_grid.sql.gz" do
200 action :create_if_missing
201 source "https://www.nominatim.org/data/country_grid.sql.gz"
207 execute "compile_nominatim" do
211 command "cmake #{source_directory} && make"
214 template "#{source_directory}/.git/hooks/post-merge" do
215 source "git-post-merge-hook.erb"
219 variables :srcdir => source_directory,
220 :builddir => build_directory,
221 :dbname => node[:nominatim][:dbname]
224 template "#{build_directory}/.env" do
225 source "nominatim.env.erb"
229 variables :base_url => node[:nominatim][:state] == "off" ? node[:fqdn] : "nominatim.openstreetmap.org",
230 :dbname => node[:nominatim][:dbname],
231 :flatnode_file => node[:nominatim][:flatnode_file],
232 :log_file => "#{node[:nominatim][:logdir]}/query.log",
233 :tokenizer => node[:nominatim][:config][:tokenizer]
238 repository node[:nominatim][:ui_repository]
239 revision node[:nominatim][:ui_revision]
244 template "#{ui_directory}/dist/theme/config.theme.js" do
245 source "ui-config.js.erb"
251 if node[:nominatim][:flatnode_file]
252 directory File.dirname(node[:nominatim][:flatnode_file]) do
257 template "/etc/logrotate.d/nominatim" do
258 source "logrotate.nominatim.erb"
265 "wikimedia-importance.sql.gz",
266 "gb_postcodes.csv.gz",
267 "us_postcodes.csv.gz"
270 external_data.each do |fname|
271 remote_file "#{build_directory}/#{fname}" do
273 source "https://www.nominatim.org/data/#{fname}"
280 if node[:nominatim][:state] == "off"
281 cron_d "nominatim-backup" do
285 cron_d "nominatim-vacuum-db" do
289 cron_d "nominatim-clean-db" do
293 systemd_timer "nominatim-update-maintenance-trigger" do
297 cron_d "nominatim-backup" do
298 action node[:nominatim][:enable_backup] ? :create : :delete
303 command "/usr/local/bin/backup-nominatim"
307 cron_d "nominatim-vacuum-db" do
311 command "/usr/local/bin/vacuum-db-nominatim"
315 cron_d "nominatim-clean-db" do
316 action node[:nominatim][:state] == "master" ? :create : :delete
320 command "/usr/local/bin/clean-db-nominatim"
324 systemd_service "nominatim-update-maintenance-trigger" do
325 description "Trigger maintenance tasks for Nominatim DB"
326 exec_start "touch #{basedir}/status/update_maintenance"
330 systemd_timer "nominatim-update-maintenance-trigger" do
332 description "Schedule maintenance tasks for Nominatim DB"
333 on_calendar "*-*-* 02:03:00 UTC"
336 service "nominatim-update-maintenance-trigger" do
341 template "#{source_directory}/utils/nominatim-update" do
346 variables :bindir => build_directory,
347 :srcdir => source_directory,
348 :logfile => "#{node[:nominatim][:logdir]}/update.log",
349 :branch => node[:nominatim][:revision],
350 :update_stop_file => "#{basedir}/status/updates_disabled",
351 :update_maintenance_trigger => "#{basedir}/status/update_maintenance",
352 :qabindir => qa_bin_directory,
353 :qadatadir => qa_data_directory
356 template "/etc/init.d/nominatim-update" do
357 source "updater.init.erb"
361 variables :source_directory => source_directory
364 %w[backup-nominatim vacuum-db-nominatim].each do |fname|
365 template "/usr/local/bin/#{fname}" do
366 source "#{fname}.erb"
370 variables :db => node[:nominatim][:dbname]
374 ## webserver frontend
376 directory "#{basedir}/etc" do
382 %w[user_agent referrer email generic].each do |name|
383 file "#{basedir}/etc/nginx_blocked_#{name}.conf" do
384 action :create_if_missing
391 node[:nominatim][:fpm_pools].each do |name, data|
395 pm_max_children data[:max_children]
397 pm_min_spare_servers 10
398 pm_max_spare_servers 20
399 pm_max_requests 10000
400 prometheus_port data[:prometheus_port]
404 ssl_certificate node[:fqdn] do
405 domains [node[:fqdn],
406 "nominatim.openstreetmap.org",
408 "nominatim.openstreetmap.com",
409 "nominatim.openstreetmap.net",
410 "nominatim.openstreetmaps.org",
411 "nominatim.openmaps.org",
412 "nominatim.qgis.org"]
413 notifies :reload, "service[nginx]"
416 include_recipe "nginx"
418 nginx_site "default" do
422 frontends = search(:node, "recipes:web\\:\\:frontend").sort_by(&:name)
424 nginx_site "nominatim" do
426 directory build_directory
427 variables :pools => node[:nominatim][:fpm_pools],
428 :frontends => frontends,
429 :confdir => "#{basedir}/etc",
430 :ui_directory => ui_directory
433 template "/etc/logrotate.d/nginx" do
434 source "logrotate.nginx.erb"
440 munin_plugin_conf "nominatim" do
442 variables :db => node[:nominatim][:dbname],
443 :querylog => "#{node[:nominatim][:logdir]}/query.log"
446 munin_plugin "nominatim_importlag" do
447 target "#{source_directory}/munin/nominatim_importlag"
450 munin_plugin "nominatim_query_speed" do
451 target "#{source_directory}/munin/nominatim_query_speed_querylog"
454 munin_plugin "nominatim_requests" do
455 target "#{source_directory}/munin/nominatim_requests_querylog"
458 directory "#{basedir}/status" do
464 include_recipe "fail2ban"
466 frontend_addresses = frontends.collect { |f| f.ipaddresses(:role => :external) }
468 fail2ban_jail "nominatim_limit_req" do
469 filter "nginx-limit-req"
470 logpath "#{node[:nominatim][:logdir]}/nominatim.openstreetmap.org-error.log"
473 ignoreips frontend_addresses.flatten.sort
476 ### QA tile generation
478 if node[:nominatim][:enable_qa_tiles]
479 git qa_bin_directory do
480 repository node[:nominatim][:qa_repository]
481 revision node[:nominatim][:qa_revision]
482 enable_submodules true
485 notifies :run, "execute[compile_qa]"
488 execute "compile_qa" do
491 cwd "#{qa_bin_directory}/clustering-vt"
495 directory qa_data_directory do
502 template "#{qa_bin_directory}/analyser/config/config.yaml" do
503 source "qa_config.erb"
507 variables :outputdir => "#{qa_data_directory}/new"
510 link "#{build_directory}/website/qa-data" do
511 to "#{qa_data_directory}/current"