Merge "Removed empty ResourceResolutionControllerTest"
[ccsdk/cds.git] / ms / command-executor / src / main / python / command_executor_handler.py
index 1fb3e26..305c83e 100644 (file)
@@ -19,6 +19,7 @@ from subprocess import CalledProcessError, PIPE
 import logging
 import os
 import subprocess
+import sys
 import virtualenv
 import venv
 import utils
@@ -37,10 +38,7 @@ class CommandExecutorHandler():
         self.installed = self.venv_home + '/.installed'
 
     def is_installed(self):
-        if os.path.exists(self.installed):
-            return True
-        else:
-            return False
+        return os.path.exists(self.installed)
 
     def prepare_env(self, request, results):
         if not self.is_installed():
@@ -65,11 +63,29 @@ class CommandExecutorHandler():
         return True
 
     def execute_command(self, request, results):
-        # if not self.activate_venv():
-        #     return False
 
+        if not self.activate_venv():
+            return False
+
+        cmd = "cd " + self.venv_home
+
+        if "ansible-playbook" in request.command:
+            cmd = cmd + "; " + request.command + " -e 'ansible_python_interpreter=" + self.venv_home + "/bin/python'"
+        else:
+            cmd = cmd + "; " + request.command
+
+        self.logger.info("Command: {}".format(cmd))
         try:
-            results.append(os.popen(request.command).read())
+            with subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
+                                  shell=True, bufsize=1, universal_newlines=True) as newProcess:
+                while True:
+                    output = newProcess.stdout.readline()
+                    if output == '' and newProcess.poll() is not None:
+                        break
+                    if output:
+                        self.logger.info(output.strip())
+                        results.append(output.strip())
+                    rc = newProcess.poll()
         except Exception as e:
             self.logger.info("{} - Failed to execute command. Error: {}".format(self.blueprint_id, e))
             results.append(e)
@@ -147,9 +163,16 @@ class CommandExecutorHandler():
     def activate_venv(self):
         self.logger.info("{} - Activate Python Virtual Environment".format(self.blueprint_id))
 
+        # Fix: The python generated activate_this.py script concatenates the env bin dir to PATH on every call
+        #      eventually this process PATH variable was so big (128Kb) that no child process could be spawn
+        #      This script will remove all duplicates; while keeping the order of the PATH folders
+        fixpathenvvar = "os.environ['PATH']=os.pathsep.join(list(dict.fromkeys(os.environ['PATH'].split(':'))))"
+
         path = "%s/bin/activate_this.py" % self.venv_home
         try:
             exec (open(path).read(), {'__file__': path})
+            exec (fixpathenvvar)
+            self.logger.info("Running with PATH : {}".format(os.environ['PATH']))
             return True
         except Exception as err:
             self.logger.info(