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
100 tile_directories = node[:tile][:styles].collect do |_, style|
101 style[:tile_directories].collect { |directory| directory[:name] }
102 end.flatten.sort.uniq
106 systemd_service "renderd" do
108 after "postgresql.service"
109 wants "postgresql.service"
112 restrict_address_families "AF_UNIX"
113 read_write_paths tile_directories
117 systemd_service "renderd" do
122 action [:enable, :start]
123 subscribes :restart, "systemd_service[renderd]"
126 directory "/srv/tile.openstreetmap.org/tiles" do
132 template "/etc/renderd.conf" do
133 source "renderd.conf.erb"
137 notifies :reload, "service[apache2]"
138 notifies :restart, "service[renderd]"
141 remote_directory "/srv/tile.openstreetmap.org/html" do
151 template "/srv/tile.openstreetmap.org/html/index.html" do
152 source "index.html.erb"
165 python_package "pyotp" do
169 directory "/srv/tile.openstreetmap.org/cgi-bin" do
175 template "/srv/tile.openstreetmap.org/cgi-bin/export" do
180 variables :blocks => blocks, :totp_key => web_passwords["totp_key"]
183 template "/srv/tile.openstreetmap.org/cgi-bin/debug" do
190 systemd_service "export-cleanup" do
191 description "Cleanup stale export temporary files"
192 joins_namespace_of "apache2.service"
193 exec_start "find /tmp -ignore_readdir_race -name 'export??????' -mmin +60 -delete"
198 systemd_timer "export-cleanup" do
199 description "Cleanup stale export temporary files"
201 on_unit_inactive_sec "60m"
204 service "export-cleanup.timer" do
205 action [:enable, :start]
208 directory "/srv/tile.openstreetmap.org/data" do
220 node[:tile][:data].each_value do |data|
222 file = "/srv/tile.openstreetmap.org/data/#{File.basename(url)}"
225 directory = "/srv/tile.openstreetmap.org/data/#{data[:directory]}"
227 directory directory do
233 directory = "/srv/tile.openstreetmap.org/data"
239 command "tar -zxf #{file} -C #{directory}"
243 elsif file =~ /\.tar\.bz2$/
246 command "tar -jxf #{file} -C #{directory}"
250 elsif file =~ /\.zip$/
253 command "unzip -qq -o #{file} -d #{directory}"
259 execute "#{file}_shapeindex" do
261 command "find #{directory} -type f -iname '*.shp' -print0 | xargs -0 --no-run-if-empty shapeindex --shape_files"
264 subscribes :run, "execute[#{file}]", :immediately
270 use_conditional_get true
273 action :create_if_missing
281 notifies :run, "execute[#{file}]", :immediately
282 notifies :restart, "service[renderd]"
286 nodejs_package "carto"
288 systemd_service "update-lowzoom@" do
289 description "Low zoom tile update service for %i layer"
291 exec_start_pre "+/bin/systemctl stop render-lowzoom.service"
292 exec_start "/bin/bash /usr/local/bin/update-lowzoom-%i"
293 runtime_directory "update-lowzoom-%i"
295 restrict_address_families "AF_UNIX"
297 "/srv/tile.openstreetmap.org/tiles/%i",
303 directory "/srv/tile.openstreetmap.org/styles" do
309 node[:tile][:styles].each do |name, details|
310 style_directory = "/srv/tile.openstreetmap.org/styles/#{name}"
311 tile_directory = "/srv/tile.openstreetmap.org/tiles/#{name}"
313 template "/usr/local/bin/update-lowzoom-#{name}" do
314 source "update-lowzoom.erb"
318 variables :style => name
321 service "update-lowzoom@#{name}" do
323 supports :restart => true
326 directory tile_directory do
332 details[:tile_directories].each do |directory|
333 directory directory[:name] do
339 directory[:min_zoom].upto(directory[:max_zoom]) do |zoom|
340 directory "#{directory[:name]}/#{zoom}" do
346 link "#{tile_directory}/#{zoom}" do
347 to "#{directory[:name]}/#{zoom}"
354 file "#{tile_directory}/planet-import-complete" do
355 action :create_if_missing
361 git style_directory do
363 repository details[:repository]
364 revision details[:revision]
369 link "#{style_directory}/data" do
370 to "/srv/tile.openstreetmap.org/data"
375 if details[:fonts_script]
376 execute details[:fonts_script] do
378 command details[:fonts_script]
382 subscribes :run, "git[#{style_directory}]"
386 execute "#{style_directory}/project.mml" do
388 command "carto -a 3.0.22 project.mml > project.xml"
392 subscribes :run, "git[#{style_directory}]"
393 notifies :restart, "service[renderd]", :immediately
394 notifies :restart, "service[update-lowzoom@#{name}]"
398 postgresql_version = node[:tile][:database][:cluster].split("/").first
399 postgis_version = node[:tile][:database][:postgis]
401 package "postgresql-#{postgresql_version}-postgis-#{postgis_version}"
403 postgresql_user "jburgess" do
404 cluster node[:tile][:database][:cluster]
408 postgresql_user "tomh" do
409 cluster node[:tile][:database][:cluster]
413 postgresql_user "pnorman" do
414 cluster node[:tile][:database][:cluster]
418 postgresql_user "tile" do
419 cluster node[:tile][:database][:cluster]
422 postgresql_user "www-data" do
423 cluster node[:tile][:database][:cluster]
426 postgresql_user "_renderd" do
427 cluster node[:tile][:database][:cluster]
430 postgresql_database "gis" do
431 cluster node[:tile][:database][:cluster]
435 postgresql_extension "postgis" do
436 cluster node[:tile][:database][:cluster]
440 postgresql_extension "hstore" do
441 cluster node[:tile][:database][:cluster]
443 only_if { node[:tile][:database][:hstore] }
446 %w[geography_columns planet_osm_nodes planet_osm_rels planet_osm_ways raster_columns raster_overviews spatial_ref_sys].each do |table|
447 postgresql_table table do
448 cluster node[:tile][:database][:cluster]
451 permissions "tile" => :all
455 %w[geometry_columns planet_osm_line planet_osm_point planet_osm_polygon planet_osm_roads].each do |table|
456 postgresql_table table do
457 cluster node[:tile][:database][:cluster]
460 permissions "tile" => :all, "www-data" => :select, "_renderd" => :select
470 if node[:tile][:database][:external_data_script]
471 execute node[:tile][:database][:external_data_script] do
472 command "#{node[:tile][:database][:external_data_script]} -R _renderd"
473 cwd "/srv/tile.openstreetmap.org"
479 Array(node[:tile][:database][:external_data_tables]).each do |table|
480 postgresql_table table do
481 cluster node[:tile][:database][:cluster]
484 permissions "tile" => :all, "www-data" => :select, "_renderd" => :select
489 postgresql_munin "gis" do
490 cluster node[:tile][:database][:cluster]
494 directory File.dirname(node[:tile][:database][:node_file]) do
501 file node[:tile][:database][:node_file] do
507 directory "/var/log/tile" do
520 gem_package "apachelogregex" do
521 gem_binary node[:ruby][:gem]
524 gem_package "file-tail" do
525 gem_binary node[:ruby][:gem]
528 gem_package "lru_redux" do
529 gem_binary node[:ruby][:gem]
532 remote_directory "/usr/local/bin" do
542 template "/usr/local/bin/tile-ratelimit" do
543 source "tile-ratelimit.erb"
549 systemd_service "tile-ratelimit" do
550 description "Monitor tile requests and enforce rate limits"
551 after "apache2.service"
554 exec_start "/usr/local/bin/tile-ratelimit"
557 read_write_paths "/srv/tile.openstreetmap.org/conf"
561 service "tile-ratelimit" do
562 action [:enable, :start]
563 subscribes :restart, "file[/usr/local/bin/tile-ratelimit]"
564 subscribes :restart, "systemd_service[tile-ratelimit]"
567 template "/usr/local/bin/expire-tiles" do
568 source "expire-tiles.erb"
574 directory "/var/lib/replicate" do
580 directory "/var/lib/replicate/expire-queue" do
586 template "/usr/local/bin/replicate" do
587 source "replicate.erb"
591 variables :postgresql_version => postgresql_version.to_f
594 systemd_service "expire-tiles" do
595 description "Tile dirtying service"
598 exec_start "/usr/local/bin/expire-tiles"
600 standard_output "null"
602 read_write_paths tile_directories + [
603 "/store/database/nodes",
604 "/var/lib/replicate/expire-queue",
609 systemd_path "expire-tiles" do
610 description "Tile dirtying trigger"
611 directory_not_empty "/var/lib/replicate/expire-queue"
614 service "expire-tiles.path" do
615 action [:enable, :start]
616 subscribes :restart, "systemd_path[expire-tiles]"
619 systemd_service "replicate" do
620 description "Rendering database replication service"
621 after "postgresql.service"
622 wants "postgresql.service"
624 exec_start "/usr/local/bin/replicate"
625 sandbox :enable_network => true
626 restrict_address_families "AF_UNIX"
628 "/store/database/nodes",
629 "/var/lib/replicate",
635 service "replicate" do
636 action [:enable, :start]
637 subscribes :restart, "template[/usr/local/bin/replicate]"
638 subscribes :restart, "systemd_service[replicate]"
641 template "/etc/logrotate.d/replicate" do
642 source "replicate.logrotate.erb"
648 template "/usr/local/bin/render-lowzoom" do
649 source "render-lowzoom.erb"
655 systemd_service "render-lowzoom" do
656 description "Render low zoom tiles"
657 condition_path_exists_glob "!/run/update-lowzoom-*"
659 exec_start "/usr/local/bin/render-lowzoom"
661 restrict_address_families "AF_UNIX"
662 read_write_paths "/var/log/tile"
665 systemd_timer "render-lowzoom" do
666 description "Render low zoom tiles"
667 on_calendar "Fri *-*-* 23:00:00 UTC"
670 service "render-lowzoom.timer" do
671 action [:enable, :start]
674 package "liblockfile-simple-perl"
675 package "libfilesys-df-perl"
677 template "/usr/local/bin/cleanup-tiles" do
678 source "cleanup-tiles.erb"
684 systemd_service "cleanup-tiles@" do
685 description "Cleanup old tiles for /%I"
686 exec_start "/usr/local/bin/cleanup-tiles /%I"
688 io_scheduling_class "idle"
690 read_write_paths "/%I"
693 systemd_timer "cleanup-tiles@" do
694 description "Cleanup old tiles for /%I"
696 on_unit_inactive_sec "60m"
697 randomized_delay_sec "10m"
700 tile_directories.each do |directory|
701 label = directory[1..].gsub("/", "-")
703 service "cleanup-tiles@#{label}.timer" do
704 action [:enable, :start]
708 munin_plugin "mod_tile_fresh"
709 munin_plugin "mod_tile_latency"
710 munin_plugin "mod_tile_response"
711 munin_plugin "mod_tile_zoom"
713 munin_plugin "renderd_processed"
714 munin_plugin "renderd_queue"
715 munin_plugin "renderd_queue_time"
716 munin_plugin "renderd_zoom"
717 munin_plugin "renderd_zoom_time"
719 munin_plugin "replication_delay"
721 package "ruby-webrick"
723 prometheus_exporter "modtile" do
727 prometheus_exporter "renderd" do