# # Copyright (C) 2019 - 2020 Bell Canada. # # Licensed 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. # from builtins import Exception, open, dict from subprocess import CalledProcessError, PIPE, TimeoutExpired from google.protobuf.json_format import MessageToJson import tempfile import logging import os import sys import re import subprocess import venv import utils import proto.CommandExecutor_pb2 as CommandExecutor_pb2 from zipfile import ZipFile import io import time import prometheus_client as prometheus import shlex REQUIREMENTS_TXT = "requirements.txt" class CommandExecutorHandler(): BLUEPRINTS_DEPLOY_DIR = '/opt/app/onap/blueprints/deploy/' TOSCA_META_FILE = 'TOSCA-Metadata/TOSCA.meta' PROMETHEUS_METRICS_UPLOAD_CBA_LABEL = 'upload_cba' PROMETHEUS_METRICS_PREP_ENV_LABEL = 'prepare_env' PROMETHEUS_METRICS_EXEC_COMMAND_LABEL = 'execute_command' def __init__(self, request): self.request = request self.logger = logging.getLogger(self.__class__.__name__) self.blueprint_name = utils.get_blueprint_name(request) self.blueprint_version = utils.get_blueprint_version(request) self.uuid = utils.get_blueprint_uuid(request) self.request_id = utils.get_blueprint_requestid(request) self.sub_request_id = utils.get_blueprint_subRequestId(request) self.blueprint_name_version = utils.blueprint_name_version(request) #for legacy SR7 support self.blueprint_name_version_uuid = utils.blueprint_name_version_uuid(request) self.execution_timeout = utils.get_blueprint_timeout(request) # onap/blueprints/deploy will be ephemeral now self.blueprint_dir = self.BLUEPRINTS_DEPLOY_DIR + self.blueprint_name_version_uuid # if the command matches “/opt/app/onap/blueprints/deploy/$cba_name/$cba_version/stuff_that_is_not_cba_uuid/” # then prepend the $cba_uuid before “stuff_that_is_not_cba_uuid” self.blueprint_tosca_meta_file = self.blueprint_dir + '/' + self.TOSCA_META_FILE self.extra = utils.getExtraLogData(request) self.installed = self.blueprint_dir + '/.installed' self.prometheus_histogram = self.get_prometheus_histogram() self.prometheus_counter = self.get_prometheus_counter() self.start_prometheus_server() def get_prometheus_histogram(self): histogram = getattr(prometheus.REGISTRY, '_command_executor_histogram', None) if not histogram: histogram = prometheus.Histogram('cds_ce_execution_duration_seconds', 'How many times CE actions (upload, prepare env and execute) got executed and how long it took to complete for each CBA python script.', ['step', 'blueprint_name', 'blueprint_version', 'script_name']) prometheus.REGISTRY._command_executor_histogram = histogram return histogram def get_prometheus_counter(self): counter = getattr(prometheus.REGISTRY, '_command_executor_counter', None) if not counter: counter = prometheus.Counter('cds_ce_execution_error_total', 'How many times CE actions (upload, prepare env and execute) got executed and failed for each CBA python script', ['step', 'blueprint_name', 'blueprint_version', 'script_name']) prometheus.REGISTRY._command_executor_counter = counter return counter def start_prometheus_server(self): self.logger.info("PROMETHEUS_METRICS_ENABLED: {}".format(os.environ.get('PROMETHEUS_METRICS_ENABLED')), extra=self.extra) if (os.environ.get('PROMETHEUS_METRICS_ENABLED')): if not "PROMETHEUS_PORT" in os.environ: err_msg = "ERROR: failed to start prometheus server, PROMETHEUS_PORT env variable is not found." self.logger.error(err_msg, extra=self.extra) return utils.build_ret_data(False, results_log=[], error=err_msg) server_started = getattr(prometheus.REGISTRY, '_command_executor_prometheus_server_started', None) if not server_started: self.logger.info("PROMETHEUS_PORT: {}".format(os.environ.get('PROMETHEUS_PORT')), extra=self.extra) prometheus.start_http_server(int(os.environ.get('PROMETHEUS_PORT'))) prometheus.REGISTRY._command_executor_prometheus_server_started = True def is_installed(self): return os.path.exists(self.installed) def blueprint_dir_exists(self): return os.path.exists(self.blueprint_dir) # used to validate if the blueprint actually had a chace of getting uploaded def blueprint_tosca_meta_file_exists(self): return os.path.exists(self.blueprint_tosca_meta_file) def err_exit(self, msg): self.logger.error(msg, extra=self.extra) return utils.build_ret_data(False, error=msg) def is_valid_archive_type(self, archiveType): return archiveType=="CBA_ZIP" or archiveType=="CBA_GZIP" # Handle uploading blueprint request # accept UploadBlueprintInput (CommandExecutor.proto) struct # create dir blueprintName/BlueprintVersion/BlueprintUUID, and extract binData as either ZIP file or GZIP # based on archiveType field... def uploadBlueprint(self, request): start_time = time.time() archive_type = request.archiveType compressed_cba_stream = io.BytesIO(request.binData) ### request_copy = request.pop("binData") # Do not show binData of the uploaded compressed CBA in the log ### self.logger.info("uploadBlueprint request\n{}".format(request_copy), extra=self.extra) self.logger.info("uploadBlueprint request\n{}".format(request), extra=self.extra) if not self.is_valid_archive_type(archive_type): self.prometheus_counter.labels(self.PROMETHEUS_METRICS_UPLOAD_CBA_LABEL, self.blueprint_name, self.blueprint_version, None).inc() return utils.build_grpc_blueprint_upload_response(self.request_id, self.sub_request_id, False, ["Archive type {} is not valid.".format(archive_type)]) # create the BP dir self.blueprint_dir try: os.makedirs(name=self.blueprint_dir, mode=0o755, exist_ok=True) except OSError as ex: self.prometheus_counter.labels(self.PROMETHEUS_METRICS_UPLOAD_CBA_LABEL, self.blueprint_name, self.blueprint_version, None).inc() err_msg = "Failed to create blueprint dir: {} exception message: {}".format(self.blueprint_dir, ex.strerror) self.logger.error(err_msg, extra=self.extra) return utils.build_grpc_blueprint_upload_response(self.request_id, self.sub_request_id, False, [err_msg]) if archive_type=="CBA_ZIP": self.logger.info("Extracting ZIP data to dir {}".format(self.blueprint_dir), extra=self.extra) try: with ZipFile(compressed_cba_stream,'r') as zipfile: zipfile.extractall(self.blueprint_dir) self.logger.info("Done extracting ZIP data to dir {}".format(self.blueprint_dir), extra=self.extra) except (IOError, zipfile.error) as e: self.prometheus_counter.labels(self.PROMETHEUS_METRICS_UPLOAD_CBA_LABEL, self.blueprint_name, self.blueprint_version, None).inc() err_msg = "Error extracting ZIP data to dir {} exception: {}".format(self.blueprint_dir, e) self.logger.error(err_msg, extra=self.extra) return utils.build_grpc_blueprint_upload_response(self.request_id, self.sub_request_id, False, [err_msg]) # TODO with an actual test gzip cba... elif archive_type=="CBA_GZIP": self.prometheus_counter.labels(self.PROMETHEUS_METRICS_UPLOAD_CBA_LABEL, self.blueprint_name, self.blueprint_version, None).inc() self.logger.error("CBA_GZIP TODO", extra=self.extra) return utils.build_grpc_blueprint_upload_response(self.request_id, self.sub_request_id, False, ["Error extracting GZIP data to {} GZIP todo!".format(self.blueprint_dir)]) # Finally, everything is ok! self.prometheus_histogram.labels(self.PROMETHEUS_METRICS_UPLOAD_CBA_LABEL, self.blueprint_name, self.blueprint_version, None).observe(time.time() - start_time) return utils.build_grpc_blueprint_upload_response(self.request_id, self.sub_request_id, True, []) def prepare_env(self, request): results_log = [] start_time = time.time() self.logger.info("prepare_env request {}".format(request), extra=self.extra) # validate that the blueprint name in the request exists, if not, notify the caller if not self.blueprint_dir_exists(): self.prometheus_counter.labels(self.PROMETHEUS_METRICS_PREP_ENV_LABEL, self.blueprint_name, self.blueprint_version, None).inc() err_msg = "CBA directory {} not found on cmd-exec. CBA will be uploaded by BP proc.".format(self.blueprint_name_version_uuid) self.logger.info(err_msg, extra=self.extra) return utils.build_ret_data(False, results_log=results_log, error=err_msg, reupload_cba=True) if not self.blueprint_tosca_meta_file_exists(): self.prometheus_counter.labels(self.PROMETHEUS_METRICS_PREP_ENV_LABEL, self.blueprint_name, self.blueprint_version, None).inc() err_msg = "CBA directory {} exists on cmd-exec, but TOSCA meta file is not found!!! Returning (null) as UUID. CBA will be uploaded by BP proc.".format(self.blueprint_name_version_uuid) self.logger.info(err_msg, extra=self.extra) return utils.build_ret_data(False, results_log=results_log, error=err_msg, reupload_cba=True) self.logger.info("CBA directory {} exists on cmd-exec.".format(self.blueprint_name_version_uuid), extra=self.extra) if not self.is_installed(): create_venv_status = self.create_venv() if not create_venv_status[utils.CDS_IS_SUCCESSFUL_KEY]: self.prometheus_counter.labels(self.PROMETHEUS_METRICS_PREP_ENV_LABEL, self.blueprint_name, self.blueprint_version, None).inc() return self.err_exit("ERROR: failed to prepare environment for request {} due to error in creating virtual Python env. Original error {}".format(self.blueprint_name_version_uuid, create_venv_status[utils.ERR_MSG_KEY])) # Upgrade pip - venv comes with PIP 18.1, which is too old. if not self.upgrade_pip(results_log): self.prometheus_counter.labels(self.PROMETHEUS_METRICS_PREP_ENV_LABEL, self.blueprint_name, self.blueprint_version, None).inc() err_msg = "ERROR: failed to prepare environment for request {} due to error in upgrading pip.".format(self.blueprint_name_version_uuid) return utils.build_ret_data(False, results_log=results_log, error=err_msg) try: # NOTE: pip or ansible selection is done later inside 'install_packages' with open(self.installed, "w+") as f: if not self.install_packages(request, CommandExecutor_pb2.pip, f, results_log): self.prometheus_counter.labels(self.PROMETHEUS_METRICS_PREP_ENV_LABEL, self.blueprint_name, self.blueprint_version, None).inc() err_msg = "ERROR: failed to prepare environment for request {} during pip package install.".format(self.blueprint_name_version_uuid) return utils.build_ret_data(False, results_log=results_log, error=err_msg) f.write("\r\n") # TODO: is \r needed? results_log.append("\n") if not self.install_packages(request, CommandExecutor_pb2.ansible_galaxy, f, results_log): self.prometheus_counter.labels(self.PROMETHEUS_METRICS_PREP_ENV_LABEL, self.blueprint_name, self.blueprint_version, None).inc() err_msg = "ERROR: failed to prepare environment for request {} during Ansible install.".format(self.blueprint_name_version_uuid) return utils.build_ret_data(False, results_log=results_log, error=err_msg) except Exception as ex: self.prometheus_counter.labels(self.PROMETHEUS_METRICS_PREP_ENV_LABEL, self.blueprint_name, self.blueprint_version, None).inc() err_msg = "ERROR: failed to prepare environment for request {} during installing packages. Exception: {}".format(self.blueprint_name_version_uuid, ex) self.logger.error(err_msg, extra=self.extra) return utils.build_ret_data(False, error=err_msg) else: try: self.logger.info(".installed file was found for request {}".format(self.blueprint_name_version_uuid), extra=self.extra) with open(self.installed, "r") as f: results_log.append(f.read()) except Exception as ex: self.prometheus_counter.labels(self.PROMETHEUS_METRICS_PREP_ENV_LABEL, self.blueprint_name, self.blueprint_version, None).inc() err_msg="ERROR: failed to prepare environment during reading 'installed' file {}. Exception: {}".format(self.installed, ex) return utils.build_ret_data(False, error=err_msg) # deactivate_venv(blueprint_id) self.prometheus_histogram.labels(self.PROMETHEUS_METRICS_PREP_ENV_LABEL, self.blueprint_name, self.blueprint_version, None).observe(time.time() - start_time) return utils.build_ret_data(True, results_log=results_log) def execute_command(self, request): start_time = time.time() # STDOUT/STDERR output of the process results_log = [] # encoded payload returned by the process result = {} script_err_msg = [] self.logger.info("execute_command request {}".format(request), extra=self.extra) #Get the script name to be used for prometheus metrics #Command looks like this: python