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"
89 revision "6df586489889dae1c5ffed33071067ef09771acf"
94 tilekiln_directory = "/opt/tilekiln"
96 python_virtualenv tilekiln_directory do
97 interpreter "/usr/bin/python3"
100 python_package "tilekiln" do
101 python_virtualenv tilekiln_directory
106 template "/srv/vector.openstreetmap.org/html/index.html" do
107 source "index.html.erb"
113 directory "/srv/vector.openstreetmap.org/data" do
119 node_store_options = node[:vectortile][:database][:nodes_store] == :flat ? "--flat-nodes '/srv/vector.openstreetmap.org/data/nodes.bin'" : ""
120 template "/usr/local/bin/import-planet" do
121 source "import-planet.erb"
125 variables :node_store_options => "#{node_store_options}"
128 template "/usr/local/bin/tilekiln-storage-init" do
129 source "tilekiln-storage-init.erb"
133 variables :tilekiln_bin => "#{tilekiln_directory}/bin/tilekiln", :storage_database => "tiles", :config_path => "#{shortbread_config}"
136 postgresql_user "tomh" do
137 cluster node[:vectortile][:database][:cluster]
141 postgresql_user "pnorman" do
142 cluster node[:vectortile][:database][:cluster]
146 postgresql_user "tilekiln" do
147 cluster node[:vectortile][:database][:cluster]
150 postgresql_user "tileupdate" do
151 cluster node[:vectortile][:database][:cluster]
154 postgresql_database "tiles" do
155 cluster node[:vectortile][:database][:cluster]
159 postgresql_database "spirit" do
160 cluster node[:vectortile][:database][:cluster]
164 postgresql_extension "postgis" do
165 cluster node[:vectortile][:database][:cluster]
169 postgresql_schema "tilekiln" do
170 cluster node[:vectortile][:database][:cluster]
173 permissions "tileupdate" => :all, "tilekiln" => :usage
174 notifies :run, "execute[tilekiln-storage-init]", :immediately
177 execute "tilekiln-storage-init" do
179 command "/usr/local/bin/tilekiln-storage-init"
183 %w[metadata shortbread_v1].each do |table|
184 postgresql_table table do
185 cluster node[:vectortile][:database][:cluster]
189 permissions "tileupdate" => :all, "tilekiln" => :select
193 postgresql_table "tile_stats" do
194 cluster node[:vectortile][:database][:cluster]
200 (0..14).each do |zoom|
201 postgresql_table "shortbread_v1_z#{zoom}" do
202 cluster node[:vectortile][:database][:cluster]
206 permissions "tileupdate" => :all, "tilekiln" => node[:vectortile][:serve][:mode] == :live ? [:select, :insert, :update] : :select
210 %w[addresses aerialways aeroways boundaries boundary_labels bridges buildings
211 dam_lines dam_polygons ferries land pier_lines pier_polygons place_labels
212 planet_osm_nodes planet_osm_rels planet_osm_ways pois public_transport railways
213 road_routes roads sites street_polygons streets_labels_points
214 streets_polygons_labels water_area_labels water_areas water_lines water_lines_labels].each do |table|
215 postgresql_table table do
216 cluster node[:vectortile][:database][:cluster]
220 permissions "tileupdate" => :all, "tilekiln" => :select
224 tilekiln_mode = node[:vectortile][:serve][:mode] == :live ? "live --config #{shortbread_config} --source-dbname spirit" : "static"
226 systemd_service "tilekiln" do
227 description "Tilekiln vector tile server"
229 after "postgresql.service"
230 wants "postgresql.service"
231 sandbox :enable_network => true
232 restrict_address_families "AF_UNIX"
233 exec_start "#{tilekiln_directory}/bin/tilekiln serve #{tilekiln_mode} --storage-dbname tiles --num-threads #{node[:vectortile][:serve][:threads]} --base-url 'https://vector.openstreetmap.org'"
236 service "tilekiln" do
237 action [:enable, :start]
240 execute "/srv/vector.openstreetmap.org/spirit/scripts/get-external-data.py" do
241 command "/srv/vector.openstreetmap.org/spirit/scripts/get-external-data.py -R tilekiln"
242 cwd "/srv/vector.openstreetmap.org/spirit"
248 template "/usr/local/bin/vector-update" do
249 source node[:vectortile][:replication][:tileupdate] == :enabled ? "vector-update-tile.erb" : "vector-update-notile.erb"
253 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"
256 template "/usr/local/bin/tiles-rerender" do
257 source "tiles-rerender.erb"
261 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
264 systemd_service "replicate" do
265 description "Get replication updates"
267 after "postgresql.service"
268 wants "postgresql.service"
269 sandbox :enable_network => true
270 restrict_address_families "AF_UNIX"
271 read_write_paths ["/srv/vector.openstreetmap.org/data/"]
272 exec_start "/usr/local/bin/vector-update"
275 systemd_timer "replicate" do
276 description "Get replication updates"
278 on_unit_active_sec 30
282 if node[:vectortile][:replication][:status] == :enabled
283 service "replicate.timer" do
284 action [:enable, :start]
287 service "replicate.timer" do
288 action [:stop, :disable]
297 prometheus_exporter "osm2pgsql" do
300 restrict_address_families "AF_UNIX"
302 "--database-name=spirit"
306 systemd_service "tilekiln-prometheus" do
307 description "Tilekiln vector tile server"
309 after "postgresql.service"
310 wants "postgresql.service"
311 sandbox :enable_network => true
312 restrict_address_families "AF_UNIX"
313 exec_start "#{tilekiln_directory}/bin/tilekiln prometheus --bind-host #{node[:prometheus][:address]} --storage-dbname tiles"
316 service "tilekiln-prometheus" do
317 action [:enable, :start]
320 node.default[:prometheus][:exporters][10013] = {
322 :address => "#{node[:prometheus][:address]}:10013",