summaryrefslogtreecommitdiffstats
path: root/lib/facter/gluster_vrrp.rb
blob: 79676e6713d2c82362e52ea55c1ae4be4b8a2461 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# 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'
require 'digest/sha1'
require 'ipaddr'

length = 16
# pass regexp
regexp = /^[a-zA-Z0-9]{#{length}}$/
ipregexp = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
netmaskregexp = /^(((128|192|224|240|248|252|254)\.0\.0\.0)|(255\.(0|128|192|224|240|248|252|254)\.0\.0)|(255\.255\.(0|128|192|224|240|248|252|254)\.0)|(255\.255\.255\.(0|128|192|224|240|248|252|254)))$/
chars = [('a'..'z'), ('A'..'Z'), (0..9)].map { |i| i.to_a }.flatten


# 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
	module_vardir = nil
	vrrpdir = nil
	vrrpfile = nil
	ipfile = nil
else
	module_vardir = var+'gluster/'
	vrrpdir = module_vardir+'vrrp/'
	vrrpfile = vrrpdir+'vrrp'
	ipfile = vrrpdir+'ip'
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(vrrpdir.nil?) and not File.directory?(vrrpdir)
	Dir::mkdir(vrrpdir)
end

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

	# create a pass and store it in our vardir if it doesn't already exist!
	if File.directory?(vrrpdir) and ((not File.exist?(vrrpfile)) or (File.size(vrrpfile) == 0))
		# include a built-in pwgen-like backup
		string = (0..length-1).map { chars[rand(chars.length)] }.join
		result = system("(/usr/bin/test -z /usr/bin/pwgen && /usr/bin/pwgen -N 1 #{length} || /bin/echo '#{string}') > '" + vrrpfile + "'")
		if not(result)
			# TODO: print warning
		end
	end
end

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

# create facts from externally collected vrrp files
_pass = ''
found = {}
prefix = 'vrrp_'
if not(vrrpdir.nil?) and File.directory?(vrrpdir)
	Dir.glob(vrrpdir+prefix+'*').each do |f|

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

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

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

#Facter.add('gluster_vrrp_facts') do
#	#confine :operatingsystem => %w{CentOS, RedHat, Fedora}
#	setcode {
#		found.keys.collect {|x| 'gluster_vrrp_'+x }.join(',')
#	}
#end

# distributed password (uses a piece from each host)
collected = found.keys.sort.collect {|x| found[x] }.join('#')	# combine pieces
Facter.add('gluster_vrrp_password') do
	#confine :operatingsystem => %w{CentOS, RedHat, Fedora}
	setcode {
		Digest::SHA1.hexdigest(collected)
	}
end

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

# create these facts if the ip file contains a valid ip address
if not(ipfile.nil?) and File.exist?(ipfile)
	ip = File.open(ipfile, 'r').read.strip.downcase	# read into str
	# skip over ip that doesn't match (security!!)
	if ipregexp.match(ip)

		# TODO: replace with system-getifaddrs if i can get it working!
		cmd = "/sbin/ip -o a show to #{ip} | /bin/awk '{print $2}'"
		interface = `#{cmd}`.strip
		if $?.exitstatus == 0 and interface.length > 0

			Facter.add('gluster_vrrp_interface') do
				#confine :operatingsystem => %w{CentOS, RedHat, Fedora}
				setcode {
					interface
				}
			end

			# lookup from fact
			netmask = Facter.value('netmask_'+interface)
			if netmaskregexp.match(netmask)

				Facter.add('gluster_vrrp_netmask') do
					#confine :operatingsystem => %w{CentOS, RedHat, Fedora}
					setcode {
						netmask
					}
				end

				cidr = IPAddr.new("#{netmask}").to_i.to_s(2).count('1')
				Facter.add('gluster_vrrp_cidr') do
					#confine :operatingsystem => %w{CentOS, RedHat, Fedora}
					setcode {
						cidr
					}
				end
			end

		# TODO: print warning on else...
		end

	# TODO: print warning on else...
	end
end

# vim: ts=8