]> git.openstreetmap.org Git - chef.git/blob - cookbooks/squid/recipes/default.rb
Reduce implicit coupling between cookbooks
[chef.git] / cookbooks / squid / recipes / default.rb
1 #
2 # Cookbook:: squid
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 include_recipe "apt"
21
22 if node[:squid][:version] >= 3
23   apt_package "squid" do
24     action :unlock
25   end
26
27   apt_package "squid-common" do
28     action :unlock
29   end
30
31   apt_package "squid" do
32     action :purge
33     only_if "dpkg-query -W squid | fgrep -q 2."
34   end
35
36   apt_package "squid-common" do
37     action :purge
38     only_if "dpkg-query -W squid-common | fgrep -q 2."
39   end
40
41   file "/store/squid/coss-01" do
42     action :delete
43     backup false
44   end
45
46   package "squidclient" do
47     action :upgrade
48   end
49 end
50
51 package "squid"
52 package "squidclient"
53
54 template "/etc/squid/squid.conf" do
55   source "squid.conf.erb"
56   owner "root"
57   group "root"
58   mode 0o644
59 end
60
61 directory "/etc/squid/squid.conf.d" do
62   owner "root"
63   group "root"
64   mode 0o755
65 end
66
67 Array(node[:squid][:cache_dir]).each do |cache_dir|
68   if cache_dir =~ /^coss (\S+) /
69     cache_dir = File.dirname(Regexp.last_match(1))
70   elsif cache_dir =~ /^\S+ (\S+) /
71     cache_dir = Regexp.last_match(1)
72   end
73
74   directory cache_dir do
75     owner "proxy"
76     group "proxy"
77     mode 0o750
78     recursive true
79     notifies :restart, "service[squid]"
80   end
81 end
82
83 systemd_tmpfile "/var/run/squid" do
84   type "d"
85   owner "proxy"
86   group "proxy"
87   mode "0755"
88 end
89
90 address_families = %w[AF_UNIX AF_INET AF_INET6]
91
92 systemd_service "squid" do
93   description "Squid caching proxy"
94   after ["network.target", "nss-lookup.target"]
95   type "forking"
96   limit_nofile 98304
97   exec_start_pre "/usr/sbin/squid --foreground -z"
98   exec_start "/usr/sbin/squid -YC"
99   exec_reload "/bin/kill -HUP $MAINPID"
100   pid_file "/var/run/squid.pid"
101   private_tmp true
102   private_devices true
103   protect_system "full"
104   protect_home true
105   restrict_address_families address_families
106   restart "always"
107   kill_mode "mixed"
108 end
109
110 service "squid" do
111   action [:enable, :start]
112   subscribes :restart, "systemd_service[squid]"
113   subscribes :restart, "template[/etc/squid/squid.conf]"
114   subscribes :reload, "template[/etc/resolv.conf]"
115 end
116
117 log "squid-restart" do
118   message "Restarting squid due to counter wraparound"
119   notifies :restart, "service[squid]"
120   only_if do
121     IO.popen(["squidclient", "--host=127.0.0.1", "--port=3128", "mgr:counters"]) do |io|
122       io.each.grep(/^[a-z][a-z_.]+ = -[0-9]+$/).count.positive?
123     end
124   end
125 end
126
127 munin_plugin "squid_cache"
128 munin_plugin "squid_times"
129 munin_plugin "squid_icp"
130 munin_plugin "squid_objectsize"
131 munin_plugin "squid_requests"
132 munin_plugin "squid_traffic"
133
134 munin_plugin "squid_delay_pools" do
135   action :delete
136 end
137
138 munin_plugin "squid_delay_pools_noreferer" do
139   action :delete
140 end