]> git.openstreetmap.org Git - chef.git/blob - cookbooks/tilecache/recipes/default.rb
395ee54dcdd8106240026b122447a12096c1fad5
[chef.git] / cookbooks / tilecache / recipes / default.rb
1 #
2 # Cookbook:: tilecache
3 # Recipe:: default
4 #
5 # Copyright:: 2011, OpenStreetMap Foundation
6 #
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
10 #
11 #     https://www.apache.org/licenses/LICENSE-2.0
12 #
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.
18 #
19
20 require "ipaddr"
21
22 include_recipe "ssl"
23 include_recipe "squid"
24 include_recipe "nginx"
25 include_recipe "fail2ban"
26
27 package "apache2" do
28   action :remove
29 end
30
31 package %w[
32   xz-utils
33   openssl
34 ]
35
36 # oathtool for QoS token
37 package "oathtool"
38
39 tilecaches = search(:node, "roles:tilecache").sort_by { |n| n[:hostname] }
40 tilerenders = search(:node, "roles:tile").sort_by { |n| n[:hostname] }
41
42 web_passwords = data_bag_item("web", "passwords")
43
44 tilecaches.each do |cache|
45   cache.ipaddresses(:family => :inet, :role => :external).sort.each do |address|
46     firewall_rule "accept-squid" do
47       action :accept
48       family "inet"
49       source "net:#{address}"
50       dest "fw"
51       proto "tcp:syn"
52       dest_ports "3128"
53       source_ports "1024:"
54     end
55
56     firewall_rule "accept-squid-icp" do
57       action :accept
58       family "inet"
59       source "net:#{address}"
60       dest "fw"
61       proto "udp"
62       dest_ports "3130"
63       source_ports "3130"
64     end
65
66     firewall_rule "accept-squid-icp-reply" do
67       action :accept
68       family "inet"
69       source "fw"
70       dest "net:#{address}"
71       proto "udp"
72       dest_ports "3130"
73       source_ports "3130"
74     end
75
76     firewall_rule "accept-squid-htcp" do
77       action :accept
78       family "inet"
79       source "net:#{address}"
80       dest "fw"
81       proto "udp"
82       dest_ports "4827"
83       source_ports "4827"
84     end
85
86     firewall_rule "accept-squid-htcp-reply" do
87       action :accept
88       family "inet"
89       source "fw"
90       dest "net:#{address}"
91       proto "udp"
92       dest_ports "4827"
93       source_ports "4827"
94     end
95   end
96 end
97
98 squid_fragment "tilecache" do
99   template "squid.conf.erb"
100   variables :caches => tilecaches, :renders => tilerenders
101 end
102
103 if node[:packages][:squid][:version].to_f < 4.9
104   Array(node[:squid][:cache_dir]).each do |cache_dir|
105     if cache_dir =~ /^coss (\S+) /
106       cache_dir = File.dirname(Regexp.last_match(1))
107     elsif cache_dir =~ /^\S+ (\S+) /
108       cache_dir = Regexp.last_match(1)
109     end
110
111     file "#{cache_dir}/rock" do
112       action :delete
113       backup false
114     end
115   end
116
117   package "squid" do
118     action :upgrade
119   end
120 end
121
122 package "rsync"
123
124 template "/etc/logrotate.d/squid" do
125   source "logrotate.squid.erb"
126   owner "root"
127   group "root"
128   mode 0o644
129 end
130
131 # Configure cron with lower cpu and IO priority
132 systemd_service "cron-load" do
133   service "cron"
134   dropin "load"
135   nice 19
136   io_scheduling_class "best-effort"
137   io_scheduling_priority 7
138   notifies :restart, "service[cron]"
139 end
140
141 nginx_site "default" do
142   action [:delete]
143 end
144
145 template "/usr/local/bin/nginx_generate_tilecache_qos_map" do
146   source "nginx_generate_tilecache_qos_map.erb"
147   owner "root"
148   group "root"
149   mode 0o750
150   variables :totp_key => web_passwords["totp_key"]
151 end
152
153 template "/etc/cron.d/tilecache" do
154   source "cron.erb"
155   owner "root"
156   group "root"
157   mode 0o644
158 end
159
160 execute "execute_nginx_generate_tilecache_qos_map" do
161   command "/usr/local/bin/nginx_generate_tilecache_qos_map"
162   creates "/etc/nginx/conf.d/tile_qos_rates.map"
163   action :run
164 end
165
166 ssl_certificate "tile.openstreetmap.org" do
167   domains ["tile.openstreetmap.org",
168            "a.tile.openstreetmap.org",
169            "b.tile.openstreetmap.org",
170            "c.tile.openstreetmap.org",
171            "tile.osm.org",
172            "a.tile.osm.org",
173            "b.tile.osm.org",
174            "c.tile.osm.org"]
175   notifies :restart, "service[nginx]"
176 end
177
178 nginx_site "tile" do
179   template "nginx_tile.conf.erb"
180   variables :caches => tilecaches
181 end
182
183 template "/etc/logrotate.d/nginx" do
184   source "logrotate.nginx.erb"
185   owner "root"
186   group "root"
187   mode 0o644
188 end
189
190 fail2ban_jail "squid" do
191   maxretry 1000
192 end
193
194 tilerenders.each do |render|
195   munin_plugin "ping_#{render[:fqdn]}" do
196     target "ping_"
197     conf "munin.ping.erb"
198     conf_variables :host => render[:fqdn]
199   end
200 end