incubator-ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ey...@apache.org
Subject svn commit: r1183446 - in /incubator/ambari/trunk/agent/src/main/python/ambari_agent: ActionQueue.py AmbariConfig.py Controller.py FileUtil.py main.py
Date Fri, 14 Oct 2011 18:00:10 GMT
Author: eyang
Date: Fri Oct 14 18:00:10 2011
New Revision: 1183446

URL: http://svn.apache.org/viewvc?rev=1183446&view=rev
Log:
AMBARI-75. Centralize agent configuration parsing. (Eric Yang)

Added:
    incubator/ambari/trunk/agent/src/main/python/ambari_agent/AmbariConfig.py
Modified:
    incubator/ambari/trunk/agent/src/main/python/ambari_agent/ActionQueue.py
    incubator/ambari/trunk/agent/src/main/python/ambari_agent/Controller.py
    incubator/ambari/trunk/agent/src/main/python/ambari_agent/FileUtil.py
    incubator/ambari/trunk/agent/src/main/python/ambari_agent/main.py

Modified: incubator/ambari/trunk/agent/src/main/python/ambari_agent/ActionQueue.py
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/agent/src/main/python/ambari_agent/ActionQueue.py?rev=1183446&r1=1183445&r2=1183446&view=diff
==============================================================================
--- incubator/ambari/trunk/agent/src/main/python/ambari_agent/ActionQueue.py (original)
+++ incubator/ambari/trunk/agent/src/main/python/ambari_agent/ActionQueue.py Fri Oct 14 18:00:10
2011
@@ -37,9 +37,10 @@ class ActionQueue(threading.Thread):
   clusterId = 'unknown'
   clusterDefinitionRevision = 0
 
-  def __init__(self):
+  def __init__(self, config):
     global clusterId, clusterDefinitionRevision 
     threading.Thread.__init__(self)
+    self.config = config
     self.sh = shellRunner()
 
   def put(self, response):

Added: incubator/ambari/trunk/agent/src/main/python/ambari_agent/AmbariConfig.py
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/agent/src/main/python/ambari_agent/AmbariConfig.py?rev=1183446&view=auto
==============================================================================
--- incubator/ambari/trunk/agent/src/main/python/ambari_agent/AmbariConfig.py (added)
+++ incubator/ambari/trunk/agent/src/main/python/ambari_agent/AmbariConfig.py Fri Oct 14 18:00:10
2011
@@ -0,0 +1,47 @@
+#!/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 logging
+import logging.handlers
+import ConfigParser
+import StringIO
+
+config = ConfigParser.RawConfigParser()
+content = """
+[controller]
+url=http://localhost:4080
+user=controller
+password=controller
+
+[agent]
+prefix=/tmp/ambari
+"""
+s = StringIO.StringIO(content)
+config.readfp(s)
+
+def setConfig(customConfig):
+  global config
+  config = customConfig
+
+def main():
+  print config
+
+if __name__ == "__main__":
+  main()

Modified: incubator/ambari/trunk/agent/src/main/python/ambari_agent/Controller.py
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/agent/src/main/python/ambari_agent/Controller.py?rev=1183446&r1=1183445&r2=1183446&view=diff
==============================================================================
--- incubator/ambari/trunk/agent/src/main/python/ambari_agent/Controller.py (original)
+++ incubator/ambari/trunk/agent/src/main/python/ambari_agent/Controller.py Fri Oct 14 18:00:10
2011
@@ -28,25 +28,30 @@ import time
 import threading
 import urllib2
 from urllib2 import Request, urlopen, URLError
+import AmbariConfig
 from Heartbeat import Heartbeat
 from ActionQueue import ActionQueue
-
 from optparse import OptionParser
 
 logger = logging.getLogger()
 
 class Controller(threading.Thread):
 
-  def __init__(self, url, credential=None):
+  def __init__(self, config):
     threading.Thread.__init__(self)
     logger.debug('Initializing Controller RPC thread.')
     self.lock = threading.Lock()
     self.safeMode = True
