I’m tryin to check if the drive is in the state as shown above and if not make necessary changes in chef.
Now I have my helper
module MyHelper
require 'mixlib/shellout'
# IS IT XFS?
def has_xfs_partition?(block_device)
cmd = Mixlib::ShellOut.new("lsblk -no FSTYPE #{block_device} | grep xfs")
cmd.run_command
puts "block_device: " + block_device
puts "cmd.stderr: " + cmd.stderr
puts "cmd.stdout: " + cmd.stdout
puts "cmd.status: " + cmd.status.to_s
puts "cmd.exitstatus: " + cmd.exitstatus.to_s
if !cmd.stderr.empty?
raise cmd.stderr
end
if cmd.stdout.to_s.empty?
Chef::Log.info("#{block_device}: NO XFS ")
return false
else
Chef::Log.info("#{block_device}: XFS")
return true
end
end
# DOES IT HAVE GPT?
def has_partition_table?(block_device)
cmd = Mixlib::ShellOut.new("parted -s #{block_device} print 2>/dev/null|grep -c 'Partition Table: gpt'")
cmd.run_command
puts "block_device: " + block_device
puts "cmd.stderr: " + cmd.stderr
puts "cmd.stdout: " + cmd.stdout
puts "cmd.status: " + cmd.status.to_s
puts "cmd.exitstatus: " + cmd.exitstatus.to_s
unless cmd.stderr.empty?
raise cmd.stderr
end
if cmd.stdout.to_i != 0
Chef::Log.info("YES")
return true
else
Chef::Log.info("NO")
return false
end
end
end
And I use these functions in the following simple recipe
# v['dev_path'] contains my device, here /dev/sdb
# DO WE HAVE GPT ON THE DEVICE?
Chef::Log.info("LAUNCHING has_partition_table?#{v['dev_path']}")
unless has_partition_table?(v['dev_path'])
puts "IT DIDNT HAVE PARTITION TABLE. HERE DOING SOMETHING"
end
Chef::Log.info("LAUNCHING has_xfs_partition?(#{v['dev_path']}1)")
unless has_xfs_partition?("#{v['dev_path']}1")
puts "#{v['dev_path']}1 WAS NOT XFS FORMATTED. HERE DOING SOMETHING"
else
puts "#{v['dev_path']}1 IS XFS FORMATTED"
end
Now when I run chef-client I get dissapointing result
I probably made some stupid mistake but unfortunately I can’t figure out where. As you see when I run these two commands directly in console everything’s fine but when I try to run them both in a recipe they fail. Why it’s OK to run them separately?
Any reason to not use ohai data ?
Something along the line node['filesystem2']['by_device'].include?("/dev/sdb1") to check the partition exists and node['filesystem2']['by_device']['/dev/sdb1']['fs_type'] == 'xfs' to check the formatting sounds enough to me.
Anyway as you see in the log I provided I’m printing cmd.stdout and it’s empty. So is stderr and in status you see the proces exited with code 1. No idea why, though.
You’re printing stdout of a pipeline where you filter the output to contain only xfs, so obvisouly it’s empty if there’s not xfs in the line but we have no idea what it does contain at first.
The exit status is 1 because either lsblk did fail or because grep didn’t find xfs in the output of lsblk, with a pipeline it’s impossible to know which one did return 1.
When I do it myself
[root@myhost ~]# lsblk -no FSTYPE /dev/sdb1
xfs
lsblk clearly gives me some output
I don't know why but stdout is empty and empty string | grep "something" returns 1. That's why I had code=1 before. Still no idea why stdout is empty. The same code launched without first function (one which is checking for GPT) works just fine :/.
With recipe which has only has_xfs_partition?("#{v['dev_path']}1")
instead of
has_partition_table?(v['dev_path']) and
has_xfs_partition?("#{v['dev_path']}1")
I get the following output which is correct.
I always forgot it, instead of .delete("\n") you can use .chomp. No need for replacing in place (the ! version).[quote="veluu, post:7, topic:9958"]
[2016-12-01T13:20:29+01:00] INFO: LAUNCHING has_xfs_partition?(/dev/sdb1)
block_device:/dev/sdb1
block_device: /dev/sdb1
[/quote]
Why do you have two times the block_device line? The output you're showing doesn't match your code, there's something unclean here.
I can't reproduce your case... so I've really no idea of why this is happening. sorry
I added one more print before doing cmd.stdout. For a second I had something on my mind and wanted to printout parameter before I actually do something.
It’s a pitty you don’t have any chef instance available to reproduce. Long story short.
When I launch:
has_partition_table?
has_xfs_partition?
stdout in the second function is empty.
If I comment out has_partition_table? so in my recipe there’s only:
has_xfs_partition?
stdout is not empty any more and has desired value.
You didn’t understand me, when I say I can’t repro, I did copy/paste your code in a cookbook and used it, and I can’t get this behavior in chef 11.16, chef 12.4 or chef 12.14. I don’t have isntances with gpt partitions, but that shouldn’t change much. So I really don’t get what’s going on with your case.
Gives me: lsblk: /dev/sdb1: not a block device
Either I have to add sleep 1s between these two command or use blkid instead of parted which works without using sleep. My problem had nothing to do with chef. Thank you for your time.