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
177 source_directory = "#{basedir}/nominatim"
178 build_directory = "#{basedir}/bin"
179 ui_directory = "#{basedir}/ui"
181 directory build_directory do
188 # Normally syncing via chef is a bad idea because syncing might involve
189 # an update of database functions which should not be done while an update
190 # is ongoing. Therefore we sync in between update cycles. There is an
191 # exception for slaves: they get DB function updates from the master, so
192 # only the source code needs to be updated, which chef may do.
193 git source_directory do
194 action node[:nominatim][:state] == "slave" ? :sync : :checkout
195 repository node[:nominatim][:repository]
196 revision node[:nominatim][:revision]
197 enable_submodules true
200 not_if { node[:nominatim][:state] != "slave" && File.exist?("#{source_directory}/README.md") }
201 notifies :run, "execute[compile_nominatim]"
204 remote_file "#{source_directory}/data/country_osm_grid.sql.gz" do
205 action :create_if_missing
206 source "https://www.nominatim.org/data/country_grid.sql.gz"
212 execute "compile_nominatim" do
216 command "cmake #{source_directory} && make"
219 template "#{source_directory}/.git/hooks/post-merge" do
220 source "git-post-merge-hook.erb"
224 variables :srcdir => source_directory,
225 :builddir => build_directory,
226 :dbname => node[:nominatim][:dbname]
229 template "#{build_directory}/.env" do
230 source "nominatim.env.erb"
234 variables :base_url => node[:nominatim][:state] == "off" ? node[:fqdn] : "nominatim.openstreetmap.org",
235 :dbname => node[:nominatim][:dbname],
236 :flatnode_file => node[:nominatim][:flatnode_file],
237 :log_file => "#{node[:nominatim][:logdir]}/query.log"
242 repository node[:nominatim][:ui_repository]
243 revision node[:nominatim][:ui_revision]
248 template "#{ui_directory}/dist/theme/config.theme.js" do
249 source "ui-config.js.erb"
255 if node[:nominatim][:flatnode_file]
256 directory File.dirname(node[:nominatim][:flatnode_file]) do
261 template "/etc/logrotate.d/nominatim" do
262 source "logrotate.nominatim.erb"
269 "wikimedia-importance.sql.gz",
270 "gb_postcode_data.sql.gz",
271 "us_postcode_data.sql.gz"
274 external_data.each do |fname|
275 remote_file "#{build_directory}/#{fname}" do
276 action :create_if_missing
277 source "https://www.nominatim.org/data/#{fname}"
284 if node[:nominatim][:state] == "off"
285 cron_d "nominatim-backup" do
289 cron_d "nominatim-vacuum-db" do
293 cron_d "nominatim-clean-db" do
297 cron_d "nominatim-update-maintenance-trigger" do
301 cron_d "nominatim-backup" do
302 action node[:nominatim][:enable_backup] ? :create : :delete
307 command "/usr/local/bin/backup-nominatim"
311 cron_d "nominatim-vacuum-db" do
315 command "/usr/local/bin/vacuum-db-nominatim"
319 cron_d "nominatim-clean-db" do
320 action node[:nominatim][:state] == "master" ? :create : :delete
324 command "/usr/local/bin/clean-db-nominatim"
328 cron_d "nominatim-update-maintenance-trigger" do
332 command "touch #{basedir}/status/update_maintenance"
337 template "#{source_directory}/utils/nominatim-update" do
342 variables :bindir => build_directory,
343 :srcdir => source_directory,
344 :logfile => "#{node[:nominatim][:logdir]}/update.log",
345 :branch => node[:nominatim][:revision],
346 :update_stop_file => "#{basedir}/status/updates_disabled",
347 :update_maintenance_trigger => "#{basedir}/status/update_maintenance"
350 template "/etc/init.d/nominatim-update" do
351 source "updater.init.erb"
355 variables :source_directory => source_directory
358 %w[backup-nominatim vacuum-db-nominatim].each do |fname|
359 template "/usr/local/bin/#{fname}" do
360 source "#{fname}.erb"
364 variables :db => node[:nominatim][:dbname]
368 ## webserver frontend
370 directory "#{basedir}/etc" do
376 %w[user_agent referrer email generic].each do |name|
377 file "#{basedir}/etc/nginx_blocked_#{name}.conf" do
378 action :create_if_missing
385 node[:nominatim][:fpm_pools].each do |name, data|
389 pm_max_children data[:max_children]
391 pm_min_spare_servers 10
392 pm_max_spare_servers 20
393 pm_max_requests 10000
394 prometheus_port data[:prometheus_port]
398 ssl_certificate node[:fqdn] do
399 domains [node[:fqdn],
400 "nominatim.openstreetmap.org",
402 "nominatim.openstreetmap.com",
403 "nominatim.openstreetmap.net",
404 "nominatim.openstreetmaps.org",
405 "nominatim.openmaps.org",
406 "nominatim.qgis.org"]
407 notifies :reload, "service[nginx]"
414 include_recipe "nginx"
416 nginx_site "default" do
420 frontends = search(:node, "recipes:web\\:\\:frontend").sort_by(&:name)
422 nginx_site "nominatim" do
424 directory build_directory
425 variables :pools => node[:nominatim][:fpm_pools],
426 :frontends => frontends,
427 :confdir => "#{basedir}/etc",
428 :ui_directory => ui_directory
431 template "/etc/logrotate.d/nginx" do
432 source "logrotate.nginx.erb"
438 munin_plugin_conf "nominatim" do
440 variables :db => node[:nominatim][:dbname],
441 :querylog => "#{node[:nominatim][:logdir]}/query.log"
444 munin_plugin "nominatim_importlag" do
445 target "#{source_directory}/munin/nominatim_importlag"
448 munin_plugin "nominatim_query_speed" do
449 target "#{source_directory}/munin/nominatim_query_speed_querylog"
452 munin_plugin "nominatim_requests" do
453 target "#{source_directory}/munin/nominatim_requests_querylog"
456 directory "#{basedir}/status" do
462 include_recipe "fail2ban"
464 frontend_addresses = frontends.collect { |f| f.ipaddresses(:role => :external) }
466 fail2ban_jail "nominatim_limit_req" do
467 filter "nginx-limit-req"
468 logpath "#{node[:nominatim][:logdir]}/nominatim.openstreetmap.org-error.log"
471 ignoreips frontend_addresses.flatten.sort