5 # Copyright:: 2013, 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 "apache"
23 include_recipe "munin"
24 include_recipe "nodejs"
25 include_recipe "postgresql"
26 include_recipe "prometheus"
27 include_recipe "python"
29 include_recipe "tools"
31 blocks = data_bag_item("tile", "blocks")
32 web_passwords = data_bag_item("web", "passwords")
36 apache_module "expires"
37 apache_module "headers"
38 apache_module "remoteip"
39 apache_module "rewrite"
41 apache_module "tile" do
45 apache_conf "renderd" do
49 ssl_certificate node[:fqdn] do
50 domains [node[:fqdn], "tile.openstreetmap.org", "render.openstreetmap.org"]
51 notifies :reload, "service[apache2]"
54 remote_file "#{Chef::Config[:file_cache_path]}/fastly-ip-list.json" do
55 source "https://api.fastly.com/public-ip-list"
60 fastlyips = JSON.parse(IO.read("#{Chef::Config[:file_cache_path]}/fastly-ip-list.json"))
62 apache_site "default" do
66 apache_site "tileserver_site" do
70 apache_site "tile.openstreetmap.org" do
72 variables :fastly => fastlyips["addresses"]
75 template "/etc/logrotate.d/apache2" do
76 source "logrotate.apache.erb"
82 directory "/srv/tile.openstreetmap.org" do
88 directory "/srv/tile.openstreetmap.org/conf" do
94 file "/srv/tile.openstreetmap.org/conf/ip.map" do
102 systemd_service "renderd" do
104 after "postgresql.service"
105 wants "postgresql.service"
108 restrict_address_families "AF_UNIX"
112 systemd_service "renderd" do
117 action [:enable, :start]
118 subscribes :restart, "systemd_service[renderd]"
121 directory "/srv/tile.openstreetmap.org/tiles" do
127 template "/etc/renderd.conf" do
128 source "renderd.conf.erb"
132 notifies :reload, "service[apache2]"
133 notifies :restart, "service[renderd]"
136 remote_directory "/srv/tile.openstreetmap.org/html" do
146 template "/srv/tile.openstreetmap.org/html/index.html" do
147 source "index.html.erb"
160 python_package "pyotp" do
164 directory "/srv/tile.openstreetmap.org/cgi-bin" do
170 template "/srv/tile.openstreetmap.org/cgi-bin/export" do
175 variables :blocks => blocks, :totp_key => web_passwords["totp_key"]
178 template "/srv/tile.openstreetmap.org/cgi-bin/debug" do
185 template "/etc/cron.hourly/export" do
186 source "export.cron.erb"
192 directory "/srv/tile.openstreetmap.org/data" do
204 node[:tile][:data].each_value do |data|
206 file = "/srv/tile.openstreetmap.org/data/#{File.basename(url)}"
209 directory = "/srv/tile.openstreetmap.org/data/#{data[:directory]}"
211 directory directory do
217 directory = "/srv/tile.openstreetmap.org/data"
223 command "tar -zxf #{file} -C #{directory}"
227 elsif file =~ /\.tar\.bz2$/
230 command "tar -jxf #{file} -C #{directory}"
234 elsif file =~ /\.zip$/
237 command "unzip -qq -o #{file} -d #{directory}"
243 execute "#{file}_shapeindex" do
245 command "find #{directory} -type f -iname '*.shp' -print0 | xargs -0 --no-run-if-empty shapeindex --shape_files"
248 subscribes :run, "execute[#{file}]", :immediately
254 use_conditional_get true
257 action :create_if_missing
265 notifies :run, "execute[#{file}]", :immediately
266 notifies :restart, "service[renderd]"
270 nodejs_package "carto"
272 systemd_service "update-lowzoom@" do
273 description "Low zoom tile update service for %i layer"
275 exec_start_pre "+/bin/systemctl stop render-lowzoom.service"
276 exec_start "/bin/bash /usr/local/bin/update-lowzoom-%i"
277 runtime_directory "update-lowzoom-%i"
279 restrict_address_families "AF_UNIX"
281 "/srv/tile.openstreetmap.org/tiles/%i",
287 directory "/srv/tile.openstreetmap.org/styles" do
293 node[:tile][:styles].each do |name, details|
294 style_directory = "/srv/tile.openstreetmap.org/styles/#{name}"
295 tile_directory = "/srv/tile.openstreetmap.org/tiles/#{name}"
297 template "/usr/local/bin/update-lowzoom-#{name}" do
298 source "update-lowzoom.erb"
302 variables :style => name
305 service "update-lowzoom@#{name}" do
307 supports :restart => true
310 directory tile_directory do
316 details[:tile_directories].each do |directory|
317 directory directory[:name] do
323 directory[:min_zoom].upto(directory[:max_zoom]) do |zoom|
324 directory "#{directory[:name]}/#{zoom}" do
330 link "#{tile_directory}/#{zoom}" do
331 to "#{directory[:name]}/#{zoom}"
338 file "#{tile_directory}/planet-import-complete" do
339 action :create_if_missing
345 git style_directory do
347 repository details[:repository]
348 revision details[:revision]
353 link "#{style_directory}/data" do
354 to "/srv/tile.openstreetmap.org/data"
359 if details[:fonts_script]
360 execute details[:fonts_script] do
362 command details[:fonts_script]
366 subscribes :run, "git[#{style_directory}]"
370 execute "#{style_directory}/project.mml" do
372 command "carto -a 3.0.22 project.mml > project.xml"
376 subscribes :run, "git[#{style_directory}]"
377 notifies :restart, "service[renderd]", :immediately
378 notifies :restart, "service[update-lowzoom@#{name}]"
382 postgresql_version = node[:tile][:database][:cluster].split("/").first
383 postgis_version = node[:tile][:database][:postgis]
385 package "postgresql-#{postgresql_version}-postgis-#{postgis_version}"
387 postgresql_user "jburgess" do
388 cluster node[:tile][:database][:cluster]
392 postgresql_user "tomh" do
393 cluster node[:tile][:database][:cluster]
397 postgresql_user "pnorman" do
398 cluster node[:tile][:database][:cluster]
402 postgresql_user "tile" do
403 cluster node[:tile][:database][:cluster]
406 postgresql_user "www-data" do
407 cluster node[:tile][:database][:cluster]
410 postgresql_user "_renderd" do
411 cluster node[:tile][:database][:cluster]
414 postgresql_database "gis" do
415 cluster node[:tile][:database][:cluster]
419 postgresql_extension "postgis" do
420 cluster node[:tile][:database][:cluster]
424 postgresql_extension "hstore" do
425 cluster node[:tile][:database][:cluster]
427 only_if { node[:tile][:database][:hstore] }
430 %w[geography_columns planet_osm_nodes planet_osm_rels planet_osm_ways raster_columns raster_overviews spatial_ref_sys].each do |table|
431 postgresql_table table do
432 cluster node[:tile][:database][:cluster]
435 permissions "tile" => :all
439 %w[geometry_columns planet_osm_line planet_osm_point planet_osm_polygon planet_osm_roads].each do |table|
440 postgresql_table table do
441 cluster node[:tile][:database][:cluster]
444 permissions "tile" => :all, "www-data" => :select, "_renderd" => :select
454 if node[:tile][:database][:external_data_script]
455 execute node[:tile][:database][:external_data_script] do
456 command "#{node[:tile][:database][:external_data_script]} -R _renderd"
457 cwd "/srv/tile.openstreetmap.org"
463 Array(node[:tile][:database][:external_data_tables]).each do |table|
464 postgresql_table table do
465 cluster node[:tile][:database][:cluster]
468 permissions "tile" => :all, "www-data" => :select, "_renderd" => :select
473 postgresql_munin "gis" do
474 cluster node[:tile][:database][:cluster]
478 directory File.dirname(node[:tile][:database][:node_file]) do
485 file node[:tile][:database][:node_file] do
491 directory "/var/log/tile" do
504 gem_package "apachelogregex" do
505 gem_binary node[:ruby][:gem]
508 gem_package "file-tail" do
509 gem_binary node[:ruby][:gem]
512 gem_package "lru_redux" do
513 gem_binary node[:ruby][:gem]
516 remote_directory "/usr/local/bin" do
526 template "/usr/local/bin/tile-ratelimit" do
527 source "tile-ratelimit.erb"
533 systemd_service "tile-ratelimit" do
534 description "Monitor tile requests and enforce rate limits"
535 after "apache2.service"
538 exec_start "/usr/local/bin/tile-ratelimit"
541 read_write_paths "/srv/tile.openstreetmap.org/conf"
545 service "tile-ratelimit" do
546 action [:enable, :start]
547 subscribes :restart, "file[/usr/local/bin/tile-ratelimit]"
548 subscribes :restart, "systemd_service[tile-ratelimit]"
551 template "/usr/local/bin/expire-tiles" do
552 source "expire-tiles.erb"
558 directory "/var/lib/replicate" do
564 directory "/var/lib/replicate/expire-queue" do
570 template "/usr/local/bin/replicate" do
571 source "replicate.erb"
575 variables :postgresql_version => postgresql_version.to_f
578 systemd_service "expire-tiles" do
579 description "Tile dirtying service"
582 exec_start "/usr/local/bin/expire-tiles"
584 standard_output "null"
587 "/store/database/nodes",
589 "/var/lib/replicate/expire-queue",
594 systemd_path "expire-tiles" do
595 description "Tile dirtying trigger"
596 directory_not_empty "/var/lib/replicate/expire-queue"
599 service "expire-tiles.path" do
600 action [:enable, :start]
601 subscribes :restart, "systemd_path[expire-tiles]"
604 systemd_service "replicate" do
605 description "Rendering database replication service"
606 after "postgresql.service"
607 wants "postgresql.service"
609 exec_start "/usr/local/bin/replicate"
610 sandbox :enable_network => true
612 "/store/database/nodes",
613 "/var/lib/replicate",
619 service "replicate" do
620 action [:enable, :start]
621 subscribes :restart, "template[/usr/local/bin/replicate]"
622 subscribes :restart, "systemd_service[replicate]"
625 template "/etc/logrotate.d/replicate" do
626 source "replicate.logrotate.erb"
632 template "/usr/local/bin/render-lowzoom" do
633 source "render-lowzoom.erb"
639 systemd_service "render-lowzoom" do
640 description "Render low zoom tiles"
641 condition_path_exists_glob "!/run/update-lowzoom-*"
643 exec_start "/usr/local/bin/render-lowzoom"
645 restrict_address_families "AF_UNIX"
646 read_write_paths "/var/log/tile"
649 systemd_timer "render-lowzoom" do
650 description "Render low zoom tiles"
651 on_calendar "Fri *-*-* 23:00:00 UTC"
654 service "render-lowzoom.timer" do
655 action [:enable, :start]
658 package "liblockfile-simple-perl"
659 package "libfilesys-df-perl"
661 template "/usr/local/bin/cleanup-tiles" do
662 source "cleanup-tiles.erb"
668 tile_directories = node[:tile][:styles].collect do |_, style|
669 style[:tile_directories].collect { |directory| directory[:name] }
670 end.flatten.sort.uniq
672 tile_directories.each do |directory|
673 label = directory.gsub("/", "-")
675 cron_d "cleanup-tiles#{label}" do
678 command "ionice -c 3 /usr/local/bin/cleanup-tiles #{directory}"
679 mailto "admins@openstreetmap.org"
683 munin_plugin "mod_tile_fresh"
684 munin_plugin "mod_tile_latency"
685 munin_plugin "mod_tile_response"
686 munin_plugin "mod_tile_zoom"
688 munin_plugin "renderd_processed"
689 munin_plugin "renderd_queue"
690 munin_plugin "renderd_queue_time"
691 munin_plugin "renderd_zoom"
692 munin_plugin "renderd_zoom_time"
694 munin_plugin "replication_delay"
696 package "ruby-webrick"
698 prometheus_exporter "modtile" do
702 prometheus_exporter "renderd" do