Hello
Using a custom fact (see below) generates messages during the puppet runs like
this any ideas how I can stop it ?:-
./ipfacts.rb:32: warning: already initialized constant REGEX_MAP
ipfacts.rb:-
ENV["PATH"]="/bin:/sbin:/usr/bin:/usr/sbin"
# A map of all the different regexes that work for
# a given platform or set of platforms.
REGEX_MAP = {
:linux => {
:ipaddress => /inet addr:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/,
:macaddress =>
/(?:ether|HWaddr)\s+(\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2})/,
:netmask => /Mask:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/
},
:bsd => {
:aliases => [:openbsd, :netbsd, :freebsd, :darwin],
:ipaddress => /inet\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/,
:macaddress =>
/(?:ether|lladdr)\s+(\w\w:\w\w:\w\w:\w\w:\w\w:\w\w)/,
:netmask => /netmask\s+0x(\w{8})/
},
:sunos => {
:addr => /inet\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/,
:ipaddress => /inet\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/,
:macaddress =>
/(?:ether|lladdr)\s+(\w?\w:\w?\w:\w?\w:\w?\w:\w?\w:\w?\w)/,
:netmask => /netmask\s+(\w{8})/
}
}
# Convert an interface name into purely alpha characters.
def alphafy(interface)
interface.gsub(/[:.]/, ''_'')
end
def convert_from_hex?(kernel)
kernels_to_convert = [:sunos, :openbsd, :netbsd, :freebsd, :darwin]
kernels_to_convert.include?(kernel)
end
def supported_platforms
REGEX_MAP.inject([]) do |result, tmp|
key, map = tmp
if map[:aliases]
result += map[:aliases]
else
result << key
end
result
end
end
def get_interfaces
int = nil
output = get_all_interface_output()
# We get lots of warnings on platforms that don''t get an output
# made.
if output
int = output.scan(/^\w+[.:]?\d+/).find_all { |i| i !~ /^lo[0:]?/ }
else
[]
end
end
def get_all_interface_output
case Facter.value(:kernel)
when ''Linux'', ''OpenBSD'',
''NetBSD'', ''FreeBSD'',
''Darwin''
output = %x{/sbin/ifconfig -a}
when ''SunOS''
output = %x{/usr/sbin/ifconfig -a}
end
output
end
def get_single_interface_output(interface)
output = ""
case Facter.value(:kernel)
when ''Linux'', ''OpenBSD'',
''NetBSD'', ''FreeBSD'',
''Darwin''
output = %x{/sbin/ifconfig #{interface}}
when ''SunOS''
output = %x{/usr/sbin/ifconfig #{interface}}
end
output
end
def get_bonding_master(interface)
if Facter.value(:kernel) != ''Linux''
return nil
end
# We need ip instead of ifconfig because it will show us
# the bonding master device.
if not FileTest.executable?("/sbin/ip")
return nil
end
regex = /SLAVE[,>].* (bond[0-9]+)/
ethbond = regex.match(%x{/sbin/ip link show #{interface}})
if ethbond
device = ethbond[1]
else
device = nil
end
device
end
def get_interface_value(interface, label)
tmp1 = []
kernel = Facter.value(:kernel).downcase.to_sym
# If it''s not directly in the map or aliased in the map, then
we don''t know how to deal with it.
unless map = REGEX_MAP[kernel] || REGEX_MAP.values.find { |tmp|
tmp[:aliases] and tmp[:aliases].include?(kernel) }
return []
end
# Pull the correct regex out of the map.
regex = map[label.to_sym]
# Linux changes the MAC address reported via ifconfig when an ethernet
interface
# becomes a slave of a bonding device to the master MAC address.
# We have to dig a bit to get the original/real MAC address of the
interface.
bonddev = get_bonding_master(interface)
if label == ''macaddress'' and bonddev
bondinfo = IO.readlines("/proc/net/bonding/#{bonddev}")
hwaddrre = /^Slave Interface: #{interface}\n[^\n].+?\nPermanent HW
addr: (([0-9a-fA-F]{2}:?)*)$/m
value = hwaddrre.match(bondinfo.to_s)[1].upcase
else
output_int = get_single_interface_output(interface)
if interface != /^lo[0:]?\d?/
output_int.each do |s|
if s =~ regex
value = $1
if label == ''netmask'' &&
convert_from_hex?(kernel)
value = value.scan(/../).collect do |byte|
byte.to_i(16) end.join(''.'')
end
tmp1.push(value)
end
end
end
if tmp1
value = tmp1.shift
end
end
end
def get_network_value(interface)
require ''ipaddr''
ipaddress = get_interface_value(interface, "ipaddress")
netmask = get_interface_value(interface, "netmask")
if ipaddress && netmask
ip = IPAddr.new(ipaddress, Socket::AF_INET)
subnet = IPAddr.new(netmask, Socket::AF_INET)
network = ip.mask(subnet.to_s).to_s
end
end
def get_netmask
netmask = nil;
ipregex = %r{(\d{1,3}\.){3}\d{1,3}}
ops = nil
case Facter.value(:kernel)
when ''Linux''
ops = {
:ifconfig => ''/sbin/ifconfig'',
:regex => %r{\s+ inet\saddr: #{Facter.ipaddress} .*? (Mask):
(#{ipregex})}x,
:munge => nil,
}
when ''SunOS''
ops = {
:ifconfig => ''/usr/sbin/ifconfig -a'',
:regex =>
%r{\s+inet\s+#{Facter.ipaddress}\s+(net|)mask\s+(\w{8})}x,
:munge => Proc.new { |mask| mask.scan(/../).collect do |byte|
byte.to_i(16) end.join(''.'') }
}
end
%x{#{ops[:ifconfig]}}.split(/\n/).collect do |line|
matches = line.match(ops[:regex])
if !matches.nil?
if ops[:munge].nil?
netmask = matches[2]
else
netmask = ops[:munge].call(matches[2])
end
end
end
netmask
end
Facter.add("netmask") do
confine :kernel => [ :sunos, :linux ]
setcode do
get_netmask
end
end
get_interfaces.each do |interface|
Facter.add("network_" + alphafy(interface)) do
setcode do
get_network_value(interface)
end
end
end
Facter.add("network") do
setcode do
begin
get_network_value(get_interfaces[0])
rescue => detail
""
end
end
end
Facter.add("defaultroute") do
setcode do
begin
defaultroute = ""
output = %x{netstat -rn}
if output =~
/^(default|0\.0\.0\.0)\s*(\S*)\s*\S*\s*\S*\s*\S*\s*(\S*).*/
defaultroute = $2
end
defaultroute
rescue => detail
""
end
end
end
--
You received this message because you are subscribed to the Google Groups
"Puppet Users" group.
To post to this group, send email to puppet-users@googlegroups.com.
To unsubscribe from this group, send email to
puppet-users+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/puppet-users?hl=en.
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Paul, This is *completely* shooting from the hip, but I''m guessing that there may be an issue with the fact files being pulled in as some sort of dynamically created object. If this is the case, then what you have below would be trying to re-declare a constant in the (possibly) Facter namespace. Try either locally scoping it to your file or sticking a ''self.'' in front of all mentions of REGEX_MAP. I don''t have a system to try it on at the moment, but I''d be curious to hear if it works. Otherwise, hitting the puppet-dev list might churn up a more useful answer. Good luck, Trevor On 04/30/2010 11:01 AM, Paul Seymour wrote:> Hello > > Using a custom fact (see below) generates messages during the puppet > runs like this any ideas how I can stop it ?:- > ./ipfacts.rb:32: warning: already initialized constant REGEX_MAP > > > ipfacts.rb:- > ENV["PATH"]="/bin:/sbin:/usr/bin:/usr/sbin" > > # A map of all the different regexes that work for > # a given platform or set of platforms. > REGEX_MAP = { > :linux => { > :ipaddress => /inet addr:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/, > :macaddress => > /(?:ether|HWaddr)\s+(\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2})/, > :netmask => /Mask:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/ > }, > :bsd => { > :aliases => [:openbsd, :netbsd, :freebsd, :darwin], > :ipaddress => /inet\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/, > :macaddress => > /(?:ether|lladdr)\s+(\w\w:\w\w:\w\w:\w\w:\w\w:\w\w)/, > :netmask => /netmask\s+0x(\w{8})/ > }, > :sunos => { > :addr => /inet\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/, > :ipaddress => /inet\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/, > :macaddress => > /(?:ether|lladdr)\s+(\w?\w:\w?\w:\w?\w:\w?\w:\w?\w:\w?\w)/, > :netmask => /netmask\s+(\w{8})/ > } > } > > # Convert an interface name into purely alpha characters. > def alphafy(interface) > interface.gsub(/[:.]/, ''_'') > end > > def convert_from_hex?(kernel) > kernels_to_convert = [:sunos, :openbsd, :netbsd, :freebsd, :darwin] > kernels_to_convert.include?(kernel) > end > > def supported_platforms > REGEX_MAP.inject([]) do |result, tmp| > key, map = tmp > if map[:aliases] > result += map[:aliases] > else > result << key > end > result > end > end > > def get_interfaces > int = nil > > output = get_all_interface_output() > > # We get lots of warnings on platforms that don''t get an output > # made. > if output > int = output.scan(/^\w+[.:]?\d+/).find_all { |i| i !~ > /^lo[0:]?/ } > else > [] > end > end > > def get_all_interface_output > case Facter.value(:kernel) > when ''Linux'', ''OpenBSD'', ''NetBSD'', ''FreeBSD'', ''Darwin'' > output = %x{/sbin/ifconfig -a} > when ''SunOS'' > output = %x{/usr/sbin/ifconfig -a} > end > output > end > > def get_single_interface_output(interface) > output = "" > case Facter.value(:kernel) > when ''Linux'', ''OpenBSD'', ''NetBSD'', ''FreeBSD'', ''Darwin'' > output = %x{/sbin/ifconfig #{interface}} > when ''SunOS'' > output = %x{/usr/sbin/ifconfig #{interface}} > end > output > end > > def get_bonding_master(interface) > if Facter.value(:kernel) != ''Linux'' > return nil > end > # We need ip instead of ifconfig because it will show us > # the bonding master device. > if not FileTest.executable?("/sbin/ip") > return nil > end > regex = /SLAVE[,>].* (bond[0-9]+)/ > ethbond = regex.match(%x{/sbin/ip link show #{interface}}) > if ethbond > device = ethbond[1] > else > device = nil > end > device > end > > > def get_interface_value(interface, label) > tmp1 = [] > > kernel = Facter.value(:kernel).downcase.to_sym > > # If it''s not directly in the map or aliased in the map, then we > don''t know how to deal with it. > unless map = REGEX_MAP[kernel] || REGEX_MAP.values.find { |tmp| > tmp[:aliases] and tmp[:aliases].include?(kernel) } > return [] > end > > # Pull the correct regex out of the map. > regex = map[label.to_sym] > > # Linux changes the MAC address reported via ifconfig when an > ethernet interface > # becomes a slave of a bonding device to the master MAC address. > # We have to dig a bit to get the original/real MAC address of > the interface. > bonddev = get_bonding_master(interface) > if label == ''macaddress'' and bonddev > bondinfo = IO.readlines("/proc/net/bonding/#{bonddev}") > hwaddrre = /^Slave Interface: > #{interface}\n[^\n].+?\nPermanent HW addr: (([0-9a-fA-F]{2}:?)*)$/m > value = hwaddrre.match(bondinfo.to_s)[1].upcase > else > output_int = get_single_interface_output(interface) > > if interface != /^lo[0:]?\d?/ > output_int.each do |s| > if s =~ regex > value = $1 > if label == ''netmask'' && convert_from_hex?(kernel) > value = value.scan(/../).collect do |byte| > byte.to_i(16) end.join(''.'') > end > tmp1.push(value) > end > end > end > > if tmp1 > value = tmp1.shift > end > end > end > > def get_network_value(interface) > require ''ipaddr'' > > ipaddress = get_interface_value(interface, "ipaddress") > netmask = get_interface_value(interface, "netmask") > > if ipaddress && netmask > ip = IPAddr.new(ipaddress, Socket::AF_INET) > subnet = IPAddr.new(netmask, Socket::AF_INET) > network = ip.mask(subnet.to_s).to_s > end > end > > def get_netmask > netmask = nil; > ipregex = %r{(\d{1,3}\.){3}\d{1,3}} > > ops = nil > case Facter.value(:kernel) > when ''Linux'' > ops = { > :ifconfig => ''/sbin/ifconfig'', > :regex => %r{\s+ inet\saddr: #{Facter.ipaddress} .*? > (Mask): (#{ipregex})}x, > :munge => nil, > } > when ''SunOS'' > ops = { > :ifconfig => ''/usr/sbin/ifconfig -a'', > :regex => > %r{\s+inet\s+#{Facter.ipaddress}\s+(net|)mask\s+(\w{8})}x, > :munge => Proc.new { |mask| mask.scan(/../).collect do > |byte| byte.to_i(16) end.join(''.'') } > } > end > > %x{#{ops[:ifconfig]}}.split(/\n/).collect do |line| > matches = line.match(ops[:regex]) > if !matches.nil? > if ops[:munge].nil? > netmask = matches[2] > else > netmask = ops[:munge].call(matches[2]) > end > end > end > netmask > end > > Facter.add("netmask") do > confine :kernel => [ :sunos, :linux ] > setcode do > get_netmask > end > end > > get_interfaces.each do |interface| > Facter.add("network_" + alphafy(interface)) do > setcode do > get_network_value(interface) > end > end > end > > Facter.add("network") do > setcode do > begin > get_network_value(get_interfaces[0]) > rescue => detail > "" > end > end > end > > Facter.add("defaultroute") do > setcode do > begin > defaultroute = "" > output = %x{netstat -rn} > if output =~ > /^(default|0\.0\.0\.0)\s*(\S*)\s*\S*\s*\S*\s*\S*\s*(\S*).*/ > defaultroute = $2 > end > defaultroute > rescue => detail > "" > end > end > end > > -- > You received this message because you are subscribed to the Google > Groups "Puppet Users" group. > To post to this group, send email to puppet-users@googlegroups.com. > To unsubscribe from this group, send email to > puppet-users+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/puppet-users?hl=en.- -- Trevor Vaughan Vice President, Onyx Point, Inc. email: tvaughan@onyxpoint.com phone: 410-541-ONYX (6699) pgp: 0x6C701E94 - -- This account not approved for unencrypted sensitive information -- -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.10 (GNU/Linux) iEYEARECAAYFAkvbk24ACgkQyWMIJmxwHpRDqQCfSci2AHHf+iGhEzbCODVrgvbG 9ZsAoJ9EAjITx/SfkUgwdcYLrC8Em9wX =uXXw -----END PGP SIGNATURE----- -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.