summaryrefslogtreecommitdiffstats
path: root/lib/facter/gluster_uuid.rb
blob: 78f137d7de152ca9ea09609a590073fa0117d4e6 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# 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'

# TODO: the ruby uuid method can be used when newer ruby versions are used here
# require 'securerandom'
# SecureRandom.uuid

# uuid regexp
regexp = /^[a-f0-9]{8}\-[a-f0-9]{4}\-[a-f0-9]{4}\-[a-f0-9]{4}\-[a-f0-9]{12}$/

# 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
	uuidfile = nil
else
	module_vardir = var+'gluster/'
	uuiddir = module_vardir+'uuid/'	# safe dir that won't get purged...
	uuidfile = uuiddir+'uuid'
end

# NOTE: module specific mkdirs, needed to ensure there is no blocking/deadlock!
if not(var.nil?) and not File.directory?(var)
	Dir::mkdir(var)
end

if not(module_vardir.nil?) and not File.directory?(module_vardir)
	Dir::mkdir(module_vardir)
end

if not(uuiddir.nil?) and not File.directory?(uuiddir)
	Dir::mkdir(uuiddir)
end

# generate uuid and parent directory if they don't already exist...
if not(module_vardir.nil?) and File.directory?(module_vardir)
	if not File.directory?(uuiddir)
		Dir::mkdir(uuiddir)
	end

	# create a uuid and store it in our vardir if it doesn't already exist!
	if File.directory?(uuiddir) and (not File.exist?(uuidfile))
		result = system("/usr/bin/uuidgen > '" + uuidfile + "'")
		if not(result)
			# TODO: print warning
		end
	end
end

# create the fact if the uuid file contains a valid uuid
if not(uuidfile.nil?) and File.exist?(uuidfile)
	uuid = File.open(uuidfile, 'r').read.strip.downcase	# read into str
	# skip over uuid's of the wrong length or that don't match (security!!)
	if uuid.length == 36 and regexp.match(uuid)
		Facter.add('gluster_uuid') do
			#confine :operatingsystem => %w{CentOS, RedHat, Fedora}
			setcode {
				# don't reuse uuid variable to avoid bug #:
				# http://projects.puppetlabs.com/issues/22455
				uuid
			}
		end
	# TODO: print warning on else...
	end
end

# create facts from externally collected uuid files
_uuid = ''
found = {}
prefix = 'uuid_'
if not(uuiddir.nil?) and File.directory?(uuiddir)
	Dir.glob(uuiddir+prefix+'*').each do |f|

		b = File.basename(f)
		# strip off leading prefix
		fqdn = b[prefix.length, b.length-prefix.length]

		_uuid = File.open(f, 'r').read.strip.downcase	# read into str
		if _uuid.length == 36 and regexp.match(_uuid)
			# avoid: http://projects.puppetlabs.com/issues/22455
			found[fqdn] = _uuid
		# TODO: print warning on else...
		end
	end
end

found.keys.each do |x|
	Facter.add('gluster_uuid_'+x) do
		#confine :operatingsystem => %w{CentOS, RedHat, Fedora}
		setcode {
			found[x]
		}
	end
end

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

Facter.add('gluster_fqdns') do
	#confine :operatingsystem => %w{CentOS, RedHat, Fedora}
	setcode {
		found.keys.sort.join(',')
	}
end

# vim: ts=8