aboutsummaryrefslogtreecommitdiffstats
path: root/ms/command-executor/src/main/python/command_executor_handler.py
diff options
context:
space:
mode:
Diffstat (limited to 'ms/command-executor/src/main/python/command_executor_handler.py')
-rw-r--r--ms/command-executor/src/main/python/command_executor_handler.py79
1 files changed, 66 insertions, 13 deletions
diff --git a/ms/command-executor/src/main/python/command_executor_handler.py b/ms/command-executor/src/main/python/command_executor_handler.py
index 4ae575b0f..248e44308 100644
--- a/ms/command-executor/src/main/python/command_executor_handler.py
+++ b/ms/command-executor/src/main/python/command_executor_handler.py
@@ -13,32 +13,51 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
+from builtins import Exception, open, dict
+from subprocess import CalledProcessError, PIPE
+
import logging
import os
import subprocess
import virtualenv
import venv
-from builtins import Exception, open, dict
-from subprocess import CalledProcessError, PIPE
-
import utils
+import proto.CommandExecutor_pb2 as CommandExecutor_pb2
-class CommandExecutorHandler:
+class CommandExecutorHandler():
def __init__(self, request):
+ self.request = request
self.logger = logging.getLogger(self.__class__.__name__)
self.blueprint_id = utils.get_blueprint_id(request)
self.venv_home = '/opt/app/onap/blueprints/deploy/' + self.blueprint_id
+ self.installed = self.venv_home + '/.installed'
- def prepare_env(self, request, results):
- self.create_venv()
- if not self.activate_venv():
+ def is_installed(self):
+ if os.path.exists(self.installed):
+ return True
+ else:
return False
- for package in request.packages:
- if not self.install(package, results):
+ def prepare_env(self, request, results):
+ if not self.is_installed():
+ self.create_venv()
+ if not self.activate_venv():
+ return False
+
+ f = open(self.installed, "w+")
+ if not self.install_packages(request, CommandExecutor_pb2.PYTHON, f, results):
+ return False
+ f.write("\r\n")
+ results.append("\n")
+ if not self.install_packages(request, CommandExecutor_pb2.ANSIBLE, f, results):
return False
+ f.close()
+ else:
+ f = open(self.installed, "r")
+ results.append(f.read())
+ f.close()
# deactivate_venv(blueprint_id)
return True
@@ -57,16 +76,50 @@ class CommandExecutorHandler:
# deactivate_venv(blueprint_id)
return True
- def install(self, package, results):
- self.logger.info("{} - Install package({}) in Python Virtual Environment".format(self.blueprint_id, package))
+ def install_packages(self, request, type, f, results):
+ for package in request.packages:
+ if package.type == type:
+ f.write("Installed %s packages:\r\n" % CommandExecutor_pb2.PackageType.Name(type))
+ for python_package in package.package:
+ f.write(" %s\r\n" % python_package)
+ if package.type == CommandExecutor_pb2.PYTHON:
+ success = self.install_python_packages(python_package, results)
+ else:
+ success = self.install_ansible_packages(python_package, results)
+ if not success:
+ f.close()
+ os.remove(self.installed)
+ return False
+ return True
+
+ def install_python_packages(self, package, results):
+ self.logger.info(
+ "{} - Install Python package({}) in Python Virtual Environment".format(self.blueprint_id, package))
command = ["pip", "install", package]
env = dict(os.environ)
- # fixme - parameterize
- # env['https_proxy'] = "https://fastweb.int.bell.ca:8083"
+ env['https_proxy'] = os.environ['https_proxy']
+
+ try:
+ results.append(subprocess.run(command, check=True, stdout=PIPE, stderr=PIPE, env=env).stdout.decode())
+ results.append("\n")
+ return True
+ except CalledProcessError as e:
+ results.append(e.stderr.decode())
+ return False
+
+ def install_ansible_packages(self, package, results):
+ self.logger.info(
+ "{} - Install Ansible Role package({}) in Python Virtual Environment".format(self.blueprint_id, package))
+ command = ["ansible-galaxy", "install", package, "-p", "Scripts/ansible/roles"]
+
+ env = dict(os.environ)
+ # ansible galaxy uses https_proxy environment variable, but requires it to be set with http proxy value.
+ env['https_proxy'] = os.environ['http_proxy']
try:
results.append(subprocess.run(command, check=True, stdout=PIPE, stderr=PIPE, env=env).stdout.decode())
+ results.append("\n")
return True
except CalledProcessError as e:
results.append(e.stderr.decode())