]> git.openstreetmap.org Git - chef.git/blob - cookbooks/chef/recipes/default.rb
Merge remote-tracking branch 'github/pull/641'
[chef.git] / cookbooks / chef / recipes / default.rb
1 #
2 # Cookbook:: chef
3 # Recipe:: default
4 #
5 # Copyright:: 2010, 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 cache_dir = Chef::Config[:file_cache_path]
21
22 chef_version = node[:chef][:client][:version]
23
24 chef_platform = if platform?("debian")
25                   "debian"
26                 else
27                   "ubuntu"
28                 end
29
30 chef_arch = if arm?
31               "arm64"
32             else
33               "amd64"
34             end
35
36 os_release = if platform?("debian") && node[:lsb][:release].to_f > 11
37                11
38              else
39                node[:lsb][:release]
40              end
41
42 # Chef is currently not available for Debian 11 on arm64.
43 if chef_platform == "debian" && os_release == 11 && chef_arch == "arm64"
44   chef_platform = "ubuntu"
45   os_release = "22.04"
46 end
47
48 chef_package = "chef_#{chef_version}-1_#{chef_arch}.deb"
49
50 directory "/var/cache/chef" do
51   action :delete
52   recursive true
53 end
54
55 Dir.glob("#{cache_dir}/chef_*.deb").each do |deb|
56   next if deb == "#{cache_dir}/#{chef_package}"
57
58   file deb do
59     action :delete
60     backup false
61   end
62 end
63
64 remote_file "#{cache_dir}/#{chef_package}" do
65   source "https://packages.chef.io/files/stable/chef/#{chef_version}/#{chef_platform}/#{os_release}/#{chef_package}"
66   owner "root"
67   group "root"
68   mode "644"
69   backup false
70   ignore_failure true
71 end
72
73 dpkg_package "chef" do
74   source "#{cache_dir}/#{chef_package}"
75   version "#{chef_version}-1"
76 end
77
78 directory "/etc/chef" do
79   owner "root"
80   group "root"
81   mode "755"
82 end
83
84 template "/etc/chef/client.rb" do
85   source "client.rb.erb"
86   owner "root"
87   group "root"
88   mode "640"
89 end
90
91 file "/etc/chef/client.pem" do
92   owner "root"
93   group "root"
94   mode "400"
95 end
96
97 template "/etc/chef/report.rb" do
98   source "report.rb.erb"
99   owner "root"
100   group "root"
101   mode "644"
102 end
103
104 template "/etc/logrotate.d/chef" do
105   source "logrotate.erb"
106   owner "root"
107   group "root"
108   mode "644"
109 end
110
111 # Remove the ancient verisign certificate workaround
112 file "/etc/chef/trusted_certs/verisign.pem" do
113   action :delete
114 end
115
116 directory node[:ohai][:plugin_dir] do
117   owner "root"
118   group "root"
119   mode "755"
120 end
121
122 directory "/var/log/chef" do
123   owner "root"
124   group "root"
125   mode "755"
126 end
127
128 systemd_service "chef-client" do
129   description "Chef client"
130   exec_start "/usr/bin/chef-client"
131   nice 10
132 end
133
134 systemd_timer "chef-client" do
135   description "Chef client"
136   after "network.target"
137   on_active_sec 60
138   on_unit_inactive_sec 25 * 60
139   randomized_delay_sec 10 * 60
140 end
141
142 service "chef-client.timer" do
143   action [:enable, :start]
144 end
145
146 service "chef-client.service" do
147   action :disable
148   subscribes :stop, "service[chef-client.timer]"
149 end