You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by sm...@apache.org on 2015/03/26 01:07:27 UTC
ambari git commit: Ambari-7767. Add an agent config to discover
server hostname via a script (Chuan Liu via smohanty)
Repository: ambari
Updated Branches:
refs/heads/trunk 7f8992ec8 -> 6e67329b8
Ambari-7767. Add an agent config to discover server hostname via a script (Chuan Liu via smohanty)
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/6e67329b
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/6e67329b
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/6e67329b
Branch: refs/heads/trunk
Commit: 6e67329b81d429e26ac55f62c46818aa329aaa91
Parents: 7f8992e
Author: Sumit Mohanty <sm...@hortonworks.com>
Authored: Wed Mar 25 16:54:07 2015 -0700
Committer: Sumit Mohanty <sm...@hortonworks.com>
Committed: Wed Mar 25 17:05:44 2015 -0700
----------------------------------------------------------------------
.../main/python/ambari_agent/AmbariConfig.py | 3 +-
.../src/main/python/ambari_agent/Controller.py | 2 +-
.../src/main/python/ambari_agent/hostname.py | 24 ++++++++++++++
.../src/main/python/ambari_agent/main.py | 2 +-
.../src/main/python/ambari_agent/security.py | 4 +--
.../test/python/ambari_agent/TestHostname.py | 34 +++++++++++++++++++-
6 files changed, 63 insertions(+), 6 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/ambari/blob/6e67329b/ambari-agent/src/main/python/ambari_agent/AmbariConfig.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/main/python/ambari_agent/AmbariConfig.py b/ambari-agent/src/main/python/ambari_agent/AmbariConfig.py
index 05af243..ffaaac7 100644
--- a/ambari-agent/src/main/python/ambari_agent/AmbariConfig.py
+++ b/ambari-agent/src/main/python/ambari_agent/AmbariConfig.py
@@ -20,6 +20,7 @@ limitations under the License.
import ConfigParser
import StringIO
+import hostname
import json
from NetUtil import NetUtil
import os
@@ -231,7 +232,7 @@ class AmbariConfig:
def get_api_url(self):
return "%s://%s:%s" % (self.CONNECTION_PROTOCOL,
- self.get('server', 'hostname'),
+ hostname.server_hostname(self),
self.get('server', 'url_port'))
def isTwoWaySSLConnection(self):
http://git-wip-us.apache.org/repos/asf/ambari/blob/6e67329b/ambari-agent/src/main/python/ambari_agent/Controller.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/main/python/ambari_agent/Controller.py b/ambari-agent/src/main/python/ambari_agent/Controller.py
index bb85337..3f3e55d 100644
--- a/ambari-agent/src/main/python/ambari_agent/Controller.py
+++ b/ambari-agent/src/main/python/ambari_agent/Controller.py
@@ -63,7 +63,7 @@ class Controller(threading.Thread):
self.credential = None
self.config = config
self.hostname = hostname.hostname(config)
- self.serverHostname = config.get('server', 'hostname')
+ self.serverHostname = hostname.server_hostname(config)
server_secured_url = 'https://' + self.serverHostname + \
':' + config.get('server', 'secured_url_port')
self.registerUrl = server_secured_url + '/agent/v1/register/' + self.hostname
http://git-wip-us.apache.org/repos/asf/ambari/blob/6e67329b/ambari-agent/src/main/python/ambari_agent/hostname.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/main/python/ambari_agent/hostname.py b/ambari-agent/src/main/python/ambari_agent/hostname.py
index 437eb84..e6140da 100644
--- a/ambari-agent/src/main/python/ambari_agent/hostname.py
+++ b/ambari-agent/src/main/python/ambari_agent/hostname.py
@@ -29,6 +29,7 @@ logger = logging.getLogger()
cached_hostname = None
cached_public_hostname = None
+cached_server_hostname = None
def hostname(config):
@@ -66,6 +67,7 @@ def public_hostname(config):
out, err = output.communicate()
if (0 == output.returncode and 0 != len(out.strip())):
cached_public_hostname = out.strip()
+ logger.info("Read public hostname " + cached_public_hostname + "using agent:public_hostname_script")
return cached_public_hostname
except:
#ignore for now.
@@ -84,9 +86,31 @@ def public_hostname(config):
cached_public_hostname = socket.getfqdn().lower()
return cached_public_hostname
+def server_hostname(config):
+ global cached_server_hostname
+ if cached_server_hostname is not None:
+ return cached_server_hostname
+
+ if config.has_option('server', 'hostname_script'):
+ scriptname = config.get('server', 'hostname_script')
+ try:
+ osStat = subprocess.Popen([scriptname], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+ out, err = osStat.communicate()
+ if (0 == osStat.returncode and 0 != len(out.strip())):
+ cached_server_hostname = out.strip()
+ logger.info("Read server hostname " + cached_server_hostname + "using server:hostname_script")
+ except Exception, err:
+ logger.info("Unable to execute hostname_script for server hostname. " + str(err))
+
+ if cached_server_hostname is None:
+ cached_server_hostname = config.get('server', 'hostname')
+ return cached_server_hostname
+
+
def main(argv=None):
print hostname()
print public_hostname()
+ print server_hostname()
if __name__ == '__main__':
main()
http://git-wip-us.apache.org/repos/asf/ambari/blob/6e67329b/ambari-agent/src/main/python/ambari_agent/main.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/main/python/ambari_agent/main.py b/ambari-agent/src/main/python/ambari_agent/main.py
index fe95e1a..2eeadca 100644
--- a/ambari-agent/src/main/python/ambari_agent/main.py
+++ b/ambari-agent/src/main/python/ambari_agent/main.py
@@ -247,7 +247,7 @@ def main(heartbeat_stop_callback=None):
update_log_level(config)
- server_hostname = config.get('server', 'hostname')
+ server_hostname = hostname.server_hostname(config)
server_url = config.get_api_url()
if not OSCheck.get_os_family() == OSConst.WINSRV_FAMILY:
http://git-wip-us.apache.org/repos/asf/ambari/blob/6e67329b/ambari-agent/src/main/python/ambari_agent/security.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/main/python/ambari_agent/security.py b/ambari-agent/src/main/python/ambari_agent/security.py
index 46eddca..ec14921 100644
--- a/ambari-agent/src/main/python/ambari_agent/security.py
+++ b/ambari-agent/src/main/python/ambari_agent/security.py
@@ -106,7 +106,7 @@ class CachedHTTPSConnection:
def __init__(self, config):
self.connected = False
self.config = config
- self.server = config.get('server', 'hostname')
+ self.server = hostname.server_hostname(config)
self.port = config.get('server', 'secured_url_port')
self.connect()
@@ -143,7 +143,7 @@ class CertificateManager():
self.config = config
self.keysdir = os.path.abspath(self.config.get('security', 'keysdir'))
self.server_crt = self.config.get('security', 'server_crt')
- self.server_url = 'https://' + self.config.get('server', 'hostname') + ':' \
+ self.server_url = 'https://' + hostname.server_hostname(config) + ':' \
+ self.config.get('server', 'url_port')
def getAgentKeyName(self):
http://git-wip-us.apache.org/repos/asf/ambari/blob/6e67329b/ambari-agent/src/test/python/ambari_agent/TestHostname.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/test/python/ambari_agent/TestHostname.py b/ambari-agent/src/test/python/ambari_agent/TestHostname.py
index 87c69b8..8cfcc96 100644
--- a/ambari-agent/src/test/python/ambari_agent/TestHostname.py
+++ b/ambari-agent/src/test/python/ambari_agent/TestHostname.py
@@ -28,7 +28,7 @@ import shutil
import os, pprint, json,stat
from mock.mock import patch, MagicMock
from ambari_commons import OSCheck
-from only_for_platform import only_for_platform, get_platform, PLATFORM_LINUX, PLATFORM_WINDOWS
+from only_for_platform import only_for_platform, get_platform, not_for_platform, PLATFORM_LINUX, PLATFORM_WINDOWS
if get_platform() != PLATFORM_WINDOWS:
os_distro_value = ('Suse','11','Final')
@@ -46,6 +46,38 @@ class TestHostname(TestCase):
"hostname should equal the socket-based hostname")
pass
+ def test_server_hostname(self):
+ hostname.cached_server_hostname = None
+ config = AmbariConfig()
+ default_server_hostname = config.get('server', 'hostname')
+ config.set('server', 'hostname', 'ambari-host')
+ self.assertEquals('ambari-host', hostname.server_hostname(config),
+ "hostname should equal the socket-based hostname")
+ config.set('server', 'hostname', default_server_hostname)
+ pass
+
+ @not_for_platform(PLATFORM_WINDOWS)
+ def test_server_hostname_override(self):
+ hostname.cached_server_hostname = None
+ 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()
+ try:
+ tmpfile.write("#!/bin/sh\n\necho 'test.example.com'")
+ tmpfile.close()
+
+ config.set('server', 'hostname_script', tmpname)
+
+ self.assertEquals(hostname.server_hostname(config), 'test.example.com', "expected hostname 'test.example.com'")
+ finally:
+ os.remove(tmpname)
+ config.remove_option('server', 'hostname_script')
+ pass
+
@only_for_platform(PLATFORM_LINUX)
def test_hostname_override(self):
hostname.cached_hostname = None