Skip to content

Commit 548baa7

Browse files
committed
Use xml output of gluster for retrieving facts
1 parent 5c2e385 commit 548baa7

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

lib/facter/gluster.rb

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
peer_count = 0
21
peer_list = ''
32
volume_bricks = {}
43
volume_options = {}
@@ -25,28 +24,24 @@
2524
binary
2625
end
2726
end
28-
output = Facter::Util::Resolution.exec("#{binary} peer status")
29-
peer_count = Regexp.last_match[1].to_i if output =~ %r{^Number of Peers: (\d+)$}
27+
require 'rexml/document'
28+
29+
peer_status = REXML::Document.new(Facter::Util::Resolution.exec("#{binary} peer status --xml"))
30+
peers = REXML::XPath.match(peer_status, '/cliOutput/peerStatus/peer/hostname/text()')
31+
peer_count = peers.size
3032
if peer_count > 0
31-
peer_list = output.scan(%r{^Hostname: (.+)$}).flatten.join(',')
32-
# note the stderr redirection here
33-
# `gluster volume list` spits to stderr :(
34-
output = Facter::Util::Resolution.exec("#{binary} volume list 2>&1")
35-
if output != 'No volumes present in cluster'
36-
output.split.each do |vol|
37-
info = Facter::Util::Resolution.exec("#{binary} volume info #{vol}")
38-
# rubocop:disable Metrics/BlockNesting
39-
vol_status = Regexp.last_match[1] if info =~ %r{^Status: (.+)$}
40-
bricks = info.scan(%r{^Brick[^:]+: (.+)$}).flatten
41-
volume_bricks[vol] = bricks
42-
options = info.scan(%r{^(\w+\.[^:]+: .+)$}).flatten
43-
volume_options[vol] = options if options
44-
next unless vol_status == 'Started'
45-
status = Facter::Util::Resolution.exec("#{binary} volume status #{vol} 2>/dev/null")
46-
if status =~ %r{^Brick}
47-
volume_ports[vol] = status.scan(%r{^Brick [^\t]+\t+(\d+)}).flatten.uniq.sort
48-
end
49-
end
33+
peer_list = peers.join(',')
34+
volumes = REXML::Document.new(Facter::Util::Resolution.exec("#{binary} volume info --xml"))
35+
REXML::XPath.match(volumes, '/cliOutput/volInfo/volumes/volume').each do |vol|
36+
vol_name = vol.elements['name'].text
37+
vol_status = vol.elements['statusStr'].text
38+
bricks = REXML::XPath.match(vol, 'bricks/brick/name/text()')
39+
volume_bricks[vol_name] = bricks
40+
options = REXML::XPath.match(vol, 'options/option').map { |option| "#{option.elements['name'].text}: #{option.elements['value'].text}" }
41+
volume_options[vol_name] = options if options
42+
next unless vol_status == 'Started'
43+
status = REXML::Document.new(Facter::Util::Resolution.exec("#{binary} volume status #{vol_name} --xml"))
44+
volume_ports[vol_name] = REXML::XPath.match(status, "/cliOutput/volStatus/volumes/volume/node[starts-with(hostname/text(), '#{Facter.value('hostname')}')]/port/text()")
5045
end
5146
end
5247

@@ -78,6 +73,7 @@
7873
end
7974
end
8075
end
76+
# rubocop:disable Metrics/BlockNesting
8177
if volume_options
8278
volume_options.each do |vol, opts|
8379
Facter.add("gluster_volume_#{vol}_options".to_sym) do

0 commit comments

Comments
 (0)