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 template "/etc/cron.hourly/export" do
191 source "export.cron.erb"
197 directory "/srv/tile.openstreetmap.org/data" do
209 node[:tile][:data].each_value do |data|
211 file = "/srv/tile.openstreetmap.org/data/#{File.basename(url)}"
214 directory = "/srv/tile.openstreetmap.org/data/#{data[:directory]}"
216 directory directory do
222 directory = "/srv/tile.openstreetmap.org/data"
228 command "tar -zxf #{file} -C #{directory}"
232 elsif file =~ /\.tar\.bz2$/
235 command "tar -jxf #{file} -C #{directory}"
239 elsif file =~ /\.zip$/
242 command "unzip -qq -o #{file} -d #{directory}"
248 execute "#{file}_shapeindex" do
250 command "find #{directory} -type f -iname '*.shp' -print0 | xargs -0 --no-run-if-empty shapeindex --shape_files"
253 subscribes :run, "execute[#{file}]", :immediately
259 use_conditional_get true
262 action :create_if_missing
270 notifies :run, "execute[#{file}]", :immediately
271 notifies :restart, "service[renderd]"
275 nodejs_package "carto"
277 systemd_service "update-lowzoom@" do
278 description "Low zoom tile update service for %i layer"
280 exec_start_pre "+/bin/systemctl stop render-lowzoom.service"
281 exec_start "/bin/bash /usr/local/bin/update-lowzoom-%i"
282 runtime_directory "update-lowzoom-%i"
284 restrict_address_families "AF_UNIX"
286 "/srv/tile.openstreetmap.org/tiles/%i",
292 directory "/srv/tile.openstreetmap.org/styles" do
298 node[:tile][:styles].each do |name, details|
299 style_directory = "/srv/tile.openstreetmap.org/styles/#{name}"
300 tile_directory = "/srv/tile.openstreetmap.org/tiles/#{name}"
302 template "/usr/local/bin/update-lowzoom-#{name}" do
303 source "update-lowzoom.erb"
307 variables :style => name
310 service "update-lowzoom@#{name}" do
312 supports :restart => true
315 directory tile_directory do
321 details[:tile_directories].each do |directory|
322 directory directory[:name] do
328 directory[:min_zoom].upto(directory[:max_zoom]) do |zoom|
329 directory "#{directory[:name]}/#{zoom}" do
335 link "#{tile_directory}/#{zoom}" do
336 to "#{directory[:name]}/#{zoom}"
343 file "#{tile_directory}/planet-import-complete" do
344 action :create_if_missing
350 git style_directory do
352 repository details[:repository]
353 revision details[:revision]
358 link "#{style_directory}/data" do
359 to "/srv/tile.openstreetmap.org/data"
364 if details[:fonts_script]
365 execute details[:fonts_script] do
367 command details[:fonts_script]
371 subscribes :run, "git[#{style_directory}]"
375 execute "#{style_directory}/project.mml" do
377 command "carto -a 3.0.22 project.mml > project.xml"
381 subscribes :run, "git[#{style_directory}]"
382 notifies :restart, "service[renderd]", :immediately
383 notifies :restart, "service[update-lowzoom@#{name}]"
387 postgresql_version = node[:tile][:database][:cluster].split("/").first
388 postgis_version = node[:tile][:database][:postgis]
390 package "postgresql-#{postgresql_version}-postgis-#{postgis_version}"
392 postgresql_user "jburgess" do
393 cluster node[:tile][:database][:cluster]
397 postgresql_user "tomh" do
398 cluster node[:tile][:database][:cluster]
402 postgresql_user "pnorman" do
403 cluster node[:tile][:database][:cluster]
407 postgresql_user "tile" do
408 cluster node[:tile][:database][:cluster]
411 postgresql_user "www-data" do
412 cluster node[:tile][:database][:cluster]
415 postgresql_user "_renderd" do
416 cluster node[:tile][:database][:cluster]
419 postgresql_database "gis" do
420 cluster node[:tile][:database][:cluster]
424 postgresql_extension "postgis" do
425 cluster node[:tile][:database][:cluster]
429 postgresql_extension "hstore" do
430 cluster node[:tile][:database][:cluster]
432 only_if { node[:tile][:database][:hstore] }
435 %w[geography_columns planet_osm_nodes planet_osm_rels planet_osm_ways raster_columns raster_overviews spatial_ref_sys].each do |table|
436 postgresql_table table do
437 cluster node[:tile][:database][:cluster]
440 permissions "tile" => :all
444 %w[geometry_columns planet_osm_line planet_osm_point planet_osm_polygon planet_osm_roads].each do |table|
445 postgresql_table table do
446 cluster node[:tile][:database][:cluster]
449 permissions "tile" => :all, "www-data" => :select, "_renderd" => :select
459 if node[:tile][:database][:external_data_script]
460 execute node[:tile][:database][:external_data_script] do
461 command "#{node[:tile][:database][:external_data_script]} -R _renderd"
462 cwd "/srv/tile.openstreetmap.org"
468 Array(node[:tile][:database][:external_data_tables]).each do |table|
469 postgresql_table table do
470 cluster node[:tile][:database][:cluster]
473 permissions "tile" => :all, "www-data" => :select, "_renderd" => :select
478 postgresql_munin "gis" do
479 cluster node[:tile][:database][:cluster]
483 directory File.dirname(node[:tile][:database][:node_file]) do
490 file node[:tile][:database][:node_file] do
496 directory "/var/log/tile" do
509 gem_package "apachelogregex" do
510 gem_binary node[:ruby][:gem]
513 gem_package "file-tail" do
514 gem_binary node[:ruby][:gem]
517 gem_package "lru_redux" do
518 gem_binary node[:ruby][:gem]
521 remote_directory "/usr/local/bin" do
531 template "/usr/local/bin/tile-ratelimit" do
532 source "tile-ratelimit.erb"
538 systemd_service "tile-ratelimit" do
539 description "Monitor tile requests and enforce rate limits"
540 after "apache2.service"
543 exec_start "/usr/local/bin/tile-ratelimit"
546 read_write_paths "/srv/tile.openstreetmap.org/conf"
550 service "tile-ratelimit" do
551 action [:enable, :start]
552 subscribes :restart, "file[/usr/local/bin/tile-ratelimit]"
553 subscribes :restart, "systemd_service[tile-ratelimit]"
556 template "/usr/local/bin/expire-tiles" do
557 source "expire-tiles.erb"
563 directory "/var/lib/replicate" do
569 directory "/var/lib/replicate/expire-queue" do
575 template "/usr/local/bin/replicate" do
576 source "replicate.erb"
580 variables :postgresql_version => postgresql_version.to_f
583 systemd_service "expire-tiles" do
584 description "Tile dirtying service"
587 exec_start "/usr/local/bin/expire-tiles"
589 standard_output "null"
591 read_write_paths tile_directories + [
592 "/store/database/nodes",
593 "/var/lib/replicate/expire-queue",
598 systemd_path "expire-tiles" do
599 description "Tile dirtying trigger"
600 directory_not_empty "/var/lib/replicate/expire-queue"
603 service "expire-tiles.path" do
604 action [:enable, :start]
605 subscribes :restart, "systemd_path[expire-tiles]"
608 systemd_service "replicate" do
609 description "Rendering database replication service"
610 after "postgresql.service"
611 wants "postgresql.service"
613 exec_start "/usr/local/bin/replicate"
614 sandbox :enable_network => true
615 restrict_address_families "AF_UNIX"
617 "/store/database/nodes",
618 "/var/lib/replicate",
624 service "replicate" do
625 action [:enable, :start]
626 subscribes :restart, "template[/usr/local/bin/replicate]"
627 subscribes :restart, "systemd_service[replicate]"
630 template "/etc/logrotate.d/replicate" do
631 source "replicate.logrotate.erb"
637 template "/usr/local/bin/render-lowzoom" do
638 source "render-lowzoom.erb"
644 systemd_service "render-lowzoom" do
645 description "Render low zoom tiles"
646 condition_path_exists_glob "!/run/update-lowzoom-*"
648 exec_start "/usr/local/bin/render-lowzoom"
650 restrict_address_families "AF_UNIX"
651 read_write_paths "/var/log/tile"
654 systemd_timer "render-lowzoom" do
655 description "Render low zoom tiles"
656 on_calendar "Fri *-*-* 23:00:00 UTC"
659 service "render-lowzoom.timer" do
660 action [:enable, :start]
663 package "liblockfile-simple-perl"
664 package "libfilesys-df-perl"
666 template "/usr/local/bin/cleanup-tiles" do
667 source "cleanup-tiles.erb"
673 systemd_service "cleanup-tiles@" do
674 description "Cleanup old tiles for /%I"
675 exec_start "/usr/local/bin/cleanup-tiles /%I"
677 io_scheduling_class "idle"
679 read_write_paths "/%I"
682 systemd_timer "cleanup-tiles@" do
683 description "Cleanup old tiles for /%I"
685 on_unit_inactive_sec "60m"
686 randomized_delay_sec "10m"
689 tile_directories.each do |directory|
690 label = directory[1..].gsub("/", "-")
692 service "cleanup-tiles@#{label}.timer" do
693 action [:enable, :start]
697 munin_plugin "mod_tile_fresh"
698 munin_plugin "mod_tile_latency"
699 munin_plugin "mod_tile_response"
700 munin_plugin "mod_tile_zoom"
702 munin_plugin "renderd_processed"
703 munin_plugin "renderd_queue"
704 munin_plugin "renderd_queue_time"
705 munin_plugin "renderd_zoom"
706 munin_plugin "renderd_zoom_time"
708 munin_plugin "replication_delay"
710 package "ruby-webrick"
712 prometheus_exporter "modtile" do
716 prometheus_exporter "renderd" do