summaryrefslogtreecommitdiffstats
path: root/lib/facter/gluster_ports.rb
blob: 209538a57a64155665148a55e39a0d46f6dab4fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# GlusterFS module by James
# Copyright (C) 2010-2013+ James Shubin
# Written by James Shubin <james@shubin.ca>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

require 'facter'

# get the gluster path. this fact comes from an external fact set in: params.pp
gluster = Facter.value('gluster_program_gluster').to_s.chomp
if gluster == ''
	gluster = `which gluster 2> /dev/null`.chomp
	if gluster == ''
		gluster = '/usr/sbin/gluster'
	end
end

# find the module_vardir
dir = Facter.value('puppet_vardirtmp')		# nil if missing
if dir.nil?					# let puppet decide if present!
	dir = Facter.value('puppet_vardir')
	if dir.nil?
		var = nil
	else
		var = dir.gsub(/\/$/, '')+'/'+'tmp/'	# ensure trailing slash
	end
else
	var = dir.gsub(/\/$/, '')+'/'
end

if var.nil?
	# if we can't get a valid vardirtmp, then we can't continue
	xmlfile = nil
else
	module_vardir = var+'gluster/'
	xmlfile = module_vardir+'xml.py'
end

host = Facter.value('fqdn')
found = {}

# we need the script installed first to be able to generate the port facts...
if not(xmlfile.nil?) and File.exist?(xmlfile)
	volumes = `#{gluster} volume list`
	if $?.exitstatus == 0
		volumes.split.each do |x|
			# values come out as comma separated strings for direct usage
			cmd = gluster+' volume status --xml | '+xmlfile+" ports --volume '"+x+"' --host '"+host+"'"
			result = `#{cmd}`
			if $?.exitstatus == 0
				found[x] = result
			# TODO: else, print warning
			end
		end
	# TODO: else, print warning
	end
end

found.keys.each do |x|
	Facter.add('gluster_ports_volume_'+x) do
		#confine :operatingsystem => %w{CentOS, RedHat, Fedora}
		setcode {
			# don't reuse single variable to avoid bug #:
			# http://projects.puppetlabs.com/issues/22455
			found[x]
		}
	end
end

# list of generated gluster_ports_volume's
Facter.add('gluster_ports_volumes_facts') do
	#confine :operatingsystem => %w{CentOS, RedHat, Fedora}
	setcode {
		found.keys.collect {|x| 'gluster_ports_volume_'+x }.join(',')
	}
end

# vim: ts=8