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 file "/etc/cron.hourly/export" do
212 directory "/srv/tile.openstreetmap.org/data" do
224 node[:tile][:data].each_value do |data|
226 file = "/srv/tile.openstreetmap.org/data/#{File.basename(url)}"
229 directory = "/srv/tile.openstreetmap.org/data/#{data[:directory]}"
231 directory directory do
237 directory = "/srv/tile.openstreetmap.org/data"
243 command "tar -zxf #{file} -C #{directory}"
247 elsif file =~ /\.tar\.bz2$/
250 command "tar -jxf #{file} -C #{directory}"
254 elsif file =~ /\.zip$/
257 command "unzip -qq -o #{file} -d #{directory}"
263 execute "#{file}_shapeindex" do
265 command "find #{directory} -type f -iname '*.shp' -print0 | xargs -0 --no-run-if-empty shapeindex --shape_files"
268 subscribes :run, "execute[#{file}]", :immediately
274 use_conditional_get true
277 action :create_if_missing
285 notifies :run, "execute[#{file}]", :immediately
286 notifies :restart, "service[renderd]"
290 nodejs_package "carto"
292 systemd_service "update-lowzoom@" do
293 description "Low zoom tile update service for %i layer"
295 exec_start_pre "+/bin/systemctl stop render-lowzoom.service"
296 exec_start "/bin/bash /usr/local/bin/update-lowzoom-%i"
297 runtime_directory "update-lowzoom-%i"
299 restrict_address_families "AF_UNIX"
301 "/srv/tile.openstreetmap.org/tiles/%i",
307 directory "/srv/tile.openstreetmap.org/styles" do
313 node[:tile][:styles].each do |name, details|
314 style_directory = "/srv/tile.openstreetmap.org/styles/#{name}"
315 tile_directory = "/srv/tile.openstreetmap.org/tiles/#{name}"
317 template "/usr/local/bin/update-lowzoom-#{name}" do
318 source "update-lowzoom.erb"
322 variables :style => name
325 service "update-lowzoom@#{name}" do
327 supports :restart => true
330 directory tile_directory do
336 details[:tile_directories].each do |directory|
337 directory directory[:name] do
343 directory[:min_zoom].upto(directory[:max_zoom]) do |zoom|
344 directory "#{directory[:name]}/#{zoom}" do
350 link "#{tile_directory}/#{zoom}" do
351 to "#{directory[:name]}/#{zoom}"
358 file "#{tile_directory}/planet-import-complete" do
359 action :create_if_missing
365 git style_directory do
367 repository details[:repository]
368 revision details[:revision]
373 link "#{style_directory}/data" do
374 to "/srv/tile.openstreetmap.org/data"
379 if details[:fonts_script]
380 execute details[:fonts_script] do
382 command details[:fonts_script]
386 subscribes :run, "git[#{style_directory}]"
390 execute "#{style_directory}/project.mml" do
392 command "carto -a 3.0.22 project.mml > project.xml"
396 subscribes :run, "git[#{style_directory}]"
397 notifies :restart, "service[renderd]", :immediately
398 notifies :restart, "service[update-lowzoom@#{name}]"
402 postgresql_version = node[:tile][:database][:cluster].split("/").first
403 postgis_version = node[:tile][:database][:postgis]
405 package "postgresql-#{postgresql_version}-postgis-#{postgis_version}"
407 postgresql_user "jburgess" do
408 cluster node[:tile][:database][:cluster]
412 postgresql_user "tomh" do
413 cluster node[:tile][:database][:cluster]
417 postgresql_user "pnorman" do
418 cluster node[:tile][:database][:cluster]
422 postgresql_user "tile" do
423 cluster node[:tile][:database][:cluster]
426 postgresql_user "www-data" do
427 cluster node[:tile][:database][:cluster]
430 postgresql_user "_renderd" do
431 cluster node[:tile][:database][:cluster]
434 postgresql_database "gis" do
435 cluster node[:tile][:database][:cluster]
439 postgresql_extension "postgis" do
440 cluster node[:tile][:database][:cluster]
444 postgresql_extension "hstore" do
445 cluster node[:tile][:database][:cluster]
447 only_if { node[:tile][:database][:hstore] }
450 %w[geography_columns planet_osm_nodes planet_osm_rels planet_osm_ways raster_columns raster_overviews spatial_ref_sys].each do |table|
451 postgresql_table table do
452 cluster node[:tile][:database][:cluster]
455 permissions "tile" => :all
459 %w[geometry_columns planet_osm_line planet_osm_point planet_osm_polygon planet_osm_roads].each do |table|
460 postgresql_table table do
461 cluster node[:tile][:database][:cluster]
464 permissions "tile" => :all, "www-data" => :select, "_renderd" => :select
474 if node[:tile][:database][:external_data_script]
475 execute node[:tile][:database][:external_data_script] do
476 command "#{node[:tile][:database][:external_data_script]} -R _renderd"
477 cwd "/srv/tile.openstreetmap.org"
483 Array(node[:tile][:database][:external_data_tables]).each do |table|
484 postgresql_table table do
485 cluster node[:tile][:database][:cluster]
488 permissions "tile" => :all, "www-data" => :select, "_renderd" => :select
493 postgresql_munin "gis" do
494 cluster node[:tile][:database][:cluster]
498 directory File.dirname(node[:tile][:database][:node_file]) do
505 file node[:tile][:database][:node_file] do
511 directory "/var/log/tile" do
524 gem_package "apachelogregex" do
525 gem_binary node[:ruby][:gem]
528 gem_package "file-tail" do
529 gem_binary node[:ruby][:gem]
532 gem_package "lru_redux" do
533 gem_binary node[:ruby][:gem]
536 remote_directory "/usr/local/bin" do
546 template "/usr/local/bin/tile-ratelimit" do
547 source "tile-ratelimit.erb"
553 systemd_service "tile-ratelimit" do
554 description "Monitor tile requests and enforce rate limits"
555 after "apache2.service"
558 exec_start "/usr/local/bin/tile-ratelimit"
561 read_write_paths "/srv/tile.openstreetmap.org/conf"
565 service "tile-ratelimit" do
566 action [:enable, :start]
567 subscribes :restart, "file[/usr/local/bin/tile-ratelimit]"
568 subscribes :restart, "systemd_service[tile-ratelimit]"
571 template "/usr/local/bin/expire-tiles" do
572 source "expire-tiles.erb"
578 directory "/var/lib/replicate" do
584 directory "/var/lib/replicate/expire-queue" do
590 template "/usr/local/bin/replicate" do
591 source "replicate.erb"
595 variables :postgresql_version => postgresql_version.to_f
598 systemd_service "expire-tiles" do
599 description "Tile dirtying service"
602 exec_start "/usr/local/bin/expire-tiles"
604 standard_output "null"
606 read_write_paths tile_directories + [
607 "/store/database/nodes",
608 "/var/lib/replicate/expire-queue",
613 systemd_path "expire-tiles" do
614 description "Tile dirtying trigger"
615 directory_not_empty "/var/lib/replicate/expire-queue"
618 service "expire-tiles.path" do
619 action [:enable, :start]
620 subscribes :restart, "systemd_path[expire-tiles]"
623 systemd_service "replicate" do
624 description "Rendering database replication service"
625 after "postgresql.service"
626 wants "postgresql.service"
628 exec_start "/usr/local/bin/replicate"
629 sandbox :enable_network => true
630 restrict_address_families "AF_UNIX"
632 "/store/database/nodes",
633 "/var/lib/replicate",
639 service "replicate" do
640 action [:enable, :start]
641 subscribes :restart, "template[/usr/local/bin/replicate]"
642 subscribes :restart, "systemd_service[replicate]"
645 template "/etc/logrotate.d/replicate" do
646 source "replicate.logrotate.erb"
652 template "/usr/local/bin/render-lowzoom" do
653 source "render-lowzoom.erb"
659 systemd_service "render-lowzoom" do
660 description "Render low zoom tiles"
661 condition_path_exists_glob "!/run/update-lowzoom-*"
663 exec_start "/usr/local/bin/render-lowzoom"
665 restrict_address_families "AF_UNIX"
666 read_write_paths "/var/log/tile"
669 systemd_timer "render-lowzoom" do
670 description "Render low zoom tiles"
671 on_calendar "Fri *-*-* 23:00:00 UTC"
674 service "render-lowzoom.timer" do
675 action [:enable, :start]
678 package "liblockfile-simple-perl"
679 package "libfilesys-df-perl"
681 template "/usr/local/bin/cleanup-tiles" do
682 source "cleanup-tiles.erb"
688 systemd_service "cleanup-tiles@" do
689 description "Cleanup old tiles for /%I"
690 exec_start "/usr/local/bin/cleanup-tiles /%I"
692 io_scheduling_class "idle"
694 read_write_paths "/%I"
697 systemd_timer "cleanup-tiles@" do
698 description "Cleanup old tiles for /%I"
700 on_unit_inactive_sec "60m"
701 randomized_delay_sec "10m"
704 tile_directories.each do |directory|
705 label = directory[1..].gsub("/", "-")
707 service "cleanup-tiles@#{label}.timer" do
708 action [:enable, :start]
712 munin_plugin "mod_tile_fresh"
713 munin_plugin "mod_tile_latency"
714 munin_plugin "mod_tile_response"
715 munin_plugin "mod_tile_zoom"
717 munin_plugin "renderd_processed"
718 munin_plugin "renderd_queue"
719 munin_plugin "renderd_queue_time"
720 munin_plugin "renderd_zoom"
721 munin_plugin "renderd_zoom_time"
723 munin_plugin "replication_delay"
725 package "ruby-webrick"
727 prometheus_exporter "modtile" do
731 prometheus_exporter "renderd" do