]> git.openstreetmap.org Git - chef.git/blob - cookbooks/tilecache/recipes/default.rb
tilecache: switch port 80 traffic to nginx
[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 package "rsync"
104
105 template "/etc/logrotate.d/squid" do
106   source "logrotate.squid.erb"
107   owner "root"
108   group "root"
109   mode 0o644
110 end
111
112 # Configure cron with lower cpu and IO priority
113 systemd_service "cron-load" do
114   service "cron"
115   dropin "load"
116   nice 19
117   io_scheduling_class "best-effort"
118   io_scheduling_priority 7
119   notifies :restart, "service[cron]"
120 end
121
122 nginx_site "default" do
123   action [:delete]
124 end
125
126 template "/usr/local/bin/nginx_generate_tilecache_qos_map" do
127   source "nginx_generate_tilecache_qos_map.erb"
128   owner "root"
129   group "root"
130   mode 0o750
131   variables :totp_key => web_passwords["totp_key"]
132 end
133
134 template "/etc/cron.d/tilecache" do
135   source "cron.erb"
136   owner "root"
137   group "root"
138   mode 0o644
139 end
140
141 execute "execute_nginx_generate_tilecache_qos_map" do
142   command "/usr/local/bin/nginx_generate_tilecache_qos_map"
143   creates "/etc/nginx/conf.d/tile_qos_rates.map"
144   action :run
145 end
146
147 ssl_certificate "tile.openstreetmap.org" do
148   domains ["tile.openstreetmap.org",
149            "a.tile.openstreetmap.org",
150            "b.tile.openstreetmap.org",
151            "c.tile.openstreetmap.org",
152            "tile.osm.org",
153            "a.tile.osm.org",
154            "b.tile.osm.org",
155            "c.tile.osm.org"]
156   notifies :restart, "service[nginx]"
157 end
158
159 nginx_site "tile" do
160   template "nginx_tile.conf.erb"
161   variables :caches => tilecaches
162 end
163
164 template "/etc/logrotate.d/nginx" do
165   source "logrotate.nginx.erb"
166   owner "root"
167   group "root"
168   mode 0o644
169 end
170
171 fail2ban_jail "squid" do
172   maxretry 1000
173 end
174
175 tilerenders.each do |render|
176   munin_plugin "ping_#{render[:fqdn]}" do
177     target "ping_"
178     conf "munin.ping.erb"
179     conf_variables :host => render[:fqdn]
180   end
181 end