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