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
175 source_directory = "#{basedir}/nominatim"
176 build_directory = "#{basedir}/bin"
178 directory build_directory do
185 # Normally syncing via chef is a bad idea because syncing might involve
186 # an update of database functions which should not be done while an update
187 # is ongoing. Therefore we sync in between update cycles. There is an
188 # exception for slaves: they get DB function updates from the master, so
189 # only the source code needs to be updated, which chef may do.
190 git source_directory do
191 action node[:nominatim][:state] == "slave" ? :sync : :checkout
192 repository node[:nominatim][:repository]
193 revision node[:nominatim][:revision]
194 enable_submodules true
197 not_if { node[:nominatim][:state] != "slave" && File.exist?("#{source_directory}/README.md") }
198 notifies :run, "execute[compile_nominatim]", :immediately
201 execute "compile_nominatim" do
205 command "cmake #{source_directory} && make"
208 template "#{source_directory}/.git/hooks/post-merge" do
209 source "git-post-merge-hook.erb"
213 variables :srcdir => source_directory,
214 :builddir => build_directory,
215 :dbname => node[:nominatim][:dbname]
218 template "#{build_directory}/settings/local.php" do
219 source "settings.erb"
223 variables :base_url => node[:nominatim][:state] == "off" ? node[:fqdn] : "nominatim.openstreetmap.org",
224 :dbname => node[:nominatim][:dbname],
225 :flatnode_file => node[:nominatim][:flatnode_file],
226 :log_file => "#{node[:nominatim][:logdir]}/query.log"
229 if node[:nominatim][:flatnode_file]
230 directory File.dirname(node[:nominatim][:flatnode_file]) do
235 template "/etc/logrotate.d/nominatim" do
236 source "logrotate.nominatim.erb"
243 "wikimedia-importance.sql.gz",
244 "gb_postcode_data.sql.gz"
247 external_data.each do |fname|
248 remote_file "#{source_directory}/data/#{fname}" do
249 action :create_if_missing
250 source "https://www.nominatim.org/data/#{fname}"
257 remote_file "#{source_directory}/data/country_osm_grid.sql.gz" do
258 action :create_if_missing
259 source "https://www.nominatim.org/data/country_grid.sql.gz"
265 file "/etc/cron.d/nominatim" do
269 if node[:nominatim][:state] == "off"
270 cron_d "nominatim-backup" do
274 cron_d "nominatim-vacuum-db" do
278 cron_d "nominatim-clean-db" do
282 cron_d "nominatim-update-maintenance-trigger" do
286 cron_d "nominatim-backup" do
287 action node[:nominatim][:enable_backup] ? :create : :delete
292 command "/usr/local/bin/backup-nominatim"
296 cron_d "nominatim-vacuum-db" do
300 command "/usr/local/bin/vacuum-db-nominatim"
304 cron_d "nominatim-clean-db" do
305 action node[:nominatim][:state] == "master" ? :create : :delete
309 command "/usr/local/bin/clean-db-nominatim"
313 cron_d "nominatim-update-maintenance-trigger" do
317 command "touch #{basedir}/status/update_maintenance"
322 template "#{source_directory}/utils/nominatim-update" do
327 variables :bindir => build_directory,
328 :srcdir => source_directory,
329 :logfile => "#{node[:nominatim][:logdir]}/update.log",
330 :branch => node[:nominatim][:revision],
331 :update_stop_file => "#{basedir}/status/updates_disabled",
332 :update_maintenance_trigger => "#{basedir}/status/update_maintenance"
335 template "/etc/init.d/nominatim-update" do
336 source "updater.init.erb"
340 variables :source_directory => source_directory
343 %w[backup-nominatim vacuum-db-nominatim].each do |fname|
344 template "/usr/local/bin/#{fname}" do
345 source "#{fname}.erb"
349 variables :db => node[:nominatim][:dbname]
353 ## webserver frontend
355 directory "#{basedir}/etc" do
361 %w[user_agent referrer email].each do |name|
362 file "#{basedir}/etc/nginx_blocked_#{name}.conf" do
363 action :create_if_missing
370 node[:nominatim][:fpm_pools].each do |name, data|
372 template "fpm.conf.erb"
373 variables data.merge(:name => name)
377 ssl_certificate node[:fqdn] do
378 domains [node[:fqdn],
379 "nominatim.openstreetmap.org",
381 "nominatim.openstreetmap.com",
382 "nominatim.openstreetmap.net",
383 "nominatim.openstreetmaps.org",
384 "nominatim.openmaps.org"]
385 notifies :reload, "service[nginx]"
392 include_recipe "nginx"
394 nginx_site "default" do
398 nginx_site "nominatim" do
400 directory build_directory
401 variables :pools => node[:nominatim][:fpm_pools],
402 :frontends => search(:node, "recipes:web\\:\\:frontend"),
403 :confdir => "#{basedir}/etc"
406 template "/etc/logrotate.d/nginx" do
407 source "logrotate.nginx.erb"
413 munin_plugin_conf "nominatim" do
415 variables :db => node[:nominatim][:dbname],
416 :querylog => "#{node[:nominatim][:logdir]}/query.log"
419 munin_plugin "nominatim_importlag" do
420 target "#{source_directory}/munin/nominatim_importlag"
423 munin_plugin "nominatim_query_speed" do
424 target "#{source_directory}/munin/nominatim_query_speed_querylog"
427 munin_plugin "nominatim_requests" do
428 target "#{source_directory}/munin/nominatim_requests_querylog"
431 directory "#{basedir}/status" do
437 include_recipe "fail2ban"
439 fail2ban_jail "nominatim_limit_req" do
440 filter "nginx-limit-req"
441 logpath "#{node[:nominatim][:logdir]}/nominatim.openstreetmap.org-error.log"