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 "nodejs"
24 include_recipe "postgresql"
25 include_recipe "prometheus"
26 include_recipe "python"
28 include_recipe "tools"
30 blocks = data_bag_item("tile", "blocks")
31 admins = data_bag_item("apache", "admins")
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 remote_file "#{Chef::Config[:file_cache_path]}/statuscake-locations.json" do
63 source "https://app.statuscake.com/Workfloor/Locations.php?format=json"
68 statuscakelocations = JSON.parse(IO.read("#{Chef::Config[:file_cache_path]}/statuscake-locations.json"))
70 apache_site "default" do
74 apache_site "tileserver_site" do
78 apache_site "tile.openstreetmap.org" do
80 variables :fastly => fastlyips["addresses"] + fastlyips["ipv6_addresses"],
81 :statuscake => statuscakelocations.flat_map { |_, v| [v["ip"], v["ipv6"]] },
82 :admins => admins["hosts"]
85 template "/etc/logrotate.d/apache2" do
86 source "logrotate.apache.erb"
92 directory "/srv/tile.openstreetmap.org" do
98 tile_directories = node[:tile][:styles].collect do |_, style|
99 style[:tile_directories].collect { |directory| directory[:name] }
100 end.flatten.sort.uniq
107 systemd_service "renderd" do
109 after "postgresql.service"
110 wants "postgresql.service"
111 environment "LD_PRELOAD" => "libtcmalloc.so.4"
116 restrict_address_families "AF_UNIX"
117 read_write_paths tile_directories
118 system_call_filter ["@system-service", "mincore"]
123 action [:enable, :start]
124 subscribes :restart, "systemd_service[renderd]"
127 directory "/srv/tile.openstreetmap.org/tiles" do
133 template "/etc/renderd.conf" do
134 source "renderd.conf.erb"
138 notifies :reload, "service[apache2]"
139 notifies :restart, "service[renderd]"
142 remote_directory "/srv/tile.openstreetmap.org/html" do
152 template "/srv/tile.openstreetmap.org/html/index.html" do
153 source "index.html.erb"
166 python_package "pyotp" do
170 directory "/srv/tile.openstreetmap.org/cgi-bin" do
176 template "/srv/tile.openstreetmap.org/cgi-bin/export" do
181 variables :blocks => blocks, :totp_key => web_passwords["totp_key"]
184 template "/srv/tile.openstreetmap.org/cgi-bin/debug" do
191 systemd_service "export-cleanup" do
192 description "Cleanup stale export temporary files"
193 joins_namespace_of "apache2.service"
194 exec_start "find /tmp -ignore_readdir_race -name 'export??????' -mmin +60 -delete"
199 systemd_timer "export-cleanup" do
200 description "Cleanup stale export temporary files"
202 on_unit_inactive_sec "60m"
205 service "export-cleanup.timer" do
206 action [:enable, :start]
209 directory "/srv/tile.openstreetmap.org/data" do
221 node[:tile][:data].each_value do |data|
223 file = "/srv/tile.openstreetmap.org/data/#{File.basename(url)}"
226 directory = "/srv/tile.openstreetmap.org/data/#{data[:directory]}"
228 directory directory do
234 directory = "/srv/tile.openstreetmap.org/data"
240 command "tar -zxf #{file} -C #{directory}"
244 elsif file =~ /\.tar\.bz2$/
247 command "tar -jxf #{file} -C #{directory}"
251 elsif file =~ /\.zip$/
254 command "unzip -qq -o #{file} -d #{directory}"
260 execute "#{file}_shapeindex" do
262 command "find #{directory} -type f -iname '*.shp' -print0 | xargs -0 --no-run-if-empty shapeindex --shape_files"
265 subscribes :run, "execute[#{file}]", :immediately
271 use_conditional_get true
274 action :create_if_missing
282 notifies :run, "execute[#{file}]", :immediately
283 notifies :restart, "service[renderd]"
287 nodejs_package "carto"
289 lowzoom_threads = node.cpu_cores - 1
291 systemd_service "update-lowzoom@" do
292 description "Low zoom tile update service for %i layer"
294 exec_start_pre "+/bin/systemctl stop render-lowzoom.service"
295 exec_start "/bin/bash /usr/local/bin/update-lowzoom-%i"
296 runtime_directory "update-lowzoom-%i"
298 restrict_address_families "AF_UNIX"
300 "/srv/tile.openstreetmap.org/tiles/%i",
306 directory "/srv/tile.openstreetmap.org/styles" do
312 node[:tile][:styles].each do |name, details|
313 style_directory = "/srv/tile.openstreetmap.org/styles/#{name}"
314 tile_directory = "/srv/tile.openstreetmap.org/tiles/#{name}"
316 template "/usr/local/bin/update-lowzoom-#{name}" do
317 source "update-lowzoom.erb"
321 variables :style => name, :threads => lowzoom_threads
324 service "update-lowzoom@#{name}" do
326 supports :restart => true
329 directory tile_directory do
335 details[:tile_directories].each do |directory|
336 directory directory[:name] do
342 directory[:min_zoom].upto(directory[:max_zoom]) do |zoom|
343 directory "#{directory[:name]}/#{zoom}" do
349 link "#{tile_directory}/#{zoom}" do
350 to "#{directory[:name]}/#{zoom}"
357 file "#{tile_directory}/planet-import-complete" do
358 action :create_if_missing
364 git style_directory do
366 repository details[:repository]
367 revision details[:revision]
372 link "#{style_directory}/data" do
373 to "/srv/tile.openstreetmap.org/data"
378 if details[:fonts_script]
379 execute details[:fonts_script] do
381 command details[:fonts_script]
385 subscribes :run, "git[#{style_directory}]"
389 execute "#{style_directory}/project.mml" do
391 command "carto -a 3.0.22 project.mml > project.xml"
395 subscribes :run, "git[#{style_directory}]"
396 notifies :restart, "service[renderd]", :immediately
397 notifies :restart, "service[update-lowzoom@#{name}]"
401 postgresql_version = node[:tile][:database][:cluster].split("/").first
402 postgis_version = node[:tile][:database][:postgis]
404 package "postgresql-#{postgresql_version}-postgis-#{postgis_version}"
406 postgresql_user "jburgess" do
407 cluster node[:tile][:database][:cluster]
411 postgresql_user "tomh" do
412 cluster node[:tile][:database][:cluster]
416 postgresql_user "pnorman" do
417 cluster node[:tile][:database][:cluster]
421 postgresql_user "tile" do
422 cluster node[:tile][:database][:cluster]
425 postgresql_user "www-data" do
426 cluster node[:tile][:database][:cluster]
429 postgresql_user "_renderd" do
430 cluster node[:tile][:database][:cluster]
433 postgresql_database "gis" do
434 cluster node[:tile][:database][:cluster]
438 postgresql_extension "postgis" do
439 cluster node[:tile][:database][:cluster]
443 postgresql_extension "hstore" do
444 cluster node[:tile][:database][:cluster]
446 only_if { node[:tile][:database][:hstore] }
449 %w[geography_columns planet_osm_nodes planet_osm_rels planet_osm_ways raster_columns raster_overviews].each do |table|
450 postgresql_table table do
451 cluster node[:tile][:database][:cluster]
454 permissions "tile" => :all
458 %w[geometry_columns planet_osm_line planet_osm_point planet_osm_polygon planet_osm_roads spatial_ref_sys].each do |table|
459 postgresql_table table do
460 cluster node[:tile][:database][:cluster]
463 permissions "tile" => :all, "www-data" => :select, "_renderd" => :select
473 if node[:tile][:database][:external_data_script]
474 execute node[:tile][:database][:external_data_script] do
475 command "#{node[:tile][:database][:external_data_script]} -R _renderd"
476 cwd "/srv/tile.openstreetmap.org"
482 Array(node[:tile][:database][:external_data_tables]).each do |table|
483 postgresql_table table do
484 cluster node[:tile][:database][:cluster]
487 permissions "tile" => :all, "www-data" => :select, "_renderd" => :select
492 directory File.dirname(node[:tile][:database][:node_file]) do
499 file node[:tile][:database][:node_file] do
505 directory "/var/log/tile" do
517 directory node[:tile][:replication][:directory] do
523 link "/var/lib/replicate" do
524 to node[:tile][:replication][:directory]
527 not_if { node[:tile][:replication][:directory] == "/var/lib/replicate" }
530 template "/usr/local/bin/expire-tiles" do
531 source "expire-tiles.erb"
537 directory "/var/lib/replicate/expire-queue" do
543 template "/usr/local/bin/replicate" do
544 source "replicate.erb"
550 systemd_service "expire-tiles" do
551 description "Tile dirtying service"
554 exec_start "/usr/local/bin/expire-tiles"
557 restrict_address_families "AF_UNIX"
558 read_write_paths tile_directories + [
559 "/var/lib/replicate/expire-queue"
563 systemd_path "expire-tiles" do
564 description "Tile dirtying trigger"
565 directory_not_empty "/var/lib/replicate/expire-queue"
568 service "expire-tiles.path" do
569 action [:enable, :start]
570 subscribes :restart, "systemd_path[expire-tiles]"
573 template "/usr/local/bin/replicate-post" do
574 source "replicate-post.erb"
580 systemd_service "replicate" do
581 description "Rendering database replication service"
582 after "postgresql.service"
583 wants "postgresql.service"
585 exec_start "/usr/local/bin/replicate"
586 sandbox :enable_network => true
587 restrict_address_families "AF_UNIX"
589 "/store/database/nodes",
595 service "replicate" do
596 action [:enable, :start]
597 subscribes :restart, "template[/usr/local/bin/replicate]"
598 subscribes :restart, "systemd_service[replicate]"
601 template "/usr/local/bin/render-lowzoom" do
602 source "render-lowzoom.erb"
606 variables :threads => lowzoom_threads
609 systemd_service "render-lowzoom" do
610 description "Render low zoom tiles"
611 condition_path_exists_glob "!/run/update-lowzoom-*"
613 exec_start "/usr/local/bin/render-lowzoom"
615 restrict_address_families "AF_UNIX"
616 read_write_paths "/var/log/tile"
619 systemd_timer "render-lowzoom" do
620 description "Render low zoom tiles"
621 on_calendar "23:00 #{node[:timezone]}"
624 service "render-lowzoom.timer" do
625 action [:enable, :start]
628 package "liblockfile-simple-perl"
629 package "libfilesys-df-perl"
631 template "/usr/local/bin/cleanup-tiles" do
632 source "cleanup-tiles.erb"
638 systemd_service "cleanup-tiles@" do
639 description "Cleanup old tiles for /%I"
640 exec_start "/usr/local/bin/cleanup-tiles /%I"
642 io_scheduling_class "idle"
644 read_write_paths "/%I"
647 systemd_timer "cleanup-tiles@" do
648 description "Cleanup old tiles for /%I"
650 on_unit_inactive_sec "60m"
651 randomized_delay_sec "10m"
654 tile_directories.each do |directory|
655 label = directory[1..].gsub("/", "-")
657 service "cleanup-tiles@#{label}.timer" do
658 action [:enable, :start]
662 package "ruby-webrick"
664 prometheus_exporter "modtile" do
668 prometheus_exporter "renderd" do