2 # Cookbook Name:: nominatim
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 # http://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 basedir = data_bag_item("accounts", "nominatim")["home"]
21 email_errors = data_bag_item("accounts", "lonvia")["email"]
30 directory node[:nominatim][:logdir] do
37 file "#{node[:nominatim][:logdir]}/query.log" do
38 action :create_if_missing
44 file "#{node[:nominatim][:logdir]}/update.log" do
45 action :create_if_missing
53 include_recipe "postgresql"
55 postgresql_version = node[:nominatim][:dbcluster].split("/").first
58 package "postgresql-#{postgresql_version}-postgis-2.3"
60 node[:nominatim][:dbadmins].each do |user|
61 postgresql_user user do
62 cluster node[:nominatim][:dbcluster]
64 only_if { node[:nominatim][:state] != "slave" }
68 postgresql_user "nominatim" do
69 cluster node[:nominatim][:dbcluster]
71 only_if { node[:nominatim][:state] != "slave" }
74 postgresql_user "www-data" do
75 cluster node[:nominatim][:dbcluster]
76 only_if { node[:nominatim][:state] != "slave" }
79 postgresql_munin "nominatim" do
80 cluster node[:nominatim][:dbcluster]
81 database node[:nominatim][:dbname]
84 directory "#{basedir}/tablespaces" do
90 # Note: tablespaces must be exactly in the same location on each
91 # Nominatim instance when replication is in use. Therefore
92 # use symlinks to canonical directory locations.
93 node[:nominatim][:tablespaces].each do |name, location|
101 link "#{basedir}/tablespaces/#{name}" do
105 postgresql_tablespace name do
106 cluster node[:nominatim][:dbcluster]
107 location "#{basedir}/tablespaces/#{name}"
111 if node[:nominatim][:state] == "master" # ~FC023
112 postgresql_user "replication" do
113 cluster node[:nominatim][:dbcluster]
114 password data_bag_item("nominatim", "passwords")["replication"]
118 directory node[:rsyncd][:modules][:archive][:path] do
124 template "/usr/local/bin/clean-db-nominatim" do
125 source "clean-db-nominatim.erb"
129 variables :archive_dir => node[:rsyncd][:modules][:archive][:path],
130 :update_stop_file => "#{basedir}/status/updates_disabled",
131 :streaming_clients => search(:node, "nominatim_state:slave").map { |slave| slave[:fqdn] }.join(" ")
138 include_recipe "python"
140 package "build-essential"
143 package "libboost-dev"
144 package "libboost-system-dev"
145 package "libboost-filesystem-dev"
146 package "libboost-python-dev"
147 package "libexpat1-dev"
149 package "libxml2-dev"
152 package "libgeos++-dev"
153 package "libproj-dev"
156 python_package "osmium"
158 source_directory = "#{basedir}/nominatim"
159 build_directory = "#{basedir}/bin"
161 directory build_directory do
168 # Normally syncing via chef is a bad idea because syncing might involve
169 # an update of database functions which should not be done while an update
170 # is ongoing. Therefore we sync in between update cycles. There is an
171 # exception for slaves: they get DB function updates from the master, so
172 # only the source code needs to be updated, which chef may do.
173 git source_directory do
174 action node[:nominatim][:state] == "slave" ? :sync : :checkout
175 repository node[:nominatim][:repository]
176 revision node[:nominatim][:revision]
177 enable_submodules true
180 not_if { node[:nominatim][:state] != "slave" && File.exist?("#{source_directory}/README.md") }
181 notifies :run, "execute[compile_nominatim]", :immediately
184 execute "compile_nominatim" do
188 command "cmake #{source_directory} && make"
191 template "#{source_directory}/.git/hooks/post-merge" do
192 source "git-post-merge-hook.erb"
196 variables :srcdir => source_directory,
197 :builddir => build_directory,
198 :dbname => node[:nominatim][:dbname]
201 template "#{build_directory}/settings/local.php" do
202 source "settings.erb"
206 variables :base_url => node[:nominatim][:state] == "off" ? node[:fqdn] : "nominatim.openstreetmap.org",
207 :dbname => node[:nominatim][:dbname],
208 :flatnode_file => node[:nominatim][:flatnode_file],
209 :log_file => "#{node[:nominatim][:logdir]}/query.log"
212 if node[:nominatim][:flatnode_file] # ~FC023
213 directory File.dirname(node[:nominatim][:flatnode_file]) do
218 template "/etc/logrotate.d/nominatim" do
219 source "logrotate.nominatim.erb"
226 "wikipedia_article.sql.bin",
227 "wikipedia_redirect.sql.bin",
228 "gb_postcode_data.sql.gz"
231 external_data.each do |fname|
232 remote_file "#{source_directory}/data/#{fname}" do
233 action :create_if_missing
234 source "http://www.nominatim.org/data/#{fname}"
241 remote_file "#{source_directory}/data/country_osm_grid.sql.gz" do
242 action :create_if_missing
243 source "http://www.nominatim.org/data/country_grid.sql.gz"
249 template "/etc/cron.d/nominatim" do
250 action node[:nominatim][:state] == :off ? :delete : :create
251 source "nominatim.cron.erb"
255 variables :bin_directory => "#{source_directory}/utils", :mailto => email_errors
258 template "#{source_directory}/utils/nominatim-update" do
263 variables :bindir => build_directory,
264 :srcdir => source_directory,
265 :logfile => "#{node[:nominatim][:logdir]}/update.log",
266 :branch => node[:nominatim][:revision],
267 :update_stop_file => "#{basedir}/status/updates_disabled"
270 template "/etc/init.d/nominatim-update" do
271 source "updater.init.erb"
275 variables :source_directory => source_directory
278 %w[backup-nominatim vacuum-db-nominatim].each do |fname|
279 template "/usr/local/bin/#{fname}" do
280 source "#{fname}.erb"
284 variables :db => node[:nominatim][:dbname]
288 ## webserver frontend
290 template "#{build_directory}/settings/ip_blocks.conf" do
291 action :create_if_missing
292 source "ipblocks.erb"
298 file "#{build_directory}/settings/apache_blocks.conf" do
299 action :create_if_missing
305 file "#{build_directory}/settings/ip_blocks.map" do
306 action :create_if_missing
312 include_recipe "apache"
321 apache_module "rewrite"
322 apache_module "proxy"
323 apache_module "proxy_fcgi"
324 apache_module "proxy_http"
325 apache_module "headers"
327 service "php7.0-fpm" do
328 action [:enable, :start]
329 supports :status => true, :restart => true, :reload => true
332 node[:nominatim][:fpm_pools].each do |name, data|
333 template "/etc/php/7.0/fpm/pool.d/#{name}.conf" do
334 source "fpm.conf.erb"
338 variables data.merge(:name => name)
339 notifies :reload, "service[php7.0-fpm]"
343 ssl_certificate "nominatim.openstreetmap.org" do
344 domains ["nominatim.openstreetmap.org",
346 "nominatim.openstreetmap.com",
347 "nominatim.openstreetmap.net",
348 "nominatim.openstreetmaps.org",
349 "nominatim.openmaps.org"]
350 notifies :reload, "service[apache2]"
353 apache_site "nominatim.openstreetmap.org" do
354 template "apache.erb"
355 directory build_directory
356 variables :pools => node[:nominatim][:fpm_pools]
359 apache_site "default" do
363 template "/etc/logrotate.d/apache2" do
364 source "logrotate.apache.erb"
370 include_recipe "fail2ban"
372 web_servers = search(:node, "recipes:web\\:\\:frontend").collect do |n| # ~FC010
373 n.ipaddresses(:role => :external)
376 fail2ban_filter "nominatim" do
377 failregex '^<HOST> - - \[\] "[^"]+" (408|429) '
380 fail2ban_jail "nominatim" do
382 logpath "#{node[:nominatim][:logdir]}/nominatim.openstreetmap.org-access.log"
385 ignoreips web_servers
388 munin_plugin_conf "nominatim" do
390 variables :db => node[:nominatim][:dbname],
391 :querylog => "#{node[:nominatim][:logdir]}/query.log"
394 munin_plugin "nominatim_importlag" do
395 target "#{source_directory}/munin/nominatim_importlag"
398 munin_plugin "nominatim_query_speed" do
399 target "#{source_directory}/munin/nominatim_query_speed_querylog"
402 munin_plugin "nominatim_requests" do
403 target "#{source_directory}/munin/nominatim_requests_querylog"
406 munin_plugin "nominatim_throttled_ips" do
407 target "#{source_directory}/munin/nominatim_throttled_ips"
410 directory "#{basedir}/status" do