]> git.openstreetmap.org Git - chef.git/blob - cookbooks/hardware/recipes/default.rb
Drop support for older Ubuntu versions
[chef.git] / cookbooks / hardware / recipes / default.rb
1 #
2 # Cookbook Name:: hardware
3 # Recipe:: default
4 #
5 # Copyright 2012, 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 "tools"
21 include_recipe "munin"
22
23 ohai_plugin "hardware" do
24   template "ohai.rb.erb"
25 end
26
27 case node[:cpu][:"0"][:vendor_id]
28 when "GenuineIntel"
29   package "intel-microcode"
30 when "AuthenticAMD"
31   package "amd64-microcode"
32 end
33
34 if node[:dmi] && node[:dmi][:system]
35   case node[:dmi][:system][:manufacturer]
36   when "empty"
37     manufacturer = node[:dmi][:base_board][:manufacturer]
38     product = node[:dmi][:base_board][:product_name]
39   else
40     manufacturer = node[:dmi][:system][:manufacturer]
41     product = node[:dmi][:system][:product_name]
42   end
43 else
44   manufacturer = "Unknown"
45   product = "Unknown"
46 end
47
48 units = []
49
50 if node[:roles].include?("bytemark") || node[:roles].include?("exonetric")
51   units << "0"
52 end
53
54 case manufacturer
55 when "HP"
56   package "hponcfg"
57
58   package "hp-health" do
59     action :install
60     notifies :restart, "service[hp-health]"
61   end
62
63   service "hp-health" do
64     action [:enable, :start]
65     supports :status => true, :restart => true
66   end
67
68   if product.end_with?("Gen8", "Gen9")
69     package "hp-ams" do
70       action :install
71       notifies :restart, "service[hp-ams]"
72     end
73
74     service "hp-ams" do
75       action [:enable, :start]
76       supports :status => true, :restart => true
77     end
78   end
79
80   units << "1"
81 when "TYAN"
82   units << "0"
83 when "TYAN Computer Corporation"
84   units << "0"
85 when "Supermicro"
86   case product
87   when "H8DGU", "X9SCD", "X7DBU", "X7DW3", "X9DR7/E-(J)LN4F", "X9DR3-F", "X9DRW", "SYS-1028U-TN10RT+", "SYS-2028U-TN24R4T+", "Super Server"
88     units << "1"
89   else
90     units << "0"
91   end
92 when "IBM"
93   units << "0"
94 end
95
96 units.sort.uniq.each do |unit|
97   service "serial-getty@ttyS#{unit}" do
98     action [:enable, :start]
99   end
100 end
101
102 # if we need a different / special kernel version to make the hardware
103 # work (e.g: https://github.com/openstreetmap/operations/issues/45) then
104 # ensure that we have the package installed. the grub template will
105 # make sure that this is the default on boot.
106 if node[:hardware][:grub][:kernel]
107   kernel_version = node[:hardware][:grub][:kernel]
108
109   package "linux-image-#{kernel_version}-generic"
110   package "linux-image-extra-#{kernel_version}-generic"
111   package "linux-headers-#{kernel_version}-generic"
112
113   boot_device = IO.popen(["df", "/boot"]).readlines.last.split.first
114   boot_uuid = IO.popen(["blkid", "-o", "value", "-s", "UUID", boot_device]).readlines.first.chomp
115   grub_entry = "gnulinux-advanced-#{boot_uuid}>gnulinux-#{kernel_version}-advanced-#{boot_uuid}"
116 else
117   grub_entry = "0"
118 end
119
120 if File.exist?("/etc/default/grub")
121   execute "update-grub" do
122     action :nothing
123     command "/usr/sbin/update-grub"
124   end
125
126   template "/etc/default/grub" do
127     source "grub.erb"
128     owner "root"
129     group "root"
130     mode 0o644
131     variables :units => units, :entry => grub_entry
132     notifies :run, "execute[update-grub]"
133   end
134 end
135
136 execute "update-initramfs" do
137   action :nothing
138   command "update-initramfs -u -k all"
139   user "root"
140   group "root"
141 end
142
143 template "/etc/initramfs-tools/conf.d/mdadm" do
144   source "initramfs-mdadm.erb"
145   owner "root"
146   group "root"
147   mode 0o644
148   notifies :run, "execute[update-initramfs]"
149 end
150
151 package "haveged"
152 service "haveged" do
153   action [:enable, :start]
154 end
155
156 package "ipmitool" if node[:kernel][:modules].include?("ipmi_si")
157
158 package "irqbalance"
159
160 template "/etc/default/irqbalance" do
161   source "irqbalance.erb"
162   owner "root"
163   group "root"
164   mode 0o644
165 end
166
167 service "irqbalance" do
168   action [:start, :enable]
169   supports :status => false, :restart => true, :reload => false
170   subscribes :restart, "template[/etc/default/irqbalance]"
171 end
172
173 # Link Layer Discovery Protocol Daemon
174 package "lldpd"
175 service "lldpd" do
176   action [:start, :enable]
177   supports :status => true, :restart => true, :reload => true
178 end
179
180 tools_packages = []
181 status_packages = {}
182
183 node[:kernel][:modules].each_key do |modname|
184   case modname
185   when "cciss"
186     tools_packages << "ssacli"
187     status_packages["cciss-vol-status"] ||= []
188   when "hpsa"
189     tools_packages << "ssacli"
190     status_packages["cciss-vol-status"] ||= []
191   when "mptsas"
192     tools_packages << "lsiutil"
193     status_packages["mpt-status"] ||= []
194   when "mpt2sas", "mpt3sas"
195     tools_packages << "sas2ircu"
196     status_packages["sas2ircu-status"] ||= []
197   when "megaraid_mm"
198     tools_packages << "megactl"
199     status_packages["megaraid-status"] ||= []
200   when "megaraid_sas"
201     tools_packages << "megacli"
202     status_packages["megaclisas-status"] ||= []
203   when "aacraid"
204     tools_packages << "arcconf"
205     status_packages["aacraid-status"] ||= []
206   when "arcmsr"
207     tools_packages << "areca"
208   end
209 end
210
211 node[:block_device].each do |name, attributes|
212   next unless attributes[:vendor] == "HP" && attributes[:model] == "LOGICAL VOLUME"
213
214   if name =~ /^cciss!(c[0-9]+)d[0-9]+$/
215     status_packages["cciss-vol-status"] |= ["cciss/#{Regexp.last_match[1]}d0"]
216   else
217     Dir.glob("/sys/block/#{name}/device/scsi_generic/*").each do |sg|
218       status_packages["cciss-vol-status"] |= [File.basename(sg)]
219     end
220   end
221 end
222
223 %w[ssacli lsiutil sas2ircu megactl megacli arcconf].each do |tools_package|
224   if tools_packages.include?(tools_package)
225     package tools_package
226   else
227     package tools_package do
228       action :purge
229     end
230   end
231 end
232
233 if tools_packages.include?("areca")
234   include_recipe "git"
235
236   git "/opt/areca" do
237     action :sync
238     repository "https://git.openstreetmap.org/private/areca.git"
239     user "root"
240     group "root"
241   end
242 else
243   directory "/opt/areca" do
244     action :delete
245     recursive true
246   end
247 end
248
249 ["cciss-vol-status", "mpt-status", "sas2ircu-status", "megaraid-status", "megaclisas-status", "aacraid-status"].each do |status_package|
250   if status_packages.include?(status_package)
251     package status_package
252
253     template "/etc/default/#{status_package}d" do
254       source "raid.default.erb"
255       owner "root"
256       group "root"
257       mode 0o644
258       variables :devices => status_packages[status_package]
259     end
260
261     service "#{status_package}d" do
262       action [:start, :enable]
263       supports :status => false, :restart => true, :reload => false
264       subscribes :restart, "template[/etc/default/#{status_package}d]"
265     end
266   else
267     package status_package do
268       action :purge
269     end
270
271     file "/etc/default/#{status_package}d" do
272       action :delete
273     end
274   end
275 end
276
277 disks = if node[:hardware][:disk]
278           node[:hardware][:disk][:disks]
279         else
280           []
281         end
282
283 intel_ssds = disks.select { |d| d[:vendor] == "INTEL" && d[:model] =~ /^SSD/ }
284
285 nvmes = if node[:hardware][:pci]
286           node[:hardware][:pci].values.select { |pci| pci[:driver] == "nvme" }
287         else
288           []
289         end
290
291 intel_nvmes = nvmes.select { |pci| pci[:vendor_name] == "Intel Corporation" }
292
293 if !intel_ssds.empty? || !intel_nvmes.empty?
294   package "unzip"
295
296   remote_file "#{Chef::Config[:file_cache_path]}/Intel_SSD_Data_Center_Tool_3.0.13_Linux.zip" do
297     source "https://downloadmirror.intel.com/28460/eng/Intel_SSD_Data_Center_Tool_3.0.17_Linux.zip"
298   end
299
300   execute "#{Chef::Config[:file_cache_path]}/Intel_SSD_Data_Center_Tool_3.0.13_Linux.zip" do
301     command "unzip Intel_SSD_Data_Center_Tool_3.0.13_Linux.zip isdct_3.0.13.400-17_amd64.deb"
302     cwd Chef::Config[:file_cache_path]
303     user "root"
304     group "root"
305     not_if { File.exist?("#{Chef::Config[:file_cache_path]}/isdct_3.0.13.400-17_amd64.deb") }
306   end
307
308   dpkg_package "isdct" do
309     version "3.0.13.400-17"
310     source "#{Chef::Config[:file_cache_path]}/isdct_3.0.13.400-17_amd64.deb"
311   end
312 end
313
314 disks = disks.map do |disk|
315   next if disk[:state] == "spun_down" || %w[unconfigured failed].any?(disk[:status])
316
317   if disk[:smart_device]
318     controller = node[:hardware][:disk][:controllers][disk[:controller]]
319
320     if controller && controller[:device]
321       device = controller[:device].sub("/dev/", "")
322       smart = disk[:smart_device]
323
324       if device.start_with?("cciss/") && smart =~ /^cciss,(\d+)$/
325         array = node[:hardware][:disk][:arrays][disk[:arrays].first]
326         munin = "cciss-3#{array[:wwn]}-#{Regexp.last_match(1)}"
327       elsif smart =~ /^.*,(\d+)$/
328         munin = "#{device}-#{Regexp.last_match(1)}"
329       elsif smart =~ %r{^.*,(\d+)/(\d+)$}
330         munin = "#{device}-#{Regexp.last_match(1)}:#{Regexp.last_match(2)}"
331       end
332     end
333   elsif disk[:device]
334     device = disk[:device].sub("/dev/", "")
335     munin = device
336   end
337
338   next if device.nil?
339
340   Hash[
341     :device => device,
342     :smart => smart,
343     :munin => munin,
344     :hddtemp => munin.tr("-:", "_")
345   ]
346 end
347
348 disks = disks.compact
349
350 if disks.count.positive?
351   package "smartmontools"
352
353   template "/usr/local/bin/smartd-mailer" do
354     source "smartd-mailer.erb"
355     owner "root"
356     group "root"
357     mode 0o755
358   end
359
360   template "/etc/smartd.conf" do
361     source "smartd.conf.erb"
362     owner "root"
363     group "root"
364     mode 0o644
365     variables :disks => disks
366   end
367
368   template "/etc/default/smartmontools" do
369     source "smartmontools.erb"
370     owner "root"
371     group "root"
372     mode 0o644
373   end
374
375   service "smartd" do
376     action [:enable, :start]
377     subscribes :reload, "template[/etc/smartd.conf]"
378     subscribes :restart, "template[/etc/default/smartmontools]"
379   end
380
381   # Don't try and do munin monitoring of disks behind
382   # an Areca controller as they only allow one thing to
383   # talk to the controller at a time and smartd will
384   # throw errors if it clashes with munin
385   disks = disks.reject { |disk| disk[:smart]&.start_with?("areca,") }
386
387   disks.each do |disk|
388     munin_plugin "smart_#{disk[:munin]}" do
389       target "smart_"
390       conf "munin.smart.erb"
391       conf_variables :disk => disk
392     end
393   end
394 else
395   service "smartd" do
396     action [:stop, :disable]
397   end
398 end
399
400 if disks.count.positive?
401   munin_plugin "hddtemp_smartctl" do
402     conf "munin.hddtemp.erb"
403     conf_variables :disks => disks
404   end
405 else
406   munin_plugin "hddtemp_smartctl" do
407     action :delete
408     conf "munin.hddtemp.erb"
409   end
410 end
411
412 plugins = Dir.glob("/etc/munin/plugins/smart_*").map { |p| File.basename(p) } -
413           disks.map { |d| "smart_#{d[:munin]}" }
414
415 plugins.each do |plugin|
416   munin_plugin plugin do
417     action :delete
418     conf "munin.smart.erb"
419   end
420 end
421
422 if File.exist?("/etc/mdadm/mdadm.conf")
423   mdadm_conf = edit_file "/etc/mdadm/mdadm.conf" do |line|
424     line.gsub!(/^MAILADDR .*$/, "MAILADDR admins@openstreetmap.org")
425
426     line
427   end
428
429   file "/etc/mdadm/mdadm.conf" do
430     owner "root"
431     group "root"
432     mode 0o644
433     content mdadm_conf
434   end
435
436   service "mdadm" do
437     action :nothing
438     subscribes :restart, "file[/etc/mdadm/mdadm.conf]"
439   end
440 end
441
442 template "/etc/modules" do
443   source "modules.erb"
444   owner "root"
445   group "root"
446   mode 0o644
447 end
448
449 service "kmod" do
450   action :nothing
451   subscribes :start, "template[/etc/modules]"
452 end
453
454 if node[:hardware][:watchdog]
455   package "watchdog"
456
457   template "/etc/default/watchdog" do
458     source "watchdog.erb"
459     owner "root"
460     group "root"
461     mode 0o644
462     variables :module => node[:hardware][:watchdog]
463   end
464
465   service "watchdog" do
466     action [:enable, :start]
467   end
468 end
469
470 unless Dir.glob("/sys/class/hwmon/hwmon*").empty?
471   package "lm-sensors"
472
473   Dir.glob("/sys/devices/platform/coretemp.*").each do |coretemp|
474     cpu = File.basename(coretemp).sub("coretemp.", "").to_i
475     chip = format("coretemp-isa-%04d", cpu)
476
477     temps = if File.exist?("#{coretemp}/name")
478               Dir.glob("#{coretemp}/temp*_input").map do |temp|
479                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
480               end.sort
481             else
482               Dir.glob("#{coretemp}/hwmon/hwmon*/temp*_input").map do |temp|
483                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
484               end.sort
485             end
486
487     if temps.first == 1
488       node.default[:hardware][:sensors][chip][:temps][:temp1][:label] = "CPU #{cpu}"
489       temps.shift
490     end
491
492     temps.each_with_index do |temp, index|
493       node.default[:hardware][:sensors][chip][:temps]["temp#{temp}"][:label] = "CPU #{cpu} Core #{index}"
494     end
495   end
496
497   execute "/etc/sensors.d/chef.conf" do
498     action :nothing
499     command "/usr/bin/sensors -s"
500     user "root"
501     group "root"
502   end
503
504   template "/etc/sensors.d/chef.conf" do
505     source "sensors.conf.erb"
506     owner "root"
507     group "root"
508     mode 0o644
509     notifies :run, "execute[/etc/sensors.d/chef.conf]"
510   end
511 end