Bug #6976

Better EC2 fact and Arp facts

Added by James Turnbull about 1 year ago. Updated 12 months ago.

Status:Closed Start date:04/05/2011
Priority:Normal Due date:
Assignee:- % Done:

0%

Category:library
Target version:1.5.9
Keywords: Affected Facter version:
Branch:https://github.com/jamtur01/facter/tree/tickets/master/6976
Votes: 0

Description

Ohad’s updated code for EC2 and Arp.

From: Ohad Levy 

ARP facts on large network might lead to inconstant values
as we are always using the first ARP entry from the output of the ARP
command

Signed-off-by: Ohad Levy 
---
 lib/facter/arp.rb |   12 ++++++++----
 lib/facter/ec2.rb |   16 +++++-----------
 2 files changed, 13 insertions(+), 15 deletions(-)

diff --git a/lib/facter/arp.rb b/lib/facter/arp.rb
index 5035ad0..0a7cf67 100644
--- a/lib/facter/arp.rb
+++ b/lib/facter/arp.rb
@@ -3,14 +3,17 @@ require 'facter/util/ip'
 Facter.add(:arp) do
   confine :kernel => :linux
   setcode do
-    arp = []
     output = Facter::Util::Resolution.exec('arp -a')
     if not output.nil?
+      arp = ""
       output.each_line do |s|
-        arp.push($1) if s =~ /^\S+\s\S+\s\S+\s(\S+)\s\S+\s\S+\s\S+$/
+        if s =~ /^\S+\s\S+\s\S+\s(\S+)\s\S+\s\S+\s\S+$/
+          arp = $1
+          break # stops on the first match
+        end
       end
     end
-    arp[0]
+    EC2_ARP == arp ? arp : nil
   end
 end
 
@@ -18,7 +21,8 @@ Facter::Util::IP.get_interfaces.each do |interface|
   Facter.add("arp_" + Facter::Util::IP.alphafy(interface)) do
     confine :kernel => :linux
     setcode do
-      Facter::Util::IP.get_arp_value(interface)
+      arp = Facter::Util::IP.get_arp_value(interface)
+      EC2_ARP == arp ? arp : nil
     end
   end
 end
diff --git a/lib/facter/ec2.rb b/lib/facter/ec2.rb
index 29b2a1c..b36790b 100644
--- a/lib/facter/ec2.rb
+++ b/lib/facter/ec2.rb
@@ -6,9 +6,11 @@
 require 'open-uri'
 require 'socket'
 
-EC2_ADDR = "169.254.169.254"
+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)
@@ -61,19 +63,11 @@ def userdata()
 end
 
 def has_euca_mac?
-  if Facter.value(:macaddress) =~ /^[dD]0:0[dD]:/
-    return true
-  else
-    return false
-  end
+  !!(Facter.value(:macaddress) =~ EC2_EUCA_MAC)
 end
 
 def has_ec2_arp?
-  if Facter.value(:arp) == 'fe:ff:ff:ff:ff:ff'
-    return true
-  else
-    return false
-  end
+  !!(Facter.value(:arp) == EC2_ARP)
 end
 
 if (has_euca_mac? || has_ec2_arp?) && can_metadata_connect?(EC2_ADDR,80)
-- 
1.7.4

History

Updated by James Turnbull about 1 year ago

  • Category set to library
  • Status changed from Unreviewed to In Topic Branch Pending Review
  • Target version set to 1.5.9
  • Branch set to https://github.com/jamtur01/facter/tree/tickets/master/6976

Updated by James Turnbull about 1 year ago

  • Status changed from In Topic Branch Pending Review to Merged - Pending Release

commit:53cd946685fe5790dbbe5b2a3964e6d861bbfdde

Updated by Jeremy Katz about 1 year ago

This doesn’t seem to quite do the trick. On a CentOS 5 instance, I’m getting arp output of [root@ip-10-194-62-111 facter]# arp -a ip-10-194-62-1.ec2.internal (10.194.62.1) at FE:FF:FF:FF:FF:FF [ether] on eth0 instance-data.ec2.internal (169.254.169.254) at FE:FF:FF:FF:FF:FF [ether] on eth0

While EC2_ARP is all lowercase.

https://github.com/puppetlabs/facter/pull/7 has the quick fix and I now get ec2 facts on my ec2 instances.

Updated by James Turnbull about 1 year ago

  • Status changed from Merged - Pending Release to Needs Decision
  • Assignee set to Nigel Kersten

Nigel – further minor update needed for next RC.

Updated by Nigel Kersten about 1 year ago

  • Status changed from Needs Decision to Accepted
  • Assignee deleted (Nigel Kersten)

Updated by Jesse Wolfe about 1 year ago

available in master as of commit:ee93457099c286973f397ff1a03f2be7fddfe9ae

Updated by Jesse Wolfe about 1 year ago

  • Status changed from Accepted to Merged - Pending Release

Updated by James Turnbull 12 months ago

  • Status changed from Merged - Pending Release to Closed

Also available in: Atom PDF