]> git.openstreetmap.org Git - chef.git/blob - cookbooks/squid/recipes/default.rb
ac120a258acb0205d5ed419d82fa14679fb6b964
[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   package "squid" do
30     action :remove
31     only_if "dpkg-query -W squid | fgrep -q 2."
32   end
33
34   package "squid-common" do
35     action :remove
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 end
44
45 package "squid"
46 package "squidclient"
47
48 template "/etc/squid/squid.conf" do
49   source "squid.conf.erb"
50   owner "root"
51   group "root"
52   mode 0o644
53 end
54
55 template "/etc/default/squid" do
56   source "squid.erb"
57   owner "root"
58   group "root"
59   mode 0o644
60 end
61
62 directory "/etc/squid/squid.conf.d" do
63   owner "root"
64   group "root"
65   mode 0o755
66 end
67
68 if node[:squid][:cache_dir] =~ /^coss (\S+) /
69   cache_dir = File.dirname(Regexp.last_match(1))
70 elsif node[:squid][: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 end
80
81 systemd_tmpfile "/var/run/squid" do
82   type "d"
83   owner "proxy"
84   group "proxy"
85   mode "0755"
86 end
87
88 systemd_service "squid" do
89   description "Squid caching proxy"
90   after ["network.target", "nss-lookup.target"]
91   type "forking"
92   limit_nofile 65536
93   exec_start_pre "/usr/sbin/squid -N -z"
94   exec_start "/usr/sbin/squid"
95   exec_reload "/usr/sbin/squid -k reconfigure"
96   exec_stop "/usr/sbin/squid -k shutdown"
97   private_tmp true
98   private_devices true
99   protect_system "full"
100   protect_home true
101   restart "on-failure"
102   timeout_sec 0
103 end
104
105 service "squid" do
106   action [:enable, :start]
107   subscribes :restart, "systemd_service[squid]"
108   subscribes :reload, "template[/etc/squid/squid.conf]"
109   subscribes :restart, "template[/etc/default/squid]"
110   subscribes :reload, "template[/etc/resolv.conf]"
111 end
112
113 log "squid-restart" do
114   message "Restarting squid due to counter wraparound"
115   notifies :restart, "service[squid]"
116   only_if do
117     IO.popen(["squidclient", "--host=127.0.0.1", "--port=80", "mgr:counters"]) do |io|
118       io.each.grep(/^[a-z][a-z_.]+ = -[0-9]+$/).count.positive?
119     end
120   end
121 end
122
123 munin_plugin "squid_cache"
124 munin_plugin "squid_delay_pools"
125 munin_plugin "squid_delay_pools_noreferer"
126 munin_plugin "squid_times"
127 munin_plugin "squid_icp"
128 munin_plugin "squid_objectsize"
129 munin_plugin "squid_requests"
130 munin_plugin "squid_traffic"
131
132 Dir.glob("/var/log/squid/zere.log*") do |log|
133   File.unlink(log)
134 end