Ensures that ARP facts are returned only on EC2 hosts
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 <ohadlevy@gmail.com>
Ohad Levy authored 13 years ago
Jacob Helwig committed 13 years ago
2 | 2 |
Facter.add(:arp) do
|
3 | 3 |
confine :kernel => :linux
|
4 | 4 |
setcode do
|
5 | |
arp = []
|
6 | 5 |
output = Facter::Util::Resolution.exec('arp -a')
|
7 | 6 |
if not output.nil?
|
|
7 |
arp = ""
|
8 | 8 |
output.each_line do |s|
|
9 | |
arp.push($1) if s =~ /^\S+\s\S+\s\S+\s(\S+)\s\S+\s\S+\s\S+$/
|
|
9 |
if s =~ /^\S+\s\S+\s\S+\s(\S+)\s\S+\s\S+\s\S+$/
|
|
10 |
arp = $1
|
|
11 |
break # stops on the first match
|
|
12 |
end
|
10 | 13 |
end
|
11 | 14 |
end
|
12 | |
arp[0]
|
|
15 |
EC2_ARP == arp ? arp : nil
|
13 | 16 |
end
|
14 | 17 |
end
|
15 | 18 |
|
|
17 | 20 |
Facter.add("arp_" + Facter::Util::IP.alphafy(interface)) do
|
18 | 21 |
confine :kernel => :linux
|
19 | 22 |
setcode do
|
20 | |
Facter::Util::IP.get_arp_value(interface)
|
|
23 |
arp = Facter::Util::IP.get_arp_value(interface)
|
|
24 |
EC2_ARP == arp ? arp : nil
|
21 | 25 |
end
|
22 | 26 |
end
|
23 | 27 |
end
|
5 | 5 |
require 'open-uri'
|
6 | 6 |
require 'socket'
|
7 | 7 |
|
8 | |
EC2_ADDR = "169.254.169.254"
|
|
8 |
EC2_ADDR = "169.254.169.254"
|
9 | 9 |
EC2_METADATA_URL = "http://#{EC2_ADDR}/2008-02-01/meta-data"
|
10 | 10 |
EC2_USERDATA_URL = "http://#{EC2_ADDR}/2008-02-01/user-data"
|
|
11 |
EC2_ARP = "fe:ff:ff:ff:ff:ff"
|
|
12 |
EC2_EUCA_MAC = %r{^[dD]0:0[dD]:}
|
11 | 13 |
|
12 | 14 |
def can_metadata_connect?(addr, port, timeout=2)
|
13 | 15 |
t = Socket.new(Socket::Constants::AF_INET, Socket::Constants::SOCK_STREAM, 0)
|
|
60 | 62 |
end
|
61 | 63 |
|
62 | 64 |
def has_euca_mac?
|
63 | |
if Facter.value(:macaddress) =~ /^[dD]0:0[dD]:/
|
64 | |
return true
|
65 | |
else
|
66 | |
return false
|
67 | |
end
|
|
65 |
!!(Facter.value(:macaddress) =~ EC2_EUCA_MAC)
|
68 | 66 |
end
|
69 | 67 |
|
70 | 68 |
def has_ec2_arp?
|
71 | |
if Facter.value(:arp) == 'fe:ff:ff:ff:ff:ff'
|
72 | |
return true
|
73 | |
else
|
74 | |
return false
|
75 | |
end
|
|
69 |
!!(Facter.value(:arp) == EC2_ARP)
|
76 | 70 |
end
|
77 | 71 |
|
78 | 72 |
if (has_euca_mac? || has_ec2_arp?) && can_metadata_connect?(EC2_ADDR,80)
|