diff options
author | James Turnbull <james@lovedthanlost.net> | 2008-02-08 14:14:24 +1100 |
---|---|---|
committer | James Turnbull <james@lovedthanlost.net> | 2008-02-08 14:14:24 +1100 |
commit | df4636a7a8a20d94b6a1ec8577bff61456e09975 (patch) | |
tree | 43af7dcdfb7295c7035d8b37c49e98c4da3d92ff /lib/facter.rb | |
parent | 64f9fe9a23460f7afd2d18bce8ef2a46f5e795f6 (diff) | |
download | facter-df4636a7a8a20d94b6a1ec8577bff61456e09975.tar.gz facter-df4636a7a8a20d94b6a1ec8577bff61456e09975.tar.xz facter-df4636a7a8a20d94b6a1ec8577bff61456e09975.zip |
Split out facts from facter.rb and moved all support code to util
Diffstat (limited to 'lib/facter.rb')
-rw-r--r-- | lib/facter.rb | 516 |
1 files changed, 0 insertions, 516 deletions
diff --git a/lib/facter.rb b/lib/facter.rb index b87805a..65ea57b 100644 --- a/lib/facter.rb +++ b/lib/facter.rb @@ -610,522 +610,6 @@ class Facter end end - Facter.add(:kernel) do - setcode 'uname -s' - end - - Facter.add(:kernelrelease) do - setcode 'uname -r' - end - - { "LSBRelease" => %r{^LSB Version:\t(.*)$}, - "LSBDistId" => %r{^Distributor ID:\t(.*)$}, - "LSBDistRelease" => %r{^Release:\t(.*)$}, - "LSBDistDescription" => %r{^Description:\t(.*)$}, - "LSBDistCodeName" => %r{^Codename:\t(.*)$} - }.each do |fact, pattern| - Facter.add(fact) do - setcode do - unless defined?(@@lsbdata) and defined?(@@lsbtime) and (Time.now.to_i - @@lsbtime.to_i < 5) - type = nil - @@lsbtime = Time.now - @@lsbdata = Resolution.exec('lsb_release -a 2>/dev/null') - end - if pattern.match(@@lsbdata) - $1 - else - nil - end - end - end - end - - Facter.add(:operatingsystem) do - confine :kernel => :sunos - setcode do "Solaris" end - end - - Facter.add(:operatingsystem) do - confine :kernel => :linux - setcode do - if Facter.lsbdistid == "Ubuntu" - "Ubuntu" - elsif FileTest.exists?("/etc/debian_version") - "Debian" - elsif FileTest.exists?("/etc/gentoo-release") - "Gentoo" - elsif FileTest.exists?("/etc/fedora-release") - "Fedora" - elsif FileTest.exists?("/etc/mandriva-release") - "Mandriva" - elsif FileTest.exists?("/etc/mandrake-release") - "Mandrake" - elsif FileTest.exists?("/etc/redhat-release") - txt = File.read("/etc/redhat-release") - if txt =~ /centos/i - "CentOS" - else - "RedHat" - end - elsif FileTest.exists?("/etc/SuSE-release") - "SuSE" - end - end - end - - Facter.add(:operatingsystem) do - # Default to just returning the kernel as the operating system - setcode do Facter[:kernel].value end - end - - Facter.add(:operatingsystemrelease) do - confine :operatingsystem => :fedora - setcode do - File::open("/etc/fedora-release", "r") do |f| - line = f.readline.chomp - if line =~ /\(Rawhide\)$/ - "Rawhide" - elsif line =~ /release (\d+)/ - $1 - end - end - end - end - - Facter.add(:operatingsystemrelease) do - confine :operatingsystem => %w{RedHat} - setcode do - File::open("/etc/redhat-release", "r") do |f| - line = f.readline.chomp - if line =~ /\(Rawhide\)$/ - "Rawhide" - elsif line =~ /release (\d+)/ - $1 - end - end - end - end - - Facter.add(:operatingsystemrelease) do - confine :operatingsystem => %w{CentOS} - setcode do - release = Resolution.exec('rpm -q centos-release') - if release =~ /release-(\d+)/ - $1 - end - end - end - - Facter.add(:operatingsystemrelease) do - confine :operatingsystem => %w{Debian} - setcode do - release = Resolution.exec('cat /proc/version') - if release =~ /\(Debian (\d+.\d+).\d+-\d+\)/ - $1 - end - end - end - - Facter.add(:operatingsystemrelease) do - confine :operatingsystem => %w{Ubuntu} - setcode do - release = Resolution.exec('cat /etc/issue') - if release =~ /Ubuntu (\d+.\d+)/ - $1 - end - end - end - - Facter.add(:operatingsystemrelease) do - setcode do Facter[:kernelrelease].value end - end - - Facter.add(:hardwaremodel) do - setcode 'uname -m' - end - - Facter.add(:architecture) do - confine :kernel => :linux - setcode do - model = Facter.hardwaremodel - case model - # most linuxen use "x86_64" - when 'x86_64': - Facter.operatingsystem == "Debian" ? "amd64" : model; - when /(i[3456]86|pentium)/: "i386" - else - model - end - end - end - - Facter.add(:Cfkey) do - setcode do - value = nil - ["/usr/local/etc/cfkey.pub", - "/etc/cfkey.pub", - "/var/cfng/keys/localhost.pub", - "/var/cfengine/ppkeys/localhost.pub", - "/var/lib/cfengine/ppkeys/localhost.pub", - "/var/lib/cfengine2/ppkeys/localhost.pub" - ].each { |file| - if FileTest.file?(file) - File.open(file) { |openfile| - value = openfile.readlines.reject { |line| - line =~ /PUBLIC KEY/ - }.collect { |line| - line.chomp - }.join("") - } - end - if value - break - end - } - - value - end - end - - Facter.add(:domain) do - setcode do - # First force the hostname to be checked - Facter.hostname - - # Now check to see if it set the domain - if defined? $domain and ! $domain.nil? - $domain - else - nil - end - end - end - # Look for the DNS domain name command first. - Facter.add(:domain) do - setcode do - domain = Resolution.exec('dnsdomainname') or nil - # make sure it's a real domain - if domain and domain =~ /.+\..+/ - domain - else - nil - end - end - end - Facter.add(:domain) do - setcode do - domain = Resolution.exec('domainname') or nil - # make sure it's a real domain - if domain and domain =~ /.+\..+/ - domain - else - nil - end - end - end - Facter.add(:domain) do - setcode do - value = nil - if FileTest.exists?("/etc/resolv.conf") - File.open("/etc/resolv.conf") { |file| - # is the domain set? - file.each { |line| - if line =~ /domain\s+(\S+)/ - value = $1 - break - end - } - } - ! value and File.open("/etc/resolv.conf") { |file| - # is the search path set? - file.each { |line| - if line =~ /search\s+(\S+)/ - value = $1 - break - end - } - } - value - else - nil - end - end - end - Facter.add(:hostname) do - setldapname "cn" - setcode do - hostname = nil - name = Resolution.exec('hostname') or nil - if name - if name =~ /^([\w-]+)\.(.+)$/ - hostname = $1 - # the Domain class uses this - $domain = $2 - else - hostname = name - end - hostname - else - nil - end - end - end - - Facter.add(:fqdn) do - setcode do - host = Facter.value(:hostname) - domain = Facter.value(:domain) - if host and domain - [host, domain].join(".") - else - nil - end - end - end - - Facter.add(:ipaddress) do - setldapname "iphostnumber" - setcode do - require 'resolv' - - begin - if hostname = Facter.hostname - ip = Resolv.getaddress(hostname) - unless ip == "127.0.0.1" - ip - end - else - nil - end - rescue Resolv::ResolvError - nil - rescue NoMethodError # i think this is a bug in resolv.rb? - nil - end - end - end - Facter.add(:ipaddress) do - setcode do - if hostname = Facter.hostname - # we need Hostname to exist for this to work - host = nil - if host = Resolution.exec("host #{hostname}") - host = host.chomp.split(/\s/) - if defined? list[-1] and - list[-1] =~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/ - list[-1] - end - else - nil - end - else - nil - end - end - end - - ["/etc/ssh","/usr/local/etc/ssh","/etc","/usr/local/etc"].each { |dir| - {"SSHDSAKey" => "ssh_host_dsa_key.pub", - "SSHRSAKey" => "ssh_host_rsa_key.pub"}.each { |name,file| - Facter.add(name) do - setcode do - value = nil - filepath = File.join(dir,file) - if FileTest.file?(filepath) - begin - File.open(filepath) { |f| - value = f.read.chomp.split(/\s+/)[1] - } - rescue - value = nil - end - end - value - end # end of proc - end # end of add - } # end of hash each - } # end of dir each - - Facter.add(:uniqueid) do - setcode 'hostid', '/bin/sh' - confine :operatingsystem => %w{Solaris Linux Fedora RedHat CentOS SuSE Debian Gentoo} - end - - Facter.add(:hardwareisa) do - setcode 'uname -p', '/bin/sh' - confine :operatingsystem => %w{Solaris Linux Fedora RedHat CentOS SuSE Debian Gentoo FreeBSD OpenBSD NetBSD} - end - - Facter.add(:macaddress) do - confine :operatingsystem => %w{Solaris Linux Fedora RedHat CentOS SuSE Debian Gentoo} - setcode do - ether = [] - output = %x{/sbin/ifconfig -a} - output.each {|s| - ether.push($1) if s =~ /(?:ether|HWaddr) (\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2})/ - } - ether[0] - end - end - - Facter.add(:macaddress) do - confine :operatingsystem => %w{FreeBSD OpenBSD} - setcode do - ether = [] - output = %x{/sbin/ifconfig} - output.each {|s| - if s =~ /(?:ether|lladdr)\s+(\w\w:\w\w:\w\w:\w\w:\w\w:\w\w)/ - ether.push($1) - end - } - ether[0] - end - end - - Facter.add(:macaddress) do - confine :kernel => :darwin - setcode do - ether = nil - output = %x{/sbin/ifconfig} - - output.split(/^\S/).each { |str| - if str =~ /10baseT/ # we're wired - str =~ /ether (\w\w:\w\w:\w\w:\w\w:\w\w:\w\w)/ - ether = $1 - end - } - - ether - end - end - - Facter.add(:ipaddress) do - confine :kernel => :linux - setcode do - ip = nil - output = %x{/sbin/ifconfig} - - output.split(/^\S/).each { |str| - if str =~ /inet addr:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/ - tmp = $1 - unless tmp =~ /127\./ - ip = tmp - break - end - end - } - - ip - end - end - Facter.add(:ipaddress) do - confine :kernel => %w{FreeBSD OpenBSD solaris} - setcode do - ip = nil - output = %x{/sbin/ifconfig} - - output.split(/^\S/).each { |str| - if str =~ /inet ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/ - tmp = $1 - unless tmp =~ /127\./ - ip = tmp - break - end - end - } - - ip - end - end - Facter.add(:ipaddress) do - confine :kernel => %w{NetBSD} - setcode do - ip = nil - output = %x{/sbin/ifconfig -a} - - output.split(/^\S/).each { |str| - if str =~ /inet ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/ - tmp = $1 - unless tmp =~ /127\./ - ip = tmp - break - end - end - } - - ip - end - end - Facter.add(:ipaddress) do - confine :kernel => %w{darwin} - setcode do - ip = nil - iface = "" - output = %x{/usr/sbin/netstat -rn} - if output =~ /^default\s*\S*\s*\S*\s*\S*\s*\S*\s*(\S*).*/ - iface = $1 - else - warn "Could not find a default route. Using first non-loopback interface" - end - if(iface != "") - output = %x{/sbin/ifconfig #{iface}} - else - output = %x{/sbin/ifconfig} - end - - output.split(/^\S/).each { |str| - if str =~ /inet ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/ - tmp = $1 - unless tmp =~ /127\./ - ip = tmp - break - end - end - } - - ip - end - end - Facter.add(:hostname) do - confine :kernel => :darwin, :kernelrelease => "R7" - setcode do - %x{/usr/sbin/scutil --get LocalHostName} - end - end - Facter.add(:iphostnumber) do - confine :kernel => :darwin, :kernelrelease => "R6" - setcode do - %x{/usr/sbin/scutil --get LocalHostName} - end - end - Facter.add(:iphostnumber) do - confine :kernel => :darwin, :kernelrelease => "R6" - setcode do - ether = nil - output = %x{/sbin/ifconfig} - - output =~ /HWaddr (\w\w:\w\w:\w\w:\w\w:\w\w:\w\w)/ - ether = $1 - - ether - end - end - - Facter.add(:ps) do - setcode do 'ps -ef' end - end - - Facter.add(:ps) do - confine :operatingsystem => %w{FreeBSD NetBSD OpenBSD Darwin} - setcode do 'ps -auxwww' end - end - - Facter.add(:id) do - #confine :kernel => %w{Solaris Linux} - confine :operatingsystem => %w{Solaris Linux Fedora RedHat CentOS SuSE Debian Gentoo} - setcode "whoami" - end - locals = [] # Now find all our loadable facts |