ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vbrodets...@apache.org
Subject ambari git commit: AMBARI-10193. Hosts in Ambari showing IP as 127.0.0.1.(vbrodetskyi)
Date Tue, 24 Mar 2015 16:06:19 GMT
Repository: ambari
Updated Branches:
  refs/heads/trunk 2cb80ce3f -> 241f83115


AMBARI-10193. Hosts in Ambari showing IP as 127.0.0.1.(vbrodetskyi)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/241f8311
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/241f8311
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/241f8311

Branch: refs/heads/trunk
Commit: 241f83115983038ae26784b3ec36e75d50b81ece
Parents: 2cb80ce
Author: Vitaly Brodetskyi <vbrodetskyi@hortonworks.com>
Authored: Tue Mar 24 18:05:12 2015 +0200
Committer: Vitaly Brodetskyi <vbrodetskyi@hortonworks.com>
Committed: Tue Mar 24 18:05:12 2015 +0200

----------------------------------------------------------------------
 .../src/main/python/ambari_agent/Facter.py      | 48 ++++++++++----------
 .../test/python/ambari_agent/TestHardware.py    | 17 ++++++-
 2 files changed, 39 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/241f8311/ambari-agent/src/main/python/ambari_agent/Facter.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/main/python/ambari_agent/Facter.py b/ambari-agent/src/main/python/ambari_agent/Facter.py
index aaed859..fcadee5 100644
--- a/ambari-agent/src/main/python/ambari_agent/Facter.py
+++ b/ambari-agent/src/main/python/ambari_agent/Facter.py
@@ -25,6 +25,8 @@ import platform
 import re
 import shlex
 import socket
+import fcntl
+import struct
 import multiprocessing
 import subprocess
 from ambari_commons.shell import shellRunner
@@ -52,6 +54,10 @@ class Facter(object):
   def __init__(self):
     pass
 
+  # Return first ip adress
+  def getIpAddress(self):
+    return socket.gethostbyname(socket.getfqdn().lower())
+
   # Returns the currently running user id
   def getId(self):
     return getpass.getuser()
@@ -188,10 +194,6 @@ class FacterWindows(Facter):
   GET_PAGE_FILE_INFO = '$pgo=(Get-WmiObject Win32_PageFileUsage); echo "$($pgo.AllocatedBaseSize)
$($pgo.AllocatedBaseSize-$pgo.CurrentUsage)"'
   GET_UPTIME_CMD = 'echo $([int]((get-date)-[system.management.managementdatetimeconverter]::todatetime((get-wmiobject
-class win32_operatingsystem).Lastbootuptime)).TotalSeconds)'
 
-  # Return first ip adress
-  def getIpAddress(self):
-    #TODO check if we need ipconfig
-    return socket.gethostbyname(socket.gethostname().lower())
 
   # Return  netmask
   def getNetmask(self):
@@ -355,29 +357,27 @@ class FacterLinux(Facter):
 
     return result
 
-  # Return first ip adress
-  def getIpAddress(self):
-    ip_pattern="(?: inet addr:)(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"
-    if OSCheck.is_redhat7():
-      ip_pattern="(?: inet )(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"
-    result = self.data_return_first(ip_pattern,self.DATA_IFCONFIG_OUTPUT)
-    if result == '':
-      log.warn("Can't get an ip address from {0}".format(self.DATA_IFCONFIG_OUTPUT))
-      return socket.gethostbyname(socket.gethostname().lower())
-    else:
-      return result
-
   # Return  netmask
   def getNetmask(self):
-    mask_pattern="(?: Mask:)(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"
+    primary_ip = self.getIpAddress().strip()
+    interface_pattern="(\w+)(?:.*Link encap:)"
     if OSCheck.is_redhat7():
-      mask_pattern="(?: netmask )(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"
-    result = self.data_return_first(mask_pattern,self.DATA_IFCONFIG_OUTPUT)
-    if result == '':
-      log.warn("Can't get a netmask from {0}".format(self.DATA_IFCONFIG_OUTPUT))
-      return 'OS NOT SUPPORTED'
-    else:
-      return result
+      interface_pattern="(\w+)(?:.*flags=)"
+    for i in re.findall(interface_pattern, self.DATA_IFCONFIG_OUTPUT):
+      if primary_ip == self.get_ip_address_by_ifname(i.strip()).strip():
+        return socket.inet_ntoa(fcntl.ioctl(socket.socket(socket.AF_INET, socket.SOCK_DGRAM),
35099, struct.pack('256s', i))[20:24])
+        
+
+      
+  # Return IP by interface name
+  def get_ip_address_by_ifname(self, ifname):
+    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+    return socket.inet_ntoa(fcntl.ioctl(
+        s.fileno(),
+        0x8915,  # SIOCGIFADDR
+        struct.pack('256s', ifname[:15])
+    )[20:24])
+
 
   # Return interfaces
   def getInterfaces(self):

http://git-wip-us.apache.org/repos/asf/ambari/blob/241f8311/ambari-agent/src/test/python/ambari_agent/TestHardware.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/test/python/ambari_agent/TestHardware.py b/ambari-agent/src/test/python/ambari_agent/TestHardware.py
index 164417b..9bffaf6 100644
--- a/ambari-agent/src/test/python/ambari_agent/TestHardware.py
+++ b/ambari-agent/src/test/python/ambari_agent/TestHardware.py
@@ -144,11 +144,21 @@ SwapFree:        1598676 kB
     self.assertEquals(result['swapsize'], '2.04 GB')
     self.assertEquals(result['swapfree'], '1.52 GB')
 
+  @patch("fcntl.ioctl")
+  @patch("socket.socket")
+  @patch("struct.pack")
+  @patch("socket.inet_ntoa")
+  @patch.object(FacterLinux, "get_ip_address_by_ifname")
+  @patch.object(Facter, "getIpAddress")
   @patch.object(FacterLinux, "setDataIfConfigOutput")
   @patch.object(OSCheck, "get_os_type")
   @patch.object(OSCheck, "get_os_version")
-  def test_facterDataIfConfigOutput(self, get_os_version_mock, get_os_type_mock, facter_setDataIfConfigOutput_mock):
-
+  def test_facterDataIfConfigOutput(self, get_os_version_mock, get_os_type_mock, facter_setDataIfConfigOutput_mock,
+                                    getIpAddress_mock, get_ip_address_by_ifname_mock, inet_ntoa_mock,
struct_pack_mock,
+                                    socket_socket_mock, fcntl_ioctl_mock):
+    getIpAddress_mock.return_value = "10.0.2.15"
+    get_ip_address_by_ifname_mock.return_value = "10.0.2.15"
+    inet_ntoa_mock.return_value = "255.255.255.0"
     facter_setDataIfConfigOutput_mock.return_value = '''
 eth0      Link encap:Ethernet  HWaddr 08:00:27:C9:39:9E
           inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
@@ -182,6 +192,9 @@ lo        Link encap:Local Loopback
     get_os_version_mock.return_value = "11"
     result = Facter().facterInfo()
 
+    self.assertTrue(inet_ntoa_mock.called)
+    self.assertTrue(get_ip_address_by_ifname_mock.called)
+    self.assertTrue(getIpAddress_mock.called)
     self.assertEquals(result['ipaddress'], '10.0.2.15')
     self.assertEquals(result['netmask'], '255.255.255.0')
     self.assertEquals(result['interfaces'], 'eth0,eth1,lo')


Mime
View raw message