]> git.openstreetmap.org Git - chef.git/blob - cookbooks/hardware/recipes/default.rb
Start serial console on ttyS2 for OVH machines
[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 elsif node[:roles].include?("ovh")
56   units << "2"
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 "--force-yes"
68     notifies :restart, "service[hp-health]"
69   end
70
71   service "hp-health" do
72     action [:enable, :start]
73     supports :status => true, :restart => 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 package "ipmitool" if node[:kernel][:modules].include?("ipmi_si")
188
189 if node[:lsb][:release].to_f >= 12.10
190   package "irqbalance"
191
192   template "/etc/default/irqbalance" do
193     source "irqbalance.erb"
194     owner "root"
195     group "root"
196     mode 0o644
197   end
198
199   service "irqbalance" do
200     action [:start, :enable]
201     supports :status => false, :restart => true, :reload => false
202     subscribes :restart, "template[/etc/default/irqbalance]"
203   end
204 end
205
206 tools_packages = []
207 status_packages = {}
208
209 node[:kernel][:modules].each_key do |modname|
210   case modname
211   when "cciss"
212     tools_packages << "hpssacli"
213     status_packages["cciss-vol-status"] ||= []
214   when "hpsa"
215     tools_packages << "hpssacli"
216     status_packages["cciss-vol-status"] ||= []
217   when "mptsas"
218     tools_packages << "lsiutil"
219     # status_packages["mpt-status"] ||= []
220   when "mpt2sas", "mpt3sas"
221     tools_packages << "sas2ircu"
222     status_packages["sas2ircu-status"] ||= []
223   when "megaraid_mm"
224     tools_packages << "megactl"
225     status_packages["megaraid-status"] ||= []
226   when "megaraid_sas"
227     tools_packages << "megacli"
228     status_packages["megaclisas-status"] ||= []
229   when "aacraid"
230     tools_packages << "arcconf"
231     status_packages["aacraid-status"] ||= []
232   when "arcmsr"
233     tools_packages << "areca"
234   end
235 end
236
237 node[:block_device].each do |name, attributes|
238   next unless attributes[:vendor] == "HP" && attributes[:model] == "LOGICAL VOLUME"
239
240   if name =~ /^cciss!(c[0-9]+)d[0-9]+$/
241     status_packages["cciss-vol-status"] |= ["cciss/#{Regexp.last_match[1]}d0"]
242   else
243     Dir.glob("/sys/block/#{name}/device/scsi_generic/*").each do |sg|
244       status_packages["cciss-vol-status"] |= [File.basename(sg)]
245     end
246   end
247 end
248
249 %w(hpssacli lsiutil sas2ircu megactl megacli arcconf).each do |tools_package|
250   if tools_packages.include?(tools_package)
251     package tools_package
252   else
253     package tools_package do
254       action :purge
255     end
256   end
257 end
258
259 if tools_packages.include?("areca")
260   include_recipe "git"
261
262   git "/opt/areca" do
263     action :sync
264     repository "git://chef.openstreetmap.org/areca.git"
265     user "root"
266     group "root"
267   end
268 else
269   directory "/opt/areca" do
270     action :delete
271     recursive true
272   end
273 end
274
275 ["cciss-vol-status", "mpt-status", "sas2ircu-status", "megaraid-status", "megaclisas-status", "aacraid-status"].each do |status_package|
276   if status_packages.include?(status_package)
277     package status_package
278
279     template "/etc/default/#{status_package}d" do
280       source "raid.default.erb"
281       owner "root"
282       group "root"
283       mode 0o644
284       variables :devices => status_packages[status_package]
285     end
286
287     service "#{status_package}d" do
288       action [:start, :enable]
289       supports :status => false, :restart => true, :reload => false
290       subscribes :restart, "template[/etc/default/#{status_package}d]"
291     end
292   else
293     package status_package do
294       action :purge
295     end
296
297     file "/etc/default/#{status_package}d" do
298       action :delete
299     end
300   end
301 end
302
303 disks = if node[:hardware][:disk]
304           node[:hardware][:disk][:disks]
305         else
306           []
307         end
308
309 intel_ssds = disks.select { |d| d[:vendor] == "INTEL" && d[:model] =~ /^SSD/ }
310
311 nvmes = if node[:hardware][:pci]
312           node[:hardware][:pci].values.select { |pci| pci[:driver] == "nvme" }
313         else
314           []
315         end
316
317 intel_nvmes = nvmes.select { |pci| pci[:vendor_name] == "Intel Corporation" }
318
319 if !intel_ssds.empty? || !intel_nvmes.empty?
320   package "unzip"
321   package "alien"
322
323   remote_file "#{Chef::Config[:file_cache_path]}/DataCenterTool_3_0_0_Linux.zip" do
324     source "https://downloadmirror.intel.com/23931/eng/DataCenterTool_3_0_0_Linux.zip"
325   end
326
327   execute "unzip-DataCenterTool" do
328     command "unzip DataCenterTool_3_0_0_Linux.zip isdct-3.0.0.400-15.x86_64.rpm"
329     cwd Chef::Config[:file_cache_path]
330     user "root"
331     group "root"
332     not_if { File.exist?("#{Chef::Config[:file_cache_path]}/isdct-3.0.0.400-15.x86_64.rpm") }
333   end
334
335   execute "alien-isdct" do
336     command "alien --to-deb isdct-3.0.0.400-15.x86_64.rpm"
337     cwd Chef::Config[:file_cache_path]
338     user "root"
339     group "root"
340     not_if { File.exist?("#{Chef::Config[:file_cache_path]}/isdct_3.0.0.400-16_amd64.deb") }
341   end
342
343   dpkg_package "isdct" do
344     source "#{Chef::Config[:file_cache_path]}/isdct_3.0.0.400-16_amd64.deb"
345   end
346 end
347
348 disks = disks.map do |disk|
349   next if disk[:state] == "spun_down"
350
351   if disk[:smart_device]
352     controller = node[:hardware][:disk][:controllers][disk[:controller]]
353     device = controller[:device].sub("/dev/", "")
354     smart = disk[:smart_device]
355
356     if device.start_with?("cciss/") && smart =~ /^cciss,(\d+)$/
357       array = node[:hardware][:disk][:arrays][disk[:arrays].first]
358       munin = "cciss-3#{array[:wwn]}-#{Regexp.last_match(1)}"
359     elsif smart =~ /^.*,(\d+)$/
360       munin = "#{device}-#{Regexp.last_match(1)}"
361     elsif smart =~ %r{^.*,(\d+)/(\d+)$}
362       munin = "#{device}-#{Regexp.last_match(1)}:#{Regexp.last_match(2)}"
363     end
364   elsif disk[:device]
365     device = disk[:device].sub("/dev/", "")
366     munin = device
367   end
368
369   next if device.nil?
370
371   Hash[
372     :device => device,
373     :smart => smart,
374     :munin => munin,
375     :hddtemp => munin.tr("-:", "_")
376   ]
377 end
378
379 smartd_service = if node[:lsb][:release].to_f >= 16.04
380                    "smartd"
381                  else
382                    "smartmontools"
383                  end
384
385 disks = disks.compact
386
387 if disks.count > 0
388   package "smartmontools"
389
390   template "/usr/local/bin/smartd-mailer" do
391     source "smartd-mailer.erb"
392     owner "root"
393     group "root"
394     mode 0o755
395   end
396
397   template "/etc/smartd.conf" do
398     source "smartd.conf.erb"
399     owner "root"
400     group "root"
401     mode 0o644
402     variables :disks => disks
403   end
404
405   template "/etc/default/smartmontools" do
406     source "smartmontools.erb"
407     owner "root"
408     group "root"
409     mode 0o644
410   end
411
412   service smartd_service do
413     action [:enable, :start]
414     subscribes :reload, "template[/etc/smartd.conf]"
415     subscribes :restart, "template[/etc/default/smartmontools]"
416   end
417
418   # Don't try and do munin monitoring of disks behind
419   # an Areca controller as they only allow one thing to
420   # talk to the controller at a time and smartd will
421   # throw errors if it clashes with munin
422   disks = disks.reject { |disk| disk[:smart] && disk[:smart].start_with?("areca,") }
423
424   disks.each do |disk|
425     munin_plugin "smart_#{disk[:munin]}" do
426       target "smart_"
427       conf "munin.smart.erb"
428       conf_variables :disk => disk
429     end
430   end
431 else
432   service smartd_service do
433     action [:stop, :disable]
434   end
435 end
436
437 if disks.count > 0
438   munin_plugin "hddtemp_smartctl" do
439     conf "munin.hddtemp.erb"
440     conf_variables :disks => disks
441   end
442 else
443   munin_plugin "hddtemp_smartctl" do
444     action :delete
445     conf "munin.hddtemp.erb"
446   end
447 end
448
449 plugins = Dir.glob("/etc/munin/plugins/smart_*").map { |p| File.basename(p) } -
450           disks.map { |d| "smart_#{d[:munin]}" }
451
452 plugins.each do |plugin|
453   munin_plugin plugin do
454     action :delete
455   end
456 end
457
458 if File.exist?("/etc/mdadm/mdadm.conf")
459   mdadm_conf = edit_file "/etc/mdadm/mdadm.conf" do |line|
460     line.gsub!(/^MAILADDR .*$/, "MAILADDR admins@openstreetmap.org")
461
462     line
463   end
464
465   file "/etc/mdadm/mdadm.conf" do
466     owner "root"
467     group "root"
468     mode 0o644
469     content mdadm_conf
470   end
471
472   service "mdadm" do
473     action :nothing
474     subscribes :restart, "file[/etc/mdadm/mdadm.conf]"
475   end
476 end
477
478 template "/etc/modules" do
479   source "modules.erb"
480   owner "root"
481   group "root"
482   mode 0o644
483 end
484
485 if node[:lsb][:release].to_f <= 12.10
486   service "module-init-tools" do
487     provider Chef::Provider::Service::Upstart
488     action :nothing
489     subscribes :start, "template[/etc/modules]"
490   end
491 else
492   service "kmod" do
493     if node[:lsb][:release].to_f >= 15.10
494       provider Chef::Provider::Service::Systemd
495     else
496       provider Chef::Provider::Service::Upstart
497     end
498     action :nothing
499     subscribes :start, "template[/etc/modules]"
500   end
501 end
502
503 if node[:hardware][:watchdog]
504   package "watchdog"
505
506   template "/etc/default/watchdog" do
507     source "watchdog.erb"
508     owner "root"
509     group "root"
510     mode 0o644
511     variables :module => node[:hardware][:watchdog]
512   end
513
514   service "watchdog" do
515     action [:enable, :start]
516   end
517 end
518
519 unless Dir.glob("/sys/class/hwmon/hwmon*").empty?
520   package "lm-sensors"
521
522   Dir.glob("/sys/devices/platform/coretemp.*").each do |coretemp|
523     cpu = File.basename(coretemp).sub("coretemp.", "").to_i
524     chip = format("coretemp-isa-%04d", cpu)
525
526     temps = if File.exist?("#{coretemp}/name")
527               Dir.glob("#{coretemp}/temp*_input").map do |temp|
528                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
529               end.sort
530             else
531               Dir.glob("#{coretemp}/hwmon/hwmon*/temp*_input").map do |temp|
532                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
533               end.sort
534             end
535
536     if temps.first == 1
537       node.default[:hardware][:sensors][chip][:temps][:temp1][:label] = "CPU #{cpu}"
538       temps.shift
539     end
540
541     temps.each_with_index do |temp, index|
542       node.default[:hardware][:sensors][chip][:temps]["temp#{temp}"][:label] = "CPU #{cpu} Core #{index}"
543     end
544   end
545
546   execute "/etc/sensors.d/chef.conf" do
547     action :nothing
548     command "/usr/bin/sensors -s"
549     user "root"
550     group "root"
551   end
552
553   template "/etc/sensors.d/chef.conf" do
554     source "sensors.conf.erb"
555     owner "root"
556     group "root"
557     mode 0o644
558     notifies :run, "execute[/etc/sensors.d/chef.conf]"
559   end
560 end