]> git.openstreetmap.org Git - chef.git/blob - cookbooks/web/recipes/frontend.rb
Switch chef and web logrotate to zstd
[chef.git] / cookbooks / web / recipes / frontend.rb
1 #
2 # Cookbook:: web
3 # Recipe:: frontend
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 node.default[:memcached][:ip_address] = node.internal_ipaddress || "127.0.0.1"
21
22 include_recipe "memcached"
23 include_recipe "apache"
24 include_recipe "fail2ban"
25 include_recipe "web::rails"
26 include_recipe "web::cgimap"
27
28 web_passwords = data_bag_item("web", "passwords")
29
30 apache_module "alias"
31 apache_module "expires"
32 apache_module "headers"
33 apache_module "proxy"
34 apache_module "proxy_fcgi"
35 apache_module "lbmethod_byrequests"
36 apache_module "lbmethod_bybusyness"
37 apache_module "remoteip"
38 apache_module "reqtimeout"
39 apache_module "rewrite"
40 apache_module "unique_id"
41
42 apache_site "default" do
43   action [:disable]
44 end
45
46 remote_directory "#{node[:web][:base_directory]}/static" do
47   source "static"
48   owner "root"
49   group "root"
50   mode "755"
51   files_owner "root"
52   files_group "root"
53   files_mode "644"
54 end
55
56 remote_file "#{Chef::Config[:file_cache_path]}/cloudflare-ipv4-list" do
57   source "https://www.cloudflare.com/ips-v4"
58   compile_time true
59   ignore_failure true
60 end
61
62 cloudflare_ipv4 = IO.read("#{Chef::Config[:file_cache_path]}/cloudflare-ipv4-list").lines.map(&:chomp)
63
64 remote_file "#{Chef::Config[:file_cache_path]}/cloudflare-ipv6-list" do
65   source "https://www.cloudflare.com/ips-v6"
66   compile_time true
67   ignore_failure true
68 end
69
70 cloudflare_ipv6 = IO.read("#{Chef::Config[:file_cache_path]}/cloudflare-ipv6-list").lines.map(&:chomp)
71
72 remote_file "#{Chef::Config[:file_cache_path]}/fastly-ip-list.json" do
73   source "https://api.fastly.com/public-ip-list"
74   compile_time true
75   ignore_failure true
76 end
77
78 fastlyips = JSON.parse(IO.read("#{Chef::Config[:file_cache_path]}/fastly-ip-list.json"))
79
80 apache_site "www.openstreetmap.org" do
81   template "apache.frontend.erb"
82   variables :cloudflare => cloudflare_ipv4 + cloudflare_ipv6,
83             :fastly => fastlyips["addresses"] + fastlyips["ipv6_addresses"],
84             :status => node[:web][:status],
85             :secret_key_base => web_passwords["secret_key_base"]
86 end
87
88 template "/etc/logrotate.d/apache2" do
89   source "logrotate.apache.erb"
90   owner "root"
91   group "root"
92   mode "644"
93 end
94
95 fail2ban_filter "apache-request-timeout" do
96   failregex '^<ADDR> .* "-" 408 .*$'
97 end
98
99 fail2ban_jail "apache-request-timeout" do
100   filter "apache-request-timeout"
101   logpath "/var/log/apache2/access.log"
102   ports [80, 443]
103 end
104
105 fail2ban_filter "apache-trackpoints-timeout" do
106   failregex '^<ADDR> .* "GET /api/0\.6/trackpoints\?.*" 408 .*$'
107 end
108
109 fail2ban_jail "apache-trackpoints-timeout" do
110   filter "apache-trackpoints-timeout"
111   logpath "/var/log/apache2/access.log"
112   ports [80, 443]
113   bantime "12h"
114   findtime "30m"
115 end
116
117 fail2ban_filter "apache-notes-search" do
118   failregex '^<ADDR> .* "GET /api/0\.6/notes/search\?q=abcde&.*$'
119 end
120
121 fail2ban_jail "apache-notes-search" do
122   filter "apache-notes-search"
123   logpath "/var/log/apache2/access.log"
124   ports [80, 443]
125 end
126
127 if %w[database_offline database_readonly].include?(node[:web][:status])
128   service "rails-jobs@mailers" do
129     action :stop
130   end
131
132   service "rails-jobs@storage" do
133     action :stop
134   end
135
136   service "rails-jobs@traces" do
137     action :stop
138   end
139 else
140   service "rails-jobs@mailers" do
141     action [:enable, :start]
142     supports :restart => true
143     subscribes :restart, "rails_port[www.openstreetmap.org]"
144     subscribes :restart, "systemd_service[rails-jobs@]"
145   end
146
147   service "rails-jobs@storage" do
148     action [:enable, :start]
149     supports :restart => true
150     subscribes :restart, "rails_port[www.openstreetmap.org]"
151     subscribes :restart, "systemd_service[rails-jobs@]"
152   end
153
154   service "rails-jobs@traces" do
155     action [:enable, :start]
156     supports :restart => true
157     subscribes :restart, "rails_port[www.openstreetmap.org]"
158     subscribes :restart, "systemd_service[rails-jobs@]"
159   end
160 end
161
162 template "/usr/local/bin/deliver-message" do
163   source "deliver-message.erb"
164   owner "rails"
165   group "rails"
166   mode "0700"
167   variables :secret_key_base => web_passwords["secret_key_base"]
168 end