components/ruby/facter/patches/facter-06-21936192-ipaddress.patch
changeset 4995 ecffc6614cfa
child 5860 afd31ba91ee9
equal deleted inserted replaced
4994:ed928e1888f8 4995:ecffc6614cfa
       
     1 Split from BSD method and add getent hosts
       
     2 Also don't try to run `host hostname` if the host command isn't available.
       
     3 No upstream facter 2.x is dead and new bugs are not being accepted.
       
     4 --- facter-2.1.0/lib/facter/ipaddress.rb.orig	2015-10-02 13:17:19.199030743 -0400
       
     5 +++ facter-2.1.0/lib/facter/ipaddress.rb	2015-10-02 13:17:25.713426750 -0400
       
     6 @@ -64,7 +64,7 @@
       
     7  end
       
     8  
       
     9  Facter.add(:ipaddress) do
       
    10 -  confine :kernel => %w{NetBSD SunOS}
       
    11 +  confine :kernel => %w{NetBSD}
       
    12    setcode do
       
    13      ip = nil
       
    14      output = Facter::Util::IP.exec_ifconfig(["-a"])
       
    15 @@ -84,6 +84,41 @@
       
    16  end
       
    17  
       
    18  Facter.add(:ipaddress) do
       
    19 +  confine :osfamily => %w{Solaris}
       
    20 +  setcode do
       
    21 +    ip = nil
       
    22 +    output = Facter::Util::IP.exec_ifconfig(["-a"])
       
    23 +
       
    24 +    output.each_line { |str|
       
    25 +      if str =~ /inet ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/
       
    26 +        tmp = $1
       
    27 +        unless tmp =~ /^127\./ or tmp == "0.0.0.0"
       
    28 +          ip = tmp
       
    29 +          break
       
    30 +        end
       
    31 +      end
       
    32 +    }
       
    33 +
       
    34 +    # If we didn't get an IP from ifconfig see if we can get one from
       
    35 +    # the hosts database
       
    36 +    if ip.nil? && hostname = Facter.value(:hostname)
       
    37 +      # we need Hostname to exist for this to work
       
    38 +      host = nil
       
    39 +      Facter::Core::Execution.execute("getent hosts #{hostname}").each_line {
       
    40 +        |l|
       
    41 +        _ip = l.chomp.split()[0]
       
    42 +        if _ip !~ /^127\./ && _ip =~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/
       
    43 +          ip = _ip
       
    44 +          break
       
    45 +        end
       
    46 +      }
       
    47 +    end
       
    48 +
       
    49 +    ip
       
    50 +  end
       
    51 +end
       
    52 +
       
    53 +Facter.add(:ipaddress) do
       
    54    confine :kernel => %w{AIX}
       
    55    setcode do
       
    56      ip = nil
       
    57 @@ -152,8 +187,9 @@
       
    58  
       
    59  Facter.add(:ipaddress, :timeout => 2) do
       
    60    setcode do
       
    61 -    if hostname = Facter.value(:hostname)
       
    62 -      # we need Hostname to exist for this to work
       
    63 +    if hostname = Facter.value(:hostname) &&
       
    64 +      Facter::Core::Execution.which('host')
       
    65 +      # we need Hostname and `host` to exist for this to work
       
    66        host = nil
       
    67        if host = Facter::Core::Execution.execute("host #{hostname}")
       
    68          list = host.chomp.split(/\s/)