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_bin_directory = "#{basedir}/Nominatim-Data-Analyser"
34 qa_data_directory = "#{basedir}/qa-data"
43 [basedir, bin_directory, cfg_directory, project_directory, ui_directory].each do |path|
51 if node[:nominatim][:flatnode_file]
52 directory File.dirname(node[:nominatim][:flatnode_file]) do
57 directory "#{bin_directory}/maintenance" do
63 ## Log directory setup
65 directory node[:nominatim][:logdir] do
72 file "#{node[:nominatim][:logdir]}/query.log" do
73 action :create_if_missing
81 postgresql_version = node[:nominatim][:dbcluster].split("/").first
82 postgis_version = node[:nominatim][:postgis]
84 package "postgresql-#{postgresql_version}-postgis-#{postgis_version}"
86 node[:nominatim][:dbadmins].each do |user|
87 postgresql_user user do
88 cluster node[:nominatim][:dbcluster]
93 postgresql_user "nominatim" do
94 cluster node[:nominatim][:dbcluster]
98 postgresql_user "www-data" do
99 cluster node[:nominatim][:dbcluster]
104 python_directory = "#{basedir}/venv"
118 python_virtualenv python_directory do
119 interpreter "/usr/bin/python3"
122 # These are updated during the database update.
123 python_package "nominatim-db" do
124 python_virtualenv python_directory
125 extra_index_url node[:nominatim][:pip_index]
128 python_package "nominatim-api" do
129 python_virtualenv python_directory
130 extra_index_url node[:nominatim][:pip_index]
133 remote_directory "#{project_directory}/static-website" do
138 files_owner "nominatim"
139 files_group "nominatim"
144 template "#{project_directory}/.env" do
145 source "nominatim.env.erb"
149 variables :base_url => "nominatim.openstreetmap.org",
150 :dbname => node[:nominatim][:dbname],
151 :flatnode_file => node[:nominatim][:flatnode_file],
152 :log_file => "#{node[:nominatim][:logdir]}/query.log",
153 :pool_size => node[:nominatim][:api_pool_size],
154 :query_timeout => node[:nominatim][:api_query_timeout],
155 :request_timeout => node[:nominatim][:api_request_timeout]
158 remote_file "#{project_directory}/secondary_importance.sql.gz" do
159 action :create_if_missing
160 source "https://nominatim.org/data/wikimedia-secondary-importance.sql.gz"
166 remote_file "#{project_directory}/wikimedia-importance.csv.gz" do
167 action :create_if_missing
168 source "https://nominatim.org/data/wikimedia-importance.csv.gz"
174 %w[gb_postcodes.csv.gz us_postcodes.csv.gz].each do |fname|
175 remote_file "#{project_directory}/#{fname}" do
177 source "https://nominatim.org/data/#{fname}"
184 # Webserver + frontend
186 %w[user_agent referrer email generic].each do |name|
187 file "#{cfg_directory}/nginx_blocked_#{name}.conf" do
188 action :create_if_missing
195 systemd_service "nominatim" do
196 description "Nominatim running as a gunicorn application"
199 working_directory project_directory
200 standard_output "append:#{node[:nominatim][:logdir]}/gunicorn.log"
201 standard_error "inherit"
202 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()'"
203 exec_reload "/bin/kill -s HUP $MAINPID"
207 requires "nominatim.socket"
208 after "network.target"
211 systemd_socket "nominatim" do
212 description "Gunicorn socket for Nominatim"
213 listen_stream "/run/gunicorn-nominatim.openstreetmap.org.sock"
214 socket_user "www-data"
217 ssl_certificate node[:fqdn] do
218 domains [node[:fqdn],
219 "nominatim.openstreetmap.org",
221 "nominatim.openstreetmap.com",
222 "nominatim.openstreetmap.net",
223 "nominatim.openstreetmaps.org",
224 "nominatim.openmaps.org",
225 "nominatim.qgis.org"]
226 notifies :reload, "service[nginx]"
229 nginx_site "default" do
233 frontends = search(:node, "recipes:web\\:\\:frontend").sort_by(&:name)
235 nginx_site "nominatim" do
237 directory project_directory
238 variables :pools => node[:nominatim][:fpm_pools],
239 :frontends => frontends,
240 :confdir => "#{basedir}/etc",
241 :ui_directory => ui_directory
244 template "/etc/logrotate.d/nginx" do
245 source "logrotate.nginx.erb"
251 ### Import, update and maintenance scripts
254 nominatim-update-data
255 nominatim-update-refresh-db
256 nominatim-daily-maintenance].each do |fname|
257 template "#{bin_directory}/#{fname}" do
258 source "#{fname}.erb"
262 variables :bindir => bin_directory,
263 :projectdir => project_directory,
264 :venvprefix => "#{python_directory}/bin/",
265 :qadatadir => qa_data_directory
269 systemd_service "nominatim-update" do
270 description "Update the Nominatim database"
271 exec_start "#{bin_directory}/nominatim-update"
273 standard_output "journal"
274 standard_error "inherit"
275 working_directory project_directory
278 systemd_service "nominatim-update-maintenance-trigger" do
279 description "Trigger daily maintenance tasks for Nominatim DB"
280 exec_start "ln -sf #{bin_directory}/nominatim-daily-maintenance #{bin_directory}/maintenance/"
284 systemd_timer "nominatim-update-maintenance-trigger" do
286 description "Schedule daily maintenance tasks for Nominatim DB"
287 on_calendar "*-*-* 02:03:00 UTC"
290 service "nominatim-update-maintenance-trigger" do
298 repository node[:nominatim][:ui_repository]
299 revision node[:nominatim][:ui_revision]
304 template "#{ui_directory}/dist/theme/config.theme.js" do
305 source "ui-config.js.erb"
313 if node[:nominatim][:enable_qa_tiles]
314 python_package "nominatim-data-analyser" do
315 python_virtualenv python_directory
316 extra_index_url node[:nominatim][:pip_index]
319 directory qa_data_directory do
326 template "#{project_directory}/qa-config.yaml" do
327 source "qa_config.erb"
331 variables :outputdir => "#{qa_data_directory}/new"
334 ssl_certificate "qa-tile.nominatim.openstreetmap.org" do
335 domains ["qa-tile.nominatim.openstreetmap.org"]
336 notifies :reload, "service[nginx]"
339 nginx_site "qa-tiles.nominatim" do
340 template "nginx-qa-tiles.erb"
341 directory qa_data_directory
342 variables :qa_data_directory => qa_data_directory
346 ## Logging and monitoring
348 template "/etc/logrotate.d/nominatim" do
349 source "logrotate.nominatim.erb"
355 prometheus_exporter "nominatim" do
358 restrict_address_families "AF_UNIX"
360 "--nominatim.query-log=#{node[:nominatim][:logdir]}/query.log",
361 "--nominatim.database-name=#{node[:nominatim][:dbname]}"
365 frontend_addresses = frontends.collect { |f| f.ipaddresses(:role => :external) }
367 fail2ban_jail "nominatim_limit_req" do
368 filter "nginx-limit-req"
369 logpath "#{node[:nominatim][:logdir]}/nominatim.openstreetmap.org-error.log"
372 ignoreips frontend_addresses.flatten.sort