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