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