Forráskód Böngészése

AMBARI-8318 - Fix PYTHONPATH for ambari_agent.alerts and ambari_agents.apscheduler (Florian Barca via abaranchuk)

Artem Baranchuk 10 éve
szülő
commit
d4f9cb13d6

+ 18 - 5
ambari-agent/conf/windows/service_wrapper.py

@@ -24,18 +24,15 @@ import win32api
 import win32event
 import win32event
 import win32service
 import win32service
 
 
-from ambari_commons.ambari_service import AmbariService
+from ambari_commons.ambari_service import AmbariService, ENV_PYTHON_PATH
 from ambari_commons.exceptions import *
 from ambari_commons.exceptions import *
 from ambari_commons.logging_utils import *
 from ambari_commons.logging_utils import *
 from ambari_commons.os_windows import WinServiceController
 from ambari_commons.os_windows import WinServiceController
-from ambari_agent.AmbariConfig import *
+from ambari_agent.AmbariConfig import AmbariConfig, SETUP_ACTION, START_ACTION, DEBUG_ACTION, STOP_ACTION, STATUS_ACTION
 from ambari_agent.HeartbeatHandlers_windows import HeartbeatStopHandler
 from ambari_agent.HeartbeatHandlers_windows import HeartbeatStopHandler
 
 
 AMBARI_VERSION_VAR = "AMBARI_VERSION_VAR"
 AMBARI_VERSION_VAR = "AMBARI_VERSION_VAR"
 
 
-ENV_PYTHONPATH = "PYTHONPATH"
-
-
 def parse_options():
 def parse_options():
   # parse env cmd
   # parse env cmd
   with open(os.path.join(os.getcwd(), "ambari-env.cmd"), "r") as env_cmd:
   with open(os.path.join(os.getcwd(), "ambari-env.cmd"), "r") as env_cmd:
@@ -62,6 +59,22 @@ class AmbariAgentService(AmbariService):
 
 
   heartbeat_stop_handler = None
   heartbeat_stop_handler = None
 
 
+  # Adds the necessary script dir to the Python's modules path
+  def _adjustPythonPath(self, current_dir):
+    iPos = 0
+    python_path = os.path.join(current_dir, "sbin")
+    sys.path.insert(iPos, python_path)
+
+    # Add the alerts and apscheduler subdirs to the path, for the imports to work correctly without
+    #  having to modify the files in these 2 subdirectories
+    agent_path = os.path.join(current_dir, "sbin", "ambari_agent")
+    iPos += 1
+    sys.path.insert(iPos, agent_path)
+    for subdir in os.listdir(agent_path):
+      full_subdir = os.path.join(agent_path, subdir)
+      iPos += 1
+      sys.path.insert(iPos, full_subdir)
+
   def SvcDoRun(self):
   def SvcDoRun(self):
     parse_options()
     parse_options()
     self.redirect_output_streams()
     self.redirect_output_streams()

+ 6 - 6
ambari-common/src/main/python/ambari_commons/ambari_service.py

@@ -33,6 +33,9 @@ class AmbariService(WinService):
   _svc_display_name_ = "Ambari Service"
   _svc_display_name_ = "Ambari Service"
   _svc_description_ = "Ambari Service"
   _svc_description_ = "Ambari Service"
 
 
+  def _adjustPythonPath(self, current_dir):
+    pass
+
   # Sets the current dir and adjusts the PYTHONPATH env variable before calling SvcDoRun()
   # Sets the current dir and adjusts the PYTHONPATH env variable before calling SvcDoRun()
   def SvcRun(self):
   def SvcRun(self):
     self.ReportServiceStatus(win32service.SERVICE_START_PENDING)
     self.ReportServiceStatus(win32service.SERVICE_START_PENDING)
@@ -53,13 +56,10 @@ class AmbariService(WinService):
       # the script resides in the sbin/ambari_commons subdir
       # the script resides in the sbin/ambari_commons subdir
       self.options.current_dir = os.path.normpath(script_path + "\\..\\..")
       self.options.current_dir = os.path.normpath(script_path + "\\..\\..")
       os.chdir(self.options.current_dir)
       os.chdir(self.options.current_dir)
+    else:
+      self.options.current_dir = os.getcwd()
 
 
-      python_path = os.path.normpath(script_path + "\\..")
-
-      #update the environment vars: set PYTHONPATH = $script_dir\sbin;%PYTHONPATH%
-      if os.environ.has_key(ENV_PYTHON_PATH):
-        python_path += os.pathsep + os.environ[ENV_PYTHON_PATH]
-      os.environ[ENV_PYTHON_PATH] = python_path
+    self._adjustPythonPath(self.options.current_dir)
 
 
     self.SvcDoRun()
     self.SvcDoRun()
     pass
     pass

+ 6 - 1
ambari-server/src/main/python/ambari-server-windows.py

@@ -20,7 +20,7 @@ limitations under the License.
 
 
 import optparse
 import optparse
 
 
-from ambari_commons.ambari_service import AmbariService
+from ambari_commons.ambari_service import AmbariService, ENV_PYTHON_PATH
 from ambari_commons.logging_utils import *
 from ambari_commons.logging_utils import *
 from ambari_commons.os_utils import remove_file
 from ambari_commons.os_utils import remove_file
 from ambari_commons.os_windows import SvcStatusCallback
 from ambari_commons.os_windows import SvcStatusCallback
@@ -90,6 +90,11 @@ class AmbariServerService(AmbariService):
 
 
   AmbariService._AdjustServiceVersion()
   AmbariService._AdjustServiceVersion()
 
 
+  # Adds the necessary script dir to the Python's modules path
+  def _adjustPythonPath(self, current_dir):
+    python_path = os.path.join(current_dir, "sbin")
+    sys.path.insert(0, python_path)
+
   def SvcDoRun(self):
   def SvcDoRun(self):
     scmStatus = SvcStatusCallback(self)
     scmStatus = SvcStatusCallback(self)