]> git.openstreetmap.org Git - chef.git/blob - cookbooks/hardware/recipes/default.rb
Fix rubocop warnings
[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 #     http://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 end
31
32 case node[:cpu][:"0"][:vendor_id]
33 when "AuthenticAMD"
34   package "amd64-microcode" if node[:lsb][:release].to_f >= 14.04
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   # Downgrade hp-health to 10.0.0.1.3-4. as 10.40-1815.49 has issues with reliable startup
62   package "hp-health" do
63     action :install
64     version "10.0.0.1.3-4."
65     options "--force-yes"
66     notifies :restart, "service[hp-health]"
67   end
68
69   service "hp-health" do
70     action [:enable, :start]
71     supports :status => true, :restart => true
72   end
73
74   units << "1"
75 when "TYAN"
76   units << "0"
77 when "TYAN Computer Corporation"
78   units << "0"
79 when "Supermicro"
80   case product
81   when "H8DGU", "X9SCD", "X7DBU", "X7DW3", "X9DR7/E-(J)LN4F", "X9DR3-F", "X9DRW", "SYS-2028U-TN24R4T+"
82     units << "1"
83   else
84     units << "0"
85   end
86 when "IBM"
87   units << "0"
88 end
89
90 # Remove legacy HP G4 support which breaks modern hp-health 10.4
91 if manufacturer == "HP"
92   %w(/opt/hp/hp-health/bin/hpasmd /usr/lib/libhpasmintrfc.so.3.0 %/usr/lib/libhpasmintrfc.so.3 /usr/lib/libhpasmintrfc.so).each do |filename|
93     file filename do
94       action :delete
95     end
96   end
97
98   directory "/opt/hp/hp-legacy" do
99     action :delete
100     recursive true
101   end
102 end
103
104 units.sort.uniq.each do |unit|
105   if node[:lsb][:release].to_f >= 16.04
106     service "serial-getty@ttyS#{unit}" do
107       action [:enable, :start]
108     end
109   else
110     file "/etc/init/ttySttyS#{unit}.conf" do
111       action :delete
112     end
113
114     template "/etc/init/ttyS#{unit}.conf" do
115       source "tty.conf.erb"
116       owner "root"
117       group "root"
118       mode 0o644
119       variables :unit => unit
120     end
121
122     service "ttyS#{unit}" do
123       provider Chef::Provider::Service::Upstart
124       action [:enable, :start]
125       supports :status => true, :restart => true, :reload => false
126       subscribes :restart, "template[/etc/init/ttyS#{unit}.conf]"
127     end
128   end
129 end
130
131 # if we need a different / special kernel version to make the hardware
132 # work (e.g: https://github.com/openstreetmap/operations/issues/45) then
133 # ensure that we have the package installed. the grub template will
134 # make sure that this is the default on boot.
135 if node[:hardware][:grub][:kernel]
136   kernel_version = node[:hardware][:grub][:kernel]
137
138   package "linux-image-#{kernel_version}-generic"
139   package "linux-image-extra-#{kernel_version}-generic"
140   package "linux-headers-#{kernel_version}-generic"
141
142   boot_device = IO.popen(["df", "/boot"]).readlines.last.split.first
143   boot_uuid = IO.popen(["blkid", "-o", "value", "-s", "UUID", boot_device]).readlines.first.chomp
144   grub_entry = "gnulinux-advanced-#{boot_uuid}>gnulinux-#{kernel_version}-advanced-#{boot_uuid}"
145 else
146   grub_entry = "0"
147 end
148
149 if File.exist?("/etc/default/grub")
150   execute "update-grub" do
151     action :nothing
152     command "/usr/sbin/update-grub"
153   end
154
155   template "/etc/default/grub" do
156     source "grub.erb"
157     owner "root"
158     group "root"
159     mode 0o644
160     variables :units => units, :entry => grub_entry
161     notifies :run, "execute[update-grub]"
162   end
163 end
164
165 execute "update-initramfs" do
166   action :nothing
167   command "update-initramfs -u -k all"
168   user "root"
169   group "root"
170 end
171
172 template "/etc/initramfs-tools/conf.d/mdadm" do
173   source "initramfs-mdadm.erb"
174   owner "root"
175   group "root"
176   mode 0o644
177   notifies :run, "execute[update-initramfs]"
178 end
179
180 package "haveged"
181 service "haveged" do
182   action [:enable, :start]
183 end
184
185 package "ipmitool" if node[:kernel][:modules].include?("ipmi_si")
186
187 if node[:lsb][:release].to_f >= 12.10
188   package "irqbalance"
189
190   template "/etc/default/irqbalance" do
191     source "irqbalance.erb"
192     owner "root"
193     group "root"
194     mode 0o644
195   end
196
197   service "irqbalance" do
198     action [:start, :enable]
199     supports :status => false, :restart => true, :reload => false
200     subscribes :restart, "template[/etc/default/irqbalance]"
201   end
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 << "hpssacli"
211     status_packages["cciss-vol-status"] ||= []
212   when "hpsa"
213     tools_packages << "hpssacli"
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(hpssacli 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 "git://chef.openstreetmap.org/areca.git"
263     user "root"
264     group "root"
265   end
266 else
267   directory "/opt/areca" do
268     action :delete
269     recursive true
270   end
271 end
272
273 ["cciss-vol-status", "mpt-status", "sas2ircu-status", "megaraid-status", "megaclisas-status", "aacraid-status"].each do |status_package|
274   if status_packages.include?(status_package)
275     package status_package
276
277     template "/etc/default/#{status_package}d" do
278       source "raid.default.erb"
279       owner "root"
280       group "root"
281       mode 0o644
282       variables :devices => status_packages[status_package]
283     end
284
285     service "#{status_package}d" do
286       action [:start, :enable]
287       supports :status => false, :restart => true, :reload => false
288       subscribes :restart, "template[/etc/default/#{status_package}d]"
289     end
290   else
291     package status_package do
292       action :purge
293     end
294
295     file "/etc/default/#{status_package}d" do
296       action :delete
297     end
298   end
299 end
300
301 disks = if node[:hardware][:disk]
302           node[:hardware][:disk][:disks]
303         else
304           []
305         end
306
307 intel_ssds = disks.select { |d| d[:vendor] == "INTEL" && d[:model] =~ /^SSD/ }
308
309 nvmes = if node[:hardware][:pci]
310           node[:hardware][:pci].values.select { |pci| pci[:driver] == "nvme" }
311         else
312           []
313         end
314
315 intel_nvmes = nvmes.select { |pci| pci[:vendor_name] == "Intel Corporation" }
316
317 if !intel_ssds.empty? || !intel_nvmes.empty?
318   package "unzip"
319   package "alien"
320
321   remote_file "#{Chef::Config[:file_cache_path]}/DataCenterTool_3_0_0_Linux.zip" do
322     source "https://downloadmirror.intel.com/23931/eng/DataCenterTool_3_0_0_Linux.zip"
323   end
324
325   execute "unzip-DataCenterTool" do
326     command "unzip DataCenterTool_3_0_0_Linux.zip isdct-3.0.0.400-15.x86_64.rpm"
327     cwd Chef::Config[:file_cache_path]
328     user "root"
329     group "root"
330     not_if { File.exist?("#{Chef::Config[:file_cache_path]}/isdct-3.0.0.400-15.x86_64.rpm") }
331   end
332
333   execute "alien-isdct" do
334     command "alien --to-deb isdct-3.0.0.400-15.x86_64.rpm"
335     cwd Chef::Config[:file_cache_path]
336     user "root"
337     group "root"
338     not_if { File.exist?("#{Chef::Config[:file_cache_path]}/isdct_3.0.0.400-16_amd64.deb") }
339   end
340
341   dpkg_package "isdct" do
342     source "#{Chef::Config[:file_cache_path]}/isdct_3.0.0.400-16_amd64.deb"
343   end
344 end
345
346 disks = disks.map do |disk|
347   next if disk[:state] == "spun_down"
348
349   if disk[:smart_device]
350     controller = node[:hardware][:disk][:controllers][disk[:controller]]
351     device = controller[:device].sub("/dev/", "")
352     smart = disk[:smart_device]
353
354     if device.start_with?("cciss/") && smart =~ /^cciss,(\d+)$/
355       array = node[:hardware][:disk][:arrays][disk[:arrays].first]
356       munin = "cciss-3#{array[:wwn]}-#{Regexp.last_match(1)}"
357     elsif smart =~ /^.*,(\d+)$/
358       munin = "#{device}-#{Regexp.last_match(1)}"
359     elsif smart =~ %r{^.*,(\d+)/(\d+)$}
360       munin = "#{device}-#{Regexp.last_match(1)}:#{Regexp.last_match(2)}"
361     end
362   elsif disk[:device]
363     device = disk[:device].sub("/dev/", "")
364     munin = device
365   end
366
367   next if device.nil?
368
369   Hash[
370     :device => device,
371     :smart => smart,
372     :munin => munin,
373     :hddtemp => munin.tr("-:", "_")
374   ]
375 end
376
377 smartd_service = if node[:lsb][:release].to_f >= 16.04
378                    "smartd"
379                  else
380                    "smartmontools"
381                  end
382
383 disks = disks.compact
384
385 if disks.count > 0
386   package "smartmontools"
387
388   template "/usr/local/bin/smartd-mailer" do
389     source "smartd-mailer.erb"
390     owner "root"
391     group "root"
392     mode 0o755
393   end
394
395   template "/etc/smartd.conf" do
396     source "smartd.conf.erb"
397     owner "root"
398     group "root"
399     mode 0o644
400     variables :disks => disks
401   end
402
403   template "/etc/default/smartmontools" do
404     source "smartmontools.erb"
405     owner "root"
406     group "root"
407     mode 0o644
408   end
409
410   service smartd_service do
411     action [:enable, :start]
412     subscribes :reload, "template[/etc/smartd.conf]"
413     subscribes :restart, "template[/etc/default/smartmontools]"
414   end
415
416   # Don't try and do munin monitoring of disks behind
417   # an Areca controller as they only allow one thing to
418   # talk to the controller at a time and smartd will
419   # throw errors if it clashes with munin
420   disks = disks.reject { |disk| disk[:smart] && disk[:smart].start_with?("areca,") }
421
422   disks.each do |disk|
423     munin_plugin "smart_#{disk[:munin]}" do
424       target "smart_"
425       conf "munin.smart.erb"
426       conf_variables :disk => disk
427     end
428   end
429 else
430   service smartd_service do
431     action [:stop, :disable]
432   end
433 end
434
435 if disks.count > 0
436   munin_plugin "hddtemp_smartctl" do
437     conf "munin.hddtemp.erb"
438     conf_variables :disks => disks
439   end
440 else
441   munin_plugin "hddtemp_smartctl" do
442     action :delete
443     conf "munin.hddtemp.erb"
444   end
445 end
446
447 plugins = Dir.glob("/etc/munin/plugins/smart_*").map { |p| File.basename(p) } -
448           disks.map { |d| "smart_#{d[:munin]}" }
449
450 plugins.each do |plugin|
451   munin_plugin plugin do
452     action :delete
453   end
454 end
455
456 if File.exist?("/etc/mdadm/mdadm.conf")
457   mdadm_conf = edit_file "/etc/mdadm/mdadm.conf" do |line|
458     line.gsub!(/^MAILADDR .*$/, "MAILADDR admins@openstreetmap.org")
459
460     line
461   end
462
463   file "/etc/mdadm/mdadm.conf" do
464     owner "root"
465     group "root"
466     mode 0o644
467     content mdadm_conf
468   end
469
470   service "mdadm" do
471     action :nothing
472     subscribes :restart, "file[/etc/mdadm/mdadm.conf]"
473   end
474 end
475
476 template "/etc/modules" do
477   source "modules.erb"
478   owner "root"
479   group "root"
480   mode 0o644
481 end
482
483 if node[:lsb][:release].to_f <= 12.10
484   service "module-init-tools" do
485     provider Chef::Provider::Service::Upstart
486     action :nothing
487     subscribes :start, "template[/etc/modules]"
488   end
489 else
490   service "kmod" do
491     if node[:lsb][:release].to_f >= 15.10
492       provider Chef::Provider::Service::Systemd
493     else
494       provider Chef::Provider::Service::Upstart
495     end
496     action :nothing
497     subscribes :start, "template[/etc/modules]"
498   end
499 end
500
501 if node[:hardware][:watchdog]
502   package "watchdog"
503
504   template "/etc/default/watchdog" do
505     source "watchdog.erb"
506     owner "root"
507     group "root"
508     mode 0o644
509     variables :module => node[:hardware][:watchdog]
510   end
511
512   service "watchdog" do
513     action [:enable, :start]
514   end
515 end
516
517 unless Dir.glob("/sys/class/hwmon/hwmon*").empty?
518   package "lm-sensors"
519
520   Dir.glob("/sys/devices/platform/coretemp.*").each do |coretemp|
521     cpu = File.basename(coretemp).sub("coretemp.", "").to_i
522     chip = format("coretemp-isa-%04d", cpu)
523
524     temps = if File.exist?("#{coretemp}/name")
525               Dir.glob("#{coretemp}/temp*_input").map do |temp|
526                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
527               end.sort
528             else
529               Dir.glob("#{coretemp}/hwmon/hwmon*/temp*_input").map do |temp|
530                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
531               end.sort
532             end
533
534     if temps.first == 1
535       node.default[:hardware][:sensors][chip][:temps][:temp1][:label] = "CPU #{cpu}"
536       temps.shift
537     end
538
539     temps.each_with_index do |temp, index|
540       node.default[:hardware][:sensors][chip][:temps]["temp#{temp}"][:label] = "CPU #{cpu} Core #{index}"
541     end
542   end
543
544   execute "/etc/sensors.d/chef.conf" do
545     action :nothing
546     command "/usr/bin/sensors -s"
547     user "root"
548     group "root"
549   end
550
551   template "/etc/sensors.d/chef.conf" do
552     source "sensors.conf.erb"
553     owner "root"
554     group "root"
555     mode 0o644
556     notifies :run, "execute[/etc/sensors.d/chef.conf]"
557   end
558 end