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 "prometheus"
22 include_recipe "postgresql"
23 include_recipe "python"
24 include_recipe "nginx"
26 include_recipe "fail2ban"
28 basedir = data_bag_item("accounts", "nominatim")["home"]
29 project_directory = "#{basedir}/planet-project"
30 bin_directory = "#{basedir}/bin"
31 cfg_directory = "#{basedir}/etc"
32 ui_directory = "#{basedir}/ui"
33 qa_data_directory = "#{basedir}/qa-data"
42 [basedir, bin_directory, cfg_directory, project_directory, ui_directory].each do |path|
50 if node[:nominatim][:flatnode_file]
51 directory File.dirname(node[:nominatim][:flatnode_file]) do
56 directory "#{bin_directory}/maintenance" do
62 ## Log directory setup
64 directory node[:nominatim][:logdir] do
71 file "#{node[:nominatim][:logdir]}/query.log" do
72 action :create_if_missing
80 postgresql_version = node[:nominatim][:dbcluster].split("/").first
81 postgis_version = node[:nominatim][:postgis]
83 package "postgresql-#{postgresql_version}-postgis-#{postgis_version}"
85 node[:nominatim][:dbadmins].each do |user|
86 postgresql_user user do
87 cluster node[:nominatim][:dbcluster]
92 postgresql_user "nominatim" do
93 cluster node[:nominatim][:dbcluster]
97 postgresql_user "www-data" do
98 cluster node[:nominatim][:dbcluster]
103 python_directory = "#{basedir}/venv"
117 python_virtualenv python_directory do
118 interpreter "/usr/bin/python3"
121 # These are updated during the database update.
122 python_package "nominatim-db" do
123 python_virtualenv python_directory
124 extra_index_url node[:nominatim][:pip_index]
127 python_package "nominatim-api" do
128 python_virtualenv python_directory
129 extra_index_url node[:nominatim][:pip_index]
132 remote_directory "#{project_directory}/static-website" do
137 files_owner "nominatim"
138 files_group "nominatim"
143 template "#{project_directory}/.env" do
144 source "nominatim.env.erb"
148 variables :base_url => "nominatim.openstreetmap.org",
149 :dbname => node[:nominatim][:dbname],
150 :flatnode_file => node[:nominatim][:flatnode_file],
151 :log_file => "#{node[:nominatim][:logdir]}/query.log",
152 :pool_size => node[:nominatim][:api_pool_size],
153 :query_timeout => node[:nominatim][:api_query_timeout],
154 :request_timeout => node[:nominatim][:api_request_timeout]
157 remote_file "#{project_directory}/secondary_importance.sql.gz" do
158 action :create_if_missing
159 source "https://nominatim.org/data/wikimedia-secondary-importance.sql.gz"
165 remote_file "#{project_directory}/wikimedia-importance.csv.gz" do
166 action :create_if_missing
167 source "https://nominatim.org/data/wikimedia-importance.csv.gz"
173 %w[gb_postcodes.csv.gz us_postcodes.csv.gz].each do |fname|
174 remote_file "#{project_directory}/#{fname}" do
176 source "https://nominatim.org/data/#{fname}"
183 # Webserver + frontend
185 %w[user_agent referrer email generic].each do |name|
186 file "#{cfg_directory}/nginx_blocked_#{name}.conf" do
187 action :create_if_missing
194 systemd_service "nominatim" do
195 description "Nominatim running as a gunicorn application"
198 working_directory project_directory
199 standard_output "append:#{node[:nominatim][:logdir]}/gunicorn.log"
200 standard_error "inherit"
201 exec_start "#{python_directory}/bin/gunicorn --max-requests 200000 -b unix:/run/gunicorn-nominatim.openstreetmap.org.sock -w #{node[:nominatim][:api_workers]} -k uvicorn.workers.UvicornWorker 'nominatim_api.server.falcon.server:run_wsgi()'"
202 exec_reload "/bin/kill -s HUP $MAINPID"
206 requires "nominatim.socket"
207 after "network.target"
210 systemd_socket "nominatim" do
211 description "Gunicorn socket for Nominatim"
212 listen_stream "/run/gunicorn-nominatim.openstreetmap.org.sock"
213 socket_user "www-data"
216 ssl_certificate node[:fqdn] do
217 domains [node[:fqdn],
218 "nominatim.openstreetmap.org",
220 "nominatim.openstreetmap.com",
221 "nominatim.openstreetmap.net",
222 "nominatim.openstreetmaps.org",
223 "nominatim.openmaps.org",
224 "nominatim.qgis.org"]
225 notifies :reload, "service[nginx]"
228 nginx_site "default" do
232 frontends = search(:node, "recipes:web\\:\\:frontend").sort_by(&:name)
234 nginx_site "nominatim" do
236 directory project_directory
237 variables :pools => node[:nominatim][:fpm_pools],
238 :frontends => frontends,
239 :confdir => "#{basedir}/etc",
240 :ui_directory => ui_directory
243 template "/etc/logrotate.d/nginx" do
244 source "logrotate.nginx.erb"
250 ### Import, update and maintenance scripts
253 nominatim-update-data
254 nominatim-update-refresh-db
255 nominatim-daily-maintenance].each do |fname|
256 template "#{bin_directory}/#{fname}" do
257 source "#{fname}.erb"
261 variables :bindir => bin_directory,
262 :projectdir => project_directory,
263 :venvprefix => "#{python_directory}/bin/",
264 :qadatadir => qa_data_directory
268 systemd_service "nominatim-update" do
269 description "Update the Nominatim database"
270 exec_start "#{bin_directory}/nominatim-update"
272 standard_output "journal"
273 standard_error "inherit"
274 working_directory project_directory
277 systemd_service "nominatim-update-maintenance-trigger" do
278 description "Trigger daily maintenance tasks for Nominatim DB"
279 exec_start "ln -sf #{bin_directory}/nominatim-daily-maintenance #{bin_directory}/maintenance/"
283 systemd_timer "nominatim-update-maintenance-trigger" do
285 description "Schedule daily maintenance tasks for Nominatim DB"
286 on_calendar "*-*-* 02:03:00 UTC"
289 service "nominatim-update-maintenance-trigger" do
297 repository node[:nominatim][:ui_repository]
298 revision node[:nominatim][:ui_revision]
303 template "#{ui_directory}/dist/theme/config.theme.js" do
304 source "ui-config.js.erb"
312 if node[:nominatim][:enable_qa_tiles]
313 python_package "nominatim-data-analyser" do
314 python_virtualenv python_directory
315 extra_index_url node[:nominatim][:pip_index]
318 directory qa_data_directory do
325 template "#{project_directory}/qa-config.yaml" do
326 source "qa_config.erb"
330 variables :outputdir => "#{qa_data_directory}/new"
333 ssl_certificate "qa-tile.nominatim.openstreetmap.org" do
334 domains ["qa-tile.nominatim.openstreetmap.org"]
335 notifies :reload, "service[nginx]"
338 nginx_site "qa-tiles.nominatim" do
339 template "nginx-qa-tiles.erb"
340 directory qa_data_directory
341 variables :qa_data_directory => qa_data_directory
345 ## Logging and monitoring
347 template "/etc/logrotate.d/nominatim" do
348 source "logrotate.nominatim.erb"
354 prometheus_exporter "nominatim" do
357 restrict_address_families "AF_UNIX"
359 "--nominatim.query-log=#{node[:nominatim][:logdir]}/query.log",
360 "--nominatim.database-name=#{node[:nominatim][:dbname]}"
364 frontend_addresses = frontends.collect { |f| f.ipaddresses(:role => :external) }
366 fail2ban_jail "nominatim_limit_req" do
367 filter "nginx-limit-req"
368 logpath "#{node[:nominatim][:logdir]}/nominatim.openstreetmap.org-error.log"
371 ignoreips frontend_addresses.flatten.sort