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
118 action [:enable, :start]
119 subscribes :restart, "systemd_service[renderd]"
122 directory "/srv/tile.openstreetmap.org/tiles" do
128 template "/etc/renderd.conf" do
129 source "renderd.conf.erb"
133 notifies :reload, "service[apache2]"
134 notifies :restart, "service[renderd]"
137 remote_directory "/srv/tile.openstreetmap.org/html" do
147 template "/srv/tile.openstreetmap.org/html/index.html" do
148 source "index.html.erb"
161 python_package "pyotp" do
165 directory "/srv/tile.openstreetmap.org/cgi-bin" do
171 template "/srv/tile.openstreetmap.org/cgi-bin/export" do
176 variables :blocks => blocks, :totp_key => web_passwords["totp_key"]
179 template "/srv/tile.openstreetmap.org/cgi-bin/debug" do
186 systemd_service "export-cleanup" do
187 description "Cleanup stale export temporary files"
188 joins_namespace_of "apache2.service"
189 exec_start "find /tmp -ignore_readdir_race -name 'export??????' -mmin +60 -delete"
194 systemd_timer "export-cleanup" do
195 description "Cleanup stale export temporary files"
197 on_unit_inactive_sec "60m"
200 service "export-cleanup.timer" do
201 action [:enable, :start]
204 directory "/srv/tile.openstreetmap.org/data" do
216 node[:tile][:data].each_value do |data|
218 file = "/srv/tile.openstreetmap.org/data/#{File.basename(url)}"
221 directory = "/srv/tile.openstreetmap.org/data/#{data[:directory]}"
223 directory directory do
229 directory = "/srv/tile.openstreetmap.org/data"
235 command "tar -zxf #{file} -C #{directory}"
239 elsif file =~ /\.tar\.bz2$/
242 command "tar -jxf #{file} -C #{directory}"
246 elsif file =~ /\.zip$/
249 command "unzip -qq -o #{file} -d #{directory}"
255 execute "#{file}_shapeindex" do
257 command "find #{directory} -type f -iname '*.shp' -print0 | xargs -0 --no-run-if-empty shapeindex --shape_files"
260 subscribes :run, "execute[#{file}]", :immediately
266 use_conditional_get true
269 action :create_if_missing
277 notifies :run, "execute[#{file}]", :immediately
278 notifies :restart, "service[renderd]"
282 nodejs_package "carto"
284 systemd_service "update-lowzoom@" do
285 description "Low zoom tile update service for %i layer"
287 exec_start_pre "+/bin/systemctl stop render-lowzoom.service"
288 exec_start "/bin/bash /usr/local/bin/update-lowzoom-%i"
289 runtime_directory "update-lowzoom-%i"
291 restrict_address_families "AF_UNIX"
293 "/srv/tile.openstreetmap.org/tiles/%i",
299 directory "/srv/tile.openstreetmap.org/styles" do
305 node[:tile][:styles].each do |name, details|
306 style_directory = "/srv/tile.openstreetmap.org/styles/#{name}"
307 tile_directory = "/srv/tile.openstreetmap.org/tiles/#{name}"
309 template "/usr/local/bin/update-lowzoom-#{name}" do
310 source "update-lowzoom.erb"
314 variables :style => name
317 service "update-lowzoom@#{name}" do
319 supports :restart => true
322 directory tile_directory do
328 details[:tile_directories].each do |directory|
329 directory directory[:name] do
335 directory[:min_zoom].upto(directory[:max_zoom]) do |zoom|
336 directory "#{directory[:name]}/#{zoom}" do
342 link "#{tile_directory}/#{zoom}" do
343 to "#{directory[:name]}/#{zoom}"
350 file "#{tile_directory}/planet-import-complete" do
351 action :create_if_missing
357 git style_directory do
359 repository details[:repository]
360 revision details[:revision]
365 link "#{style_directory}/data" do
366 to "/srv/tile.openstreetmap.org/data"
371 if details[:fonts_script]
372 execute details[:fonts_script] do
374 command details[:fonts_script]
378 subscribes :run, "git[#{style_directory}]"
382 execute "#{style_directory}/project.mml" do
384 command "carto -a 3.0.22 project.mml > project.xml"
388 subscribes :run, "git[#{style_directory}]"
389 notifies :restart, "service[renderd]", :immediately
390 notifies :restart, "service[update-lowzoom@#{name}]"
394 postgresql_version = node[:tile][:database][:cluster].split("/").first
395 postgis_version = node[:tile][:database][:postgis]
397 package "postgresql-#{postgresql_version}-postgis-#{postgis_version}"
399 postgresql_user "jburgess" do
400 cluster node[:tile][:database][:cluster]
404 postgresql_user "tomh" do
405 cluster node[:tile][:database][:cluster]
409 postgresql_user "pnorman" do
410 cluster node[:tile][:database][:cluster]
414 postgresql_user "tile" do
415 cluster node[:tile][:database][:cluster]
418 postgresql_user "www-data" do
419 cluster node[:tile][:database][:cluster]
422 postgresql_user "_renderd" do
423 cluster node[:tile][:database][:cluster]
426 postgresql_database "gis" do
427 cluster node[:tile][:database][:cluster]
431 postgresql_extension "postgis" do
432 cluster node[:tile][:database][:cluster]
436 postgresql_extension "hstore" do
437 cluster node[:tile][:database][:cluster]
439 only_if { node[:tile][:database][:hstore] }
442 %w[geography_columns planet_osm_nodes planet_osm_rels planet_osm_ways raster_columns raster_overviews spatial_ref_sys].each do |table|
443 postgresql_table table do
444 cluster node[:tile][:database][:cluster]
447 permissions "tile" => :all
451 %w[geometry_columns planet_osm_line planet_osm_point planet_osm_polygon planet_osm_roads].each do |table|
452 postgresql_table table do
453 cluster node[:tile][:database][:cluster]
456 permissions "tile" => :all, "www-data" => :select, "_renderd" => :select
466 if node[:tile][:database][:external_data_script]
467 execute node[:tile][:database][:external_data_script] do
468 command "#{node[:tile][:database][:external_data_script]} -R _renderd"
469 cwd "/srv/tile.openstreetmap.org"
475 Array(node[:tile][:database][:external_data_tables]).each do |table|
476 postgresql_table table do
477 cluster node[:tile][:database][:cluster]
480 permissions "tile" => :all, "www-data" => :select, "_renderd" => :select
485 postgresql_munin "gis" do
486 cluster node[:tile][:database][:cluster]
490 directory File.dirname(node[:tile][:database][:node_file]) do
497 file node[:tile][:database][:node_file] do
503 directory "/var/log/tile" do
516 gem_package "apachelogregex" do
517 gem_binary node[:ruby][:gem]
520 gem_package "file-tail" do
521 gem_binary node[:ruby][:gem]
524 gem_package "lru_redux" do
525 gem_binary node[:ruby][:gem]
528 remote_directory "/usr/local/bin" do
538 template "/usr/local/bin/tile-ratelimit" do
539 source "tile-ratelimit.erb"
545 systemd_service "tile-ratelimit" do
546 description "Monitor tile requests and enforce rate limits"
547 after "apache2.service"
550 exec_start "/usr/local/bin/tile-ratelimit"
553 read_write_paths "/srv/tile.openstreetmap.org/conf"
557 service "tile-ratelimit" do
558 action [:enable, :start]
559 subscribes :restart, "file[/usr/local/bin/tile-ratelimit]"
560 subscribes :restart, "systemd_service[tile-ratelimit]"
563 template "/usr/local/bin/expire-tiles" do
564 source "expire-tiles.erb"
570 directory "/var/lib/replicate" do
576 directory "/var/lib/replicate/expire-queue" do
582 template "/usr/local/bin/replicate" do
583 source "replicate.erb"
587 variables :postgresql_version => postgresql_version.to_f
590 systemd_service "expire-tiles" do
591 description "Tile dirtying service"
594 exec_start "/usr/local/bin/expire-tiles"
596 standard_output "null"
598 read_write_paths tile_directories + [
599 "/store/database/nodes",
600 "/var/lib/replicate/expire-queue",
605 systemd_path "expire-tiles" do
606 description "Tile dirtying trigger"
607 directory_not_empty "/var/lib/replicate/expire-queue"
610 service "expire-tiles.path" do
611 action [:enable, :start]
612 subscribes :restart, "systemd_path[expire-tiles]"
615 systemd_service "replicate" do
616 description "Rendering database replication service"
617 after "postgresql.service"
618 wants "postgresql.service"
620 exec_start "/usr/local/bin/replicate"
621 sandbox :enable_network => true
622 restrict_address_families "AF_UNIX"
624 "/store/database/nodes",
625 "/var/lib/replicate",
631 service "replicate" do
632 action [:enable, :start]
633 subscribes :restart, "template[/usr/local/bin/replicate]"
634 subscribes :restart, "systemd_service[replicate]"
637 template "/etc/logrotate.d/replicate" do
638 source "replicate.logrotate.erb"
644 template "/usr/local/bin/render-lowzoom" do
645 source "render-lowzoom.erb"
651 systemd_service "render-lowzoom" do
652 description "Render low zoom tiles"
653 condition_path_exists_glob "!/run/update-lowzoom-*"
655 exec_start "/usr/local/bin/render-lowzoom"
657 restrict_address_families "AF_UNIX"
658 read_write_paths "/var/log/tile"
661 systemd_timer "render-lowzoom" do
662 description "Render low zoom tiles"
663 on_calendar "Fri *-*-* 23:00:00 UTC"
666 service "render-lowzoom.timer" do
667 action [:enable, :start]
670 package "liblockfile-simple-perl"
671 package "libfilesys-df-perl"
673 template "/usr/local/bin/cleanup-tiles" do
674 source "cleanup-tiles.erb"
680 systemd_service "cleanup-tiles@" do
681 description "Cleanup old tiles for /%I"
682 exec_start "/usr/local/bin/cleanup-tiles /%I"
684 io_scheduling_class "idle"
686 read_write_paths "/%I"
689 systemd_timer "cleanup-tiles@" do
690 description "Cleanup old tiles for /%I"
692 on_unit_inactive_sec "60m"
693 randomized_delay_sec "10m"
696 tile_directories.each do |directory|
697 label = directory[1..].gsub("/", "-")
699 service "cleanup-tiles@#{label}.timer" do
700 action [:enable, :start]
704 munin_plugin "mod_tile_fresh"
705 munin_plugin "mod_tile_latency"
706 munin_plugin "mod_tile_response"
707 munin_plugin "mod_tile_zoom"
709 munin_plugin "renderd_processed"
710 munin_plugin "renderd_queue"
711 munin_plugin "renderd_queue_time"
712 munin_plugin "renderd_zoom"
713 munin_plugin "renderd_zoom_time"
715 munin_plugin "replication_delay"
717 package "ruby-webrick"
719 prometheus_exporter "modtile" do
723 prometheus_exporter "renderd" do