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 "munin"
22 basedir = data_bag_item("accounts", "nominatim")["home"]
23 email_errors = data_bag_item("accounts", "lonvia")["email"]
32 directory node[:nominatim][:logdir] do
39 file "#{node[:nominatim][:logdir]}/query.log" do
40 action :create_if_missing
46 file "#{node[:nominatim][:logdir]}/update.log" do
47 action :create_if_missing
55 include_recipe "postgresql"
57 postgresql_version = node[:nominatim][:dbcluster].split("/").first
58 postgis_version = node[:nominatim][:postgis]
60 package "postgresql-#{postgresql_version}-postgis-#{postgis_version}"
62 node[:nominatim][:dbadmins].each do |user|
63 postgresql_user user do
64 cluster node[:nominatim][:dbcluster]
66 only_if { node[:nominatim][:state] != "slave" }
70 postgresql_user "nominatim" do
71 cluster node[:nominatim][:dbcluster]
73 only_if { node[:nominatim][:state] != "slave" }
76 postgresql_user "www-data" do
77 cluster node[:nominatim][:dbcluster]
78 only_if { node[:nominatim][:state] != "slave" }
81 postgresql_munin "nominatim" do
82 cluster node[:nominatim][:dbcluster]
83 database node[:nominatim][:dbname]
86 directory "#{basedir}/tablespaces" do
92 # Note: tablespaces must be exactly in the same location on each
93 # Nominatim instance when replication is in use. Therefore
94 # use symlinks to canonical directory locations.
95 node[:nominatim][:tablespaces].each do |name, location|
103 link "#{basedir}/tablespaces/#{name}" do
107 postgresql_tablespace name do
108 cluster node[:nominatim][:dbcluster]
109 location "#{basedir}/tablespaces/#{name}"
113 if node[:nominatim][:state] == "master"
114 postgresql_user "replication" do
115 cluster node[:nominatim][:dbcluster]
116 password data_bag_item("nominatim", "passwords")["replication"]
120 directory node[:rsyncd][:modules][:archive][:path] do
126 template "/usr/local/bin/clean-db-nominatim" do
127 source "clean-db-nominatim.erb"
131 variables :archive_dir => node[:rsyncd][:modules][:archive][:path],
132 :update_stop_file => "#{basedir}/status/updates_disabled",
133 :streaming_clients => search(:node, "nominatim_state:slave").map { |slave| slave[:fqdn] }.join(" ")
147 libboost-filesystem-dev
160 source_directory = "#{basedir}/nominatim"
161 build_directory = "#{basedir}/bin"
163 directory build_directory do
170 # Normally syncing via chef is a bad idea because syncing might involve
171 # an update of database functions which should not be done while an update
172 # is ongoing. Therefore we sync in between update cycles. There is an
173 # exception for slaves: they get DB function updates from the master, so
174 # only the source code needs to be updated, which chef may do.
175 git source_directory do
176 action node[:nominatim][:state] == "slave" ? :sync : :checkout
177 repository node[:nominatim][:repository]
178 revision node[:nominatim][:revision]
179 enable_submodules true
182 not_if { node[:nominatim][:state] != "slave" && File.exist?("#{source_directory}/README.md") }
183 notifies :run, "execute[compile_nominatim]", :immediately
186 execute "compile_nominatim" do
190 command "cmake #{source_directory} && make"
193 template "#{source_directory}/.git/hooks/post-merge" do
194 source "git-post-merge-hook.erb"
198 variables :srcdir => source_directory,
199 :builddir => build_directory,
200 :dbname => node[:nominatim][:dbname]
203 template "#{build_directory}/settings/local.php" do
204 source "settings.erb"
208 variables :base_url => node[:nominatim][:state] == "off" ? node[:fqdn] : "nominatim.openstreetmap.org",
209 :dbname => node[:nominatim][:dbname],
210 :flatnode_file => node[:nominatim][:flatnode_file],
211 :log_file => "#{node[:nominatim][:logdir]}/query.log"
214 if node[:nominatim][:flatnode_file]
215 directory File.dirname(node[:nominatim][:flatnode_file]) do
220 template "/etc/logrotate.d/nominatim" do
221 source "logrotate.nominatim.erb"
228 "wikimedia-importance.sql.gz",
229 "gb_postcode_data.sql.gz"
232 external_data.each do |fname|
233 remote_file "#{source_directory}/data/#{fname}" do
234 action :create_if_missing
235 source "https://www.nominatim.org/data/#{fname}"
242 remote_file "#{source_directory}/data/country_osm_grid.sql.gz" do
243 action :create_if_missing
244 source "https://www.nominatim.org/data/country_grid.sql.gz"
250 template "/etc/cron.d/nominatim" do
251 action node[:nominatim][:state] == "off" ? :delete : :create
252 source "nominatim.cron.erb"
256 variables :bin_directory => "#{source_directory}/utils",
257 :mailto => email_errors,
258 :update_maintenance_trigger => "#{basedir}/status/update_maintenance"
261 template "#{source_directory}/utils/nominatim-update" do
266 variables :bindir => build_directory,
267 :srcdir => source_directory,
268 :logfile => "#{node[:nominatim][:logdir]}/update.log",
269 :branch => node[:nominatim][:revision],
270 :update_stop_file => "#{basedir}/status/updates_disabled",
271 :update_maintenance_trigger => "#{basedir}/status/update_maintenance"
274 template "/etc/init.d/nominatim-update" do
275 source "updater.init.erb"
279 variables :source_directory => source_directory
282 %w[backup-nominatim vacuum-db-nominatim].each do |fname|
283 template "/usr/local/bin/#{fname}" do
284 source "#{fname}.erb"
288 variables :db => node[:nominatim][:dbname]
292 ## webserver frontend
294 template "#{build_directory}/settings/ip_blocks.conf" do
295 action :create_if_missing
296 source "ipblocks.erb"
302 file "#{build_directory}/settings/apache_blocks.conf" do
303 action :create_if_missing
309 file "#{build_directory}/settings/ip_blocks.map" do
310 action :create_if_missing
316 include_recipe "apache"
323 apache_module "rewrite"
324 apache_module "proxy"
325 apache_module "proxy_fcgi"
326 apache_module "proxy_http"
327 apache_module "headers"
329 service "php7.2-fpm" do
330 action [:enable, :start]
331 supports :status => true, :restart => true, :reload => true
334 node[:nominatim][:fpm_pools].each do |name, data|
335 template "/etc/php/7.2/fpm/pool.d/#{name}.conf" do
336 source "fpm.conf.erb"
340 variables data.merge(:name => name)
341 notifies :reload, "service[php7.2-fpm]"
345 systemd_service "apache-nominatim" do
349 notifies :restart, "service[apache2]"
352 ssl_certificate "nominatim.openstreetmap.org" do
353 domains ["nominatim.openstreetmap.org",
355 "nominatim.openstreetmap.com",
356 "nominatim.openstreetmap.net",
357 "nominatim.openstreetmaps.org",
358 "nominatim.openmaps.org"]
359 notifies :reload, "service[apache2]"
362 apache_site "nominatim.openstreetmap.org" do
363 template "apache.erb"
364 directory build_directory
365 variables :pools => node[:nominatim][:fpm_pools]
366 only_if { node[:nominatim][:state] != "off" }
369 apache_site "default" do
373 template "/etc/logrotate.d/apache2" do
374 source "logrotate.apache.erb"
380 include_recipe "fail2ban"
382 munin_plugin_conf "nominatim" do
384 variables :db => node[:nominatim][:dbname],
385 :querylog => "#{node[:nominatim][:logdir]}/query.log"
388 munin_plugin "nominatim_importlag" do
389 target "#{source_directory}/munin/nominatim_importlag"
392 munin_plugin "nominatim_query_speed" do
393 target "#{source_directory}/munin/nominatim_query_speed_querylog"
396 munin_plugin "nominatim_requests" do
397 target "#{source_directory}/munin/nominatim_requests_querylog"
400 munin_plugin "nominatim_throttled_ips" do
401 target "#{source_directory}/munin/nominatim_throttled_ips"
404 directory "#{basedir}/status" do