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