Преглед на файлове

AMBARI-2472. Make public hostname for the agent configurable so that the configurable hostname is used if provided. (mahadev)

git-svn-id: https://svn.apache.org/repos/asf/incubator/ambari/trunk@1495925 13f79535-47bb-0310-9956-ffa450edef68
Mahadev Konar преди 12 години
родител
ревизия
77befe9371
променени са 2 файла, в които са добавени 44 реда и са изтрити 1 реда
  1. 20 0
      ambari-agent/src/main/python/ambari_agent/hostname.py
  2. 24 1
      ambari-agent/src/test/python/TestHostname.py

+ 20 - 0
ambari-agent/src/main/python/ambari_agent/hostname.py

@@ -22,7 +22,10 @@ import socket
 import subprocess
 import urllib2
 import AmbariConfig
+import logging
+import traceback
 
+logger = logging.getLogger()
 
 def hostname():
   config = AmbariConfig.config
@@ -41,6 +44,23 @@ def hostname():
     return socket.getfqdn()
 
 def public_hostname():
+  config = AmbariConfig.config
+  out = ''
+  err = ''
+  try:
+    if config.has_option('agent', 'public_hostname_script'):
+      scriptname = config.get('agent', 'public_hostname_script')
+      output = subprocess.Popen([scriptname], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+      out, err = output.communicate()
+      if (0 == output.returncode and 0 != len(out.strip())):
+        return out.strip()
+  except:
+    #ignore for now. 
+    trace_info = traceback.format_exc()
+    logger.info("Error using the scriptname:" +  trace_info 
+                + " :out " + out + " :err " + err)
+    logger.info("Defaulting to fqdn.")
+    
   # future - do an agent entry for this too
   try:
     handle = urllib2.urlopen('http://169.254.169.254/latest/meta-data/public-hostname', '', 2)

+ 24 - 1
ambari-agent/src/test/python/TestHostname.py

@@ -40,7 +40,6 @@ class TestHostname(TestCase):
     os.chmod(tmpname, os.stat(tmpname).st_mode | stat.S_IXUSR)
 
     tmpfile = file(tmpname, "w+")
-
     config = AmbariConfig.config
     try:
       tmpfile.write("#!/bin/sh\n\necho 'test.example.com'")
@@ -55,5 +54,29 @@ class TestHostname(TestCase):
 
     pass
 
+  def test_public_hostname_override(self):
+    fd = tempfile.mkstemp(text=True)
+    tmpname = fd[1]
+    os.close(fd[0])
+    os.chmod(tmpname, os.stat(tmpname).st_mode | stat.S_IXUSR)
+   
+    tmpfile = file(tmpname, "w+")
+
+    config = AmbariConfig.config
+    try:
+      tmpfile.write("#!/bin/sh\n\necho 'test.example.com'")
+      tmpfile.close()
+
+      config.set('agent', 'public_hostname_script', tmpname)
+
+      self.assertEquals(hostname.public_hostname(), 'test.example.com', 
+                        "expected hostname 'test.example.com'")
+    finally:
+      os.remove(tmpname)
+      config.remove_option('agent', 'public_hostname_script')
+
+    pass
+
+