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 tile_directories = node[:tile][:styles].collect do |_, style|
100 style[:tile_directories].collect { |directory| directory[:name] }
101 end.flatten.sort.uniq
105 systemd_service "renderd" do
107 after "postgresql.service"
108 wants "postgresql.service"
111 restrict_address_families "AF_UNIX"
112 read_write_paths tile_directories
117 action [:enable, :start]
118 subscribes :restart, "systemd_service[renderd]"
121 directory "/srv/tile.openstreetmap.org/tiles" do
127 template "/etc/renderd.conf" do
128 source "renderd.conf.erb"
132 notifies :reload, "service[apache2]"
133 notifies :restart, "service[renderd]"
136 remote_directory "/srv/tile.openstreetmap.org/html" do
146 template "/srv/tile.openstreetmap.org/html/index.html" do
147 source "index.html.erb"
160 python_package "pyotp" do
164 directory "/srv/tile.openstreetmap.org/cgi-bin" do
170 template "/srv/tile.openstreetmap.org/cgi-bin/export" do
175 variables :blocks => blocks, :totp_key => web_passwords["totp_key"]
178 template "/srv/tile.openstreetmap.org/cgi-bin/debug" do
185 systemd_service "export-cleanup" do
186 description "Cleanup stale export temporary files"
187 joins_namespace_of "apache2.service"
188 exec_start "find /tmp -ignore_readdir_race -name 'export??????' -mmin +60 -delete"
193 systemd_timer "export-cleanup" do
194 description "Cleanup stale export temporary files"
196 on_unit_inactive_sec "60m"
199 service "export-cleanup.timer" do
200 action [:enable, :start]
203 directory "/srv/tile.openstreetmap.org/data" do
215 node[:tile][:data].each_value do |data|
217 file = "/srv/tile.openstreetmap.org/data/#{File.basename(url)}"
220 directory = "/srv/tile.openstreetmap.org/data/#{data[:directory]}"
222 directory directory do
228 directory = "/srv/tile.openstreetmap.org/data"
234 command "tar -zxf #{file} -C #{directory}"
238 elsif file =~ /\.tar\.bz2$/
241 command "tar -jxf #{file} -C #{directory}"
245 elsif file =~ /\.zip$/
248 command "unzip -qq -o #{file} -d #{directory}"
254 execute "#{file}_shapeindex" do
256 command "find #{directory} -type f -iname '*.shp' -print0 | xargs -0 --no-run-if-empty shapeindex --shape_files"
259 subscribes :run, "execute[#{file}]", :immediately
265 use_conditional_get true
268 action :create_if_missing
276 notifies :run, "execute[#{file}]", :immediately
277 notifies :restart, "service[renderd]"
281 nodejs_package "carto"
283 systemd_service "update-lowzoom@" do
284 description "Low zoom tile update service for %i layer"
286 exec_start_pre "+/bin/systemctl stop render-lowzoom.service"
287 exec_start "/bin/bash /usr/local/bin/update-lowzoom-%i"
288 runtime_directory "update-lowzoom-%i"
290 restrict_address_families "AF_UNIX"
292 "/srv/tile.openstreetmap.org/tiles/%i",
298 directory "/srv/tile.openstreetmap.org/styles" do
304 node[:tile][:styles].each do |name, details|
305 style_directory = "/srv/tile.openstreetmap.org/styles/#{name}"
306 tile_directory = "/srv/tile.openstreetmap.org/tiles/#{name}"
308 template "/usr/local/bin/update-lowzoom-#{name}" do
309 source "update-lowzoom.erb"
313 variables :style => name
316 service "update-lowzoom@#{name}" do
318 supports :restart => true
321 directory tile_directory do
327 details[:tile_directories].each do |directory|
328 directory directory[:name] do
334 directory[:min_zoom].upto(directory[:max_zoom]) do |zoom|
335 directory "#{directory[:name]}/#{zoom}" do
341 link "#{tile_directory}/#{zoom}" do
342 to "#{directory[:name]}/#{zoom}"
349 file "#{tile_directory}/planet-import-complete" do
350 action :create_if_missing
356 git style_directory do
358 repository details[:repository]
359 revision details[:revision]
364 link "#{style_directory}/data" do
365 to "/srv/tile.openstreetmap.org/data"
370 if details[:fonts_script]
371 execute details[:fonts_script] do
373 command details[:fonts_script]
377 subscribes :run, "git[#{style_directory}]"
381 execute "#{style_directory}/project.mml" do
383 command "carto -a 3.0.22 project.mml > project.xml"
387 subscribes :run, "git[#{style_directory}]"
388 notifies :restart, "service[renderd]", :immediately
389 notifies :restart, "service[update-lowzoom@#{name}]"
393 postgresql_version = node[:tile][:database][:cluster].split("/").first
394 postgis_version = node[:tile][:database][:postgis]
396 package "postgresql-#{postgresql_version}-postgis-#{postgis_version}"
398 postgresql_user "jburgess" do
399 cluster node[:tile][:database][:cluster]
403 postgresql_user "tomh" do
404 cluster node[:tile][:database][:cluster]
408 postgresql_user "pnorman" do
409 cluster node[:tile][:database][:cluster]
413 postgresql_user "tile" do
414 cluster node[:tile][:database][:cluster]
417 postgresql_user "www-data" do
418 cluster node[:tile][:database][:cluster]
421 postgresql_user "_renderd" do
422 cluster node[:tile][:database][:cluster]
425 postgresql_database "gis" do
426 cluster node[:tile][:database][:cluster]
430 postgresql_extension "postgis" do
431 cluster node[:tile][:database][:cluster]
435 postgresql_extension "hstore" do
436 cluster node[:tile][:database][:cluster]
438 only_if { node[:tile][:database][:hstore] }
441 %w[geography_columns planet_osm_nodes planet_osm_rels planet_osm_ways raster_columns raster_overviews].each do |table|
442 postgresql_table table do
443 cluster node[:tile][:database][:cluster]
446 permissions "tile" => :all
450 %w[geometry_columns planet_osm_line planet_osm_point planet_osm_polygon planet_osm_roads spatial_ref_sys].each do |table|
451 postgresql_table table do
452 cluster node[:tile][:database][:cluster]
455 permissions "tile" => :all, "www-data" => :select, "_renderd" => :select
465 if node[:tile][:database][:external_data_script]
466 execute node[:tile][:database][:external_data_script] do
467 command "#{node[:tile][:database][:external_data_script]} -R _renderd"
468 cwd "/srv/tile.openstreetmap.org"
474 Array(node[:tile][:database][:external_data_tables]).each do |table|
475 postgresql_table table do
476 cluster node[:tile][:database][:cluster]
479 permissions "tile" => :all, "www-data" => :select, "_renderd" => :select
484 postgresql_munin "gis" do
485 cluster node[:tile][:database][:cluster]
489 directory File.dirname(node[:tile][:database][:node_file]) do
496 file node[:tile][:database][:node_file] do
502 directory "/var/log/tile" do
515 remote_directory "/usr/local/bin" do
525 template "/usr/local/bin/expire-tiles" do
526 source "expire-tiles.erb"
532 directory "/var/lib/replicate" do
538 directory "/var/lib/replicate/expire-queue" do
544 template "/usr/local/bin/replicate" do
545 source "replicate.erb"
549 variables :postgresql_version => postgresql_version.to_f
552 systemd_service "expire-tiles" do
553 description "Tile dirtying service"
556 exec_start "/usr/local/bin/expire-tiles"
559 read_write_paths tile_directories + [
560 "/store/database/nodes",
561 "/var/lib/replicate/expire-queue",
566 systemd_path "expire-tiles" do
567 description "Tile dirtying trigger"
568 directory_not_empty "/var/lib/replicate/expire-queue"
571 service "expire-tiles.path" do
572 action [:enable, :start]
573 subscribes :restart, "systemd_path[expire-tiles]"
576 systemd_service "replicate" do
577 description "Rendering database replication service"
578 after "postgresql.service"
579 wants "postgresql.service"
581 exec_start "/usr/local/bin/replicate"
582 sandbox :enable_network => true
583 restrict_address_families "AF_UNIX"
585 "/store/database/nodes",
586 "/var/lib/replicate",
592 service "replicate" do
593 action [:enable, :start]
594 subscribes :restart, "template[/usr/local/bin/replicate]"
595 subscribes :restart, "systemd_service[replicate]"
598 template "/usr/local/bin/render-lowzoom" do
599 source "render-lowzoom.erb"
605 systemd_service "render-lowzoom" do
606 description "Render low zoom tiles"
607 condition_path_exists_glob "!/run/update-lowzoom-*"
609 exec_start "/usr/local/bin/render-lowzoom"
611 restrict_address_families "AF_UNIX"
612 read_write_paths "/var/log/tile"
615 systemd_timer "render-lowzoom" do
616 description "Render low zoom tiles"
617 on_calendar "Fri *-*-* 23:00:00 UTC"
620 service "render-lowzoom.timer" do
621 action [:enable, :start]
624 package "liblockfile-simple-perl"
625 package "libfilesys-df-perl"
627 template "/usr/local/bin/cleanup-tiles" do
628 source "cleanup-tiles.erb"
634 systemd_service "cleanup-tiles@" do
635 description "Cleanup old tiles for /%I"
636 exec_start "/usr/local/bin/cleanup-tiles /%I"
638 io_scheduling_class "idle"
640 read_write_paths "/%I"
643 systemd_timer "cleanup-tiles@" do
644 description "Cleanup old tiles for /%I"
646 on_unit_inactive_sec "60m"
647 randomized_delay_sec "10m"
650 tile_directories.each do |directory|
651 label = directory[1..].gsub("/", "-")
653 service "cleanup-tiles@#{label}.timer" do
654 action [:enable, :start]
658 munin_plugin "mod_tile_fresh"
659 munin_plugin "mod_tile_latency"
660 munin_plugin "mod_tile_response"
661 munin_plugin "mod_tile_zoom"
663 munin_plugin "renderd_processed"
664 munin_plugin "renderd_queue"
665 munin_plugin "renderd_queue_time"
666 munin_plugin "renderd_zoom"
667 munin_plugin "renderd_zoom_time"
669 munin_plugin "replication_delay"
671 package "ruby-webrick"
673 prometheus_exporter "modtile" do
677 prometheus_exporter "renderd" do