Author: mahadev
Date: Fri Jan 25 23:20:45 2013
New Revision: 1438759

URL: http://svn.apache.org/viewvc?rev=1438759&view=rev
Log:
AMBARI-1255. Make the agent hostname determination scriptable. (mahadev)

Added:
    incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/hostname.py
    incubator/ambari/trunk/ambari-agent/src/test/python/TestHostname.py
Modified:
    incubator/ambari/trunk/CHANGES.txt
    
incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/Controller.py
    
incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/Heartbeat.py
    incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/Register.py
    incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/security.py
    
incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/state/host/HostImpl.java

Modified: incubator/ambari/trunk/CHANGES.txt
URL: 
http://svn.apache.org/viewvc/incubator/ambari/trunk/CHANGES.txt?rev=1438759&r1=1438758&r2=1438759&view=diff
==============================================================================
--- incubator/ambari/trunk/CHANGES.txt (original)
+++ incubator/ambari/trunk/CHANGES.txt Fri Jan 25 23:20:45 2013
@@ -26,6 +26,9 @@ Trunk (unreleased changes):
  AMBARI-1194. API support for cascade delete of a specified cluster
  (Tom Beerbower via mahadev)
 
+ AMBARI-1255. Make the agent hostname determination scriptable. 
+ (mahadev)
+
  IMPROVEMENTS
 
  AMBARI-1254. Modify App Browser to use server-side paging/sorting/filtering.

Modified: 
incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/Controller.py
URL: 
http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/Controller.py?rev=1438759&r1=1438758&r2=1438759&view=diff
==============================================================================
--- 
incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/Controller.py 
(original)
+++ 
incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/Controller.py 
Fri Jan 25 23:20:45 2013
@@ -22,7 +22,7 @@ import logging
 import logging.handlers
 import signal
 import json
-import socket
+import hostname
 import sys, traceback
 import time
 import threading
@@ -53,7 +53,7 @@ class Controller(threading.Thread):
     self.safeMode = True
     self.credential = None
     self.config = config
-    self.hostname = socket.gethostname()
+    self.hostname = hostname.hostname()
     server_secured_url = 'https://' + config.get('server', 'hostname') + ':' + 
config.get('server', 'secured_url_port')
     self.registerUrl = server_secured_url + '/agent/v1/register/' + 
self.hostname
     self.heartbeatUrl = server_secured_url + '/agent/v1/heartbeat/' + 
self.hostname

Modified: 
incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/Heartbeat.py
URL: 
http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/Heartbeat.py?rev=1438759&r1=1438758&r2=1438759&view=diff
==============================================================================
--- 
incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/Heartbeat.py 
(original)
+++ 
incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/Heartbeat.py 
Fri Jan 25 23:20:45 2013
@@ -25,7 +25,7 @@ from ActionQueue import ActionQueue
 from ServerStatus import ServerStatus
 import NetUtil
 import AmbariConfig
-import socket
+import hostname
 import time
 import traceback
 from pprint import pprint, pformat
@@ -51,7 +51,7 @@ class Heartbeat:
     
     heartbeat = { 'responseId'        : int(id),
                   'timestamp'         : timestamp,
-                  'hostname'          : socket.getfqdn(),
+                  'hostname'          : hostname.hostname(),
                   'nodeStatus'        : nodeStatus
                 }
     if (int(id) >= 0) and state_interval > 0 and (int(id) % state_interval) == 
0:

Modified: 
incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/Register.py
URL: 
http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/Register.py?rev=1438759&r1=1438758&r2=1438759&view=diff
==============================================================================
--- 
incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/Register.py 
(original)
+++ 
incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/Register.py 
Fri Jan 25 23:20:45 2013
@@ -23,7 +23,7 @@ import json
 from Hardware import Hardware
 from ActionQueue import ActionQueue
 from ServerStatus import ServerStatus
-import socket
+import hostname
 import time
 import urllib2
 import subprocess
