]> git.openstreetmap.org Git - chef.git/blob - cookbooks/hardware/recipes/default.rb
Add more depth limits to git checkouts
[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 template "/etc/default/irqbalance" do
185   source "irqbalance.erb"
186   owner "root"
187   group "root"
188   mode 0o644
189 end
190
191 service "irqbalance" do
192   action [:start, :enable]
193   supports :status => false, :restart => true, :reload => false
194   subscribes :restart, "template[/etc/default/irqbalance]"
195 end
196
197 # Link Layer Discovery Protocol Daemon
198 package "lldpd"
199 service "lldpd" do
200   action [:start, :enable]
201   supports :status => true, :restart => true, :reload => true
202 end
203
204 tools_packages = []
205 status_packages = {}
206
207 node[:kernel][:modules].each_key do |modname|
208   case modname
209   when "cciss"
210     tools_packages << "ssacli"
211     status_packages["cciss-vol-status"] ||= []
212   when "hpsa"
213     tools_packages << "ssacli"
214     status_packages["cciss-vol-status"] ||= []
215   when "mptsas"
216     tools_packages << "lsiutil"
217     status_packages["mpt-status"] ||= []
218   when "mpt2sas", "mpt3sas"
219     tools_packages << "sas2ircu"
220     status_packages["sas2ircu-status"] ||= []
221   when "megaraid_mm"
222     tools_packages << "megactl"
223     status_packages["megaraid-status"] ||= []
224   when "megaraid_sas"
225     tools_packages << "megacli"
226     status_packages["megaclisas-status"] ||= []
227   when "aacraid"
228     tools_packages << "arcconf"
229     status_packages["aacraid-status"] ||= []
230   when "arcmsr"
231     tools_packages << "areca"
232   end
233 end
234
235 node[:block_device].each do |name, attributes|
236   next unless attributes[:vendor] == "HP" && attributes[:model] == "LOGICAL VOLUME"
237
238   if name =~ /^cciss!(c[0-9]+)d[0-9]+$/
239     status_packages["cciss-vol-status"] |= ["cciss/#{Regexp.last_match[1]}d0"]
240   else
241     Dir.glob("/sys/block/#{name}/device/scsi_generic/*").each do |sg|
242       status_packages["cciss-vol-status"] |= [File.basename(sg)]
243     end
244   end
245 end
246
247 %w[ssacli lsiutil sas2ircu megactl megacli arcconf].each do |tools_package|
248   if tools_packages.include?(tools_package)
249     package tools_package
250   else
251     package tools_package do
252       action :purge
253     end
254   end
255 end
256
257 if tools_packages.include?("areca")
258   include_recipe "git"
259
260   git "/opt/areca" do
261     action :sync
262     repository "https://git.openstreetmap.org/private/areca.git"
263     depth 1
264     user "root"
265     group "root"
266     not_if { ENV["TEST_KITCHEN"] }
267   end
268 else
269   directory "/opt/areca" do
270     action :delete
271     recursive true
272   end
273 end
274
275 if status_packages.include?("cciss-vol-status")
276   template "/usr/local/bin/cciss-vol-statusd" do
277     source "cciss-vol-statusd.erb"
278     owner "root"
279     group "root"
280     mode 0o755
281     notifies :restart, "service[cciss-vol-statusd]"
282   end
283
284   systemd_service "cciss-vol-statusd" do
285     description "Check cciss_vol_status values in the background"
286     exec_start "/usr/local/bin/cciss-vol-statusd"
287     private_tmp true
288     protect_system "full"
289     protect_home true
290     no_new_privileges true
291     notifies :restart, "service[cciss-vol-statusd]"
292   end
293 end
294
295 %w[cciss-vol-status mpt-status sas2ircu-status megaraid-status megaclisas-status aacraid-status].each do |status_package|
296   if status_packages.include?(status_package)
297     package status_package
298
299     template "/etc/default/#{status_package}d" do
300       source "raid.default.erb"
301       owner "root"
302       group "root"
303       mode 0o644
304       variables :devices => status_packages[status_package]
305     end
306
307     service "#{status_package}d" do
308       action [:start, :enable]
309       supports :status => false, :restart => true, :reload => false
310       subscribes :restart, "template[/etc/default/#{status_package}d]"
311     end
312   else
313     package status_package do
314       action :purge
315     end
316
317     file "/etc/default/#{status_package}d" do
318       action :delete
319     end
320   end
321 end
322
323 disks = if node[:hardware][:disk]
324           node[:hardware][:disk][:disks]
325         else
326           []
327         end
328
329 # intel_ssds = disks.select { |d| d[:vendor] == "INTEL" && d[:model] =~ /^SSD/ }
330 #
331 # nvmes = if node[:hardware][:pci]
332 #           node[:hardware][:pci].values.select { |pci| pci[:driver] == "nvme" }
333 #         else
334 #           []
335 #         end
336 #
337 # intel_nvmes = nvmes.select { |pci| pci[:vendor_name] == "Intel Corporation" }
338 #
339 # if !intel_ssds.empty? || !intel_nvmes.empty?
340 #   package "unzip"
341 #
342 #   intel_ssd_tool_version = "3.0.21"
343 #
344 #   remote_file "#{Chef::Config[:file_cache_path]}/Intel_SSD_Data_Center_Tool_#{intel_ssd_tool_version}_Linux.zip" do
345 #     source "https://downloadmirror.intel.com/29115/eng/Intel_SSD_Data_Center_Tool_#{intel_ssd_tool_version}_Linux.zip"
346 #   end
347 #
348 #   execute "#{Chef::Config[:file_cache_path]}/Intel_SSD_Data_Center_Tool_#{intel_ssd_tool_version}_Linux.zip" do
349 #     command "unzip Intel_SSD_Data_Center_Tool_#{intel_ssd_tool_version}_Linux.zip isdct_#{intel_ssd_tool_version}-1_amd64.deb"
350 #     cwd Chef::Config[:file_cache_path]
351 #     user "root"
352 #     group "root"
353 #     not_if { File.exist?("#{Chef::Config[:file_cache_path]}/isdct_#{intel_ssd_tool_version}-1_amd64.deb") }
354 #   end
355 #
356 #   dpkg_package "isdct" do
357 #     version "#{intel_ssd_tool_version}-1"
358 #     source "#{Chef::Config[:file_cache_path]}/isdct_#{intel_ssd_tool_version}-1_amd64.deb"
359 #   end
360 # end
361
362 disks = disks.map do |disk|
363   next if disk[:state] == "spun_down" || %w[unconfigured failed].any?(disk[:status])
364
365   if disk[:smart_device]
366     controller = node[:hardware][:disk][:controllers][disk[:controller]]
367
368     if controller && controller[:device]
369       device = controller[:device].sub("/dev/", "")
370       smart = disk[:smart_device]
371
372       if device.start_with?("cciss/") && smart =~ /^cciss,(\d+)$/
373         array = node[:hardware][:disk][:arrays][disk[:arrays].first]
374         munin = "cciss-3#{array[:wwn]}-#{Regexp.last_match(1)}"
375       elsif smart =~ /^.*,(\d+)$/
376         munin = "#{device}-#{Regexp.last_match(1)}"
377       elsif smart =~ %r{^.*,(\d+)/(\d+)$}
378         munin = "#{device}-#{Regexp.last_match(1)}:#{Regexp.last_match(2)}"
379       end
380     end
381   elsif disk[:device] =~ %r{^/dev/(nvme\d+)n\d+$}
382     device = Regexp.last_match(1)
383     munin = device
384   elsif disk[:device]
385     device = disk[:device].sub("/dev/", "")
386     munin = device
387   end
388
389   next if device.nil?
390
391   Hash[
392     :device => device,
393     :smart => smart,
394     :munin => munin,
395     :hddtemp => munin.tr("-:", "_")
396   ]
397 end
398
399 disks = disks.compact.uniq
400
401 if disks.count.positive?
402   package "smartmontools"
403
404   template "/usr/local/bin/smartd-mailer" do
405     source "smartd-mailer.erb"
406     owner "root"
407     group "root"
408     mode 0o755
409   end
410
411   template "/etc/smartd.conf" do
412     source "smartd.conf.erb"
413     owner "root"
414     group "root"
415     mode 0o644
416     variables :disks => disks
417   end
418
419   template "/etc/default/smartmontools" do
420     source "smartmontools.erb"
421     owner "root"
422     group "root"
423     mode 0o644
424   end
425
426   service "smartd" do
427     action [:enable, :start]
428     subscribes :reload, "template[/etc/smartd.conf]"
429     subscribes :restart, "template[/etc/default/smartmontools]"
430   end
431
432   # Don't try and do munin monitoring of disks behind
433   # an Areca controller as they only allow one thing to
434   # talk to the controller at a time and smartd will
435   # throw errors if it clashes with munin
436   disks = disks.reject { |disk| disk[:smart]&.start_with?("areca,") }
437
438   disks.each do |disk|
439     munin_plugin "smart_#{disk[:munin]}" do
440       target "smart_"
441       conf "munin.smart.erb"
442       conf_variables :disk => disk
443     end
444   end
445 else
446   service "smartd" do
447     action [:stop, :disable]
448   end
449 end
450
451 if disks.count.positive?
452   munin_plugin "hddtemp_smartctl" do
453     conf "munin.hddtemp.erb"
454     conf_variables :disks => disks
455   end
456 else
457   munin_plugin "hddtemp_smartctl" do
458     action :delete
459     conf "munin.hddtemp.erb"
460   end
461 end
462
463 plugins = Dir.glob("/etc/munin/plugins/smart_*").map { |p| File.basename(p) } -
464           disks.map { |d| "smart_#{d[:munin]}" }
465
466 plugins.each do |plugin|
467   munin_plugin plugin do
468     action :delete
469     conf "munin.smart.erb"
470   end
471 end
472
473 if File.exist?("/etc/mdadm/mdadm.conf")
474   mdadm_conf = edit_file "/etc/mdadm/mdadm.conf" do |line|
475     line.gsub!(/^MAILADDR .*$/, "MAILADDR admins@openstreetmap.org")
476
477     line
478   end
479
480   file "/etc/mdadm/mdadm.conf" do
481     owner "root"
482     group "root"
483     mode 0o644
484     content mdadm_conf
485   end
486
487   service "mdadm" do
488     action :nothing
489     subscribes :restart, "file[/etc/mdadm/mdadm.conf]"
490   end
491 end
492
493 template "/etc/modules" do
494   source "modules.erb"
495   owner "root"
496   group "root"
497   mode 0o644
498 end
499
500 service "kmod" do
501   action :nothing
502   subscribes :start, "template[/etc/modules]"
503 end
504
505 if node[:hardware][:watchdog]
506   package "watchdog"
507
508   template "/etc/default/watchdog" do
509     source "watchdog.erb"
510     owner "root"
511     group "root"
512     mode 0o644
513     variables :module => node[:hardware][:watchdog]
514   end
515
516   service "watchdog" do
517     action [:enable, :start]
518   end
519 end
520
521 unless Dir.glob("/sys/class/hwmon/hwmon*").empty?
522   package "lm-sensors"
523
524   Dir.glob("/sys/devices/platform/coretemp.*").each do |coretemp|
525     cpu = File.basename(coretemp).sub("coretemp.", "").to_i
526     chip = format("coretemp-isa-%04d", cpu)
527
528     temps = if File.exist?("#{coretemp}/name")
529               Dir.glob("#{coretemp}/temp*_input").map do |temp|
530                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
531               end.sort
532             else
533               Dir.glob("#{coretemp}/hwmon/hwmon*/temp*_input").map do |temp|
534                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
535               end.sort
536             end
537
538     if temps.first == 1
539       node.default[:hardware][:sensors][chip][:temps][:temp1][:label] = "CPU #{cpu}"
540       temps.shift
541     end
542
543     temps.each_with_index do |temp, index|
544       node.default[:hardware][:sensors][chip][:temps]["temp#{temp}"][:label] = "CPU #{cpu} Core #{index}"
545     end
546   end
547
548   execute "/etc/sensors.d/chef.conf" do
549     action :nothing
550     command "/usr/bin/sensors -s"
551     user "root"
552     group "root"
553   end
554
555   template "/etc/sensors.d/chef.conf" do
556     source "sensors.conf.erb"
557     owner "root"
558     group "root"
559     mode 0o644
560     notifies :run, "execute[/etc/sensors.d/chef.conf]"
561   end
562 end
563
564 if node[:hardware][:shm_size]
565   execute "remount-dev-shm" do
566     action :nothing
567     command "/bin/mount -o remount /dev/shm"
568     user "root"
569     group "root"
570   end
571
572   mount "/dev/shm" do
573     action :enable
574     device "tmpfs"
575     fstype "tmpfs"
576     options "rw,nosuid,nodev,size=#{node[:hardware][:shm_size]}"
577     notifies :run, "execute[remount-dev-shm]"
578   end
579 end