summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/facter.rb26
-rw-r--r--lib/facter/application.rb108
-rw-r--r--lib/facter/macaddress.rb16
-rw-r--r--lib/facter/util/macaddress.rb28
-rw-r--r--lib/facter/util/resolution.rb7
-rw-r--r--lib/facter/util/virtual.rb2
-rw-r--r--lib/facter/virtual.rb4
7 files changed, 175 insertions, 16 deletions
diff --git a/lib/facter.rb b/lib/facter.rb
index 42d5371..f48138a 100644
--- a/lib/facter.rb
+++ b/lib/facter.rb
@@ -48,6 +48,7 @@ module Facter
GREEN = ""
RESET = ""
@@debug = 0
+ @@timing = 0
# module methods
@@ -77,6 +78,15 @@ module Facter
@@debug != 0
end
+ # show the timing information
+ def self.show_time(string)
+ puts "#{GREEN}#{string}#{RESET}" if string and Facter.timing?
+ end
+
+ def self.timing?
+ @@timing != 0
+ end
+
# Return a fact object by name. If you use this, you still have to call
# 'value' on it to retrieve the actual value.
def self.[](name)
@@ -177,6 +187,22 @@ module Facter
end
end
+ # Set timing on or off.
+ def self.timing(bit)
+ if bit
+ case bit
+ when TrueClass; @@timing = 1
+ when Fixnum
+ if bit > 0
+ @@timing = 1
+ else
+ @@timing = 0
+ end
+ end
+ else
+ @@timing = 0
+ end
+ end
def self.warn(msg)
if Facter.debugging? and msg and not msg.empty?
diff --git a/lib/facter/application.rb b/lib/facter/application.rb
new file mode 100644
index 0000000..9de9249
--- /dev/null
+++ b/lib/facter/application.rb
@@ -0,0 +1,108 @@
+module Facter
+ module Application
+ def self.run(argv)
+ require 'optparse'
+ require 'facter'
+
+ options = parse(argv)
+
+ # Accept fact names to return from the command line
+ names = argv
+
+ # Create the facts hash that is printed to standard out
+ if names.empty?
+ facts = Facter.to_hash
+ else
+ facts = {}
+ names.each { |name|
+ begin
+ facts[name] = Facter.value(name)
+ rescue => error
+ $stderr.puts "Could not retrieve #{name}: #{error}"
+ exit 10
+ end
+ }
+ end
+
+ # Print the facts as YAML and exit
+ if options[:yaml]
+ require 'yaml'
+ puts YAML.dump(facts)
+ exit(0)
+ end
+
+ # Print the value of a single fact, otherwise print a list sorted by fact
+ # name and separated by "=>"
+ if facts.length == 1
+ if value = facts.values.first
+ puts value
+ end
+ else
+ facts.sort_by{ |fact| fact.first }.each do |name,value|
+ puts "#{name} => #{value}"
+ end
+ end
+
+ rescue => e
+ if options && options[:trace]
+ raise e
+ else
+ $stderr.puts "Error: #{e}"
+ exit(12)
+ end
+ end
+
+ private
+
+ def self.parse(argv)
+ options = {}
+ OptionParser.new do |opts|
+ opts.on("-y", "--yaml") { |v| options[:yaml] = v }
+ opts.on( "--trace") { |v| options[:trace] = v }
+ opts.on("-d", "--debug") { |v| Facter.debugging(1) }
+ opts.on("-t", "--timing") { |v| Facter.timing(1) }
+ opts.on("-p", "--puppet") { |v| load_puppet }
+
+ opts.on_tail("-v", "--version") do
+ puts Facter.version
+ exit(0)
+ end
+
+ opts.on_tail("-h", "--help") do
+ begin
+ require 'rdoc/ri/ri_paths'
+ require 'rdoc/usage'
+ puts RDoc.usage
+ rescue LoadError
+ $stderr.puts "No help available unless your RDoc has RDoc.usage"
+ exit(1)
+ rescue => e
+ $stderr.puts "fatal: #{e}"
+ exit(1)
+ end
+ end
+ end.parse!
+
+ options
+ rescue OptionParser::InvalidOption => e
+ $stderr.puts e.message
+ exit(12)
+ end
+
+ def self.load_puppet
+ require 'puppet'
+ Puppet.parse_config
+
+ # If you've set 'vardir' but not 'libdir' in your
+ # puppet.conf, then the hook to add libdir to $:
+ # won't get triggered. This makes sure that it's setup
+ # correctly.
+ unless $LOAD_PATH.include?(Puppet[:libdir])
+ $LOAD_PATH << Puppet[:libdir]
+ end
+ rescue LoadError => detail
+ $stderr.puts "Could not load Puppet: #{detail}"
+ end
+
+ end
+end
diff --git a/lib/facter/macaddress.rb b/lib/facter/macaddress.rb
index 889feea..adc2c64 100644
--- a/lib/facter/macaddress.rb
+++ b/lib/facter/macaddress.rb
@@ -1,3 +1,5 @@
+require 'facter/util/macaddress'
+
Facter.add(:macaddress) do
confine :operatingsystem => %w{Solaris Linux Fedora RedHat CentOS SuSE SLES Debian Gentoo Ubuntu OEL OVS}
setcode do
@@ -26,19 +28,7 @@ end
Facter.add(:macaddress) do
confine :kernel => :darwin
- setcode do
- ether = nil
- output = %x{/sbin/ifconfig}
-
- output.split(/^\S/).each do |str|
- if str =~ /10baseT/ # we're wired
- str =~ /ether (\w\w:\w\w:\w\w:\w\w:\w\w:\w\w)/
- ether = $1
- end
- end
-
- ether
- end
+ setcode { Facter::Util::Macaddress::Darwin.macaddress }
end
Facter.add(:macaddress) do
diff --git a/lib/facter/util/macaddress.rb b/lib/facter/util/macaddress.rb
new file mode 100644
index 0000000..fc0a043
--- /dev/null
+++ b/lib/facter/util/macaddress.rb
@@ -0,0 +1,28 @@
+# A module to gather macaddress facts
+#
+module Facter::Util::Macaddress
+
+ module Darwin
+ def self.macaddress
+ iface = default_interface
+ Facter.warn "Could not find a default route. Using first non-loopback interface" if iface.empty?
+
+ macaddress = `#{ifconfig_command} #{iface} | /usr/bin/awk '/ether/{print $2;exit}'`.chomp
+ macaddress.empty? ? nil : macaddress
+ end
+
+ def self.default_interface
+ `#{netstat_command} | /usr/bin/awk '/^default/{print $6}'`.chomp
+ end
+
+ private
+
+ def self.netstat_command
+ '/usr/sbin/netstat -rn'
+ end
+
+ def self.ifconfig_command
+ '/sbin/ifconfig'
+ end
+ end
+end
diff --git a/lib/facter/util/resolution.rb b/lib/facter/util/resolution.rb
index f837f64..4a99c35 100644
--- a/lib/facter/util/resolution.rb
+++ b/lib/facter/util/resolution.rb
@@ -135,6 +135,9 @@ class Facter::Util::Resolution
def value
result = nil
return result if @code == nil and @interpreter == nil
+
+ starttime = Time.now.to_f
+
begin
Timeout.timeout(limit) do
if @code.is_a?(Proc)
@@ -156,6 +159,10 @@ class Facter::Util::Resolution
return nil
end
+ finishtime = Time.now.to_f
+ ms = (finishtime - starttime) * 1000
+ Facter.show_time "#{self.name}: #{"%.2f" % ms}ms"
+
return nil if result == ""
return result
end
diff --git a/lib/facter/util/virtual.rb b/lib/facter/util/virtual.rb
index 5a03117..e94070d 100644
--- a/lib/facter/util/virtual.rb
+++ b/lib/facter/util/virtual.rb
@@ -22,7 +22,7 @@ module Facter::Util::Virtual
def self.vserver?
return false unless FileTest.exists?("/proc/self/status")
txt = File.read("/proc/self/status")
- return true if txt =~ /^(s_context|VxID):[[:blank:]]*[1-9]/
+ return true if txt =~ /^(s_context|VxID):[[:blank:]]*[0-9]/
return false
end
diff --git a/lib/facter/virtual.rb b/lib/facter/virtual.rb
index faefb29..a8afb60 100644
--- a/lib/facter/virtual.rb
+++ b/lib/facter/virtual.rb
@@ -85,9 +85,9 @@ Facter.add("is_virtual") do
setcode do
case Facter.value(:virtual)
when "xenu", "openvzve", "vmware", "kvm", "vserver", "jail", "zone", "hpvm"
- true
+ "true"
else
- false
+ "false"
end
end
end