-    self.credential = credential
-    self.url = url + '/agent/controller/heartbeat/' + socket.gethostname()
+    self.credential = None
+    self.config = config
+    if(config.get('controller', 'user')!=None and config.get('controller', 'password')!=None):
+      self.credential = { 'user' : config.get('controller', 'user'),
+                          'password' : config.get('controller', 'password')
+      }
+    self.url = config.get('controller', 'url') + '/agent/controller/heartbeat/' + socket.gethostname()
 
   def start(self):
-    self.actionQueue = ActionQueue()
+    self.actionQueue = ActionQueue(self.config)
     self.actionQueue.start()
     self.heartbeat = Heartbeat(self.actionQueue)
     logger.info("Controller connection established.")
@@ -67,6 +72,7 @@ class Controller(threading.Thread):
     while True:
       try:
         data = json.dumps(self.heartbeat.build(id))
+        logger.info(data)
         req = urllib2.Request(self.url, data, {'Content-Type': 'application/json'})
         f = urllib2.urlopen(req)
         response = f.read()
@@ -88,24 +94,7 @@ def main(argv=None):
   # Allow Ctrl-C
   signal.signal(signal.SIGINT, signal.SIG_DFL)
 
-  parser = OptionParser()
-  parser.add_option('-v', '--verbose',
-    dest='verbose',
-    default=False,
-    action='store_true',
-    help='Verbose logging. (default: %default)')
-  parser.add_option('--controller',
-    dest='url',
-    default='http://localhost:4080',
-    help='Controller URL. (default: %default)')
-  global options
-  global args
-  (options, args) = parser.parse_args()
-
-  if options.verbose:
-    logger.setLevel(logging.DEBUG)
-  else:
-    logger.setLevel(logging.INFO)
+  logger.setLevel(logging.INFO)
   formatter = logging.Formatter("%(asctime)s %(filename)s:%(lineno)d - %(message)s")
   stream_handler = logging.StreamHandler()
   stream_handler.setFormatter(formatter)
@@ -113,8 +102,8 @@ def main(argv=None):
 
   logger.info('Starting Controller RPC Thread: %s' % ' '.join(sys.argv))
 
-  credential = { 'user' : 'controller', 'password' : 'controller' }
-  controller = Controller(options.url, credential)
+  config = AmbariConfig.config
+  controller = Controller(config)
   controller.start()
   controller.run()
 

Modified: incubator/ambari/trunk/agent/src/main/python/ambari_agent/FileUtil.py
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/agent/src/main/python/ambari_agent/FileUtil.py?rev=1183446&r1=1183445&r2=1183446&view=diff
==============================================================================
--- incubator/ambari/trunk/agent/src/main/python/ambari_agent/FileUtil.py (original)
+++ incubator/ambari/trunk/agent/src/main/python/ambari_agent/FileUtil.py Fri Oct 14 18:00:10
2011
@@ -28,15 +28,16 @@ import sys, traceback
 import ConfigParser
 import shutil
 import StringIO
+import AmbariConfig
 
 logger = logging.getLogger()
 
 def writeFile(action, result):
-  global config
   oldCwd = os.getcwd()
   fileInfo = action['file']
   try:
-    path = config.get('agent','prefix')+"/clusters/"+action['clusterId']+"-"+action['role']
+    path = AmbariConfig.config.get('agent','prefix')+"/clusters/"+action['clusterId']+"-"+action['role']
+    logger.info("path: %s" % path)
     os.chdir(path)
     user=fileInfo['owner']
     group=fileInfo['group']
@@ -76,9 +77,8 @@ def writeFile(action, result):
   return result
 
 def createStructure(action, result):
-  global config
   try:
-    path = config.get('agent','prefix')+"/clusters/"+action['clusterId']+"-"+action['role']
+    path = AmbariConfig.config.get('agent','prefix')+"/clusters/"+action['clusterId']+"-"+action['role']
     os.makedirs(path+"/stack")
     os.makedirs(path+"/logs")
     os.makedirs(path+"/data")
