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