瀏覽代碼

AMBARI-11689. All status commands fail in non-root agent mode (aonishuk)

Andrew Onishuk 10 年之前
父節點
當前提交
4117e4beea

+ 23 - 15
ambari-common/src/main/python/resource_management/core/sudo.py

@@ -19,12 +19,14 @@ Ambari Agent
 
 """
 
+import time
 import os
 import tempfile
 import shutil
 import stat
 from resource_management.core import shell
 from resource_management.core.logger import Logger
+from resource_management.core.exceptions import Fail
 from ambari_commons.os_check import OSCheck
 
 if os.geteuid() == 0:
@@ -101,6 +103,10 @@ if os.geteuid() == 0:
         self.st_uid, self.st_gid, self.st_mode = stat_val.st_uid, stat_val.st_gid, stat_val.st_mode & 07777
     return Stat(path)
   
+  def kill(pid, signal):
+    os.kill(pid, signal)
+    
+    
 else:
 
   # os.chown replacement
@@ -143,28 +149,23 @@ else:
   def rmtree(path):
     shell.checked_call(["rm","-rf", path], sudo=True)
     
-  def stat(path):
-    class Stat:
-      def __init__(self, path):
-        stat_val = os.stat(path)
-        self.st_uid, self.st_gid, self.st_mode = stat_val.st_uid, stat_val.st_gid, stat_val.st_mode & 07777
-        
-    return Stat(path)
-    
   # fp.write replacement
   def create_file(filename, content, encoding=None):
     """
     if content is None, create empty file
     """
-    tmpf = tempfile.NamedTemporaryFile()
+    content = content if content else ""
+    content = content.encode(encoding) if encoding else content
     
-    if content:
-      content = content.encode(encoding) if encoding else content
-      with open(tmpf.name, "wb") as fp:
-        fp.write(content)
+    tmpf_name = tempfile.gettempdir() + os.sep + tempfile.template + str(time.time())
     
-    with tmpf:    
-      shell.checked_call(["cp", "-f", tmpf.name, filename], sudo=True)
+    try:
+      with open(tmpf_name, "wb") as fp:
+        fp.write(content)
+        
+      shell.checked_call(["cp", "-f", tmpf_name, filename], sudo=True)
+    finally:
+      os.unlink(tmpf_name)
       
   # fp.read replacement
   def read_file(filename, encoding=None):
@@ -217,3 +218,10 @@ else:
           stat_val = os.stat(path)
           self.st_uid, self.st_gid, self.st_mode = stat_val.st_uid, stat_val.st_gid, stat_val.st_mode & 07777
     return Stat(path)
+  
+  # os.kill replacement
+  def kill(pid, signal):
+    try:
+      shell.checked_call(["kill", "-"+str(signal), str(pid)], sudo=True)
+    except Fail as ex:
+      raise OSError(str(ex))

+ 1 - 1
ambari-common/src/main/python/resource_management/libraries/functions/check_process_status.py

@@ -54,7 +54,7 @@ def check_process_status(pid_file):
     # If sig is 0, then no signal is sent, but error checking is still
     # performed; this can be used to check for the existence of a
     # process ID or process group ID.
-    os.kill(pid, 0)
+    sudo.kill(pid, 0)
   except OSError:
     Logger.info("Process with pid {0} is not running. Stale pid file"
               " at {1}".format(pid, pid_file))

+ 3 - 1
ambari-common/src/main/python/resource_management/libraries/functions/flume_agent_helper.py

@@ -25,6 +25,8 @@ import time
 
 from resource_management.core.exceptions import ComponentIsNotRunning
 from resource_management.libraries.functions import check_process_status
+from resource_management.core.logger import Logger
+from resource_management.libraries.functions import format
 
 
 def get_flume_status(flume_conf_directory, flume_run_directory):
@@ -112,7 +114,7 @@ def get_live_status(pid_file, flume_conf_directory):
       res['sinks_count'] = meta['sinks_count']
       res['channels_count'] = meta['channels_count']
   except:
-    pass
+    Logger.logger.exception(format("Error reading {flume_agent_meta_file}: "))
 
   return res