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 admins = data_bag_item("apache", "admins")
33 web_passwords = data_bag_item("web", "passwords")
37 apache_module "expires"
38 apache_module "headers"
39 apache_module "remoteip"
40 apache_module "rewrite"
42 apache_module "tile" do
46 apache_conf "renderd" do
50 ssl_certificate node[:fqdn] do
51 domains [node[:fqdn], "tile.openstreetmap.org", "render.openstreetmap.org"]
52 notifies :reload, "service[apache2]"
55 remote_file "#{Chef::Config[:file_cache_path]}/fastly-ip-list.json" do
56 source "https://api.fastly.com/public-ip-list"
61 fastlyips = JSON.parse(IO.read("#{Chef::Config[:file_cache_path]}/fastly-ip-list.json"))
63 remote_file "#{Chef::Config[:file_cache_path]}/statuscake-locations.json" do
64 source "https://app.statuscake.com/Workfloor/Locations.php?format=json"
69 statuscakelocations = JSON.parse(IO.read("#{Chef::Config[:file_cache_path]}/statuscake-locations.json"))
71 apache_site "default" do
75 apache_site "tileserver_site" do
79 apache_site "tile.openstreetmap.org" do
81 variables :fastly => fastlyips["addresses"] + fastlyips["ipv6_addresses"],
82 :statuscake => statuscakelocations.flat_map { |_, v| [v["ip"], v["ipv6"]] },
83 :admins => admins["hosts"]
86 template "/etc/logrotate.d/apache2" do
87 source "logrotate.apache.erb"
93 directory "/srv/tile.openstreetmap.org" do
99 directory "/srv/tile.openstreetmap.org/conf" do
105 file "/srv/tile.openstreetmap.org/conf/ip.map" do
111 tile_directories = node[:tile][:styles].collect do |_, style|
112 style[:tile_directories].collect { |directory| directory[:name] }
113 end.flatten.sort.uniq
117 systemd_service "renderd" do
119 after "postgresql.service"
120 wants "postgresql.service"
123 restrict_address_families "AF_UNIX"
124 read_write_paths tile_directories
129 action [:enable, :start]
130 subscribes :restart, "systemd_service[renderd]"
133 directory "/srv/tile.openstreetmap.org/tiles" do
139 template "/etc/renderd.conf" do
140 source "renderd.conf.erb"
144 notifies :reload, "service[apache2]"
145 notifies :restart, "service[renderd]"
148 remote_directory "/srv/tile.openstreetmap.org/html" do
158 template "/srv/tile.openstreetmap.org/html/index.html" do
159 source "index.html.erb"
172 python_package "pyotp" do
176 directory "/srv/tile.openstreetmap.org/cgi-bin" do
182 template "/srv/tile.openstreetmap.org/cgi-bin/export" do
187 variables :blocks => blocks, :totp_key => web_passwords["totp_key"]
190 template "/srv/tile.openstreetmap.org/cgi-bin/debug" do
197 systemd_service "export-cleanup" do
198 description "Cleanup stale export temporary files"
199 joins_namespace_of "apache2.service"
200 exec_start "find /tmp -ignore_readdir_race -name 'export??????' -mmin +60 -delete"
205 systemd_timer "export-cleanup" do
206 description "Cleanup stale export temporary files"
208 on_unit_inactive_sec "60m"
211 service "export-cleanup.timer" do
212 action [:enable, :start]
215 directory "/srv/tile.openstreetmap.org/data" do
227 node[:tile][:data].each_value do |data|
229 file = "/srv/tile.openstreetmap.org/data/#{File.basename(url)}"
232 directory = "/srv/tile.openstreetmap.org/data/#{data[:directory]}"
234 directory directory do
240 directory = "/srv/tile.openstreetmap.org/data"
246 command "tar -zxf #{file} -C #{directory}"
250 elsif file =~ /\.tar\.bz2$/
253 command "tar -jxf #{file} -C #{directory}"
257 elsif file =~ /\.zip$/
260 command "unzip -qq -o #{file} -d #{directory}"
266 execute "#{file}_shapeindex" do
268 command "find #{directory} -type f -iname '*.shp' -print0 | xargs -0 --no-run-if-empty shapeindex --shape_files"
271 subscribes :run, "execute[#{file}]", :immediately
277 use_conditional_get true
280 action :create_if_missing
288 notifies :run, "execute[#{file}]", :immediately
289 notifies :restart, "service[renderd]"
293 nodejs_package "carto"
295 systemd_service "update-lowzoom@" do
296 description "Low zoom tile update service for %i layer"
298 exec_start_pre "+/bin/systemctl stop render-lowzoom.service"
299 exec_start "/bin/bash /usr/local/bin/update-lowzoom-%i"
300 runtime_directory "update-lowzoom-%i"
302 restrict_address_families "AF_UNIX"
304 "/srv/tile.openstreetmap.org/tiles/%i",
310 directory "/srv/tile.openstreetmap.org/styles" do
316 node[:tile][:styles].each do |name, details|
317 style_directory = "/srv/tile.openstreetmap.org/styles/#{name}"
318 tile_directory = "/srv/tile.openstreetmap.org/tiles/#{name}"
320 template "/usr/local/bin/update-lowzoom-#{name}" do
321 source "update-lowzoom.erb"
325 variables :style => name
328 service "update-lowzoom@#{name}" do
330 supports :restart => true
333 directory tile_directory do
339 details[:tile_directories].each do |directory|
340 directory directory[:name] do
346 directory[:min_zoom].upto(directory[:max_zoom]) do |zoom|
347 directory "#{directory[:name]}/#{zoom}" do
353 link "#{tile_directory}/#{zoom}" do
354 to "#{directory[:name]}/#{zoom}"
361 file "#{tile_directory}/planet-import-complete" do
362 action :create_if_missing
368 git style_directory do
370 repository details[:repository]
371 revision details[:revision]
376 link "#{style_directory}/data" do
377 to "/srv/tile.openstreetmap.org/data"
382 if details[:fonts_script]
383 execute details[:fonts_script] do
385 command details[:fonts_script]
389 subscribes :run, "git[#{style_directory}]"
393 execute "#{style_directory}/project.mml" do
395 command "carto -a 3.0.22 project.mml > project.xml"
399 subscribes :run, "git[#{style_directory}]"
400 notifies :restart, "service[renderd]", :immediately
401 notifies :restart, "service[update-lowzoom@#{name}]"
405 postgresql_version = node[:tile][:database][:cluster].split("/").first
406 postgis_version = node[:tile][:database][:postgis]
408 package "postgresql-#{postgresql_version}-postgis-#{postgis_version}"
410 postgresql_user "jburgess" do
411 cluster node[:tile][:database][:cluster]
415 postgresql_user "tomh" do
416 cluster node[:tile][:database][:cluster]
420 postgresql_user "pnorman" do
421 cluster node[:tile][:database][:cluster]
425 postgresql_user "tile" do
426 cluster node[:tile][:database][:cluster]
429 postgresql_user "www-data" do
430 cluster node[:tile][:database][:cluster]
433 postgresql_user "_renderd" do
434 cluster node[:tile][:database][:cluster]
437 postgresql_database "gis" do
438 cluster node[:tile][:database][:cluster]
442 postgresql_extension "postgis" do
443 cluster node[:tile][:database][:cluster]
447 postgresql_extension "hstore" do
448 cluster node[:tile][:database][:cluster]
450 only_if { node[:tile][:database][:hstore] }
453 %w[geography_columns planet_osm_nodes planet_osm_rels planet_osm_ways raster_columns raster_overviews].each do |table|
454 postgresql_table table do
455 cluster node[:tile][:database][:cluster]
458 permissions "tile" => :all
462 %w[geometry_columns planet_osm_line planet_osm_point planet_osm_polygon planet_osm_roads spatial_ref_sys].each do |table|
463 postgresql_table table do
464 cluster node[:tile][:database][:cluster]
467 permissions "tile" => :all, "www-data" => :select, "_renderd" => :select
477 if node[:tile][:database][:external_data_script]
478 execute node[:tile][:database][:external_data_script] do
479 command "#{node[:tile][:database][:external_data_script]} -R _renderd"
480 cwd "/srv/tile.openstreetmap.org"
486 Array(node[:tile][:database][:external_data_tables]).each do |table|
487 postgresql_table table do
488 cluster node[:tile][:database][:cluster]
491 permissions "tile" => :all, "www-data" => :select, "_renderd" => :select
496 postgresql_munin "gis" do
497 cluster node[:tile][:database][:cluster]
501 directory File.dirname(node[:tile][:database][:node_file]) do
508 file node[:tile][:database][:node_file] do
514 directory "/var/log/tile" do
527 gem_package "apachelogregex" do
528 gem_binary node[:ruby][:gem]
531 gem_package "file-tail" do
532 gem_binary node[:ruby][:gem]
535 gem_package "lru_redux" do
536 gem_binary node[:ruby][:gem]
539 remote_directory "/usr/local/bin" do
549 template "/usr/local/bin/tile-ratelimit" do
550 source "tile-ratelimit.erb"
556 systemd_service "tile-ratelimit" do
557 description "Monitor tile requests and enforce rate limits"
558 after "apache2.service"
561 exec_start "/usr/local/bin/tile-ratelimit"
564 read_write_paths "/srv/tile.openstreetmap.org/conf"
568 service "tile-ratelimit" do
569 action [:enable, :start]
570 subscribes :restart, "file[/usr/local/bin/tile-ratelimit]"
571 subscribes :restart, "systemd_service[tile-ratelimit]"
574 template "/usr/local/bin/expire-tiles" do
575 source "expire-tiles.erb"
581 directory "/var/lib/replicate" do
587 directory "/var/lib/replicate/expire-queue" do
593 template "/usr/local/bin/replicate" do
594 source "replicate.erb"
598 variables :postgresql_version => postgresql_version.to_f
601 systemd_service "expire-tiles" do
602 description "Tile dirtying service"
605 exec_start "/usr/local/bin/expire-tiles"
607 standard_output "null"
609 read_write_paths tile_directories + [
610 "/store/database/nodes",
611 "/var/lib/replicate/expire-queue",
616 systemd_path "expire-tiles" do
617 description "Tile dirtying trigger"
618 directory_not_empty "/var/lib/replicate/expire-queue"
621 service "expire-tiles.path" do
622 action [:enable, :start]
623 subscribes :restart, "systemd_path[expire-tiles]"
626 systemd_service "replicate" do
627 description "Rendering database replication service"
628 after "postgresql.service"
629 wants "postgresql.service"
631 exec_start "/usr/local/bin/replicate"
632 sandbox :enable_network => true
633 restrict_address_families "AF_UNIX"
635 "/store/database/nodes",
636 "/var/lib/replicate",
642 service "replicate" do
643 action [:enable, :start]
644 subscribes :restart, "template[/usr/local/bin/replicate]"
645 subscribes :restart, "systemd_service[replicate]"
648 # FIXME: cleanup old replicate logrotate
649 file "/etc/logrotate.d/replicate" do
653 template "/usr/local/bin/render-lowzoom" do
654 source "render-lowzoom.erb"
660 systemd_service "render-lowzoom" do
661 description "Render low zoom tiles"
662 condition_path_exists_glob "!/run/update-lowzoom-*"
664 exec_start "/usr/local/bin/render-lowzoom"
666 restrict_address_families "AF_UNIX"
667 read_write_paths "/var/log/tile"
670 systemd_timer "render-lowzoom" do
671 description "Render low zoom tiles"
672 on_calendar "Fri *-*-* 23:00:00 UTC"
675 service "render-lowzoom.timer" do
676 action [:enable, :start]
679 package "liblockfile-simple-perl"
680 package "libfilesys-df-perl"
682 template "/usr/local/bin/cleanup-tiles" do
683 source "cleanup-tiles.erb"
689 systemd_service "cleanup-tiles@" do
690 description "Cleanup old tiles for /%I"
691 exec_start "/usr/local/bin/cleanup-tiles /%I"
693 io_scheduling_class "idle"
695 read_write_paths "/%I"
698 systemd_timer "cleanup-tiles@" do
699 description "Cleanup old tiles for /%I"
701 on_unit_inactive_sec "60m"
702 randomized_delay_sec "10m"
705 tile_directories.each do |directory|
706 label = directory[1..].gsub("/", "-")
708 service "cleanup-tiles@#{label}.timer" do
709 action [:enable, :start]
713 munin_plugin "mod_tile_fresh"
714 munin_plugin "mod_tile_latency"
715 munin_plugin "mod_tile_response"
716 munin_plugin "mod_tile_zoom"
718 munin_plugin "renderd_processed"
719 munin_plugin "renderd_queue"
720 munin_plugin "renderd_queue_time"
721 munin_plugin "renderd_zoom"
722 munin_plugin "renderd_zoom_time"
724 munin_plugin "replication_delay"
726 package "ruby-webrick"
728 prometheus_exporter "modtile" do
732 prometheus_exporter "renderd" do