@@ -37,15 +37,6 @@ class Register:
   def __init__(self):
     self.hardware = Hardware()
 
-  def pfqdn(self):
-    try:
-      handle = 
urllib2.urlopen('http://169.254.169.254/latest/meta-data/public-hostname', '', 
2)
-      str = handle.read()
-      handle.close()
-      return str
-    except Exception, e:
-      return socket.getfqdn()
-
   def build(self, id='-1'):
     global clusterId, clusterDefinitionRevision, firstContact
     timestamp = int(time.time()*1000)
@@ -56,8 +47,8 @@ class Register:
     
     register = { 'responseId'        : int(id),
                   'timestamp'         : timestamp,
-                  'hostname'          : socket.getfqdn(),
-                  'publicHostname'    : self.pfqdn(),
+                  'hostname'          : hostname.hostname(),
+                  'publicHostname'    : hostname.public_hostname(),
                   'hardwareProfile'   : self.hardware.get(),
                   'agentEnv'          : agentEnv
                 }

Added: 
incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/hostname.py
URL: 
http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/hostname.py?rev=1438759&view=auto
==============================================================================
--- 
incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/hostname.py 
(added)
+++ 
incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/hostname.py 
Fri Jan 25 23:20:45 2013
@@ -0,0 +1,57 @@
+#!/usr/bin/env python2.6
+
+'''
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+'''
+
+import socket
+import subprocess
+import AmbariConfig
+import urllib2
+
+def hostname():
+  config = AmbariConfig.config
+  try:
+    scriptname = config.get('agent', '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())):
+        return out.strip()
+      else:
+        return socket.getfqdn()
+    except:
+      return socket.getfqdn()
+  except:
+    return socket.getfqdn()
+
+def public_hostname():
+  # future - do an agent entry for this too
+  try:
+    handle = 
urllib2.urlopen('http://169.254.169.254/latest/meta-data/public-hostname', '', 
2)
+    str = handle.read()
+    handle.close()
+    return str
+  except Exception, e:
+    return socket.getfqdn()
+
+def main(argv=None):
+  print hostname()
+  print public_hostname()
+
+if __name__ == '__main__':
+  main()

Modified: 
incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/security.py
URL: 
http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/security.py?rev=1438759&r1=1438758&r2=1438759&view=diff
==============================================================================
--- 
incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/security.py 
(original)
+++ 
incubator/ambari/trunk/ambari-agent/src/main/python/ambari_agent/security.py 
Fri Jan 25 23:20:45 2013
@@ -20,6 +20,7 @@ import httplib
 import urllib2
 from urllib2 import Request
 import socket
+import hostname
 import ssl
 import os
 import logging
@@ -52,9 +53,9 @@ class VerifiedHTTPSConnection(httplib.HT
       self.sock = sock
       self._tunnel()
     agent_key = AmbariConfig.config.get('security', 'keysdir') + os.sep + \
-     socket.gethostname() + ".key"
+     hostname.hostname() + ".key"
     agent_crt = AmbariConfig.config.get('security', 'keysdir') + os.sep \
-    + socket.gethostname() + ".crt" 
+    + hostname.hostname() + ".crt" 
     server_crt = AmbariConfig.config.get('security', 'keysdir') + os.sep \
     + "ca.crt"
     
@@ -112,13 +113,13 @@ class CertificateManager():
     
   def getAgentKeyName(self):
     keysdir = self.config.get('security', 'keysdir')
-    return keysdir + os.sep + socket.gethostname() + ".key"
+    return keysdir + os.sep + hostname.hostname() + ".key"
   def getAgentCrtName(self):
     keysdir = self.config.get('security', 'keysdir')
-    return keysdir + os.sep + socket.gethostname() + ".crt"
+    return keysdir + os.sep + hostname.hostname() + ".crt"
   def getAgentCrtReqName(self):
     keysdir = self.config.get('security', 'keysdir')
-    return keysdir + os.sep + socket.gethostname() + ".csr"
+    return keysdir + os.sep + hostname.hostname() + ".csr"
   def getSrvrCrtName(self):
     keysdir = self.config.get('security', 'keysdir')
     return keysdir + os.sep + "ca.crt"
@@ -161,7 +162,7 @@ class CertificateManager():
     srvr_crt_f.write(response)
       
   def reqSignCrt(self):
-    sign_crt_req_url = self.server_url + '/certs/' + socket.gethostname()
+    sign_crt_req_url = self.server_url + '/certs/' + hostname.hostname()
     agent_crt_req_f = open(self.getAgentCrtReqName())
     agent_crt_req_content = agent_crt_req_f.read()
     passphrase_env_var = self.config.get('security', 'passphrase_env_var_name')
@@ -184,7 +185,7 @@ class CertificateManager():
       logger.error("Certificate signing failed")
 
   def genAgentCrtReq(self):
-    generate_script = GEN_AGENT_KEY % {'hostname': socket.gethostname(),
+    generate_script = GEN_AGENT_KEY % {'hostname': hostname.hostname(),
                                      'keysdir' : self.config.get('security', 
'keysdir')}
     logger.info(generate_script)
     p = Popen([generate_script], shell=True, stdout=PIPE)

Added: incubator/ambari/trunk/ambari-agent/src/test/python/TestHostname.py
URL: 
http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-agent/src/test/python/TestHostname.py?rev=1438759&view=auto
==============================================================================
--- incubator/ambari/trunk/ambari-agent/src/test/python/TestHostname.py (added)
+++ incubator/ambari/trunk/ambari-agent/src/test/python/TestHostname.py Fri Jan 
25 23:20:45 2013
@@ -0,0 +1,55 @@
+#!/usr/bin/env python2.6
+
+'''
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+'''
+
+from unittest import TestCase
+import ambari_agent.hostname as hostname
+from ambari_agent.AmbariConfig import AmbariConfig
+import socket
+import tempfile
+import shutil
+import os, pprint, json,stat
+
+class TestHostname(TestCase):
+
+  def test_hostname(self):
+    self.assertEquals(hostname.hostname(), socket.gethostname(), "hostname 
should equal the socket-based hostname")
+    pass
+
+  def test_hostname_override(self):
+    tmpname = tempfile.mkstemp(text=True)[1]
+    os.chmod(tmpname, os.stat(tmpname).st_mode | stat.S_IXUSR)
+
+    tmpfile = file(tmpname, "w+")
+
+    try:
+      tmpfile.write("#!/bin/sh\n\necho 'test.example.com'")
+      tmpfile.close()
+
+      config = AmbariConfig().getConfig()
+      config.set('agent', 'hostname_script', tmpname)
+
+      self.assertEquals(hostname.hostname(), 'test.example.com', "expected 
hostname 'test.example.com'")
+    finally:
+      os.remove(tmpname)
+
+    pass
+
+
+

Modified: 
incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/state/host/HostImpl.java
URL: 
http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/state/host/HostImpl.java?rev=1438759&r1=1438758&r2=1438759&view=diff
==============================================================================
--- 
incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/state/host/HostImpl.java
 (original)
+++ 
incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/state/host/HostImpl.java
 Fri Jan 25 23:20:45 2013
@@ -343,16 +343,6 @@ public class HostImpl implements Host {
   public void importHostInfo(HostInfo hostInfo) {
     try {
       writeLock.lock();
-      String fqdn = hostInfo.getFQDN();
-      if (fqdn != null
-          && !fqdn.isEmpty()
-          && !fqdn.equals(getHostName())) {
-        if (! isPersisted()) {
-          setHostName(hostInfo.getHostName());
-        } else {
-          LOG.info("Could not modify hostname of the host that is already 
persisted to DB");
-        }
-      }
 
       if (hostInfo.getIPAddress() != null
           && !hostInfo.getIPAddress().isEmpty()) {


Reply via email to