@@ -91,9 +91,8 @@ def createStructure(action, result):
   return result
 
 def deleteStructure(action, result):
-  global config
   try:
-    path = config.get('agent','prefix')+"/clusters/"+action['clusterId']+"-"+action['role']
+    path = AmbariConfig.config.get('agent','prefix')+"/clusters/"+action['clusterId']+"-"+action['role']
     if os.path.exists(path):
       shutil.rmtree(path)
     result['exitCode'] = 0
@@ -103,6 +102,11 @@ def deleteStructure(action, result):
   return result
 
 def main():
+
+  action = { 'clusterId' : 'abc', 'role' : 'hdfs' }
+  result = {}
+  print createStructure(action, result)
+
   configFile = {
     "data"       : "test", 
     "owner"      : os.getuid(), 
@@ -114,6 +118,7 @@ def main():
   action = { 'file' : configFile }
   result = { }
   print writeFile(action, result)
+
   configFile = { 
     "data"       : "test", 
     "owner"      : "eyang", 
@@ -126,17 +131,6 @@ def main():
   action = { 'file' : configFile }
   print writeFile(action, result)
 
-  global config
-  config = ConfigParser.RawConfigParser()
-  content = """
-[agent]
-prefix=/tmp/ambari
-  """
-  s = StringIO.StringIO(content)
-  config.readfp(s);
-  action = { 'clusterId' : 'abc', 'role' : 'hdfs' }
-  result = {}
-  print createStructure(action, result)
   print deleteStructure(action, result)
 
 if __name__ == "__main__":

Modified: incubator/ambari/trunk/agent/src/main/python/ambari_agent/main.py
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/agent/src/main/python/ambari_agent/main.py?rev=1183446&r1=1183445&r2=1183446&view=diff
==============================================================================
--- incubator/ambari/trunk/agent/src/main/python/ambari_agent/main.py (original)
+++ incubator/ambari/trunk/agent/src/main/python/ambari_agent/main.py Fri Oct 14 18:00:10
2011
@@ -28,6 +28,7 @@ import time
 import ConfigParser
 from createDaemon import createDaemon
 from Controller import Controller
+import AmbariConfig
 
 logger = logging.getLogger()
 
@@ -59,7 +60,7 @@ def debug(sig, frame):
     message  = "Signal recieved : entering python shell.\nTraceback:\n"
     message += ''.join(traceback.format_stack(frame))
     logger.info(message)
-      
+
 def main():
   global config
   default_cfg = { 'agent' : { 'prefix' : '/home/ambari' } }
@@ -101,25 +102,20 @@ def main():
   credential = None
 
   # Check for ambari configuration file.
-  if(os.path.exists('/etc/ambari/ambari.ini')):
-    config.read('/etc/ambari/ambari.ini')
-    try:
-      credential = {}
-      credential['user'] = config.get('controller', 'user')
-      credential['password'] = config.get('controller', 'password')
-      controllerUrl = config.get('controller', 'url')
-    except Exception, err:
-      credential = None
-      controllerUrl = "http://localhost:4080"
-  else:
-    credential = None
-    controllerUrl = "http://localhost:4080"
-  logger.info("Connecting to controller at: "+controllerUrl)
+  try:
+    config = AmbariConfig.config
+    if(os.path.exists('/etc/ambari/ambari.ini')):
+      config.read('/etc/ambari/ambari.ini')
+      AmbariConfig.setConfig(config)
+    else:
+      raise Exception("No config found, use default")
+  except Exception, err:
+    logger.warn(err)
+
+  logger.info("Connecting to controller at: "+config.get('controller', 'url'))
 
   # Launch Controller communication
-  print controllerUrl
-  print credential
-  controller = Controller(controllerUrl, credential) 
+  controller = Controller(config) 
   controller.start()
   controller.run()
   logger.info("finished")



Mime
View raw message