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
55 # exception granted for a limited time so that they can set up their own server
56 firewall_rule "increase-limits-gnome-proxy" do
59 source "net:8.43.85.23"
63 rate_limit "s:10/sec:30"
68 include_recipe "postgresql"
70 postgresql_version = node[:nominatim][:dbcluster].split("/").first
71 postgis_version = node[:nominatim][:postgis]
73 package "postgresql-#{postgresql_version}-postgis-#{postgis_version}"
75 node[:nominatim][:dbadmins].each do |user|
76 postgresql_user user do
77 cluster node[:nominatim][:dbcluster]
79 only_if { node[:nominatim][:state] != "slave" }
83 postgresql_user "nominatim" do
84 cluster node[:nominatim][:dbcluster]
86 only_if { node[:nominatim][:state] != "slave" }
89 postgresql_user "www-data" do
90 cluster node[:nominatim][:dbcluster]
91 only_if { node[:nominatim][:state] != "slave" }
94 postgresql_munin "nominatim" do
95 cluster node[:nominatim][:dbcluster]
96 database node[:nominatim][:dbname]
99 directory "#{basedir}/tablespaces" do
105 # NOTE: tablespaces must be exactly in the same location on each
106 # Nominatim instance when replication is in use. Therefore
107 # use symlinks to canonical directory locations.
108 node[:nominatim][:tablespaces].each do |name, location|
109 directory location do
116 link "#{basedir}/tablespaces/#{name}" do
120 postgresql_tablespace name do
121 cluster node[:nominatim][:dbcluster]
122 location "#{basedir}/tablespaces/#{name}"
126 if node[:nominatim][:state] == "master"
127 postgresql_user "replication" do
128 cluster node[:nominatim][:dbcluster]
129 password data_bag_item("nominatim", "passwords")["replication"]
133 directory node[:rsyncd][:modules][:archive][:path] do
139 template "/usr/local/bin/clean-db-nominatim" do
140 source "clean-db-nominatim.erb"
144 variables :archive_dir => node[:rsyncd][:modules][:archive][:path],
145 :update_stop_file => "#{basedir}/status/updates_disabled",
146 :streaming_clients => search(:node, "nominatim_state:slave").map { |slave| slave[:fqdn] }.join(" ")
160 libboost-filesystem-dev
176 source_directory = "#{basedir}/nominatim"
177 build_directory = "#{basedir}/bin"
178 ui_directory = "#{basedir}/ui"
180 directory build_directory do
187 # Normally syncing via chef is a bad idea because syncing might involve
188 # an update of database functions which should not be done while an update
189 # is ongoing. Therefore we sync in between update cycles. There is an
190 # exception for slaves: they get DB function updates from the master, so
191 # only the source code needs to be updated, which chef may do.
192 git source_directory do
193 action node[:nominatim][:state] == "slave" ? :sync : :checkout
194 repository node[:nominatim][:repository]
195 revision node[:nominatim][:revision]
196 enable_submodules true
199 not_if { node[:nominatim][:state] != "slave" && File.exist?("#{source_directory}/README.md") }
200 notifies :run, "execute[compile_nominatim]", :immediately
203 execute "compile_nominatim" do
207 command "cmake #{source_directory} && make"
210 template "#{source_directory}/.git/hooks/post-merge" do
211 source "git-post-merge-hook.erb"
215 variables :srcdir => source_directory,
216 :builddir => build_directory,
217 :dbname => node[:nominatim][:dbname]
220 template "#{build_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"
233 repository node[:nominatim][:ui_repository]
234 revision node[:nominatim][:ui_revision]
239 template "#{ui_directory}/dist/config.js" do
240 source "ui-config.js.erb"
246 if node[:nominatim][:flatnode_file]
247 directory File.dirname(node[:nominatim][:flatnode_file]) do
252 template "/etc/logrotate.d/nominatim" do
253 source "logrotate.nominatim.erb"
260 "wikimedia-importance.sql.gz",
261 "gb_postcode_data.sql.gz"
264 external_data.each do |fname|
265 remote_file "#{source_directory}/data/#{fname}" do
266 action :create_if_missing
267 source "https://www.nominatim.org/data/#{fname}"
274 remote_file "#{source_directory}/data/country_osm_grid.sql.gz" do
275 action :create_if_missing
276 source "https://www.nominatim.org/data/country_grid.sql.gz"
282 if node[:nominatim][:state] == "off"
283 cron_d "nominatim-backup" do
287 cron_d "nominatim-vacuum-db" do
291 cron_d "nominatim-clean-db" do
295 cron_d "nominatim-update-maintenance-trigger" do
299 cron_d "nominatim-backup" do
300 action node[:nominatim][:enable_backup] ? :create : :delete
305 command "/usr/local/bin/backup-nominatim"
309 cron_d "nominatim-vacuum-db" do
313 command "/usr/local/bin/vacuum-db-nominatim"
317 cron_d "nominatim-clean-db" do
318 action node[:nominatim][:state] == "master" ? :create : :delete
322 command "/usr/local/bin/clean-db-nominatim"
326 cron_d "nominatim-update-maintenance-trigger" do
330 command "touch #{basedir}/status/update_maintenance"
335 template "#{source_directory}/utils/nominatim-update" do
340 variables :bindir => build_directory,
341 :srcdir => source_directory,
342 :logfile => "#{node[:nominatim][:logdir]}/update.log",
343 :branch => node[:nominatim][:revision],
344 :update_stop_file => "#{basedir}/status/updates_disabled",
345 :update_maintenance_trigger => "#{basedir}/status/update_maintenance"
348 template "/etc/init.d/nominatim-update" do
349 source "updater.init.erb"
353 variables :source_directory => source_directory
356 %w[backup-nominatim vacuum-db-nominatim].each do |fname|
357 template "/usr/local/bin/#{fname}" do
358 source "#{fname}.erb"
362 variables :db => node[:nominatim][:dbname]
366 ## webserver frontend
368 directory "#{basedir}/etc" do
374 %w[user_agent referrer email generic].each do |name|
375 file "#{basedir}/etc/nginx_blocked_#{name}.conf" do
376 action :create_if_missing
383 node[:nominatim][:fpm_pools].each do |name, data|
387 pm_max_children data[:max_children]
389 pm_min_spare_servers 10
390 pm_max_spare_servers 20
391 pm_max_requests 10000
392 prometheus_port data[:prometheus_port]
396 ssl_certificate node[:fqdn] do
397 domains [node[:fqdn],
398 "nominatim.openstreetmap.org",
400 "nominatim.openstreetmap.com",
401 "nominatim.openstreetmap.net",
402 "nominatim.openstreetmaps.org",
403 "nominatim.openmaps.org"]
404 notifies :reload, "service[nginx]"
411 include_recipe "nginx"
413 nginx_site "default" do
417 frontends = search(:node, "recipes:web\\:\\:frontend").sort_by(&:name)
419 nginx_site "nominatim" do
421 directory build_directory
422 variables :pools => node[:nominatim][:fpm_pools],
423 :frontends => frontends,
424 :confdir => "#{basedir}/etc",
425 :ui_directory => ui_directory
428 template "/etc/logrotate.d/nginx" do
429 source "logrotate.nginx.erb"
435 munin_plugin_conf "nominatim" do
437 variables :db => node[:nominatim][:dbname],
438 :querylog => "#{node[:nominatim][:logdir]}/query.log"
441 munin_plugin "nominatim_importlag" do
442 target "#{source_directory}/munin/nominatim_importlag"
445 munin_plugin "nominatim_query_speed" do
446 target "#{source_directory}/munin/nominatim_query_speed_querylog"
449 munin_plugin "nominatim_requests" do
450 target "#{source_directory}/munin/nominatim_requests_querylog"
453 directory "#{basedir}/status" do
459 include_recipe "fail2ban"
461 frontend_addresses = frontends.collect { |f| f.ipaddresses(:role => :external) }
463 fail2ban_jail "nominatim_limit_req" do
464 filter "nginx-limit-req"
465 logpath "#{node[:nominatim][:logdir]}/nominatim.openstreetmap.org-error.log"
468 ignoreips frontend_addresses.flatten.sort