From a75f0f9bc7ea1e03dc3a19fba8fae61165ab2b05 Mon Sep 17 00:00:00 2001 From: Jacob Helwig Date: Tue, 12 Apr 2011 11:15:06 -0700 Subject: (#7039) Pre-load all facts when requesting a single fact Since multiple facts can be defined in a single file and we have no way of knowing which "additional" facts are defined in which files, we pre-load all facts when we're looking for specific one. Paired-with: Max Martin --- lib/facter/application.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/facter/application.rb b/lib/facter/application.rb index 6b351b1..bd68149 100644 --- a/lib/facter/application.rb +++ b/lib/facter/application.rb @@ -9,19 +9,21 @@ module Facter # 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 + # Create the facts hash that is printed to standard out. + # Pre-load all of the facts, since we can have multiple facts + # per file, and since we can't know ahead of time which file a + # fact will be in, we'll need to load every file. + facts = Facter.to_hash + unless names.empty? facts = {} - names.each { |name| + names.each do |name| begin facts[name] = Facter.value(name) rescue => error $stderr.puts "Could not retrieve #{name}: #{error}" exit 10 end - } + end end # Print the facts as YAML and exit -- cgit From f91c1205e4dc9a78066af8818fdb92f630aad0d9 Mon Sep 17 00:00:00 2001 From: Jeremy Katz Date: Mon, 18 Apr 2011 12:28:11 -0400 Subject: downcase arp output so that the ec2 arp is matched CentOS 5.4 arp gives the arp output as uppercase; downcase it so we're ensured a match --- lib/facter/arp.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/facter/arp.rb b/lib/facter/arp.rb index 0a7cf67..6269ae0 100644 --- a/lib/facter/arp.rb +++ b/lib/facter/arp.rb @@ -8,7 +8,7 @@ Facter.add(:arp) do arp = "" output.each_line do |s| if s =~ /^\S+\s\S+\s\S+\s(\S+)\s\S+\s\S+\s\S+$/ - arp = $1 + arp = $1.downcase break # stops on the first match end end -- cgit From cc67a0148b97e315572cdb905476df1224a78dd5 Mon Sep 17 00:00:00 2001 From: James Turnbull Date: Wed, 4 May 2011 11:02:41 +1000 Subject: Removed inappropriately uncredited Ohai method from ec2 fact --- lib/facter/arp.rb | 4 ++-- lib/facter/ec2.rb | 54 ++++++++++++++++-------------------------------------- 2 files changed, 18 insertions(+), 40 deletions(-) (limited to 'lib') diff --git a/lib/facter/arp.rb b/lib/facter/arp.rb index 6269ae0..383fb48 100644 --- a/lib/facter/arp.rb +++ b/lib/facter/arp.rb @@ -13,7 +13,7 @@ Facter.add(:arp) do end end end - EC2_ARP == arp ? arp : nil + "fe:ff:ff:ff:ff:ff" == arp ? arp : nil end end @@ -22,7 +22,7 @@ Facter::Util::IP.get_interfaces.each do |interface| confine :kernel => :linux setcode do arp = Facter::Util::IP.get_arp_value(interface) - EC2_ARP == arp ? arp : nil + "fe:ff:ff:ff:ff:ff" == arp ? arp : nil end end end diff --git a/lib/facter/ec2.rb b/lib/facter/ec2.rb index 693e78e..c52f76b 100644 --- a/lib/facter/ec2.rb +++ b/lib/facter/ec2.rb @@ -4,45 +4,24 @@ # Additional work modelled on Ohai EC2 fact require 'open-uri' -require 'socket' - -EC2_ADDR = "169.254.169.254" -EC2_METADATA_URL = "http://#{EC2_ADDR}/2008-02-01/meta-data" -EC2_USERDATA_URL = "http://#{EC2_ADDR}/2008-02-01/user-data" -EC2_ARP = "fe:ff:ff:ff:ff:ff" -EC2_EUCA_MAC = %r{^[dD]0:0[dD]:} - -def can_metadata_connect?(addr, port, timeout=2) - t = Socket.new(Socket::Constants::AF_INET, Socket::Constants::SOCK_STREAM, 0) - saddr = Socket.pack_sockaddr_in(port, addr) - connected = false - - begin - t.connect_nonblock(saddr) - rescue Errno::EINPROGRESS - r,w,e = IO::select(nil,[t],nil,timeout) - if !w.nil? - connected = true - else - begin - t.connect_nonblock(saddr) - rescue Errno::EISCONN - t.close - connected = true - rescue SystemCallError - end - end - rescue SystemCallError - end - connected +require 'timeout' + +def can_connect?(wait_sec=2) + url = "http://169.254.169.254:80/" + Timeout::timeout(wait_sec) {open(url)} + return true + rescue Timeout::Error + return false + rescue + return false end def metadata(id = "") - open("#{EC2_METADATA_URL}/#{id||=''}").read. + open("http://169.254.169.254/2008-02-01/meta-data/#{id||=''}").read. split("\n").each do |o| key = "#{id}#{o.gsub(/\=.*$/, '/')}" if key[-1..-1] != '/' - value = open("#{EC2_METADATA_URL}/#{key}").read. + value = open("http://169.254.169.254/2008-02-01/meta-data/#{key}").read. split("\n") value = value.size>1 ? value : value.first symbol = "ec2_#{key.gsub(/\-|\//, '_')}".to_sym @@ -54,23 +33,22 @@ def metadata(id = "") end def userdata() - # assumes the only expected error is the 404 if there's no user-data begin - value = OpenURI.open_uri("#{EC2_USERDATA_URL}/").read.split + value = OpenURI.open_uri("http://169.254.169.254/2008-02-01/user-data/").read.split Facter.add(:ec2_userdata) { setcode { value } } rescue OpenURI::HTTPError end end def has_euca_mac? - !!(Facter.value(:macaddress) =~ EC2_EUCA_MAC) + !!(Facter.value(:macaddress) =~ %r{^[dD]0:0[dD]:}) end def has_ec2_arp? - !!(Facter.value(:arp) == EC2_ARP) + !!(Facter.value(:arp) == "fe:ff:ff:ff:ff:ff") end -if (has_euca_mac? || has_ec2_arp?) && can_metadata_connect?(EC2_ADDR,80) +if (has_euca_mac? || has_ec2_arp?) && can_connect? metadata userdata else -- cgit