2 # Cookbook:: vectortile
5 # Copyright:: 2024, 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"
22 include_recipe "nginx"
23 include_recipe "postgresql"
24 include_recipe "prometheus"
25 include_recipe "python"
26 include_recipe "tools"
28 directory "/srv/vector.openstreetmap.org" do
34 nginx_site "default" do
38 nginx_site "vector.openstreetmap.org" do
42 ssl_certificate node[:fqdn] do
44 notifies :reload, "service[nginx]"
47 remote_directory "/srv/vector.openstreetmap.org/html" do
52 files_owner "www-data"
53 files_group "www-data"
57 template "/srv/vector.openstreetmap.org/html/index.html" do
58 source "index.html.erb"
64 postgresql_version = node[:vectortile][:database][:cluster].split("/").first
65 postgis_version = node[:vectortile][:database][:postgis]
67 package "postgresql-#{postgresql_version}-postgis-#{postgis_version}"
77 style_directory = "/srv/vector.openstreetmap.org/spirit"
78 git style_directory do
79 repository "https://github.com/pnorman/spirit.git"
84 shortbread_config = "#{style_directory}/shortbread.yaml"
86 themepark_directory = "/srv/vector.openstreetmap.org/osm2pgsql-themepark"
87 git themepark_directory do
88 repository "https://github.com/osm2pgsql-dev/osm2pgsql-themepark.git"
93 tilekiln_directory = "/opt/tilekiln"
95 python_virtualenv tilekiln_directory do
96 interpreter "/usr/bin/python3"
99 python_package "tilekiln" do
100 python_virtualenv tilekiln_directory
105 template "/srv/vector.openstreetmap.org/html/index.html" do
106 source "index.html.erb"
112 directory "/srv/vector.openstreetmap.org/data" do
118 node_store_options = node[:vectortile][:database][:nodes_store] == :flat ? "--flat-nodes '/srv/vector.openstreetmap.org/data/nodes.bin'" : ""
119 template "/usr/local/bin/import-planet" do
120 source "import-planet.erb"
124 variables :node_store_options => "#{node_store_options}"
127 template "/usr/local/bin/tilekiln-storage-init" do
128 source "tilekiln-storage-init.erb"
132 variables :tilekiln_bin => "#{tilekiln_directory}/bin/tilekiln", :storage_database => "tiles", :config_path => "#{shortbread_config}"
135 postgresql_user "tomh" do
136 cluster node[:vectortile][:database][:cluster]
140 postgresql_user "pnorman" do
141 cluster node[:vectortile][:database][:cluster]
145 postgresql_user "tilekiln" do
146 cluster node[:vectortile][:database][:cluster]
149 postgresql_user "tileupdate" do
150 cluster node[:vectortile][:database][:cluster]
153 postgresql_database "tiles" do
154 cluster node[:vectortile][:database][:cluster]
158 postgresql_database "spirit" do
159 cluster node[:vectortile][:database][:cluster]
163 postgresql_extension "postgis" do
164 cluster node[:vectortile][:database][:cluster]
168 postgresql_schema "tilekiln" do
169 cluster node[:vectortile][:database][:cluster]
172 permissions "tileupdate" => :all, "tilekiln" => :usage
173 notifies :run, "execute[tilekiln-storage-init]", :immediately
176 execute "tilekiln-storage-init" do
178 command "/usr/local/bin/tilekiln-storage-init"
182 %w[metadata shortbread_v1].each do |table|
183 postgresql_table table do
184 cluster node[:vectortile][:database][:cluster]
188 permissions "tileupdate" => :all, "tilekiln" => :select
192 postgresql_table "tile_stats" do
193 cluster node[:vectortile][:database][:cluster]
199 (0..14).each do |zoom|
200 postgresql_table "shortbread_v1_z#{zoom}" do
201 cluster node[:vectortile][:database][:cluster]
205 permissions "tileupdate" => :all, "tilekiln" => node[:vectortile][:serve][:mode] == :live ? [:select, :insert, :update] : :select
209 %w[addresses aerialways aeroways boundaries boundary_labels bridges buildings
210 dam_lines dam_polygons ferries land pier_lines pier_polygons place_labels
211 planet_osm_nodes planet_osm_rels planet_osm_ways pois public_transport railways
212 road_routes roads sites street_polygons streets_labels_points
213 streets_polygons_labels water_area_labels water_areas water_lines water_lines_labels].each do |table|
214 postgresql_table table do
215 cluster node[:vectortile][:database][:cluster]
219 permissions "tileupdate" => :all, "tilekiln" => :select
223 tilekiln_mode = node[:vectortile][:serve][:mode] == :live ? "live --config #{shortbread_config} --source-dbname spirit" : "static"
225 systemd_service "tilekiln" do
226 description "Tilekiln vector tile server"
228 after "postgresql.service"
229 wants "postgresql.service"
230 sandbox :enable_network => true
231 restrict_address_families "AF_UNIX"
232 exec_start "#{tilekiln_directory}/bin/tilekiln serve #{tilekiln_mode} --storage-dbname tiles --num-threads #{node[:vectortile][:serve][:threads]} --base-url 'https://vector.openstreetmap.org'"
235 service "tilekiln" do
236 action [:enable, :start]
239 execute "/srv/vector.openstreetmap.org/spirit/scripts/get-external-data.py" do
240 command "/srv/vector.openstreetmap.org/spirit/scripts/get-external-data.py -R tilekiln"
241 cwd "/srv/vector.openstreetmap.org/spirit"
247 template "/usr/local/bin/vector-update" do
248 source node[:vectortile][:replication][:tileupdate] == :enabled ? "vector-update-tile.erb" : "vector-update-notile.erb"
252 variables :tilekiln_bin => "#{tilekiln_directory}/bin/tilekiln", :source_database => "spirit", :config_path => "#{shortbread_config}", :diff_size => "1000", :tiles_file => "/srv/vector.openstreetmap.org/data/tiles.txt", :post_processing => "/usr/local/bin/tiles-rerender"
255 template "/usr/local/bin/tiles-rerender" do
256 source "tiles-rerender.erb"
260 variables :tilekiln_bin => "#{tilekiln_directory}/bin/tilekiln", :source_database => "spirit", :storage_database => "tiles", :config_path => "#{shortbread_config}", :tiles_file => "/srv/vector.openstreetmap.org/data/tiles.txt", :update_threads => 4
263 systemd_service "replicate" do
264 description "Get replication updates"
266 after "postgresql.service"
267 wants "postgresql.service"
268 sandbox :enable_network => true
269 restrict_address_families "AF_UNIX"
270 read_write_paths ["/srv/vector.openstreetmap.org/data/"]
271 exec_start "/usr/local/bin/vector-update"
274 systemd_timer "replicate" do
275 description "Get replication updates"
277 on_unit_active_sec 30
281 if node[:vectortile][:replication][:status] == :enabled
282 service "replicate.timer" do
283 action [:enable, :start]
286 service "replicate.timer" do
287 action [:stop, :disable]
296 prometheus_exporter "osm2pgsql" do
299 restrict_address_families "AF_UNIX"
301 "--database-name=spirit"
305 systemd_service "tilekiln-prometheus" do
306 description "Tilekiln vector tile server"
308 after "postgresql.service"
309 wants "postgresql.service"
310 sandbox :enable_network => true
311 restrict_address_families "AF_UNIX"
312 exec_start "#{tilekiln_directory}/bin/tilekiln prometheus --bind-host #{node[:prometheus][:address]} --storage-dbname tiles"
315 service "tilekiln-prometheus" do
316 action [:enable, :start]
319 node.default[:prometheus][:exporters][10013] = {
321 :address => "#{node[:prometheus][:address]}:10013",