]> git.openstreetmap.org Git - chef.git/blob - cookbooks/hardware/recipes/default.rb
Fix rubocop warning
[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   if node[:lsb][:release].to_f >= 16.04
98     service "serial-getty@ttyS#{unit}" do
99       action [:enable, :start]
100     end
101   else
102     file "/etc/init/ttySttyS#{unit}.conf" do
103       action :delete
104     end
105
106     template "/etc/init/ttyS#{unit}.conf" do
107       source "tty.conf.erb"
108       owner "root"
109       group "root"
110       mode 0o644
111       variables :unit => unit
112     end
113
114     service "ttyS#{unit}" do
115       provider Chef::Provider::Service::Upstart
116       action [:enable, :start]
117       supports :status => true, :restart => true, :reload => false
118       subscribes :restart, "template[/etc/init/ttyS#{unit}.conf]"
119     end
120   end
121 end
122
123 # if we need a different / special kernel version to make the hardware
124 # work (e.g: https://github.com/openstreetmap/operations/issues/45) then
125 # ensure that we have the package installed. the grub template will
126 # make sure that this is the default on boot.
127 if node[:hardware][:grub][:kernel]
128   kernel_version = node[:hardware][:grub][:kernel]
129
130   package "linux-image-#{kernel_version}-generic"
131   package "linux-image-extra-#{kernel_version}-generic"
132   package "linux-headers-#{kernel_version}-generic"
133
134   boot_device = IO.popen(["df", "/boot"]).readlines.last.split.first
135   boot_uuid = IO.popen(["blkid", "-o", "value", "-s", "UUID", boot_device]).readlines.first.chomp
136   grub_entry = "gnulinux-advanced-#{boot_uuid}>gnulinux-#{kernel_version}-advanced-#{boot_uuid}"
137 else
138   grub_entry = "0"
139 end
140
141 if File.exist?("/etc/default/grub")
142   execute "update-grub" do
143     action :nothing
144     command "/usr/sbin/update-grub"
145   end
146
147   template "/etc/default/grub" do
148     source "grub.erb"
149     owner "root"
150     group "root"
151     mode 0o644
152     variables :units => units, :entry => grub_entry
153     notifies :run, "execute[update-grub]"
154   end
155 end
156
157 execute "update-initramfs" do
158   action :nothing
159   command "update-initramfs -u -k all"
160   user "root"
161   group "root"
162 end
163
164 template "/etc/initramfs-tools/conf.d/mdadm" do
165   source "initramfs-mdadm.erb"
166   owner "root"
167   group "root"
168   mode 0o644
169   notifies :run, "execute[update-initramfs]"
170 end
171
172 package "haveged"
173 service "haveged" do
174   action [:enable, :start]
175 end
176
177 package "ipmitool" if node[:kernel][:modules].include?("ipmi_si")
178
179 package "irqbalance"
180
181 template "/etc/default/irqbalance" do
182   source "irqbalance.erb"
183   owner "root"
184   group "root"
185   mode 0o644
186 end
187
188 service "irqbalance" do
189   action [:start, :enable]
190   supports :status => false, :restart => true, :reload => false
191   subscribes :restart, "template[/etc/default/irqbalance]"
192 end
193
194 # Link Layer Discovery Protocol Daemon
195 package "lldpd"
196 service "lldpd" do
197   action [:start, :enable]
198   supports :status => true, :restart => true, :reload => true
199 end
200
201 if node[:hardware][:mcelog][:enabled]
202   package "mcelog"
203
204   %w[bus cache dimm iomca page socket-memory unknown].each do |trigger|
205     template "/etc/mcelog/#{trigger}-error-trigger.local" do
206       source "mcelog-trigger.erb"
207       owner "root"
208       group "root"
209       mode 0o755
210     end
211   end
212
213   service "mcelog" do
214     action [:start, :enable]
215     supports :status => true, :restart => true, :reload => false
216   end
217 end
218
219 tools_packages = []
220 status_packages = {}
221
222 node[:kernel][:modules].each_key do |modname|
223   case modname
224   when "cciss"
225     tools_packages << "ssacli"
226     status_packages["cciss-vol-status"] ||= []
227   when "hpsa"
228     tools_packages << "ssacli"
229     status_packages["cciss-vol-status"] ||= []
230   when "mptsas"
231     tools_packages << "lsiutil"
232     status_packages["mpt-status"] ||= []
233   when "mpt2sas", "mpt3sas"
234     tools_packages << "sas2ircu"
235     status_packages["sas2ircu-status"] ||= []
236   when "megaraid_mm"
237     tools_packages << "megactl"
238     status_packages["megaraid-status"] ||= []
239   when "megaraid_sas"
240     tools_packages << "megacli"
241     status_packages["megaclisas-status"] ||= []
242   when "aacraid"
243     tools_packages << "arcconf"
244     status_packages["aacraid-status"] ||= []
245   when "arcmsr"
246     tools_packages << "areca"
247   end
248 end
249
250 node[:block_device].each do |name, attributes|
251   next unless attributes[:vendor] == "HP" && attributes[:model] == "LOGICAL VOLUME"
252
253   if name =~ /^cciss!(c[0-9]+)d[0-9]+$/
254     status_packages["cciss-vol-status"] |= ["cciss/#{Regexp.last_match[1]}d0"]
255   else
256     Dir.glob("/sys/block/#{name}/device/scsi_generic/*").each do |sg|
257       status_packages["cciss-vol-status"] |= [File.basename(sg)]
258     end
259   end
260 end
261
262 %w[ssacli lsiutil sas2ircu megactl megacli arcconf].each do |tools_package|
263   if tools_packages.include?(tools_package)
264     package tools_package
265   else
266     package tools_package do
267       action :purge
268     end
269   end
270 end
271
272 if tools_packages.include?("areca")
273   include_recipe "git"
274
275   git "/opt/areca" do
276     action :sync
277     repository "https://git.openstreetmap.org/private/areca.git"
278     user "root"
279     group "root"
280   end
281 else
282   directory "/opt/areca" do
283     action :delete
284     recursive true
285   end
286 end
287
288 ["cciss-vol-status", "mpt-status", "sas2ircu-status", "megaraid-status", "megaclisas-status", "aacraid-status"].each do |status_package|
289   if status_packages.include?(status_package)
290     package status_package
291
292     template "/etc/default/#{status_package}d" do
293       source "raid.default.erb"
294       owner "root"
295       group "root"
296       mode 0o644
297       variables :devices => status_packages[status_package]
298     end
299
300     service "#{status_package}d" do
301       action [:start, :enable]
302       supports :status => false, :restart => true, :reload => false
303       subscribes :restart, "template[/etc/default/#{status_package}d]"
304     end
305   else
306     package status_package do
307       action :purge
308     end
309
310     file "/etc/default/#{status_package}d" do
311       action :delete
312     end
313   end
314 end
315
316 disks = if node[:hardware][:disk]
317           node[:hardware][:disk][:disks]
318         else
319           []
320         end
321
322 intel_ssds = disks.select { |d| d[:vendor] == "INTEL" && d[:model] =~ /^SSD/ }
323
324 nvmes = if node[:hardware][:pci]
325           node[:hardware][:pci].values.select { |pci| pci[:driver] == "nvme" }
326         else
327           []
328         end
329
330 intel_nvmes = nvmes.select { |pci| pci[:vendor_name] == "Intel Corporation" }
331
332 if !intel_ssds.empty? || !intel_nvmes.empty?
333   package "unzip"
334
335   remote_file "#{Chef::Config[:file_cache_path]}/Intel_SSD_Data_Center_Tool_3.0.13_Linux.zip" do
336     source "https://downloadmirror.intel.com/27863/eng/Intel_SSD_Data_Center_Tool_3.0.13_Linux.zip"
337   end
338
339   execute "#{Chef::Config[:file_cache_path]}/Intel_SSD_Data_Center_Tool_3.0.13_Linux.zip" do
340     command "unzip Intel_SSD_Data_Center_Tool_3.0.13_Linux.zip isdct_3.0.13.400-17_amd64.deb"
341     cwd Chef::Config[:file_cache_path]
342     user "root"
343     group "root"
344     not_if { File.exist?("#{Chef::Config[:file_cache_path]}/isdct_3.0.13.400-17_amd64.deb") }
345   end
346
347   dpkg_package "isdct" do
348     version "3.0.13.400-17"
349     source "#{Chef::Config[:file_cache_path]}/isdct_3.0.13.400-17_amd64.deb"
350   end
351 end
352
353 disks = disks.map do |disk|
354   next if disk[:state] == "spun_down"
355
356   if disk[:smart_device]
357     controller = node[:hardware][:disk][:controllers][disk[:controller]]
358
359     if controller && controller[:device]
360       device = controller[:device].sub("/dev/", "")
361       smart = disk[:smart_device]
362
363       if device.start_with?("cciss/") && smart =~ /^cciss,(\d+)$/
364         array = node[:hardware][:disk][:arrays][disk[:arrays].first]
365         munin = "cciss-3#{array[:wwn]}-#{Regexp.last_match(1)}"
366       elsif smart =~ /^.*,(\d+)$/
367         munin = "#{device}-#{Regexp.last_match(1)}"
368       elsif smart =~ %r{^.*,(\d+)/(\d+)$}
369         munin = "#{device}-#{Regexp.last_match(1)}:#{Regexp.last_match(2)}"
370       end
371     end
372   elsif disk[:device]
373     device = disk[:device].sub("/dev/", "")
374     munin = device
375   end
376
377   next if device.nil?
378
379   Hash[
380     :device => device,
381     :smart => smart,
382     :munin => munin,
383     :hddtemp => munin.tr("-:", "_")
384   ]
385 end
386
387 smartd_service = if node[:lsb][:release].to_f >= 16.04
388                    "smartd"
389                  else
390                    "smartmontools"
391                  end
392
393 disks = disks.compact
394
395 if disks.count.positive?
396   package "smartmontools"
397
398   template "/usr/local/bin/smartd-mailer" do
399     source "smartd-mailer.erb"
400     owner "root"
401     group "root"
402     mode 0o755
403   end
404
405   template "/etc/smartd.conf" do
406     source "smartd.conf.erb"
407     owner "root"
408     group "root"
409     mode 0o644
410     variables :disks => disks
411   end
412
413   template "/etc/default/smartmontools" do
414     source "smartmontools.erb"
415     owner "root"
416     group "root"
417     mode 0o644
418   end
419
420   service smartd_service do
421     action [:enable, :start]
422     subscribes :reload, "template[/etc/smartd.conf]"
423     subscribes :restart, "template[/etc/default/smartmontools]"
424   end
425
426   # Don't try and do munin monitoring of disks behind
427   # an Areca controller as they only allow one thing to
428   # talk to the controller at a time and smartd will
429   # throw errors if it clashes with munin
430   disks = disks.reject { |disk| disk[:smart]&.start_with?("areca,") }
431
432   disks.each do |disk|
433     munin_plugin "smart_#{disk[:munin]}" do
434       target "smart_"
435       conf "munin.smart.erb"
436       conf_variables :disk => disk
437     end
438   end
439 else
440   service smartd_service do
441     action [:stop, :disable]
442   end
443 end
444
445 if disks.count.positive?
446   munin_plugin "hddtemp_smartctl" do
447     conf "munin.hddtemp.erb"
448     conf_variables :disks => disks
449   end
450 else
451   munin_plugin "hddtemp_smartctl" do
452     action :delete
453     conf "munin.hddtemp.erb"
454   end
455 end
456
457 plugins = Dir.glob("/etc/munin/plugins/smart_*").map { |p| File.basename(p) } -
458           disks.map { |d| "smart_#{d[:munin]}" }
459
460 plugins.each do |plugin|
461   munin_plugin plugin do
462     action :delete
463   end
464 end
465
466 if File.exist?("/etc/mdadm/mdadm.conf")
467   mdadm_conf = edit_file "/etc/mdadm/mdadm.conf" do |line|
468     line.gsub!(/^MAILADDR .*$/, "MAILADDR admins@openstreetmap.org")
469
470     line
471   end
472
473   file "/etc/mdadm/mdadm.conf" do
474     owner "root"
475     group "root"
476     mode 0o644
477     content mdadm_conf
478   end
479
480   service "mdadm" do
481     action :nothing
482     subscribes :restart, "file[/etc/mdadm/mdadm.conf]"
483   end
484 end
485
486 template "/etc/modules" do
487   source "modules.erb"
488   owner "root"
489   group "root"
490   mode 0o644
491 end
492
493 service "kmod" do
494   action :nothing
495   subscribes :start, "template[/etc/modules]"
496 end
497
498 if node[:hardware][:watchdog]
499   package "watchdog"
500
501   template "/etc/default/watchdog" do
502     source "watchdog.erb"
503     owner "root"
504     group "root"
505     mode 0o644
506     variables :module => node[:hardware][:watchdog]
507   end
508
509   service "watchdog" do
510     action [:enable, :start]
511   end
512 end
513
514 unless Dir.glob("/sys/class/hwmon/hwmon*").empty?
515   package "lm-sensors"
516
517   Dir.glob("/sys/devices/platform/coretemp.*").each do |coretemp|
518     cpu = File.basename(coretemp).sub("coretemp.", "").to_i
519     chip = format("coretemp-isa-%04d", cpu)
520
521     temps = if File.exist?("#{coretemp}/name")
522               Dir.glob("#{coretemp}/temp*_input").map do |temp|
523                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
524               end.sort
525             else
526               Dir.glob("#{coretemp}/hwmon/hwmon*/temp*_input").map do |temp|
527                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
528               end.sort
529             end
530
531     if temps.first == 1
532       node.default[:hardware][:sensors][chip][:temps][:temp1][:label] = "CPU #{cpu}"
533       temps.shift
534     end
535
536     temps.each_with_index do |temp, index|
537       node.default[:hardware][:sensors][chip][:temps]["temp#{temp}"][:label] = "CPU #{cpu} Core #{index}"
538     end
539   end
540
541   execute "/etc/sensors.d/chef.conf" do
542     action :nothing
543     command "/usr/bin/sensors -s"
544     user "root"
545     group "root"
546   end
547
548   template "/etc/sensors.d/chef.conf" do
549     source "sensors.conf.erb"
550     owner "root"
551     group "root"
552     mode 0o644
553     notifies :run, "execute[/etc/sensors.d/chef.conf]"
554   end
555 end