]> git.openstreetmap.org Git - chef.git/blob - cookbooks/squid/recipes/default.rb
c02c67cf43fff68eec945e3cf5f4583c31390361
[chef.git] / cookbooks / squid / recipes / default.rb
1 #
2 # Cookbook Name:: 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 if node[:squid][:version] == "3"
21   apt_package "squid" do
22     action :unlock
23   end
24
25   apt_package "squid-common" do
26     action :unlock
27   end
28
29   apt_package "squid" do
30     action :purge
31     only_if "dpkg-query -W squid | fgrep -q 2."
32   end
33
34   apt_package "squid-common" do
35     action :purge
36     only_if "dpkg-query -W squid-common | fgrep -q 2."
37   end
38
39   file "/store/squid/coss-01" do
40     action :delete
41     backup false
42   end
43
44   package "squidclient" do
45     action :upgrade
46   end
47 end
48
49 package "squid"
50 package "squidclient"
51
52 template "/etc/squid/squid.conf" do
53   source "squid.conf.erb"
54   owner "root"
55   group "root"
56   mode 0o644
57 end
58
59 template "/etc/default/squid" do
60   source "squid.erb"
61   owner "root"
62   group "root"
63   mode 0o644
64 end
65
66 directory "/etc/squid/squid.conf.d" do
67   owner "root"
68   group "root"
69   mode 0o755
70 end
71
72 if node[:squid][:cache_dir] =~ /^coss (\S+) /
73   cache_dir = File.dirname(Regexp.last_match(1))
74 elsif node[:squid][:cache_dir] =~ /^\S+ (\S+) /
75   cache_dir = Regexp.last_match(1)
76 end
77
78 directory cache_dir do
79   owner "proxy"
80   group "proxy"
81   mode 0o750
82   recursive true
83 end
84
85 systemd_tmpfile "/var/run/squid" do
86   type "d"
87   owner "proxy"
88   group "proxy"
89   mode "0755"
90 end
91
92 systemd_service "squid" do
93   description "Squid caching proxy"
94   after ["network.target", "nss-lookup.target"]
95   type "forking"
96   limit_nofile 65536
97   exec_start_pre "/usr/sbin/squid -N -z"
98   exec_start "/usr/sbin/squid"
99   exec_reload "/usr/sbin/squid -k reconfigure"
100   exec_stop "/usr/sbin/squid -k shutdown"
101   private_tmp true
102   private_devices true
103   protect_system "full"
104   protect_home true
105   restart "on-failure"
106   timeout_sec 0
107 end
108
109 service "squid" do
110   action [:enable, :start]
111   subscribes :restart, "systemd_service[squid]"
112   subscribes :reload, "template[/etc/squid/squid.conf]"
113   subscribes :restart, "template[/etc/default/squid]"
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=80", "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_delay_pools"
129 munin_plugin "squid_delay_pools_noreferer"
130 munin_plugin "squid_times"
131 munin_plugin "squid_icp"
132 munin_plugin "squid_objectsize"
133 munin_plugin "squid_requests"
134 munin_plugin "squid_traffic"
135
136 Dir.glob("/var/log/squid/zere.log*") do |log|
137   File.unlink(log)
138 end