2 # Copyright (C) 2019 - 2020 Bell Canada.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 from builtins import Exception, open, dict
17 from subprocess import CalledProcessError, PIPE, TimeoutExpired
18 from google.protobuf.json_format import MessageToJson
27 import proto.CommandExecutor_pb2 as CommandExecutor_pb2
28 from zipfile import ZipFile
31 import prometheus_client as prometheus
33 REQUIREMENTS_TXT = "requirements.txt"
36 class CommandExecutorHandler():
37 BLUEPRINTS_DEPLOY_DIR = '/opt/app/onap/blueprints/deploy/'
38 TOSCA_META_FILE = 'TOSCA-Metadata/TOSCA.meta'
39 PROMETHEUS_METRICS_UPLOAD_CBA_LABEL = 'upload_cba'
40 PROMETHEUS_METRICS_PREP_ENV_LABEL = 'prepare_env'
41 PROMETHEUS_METRICS_EXEC_COMMAND_LABEL = 'execute_command'
43 def __init__(self, request):
44 self.request = request
45 self.logger = logging.getLogger(self.__class__.__name__)
46 self.blueprint_name = utils.get_blueprint_name(request)
47 self.blueprint_version = utils.get_blueprint_version(request)
48 self.uuid = utils.get_blueprint_uuid(request)
49 self.request_id = utils.get_blueprint_requestid(request)
50 self.sub_request_id = utils.get_blueprint_subRequestId(request)
51 self.blueprint_name_version = utils.blueprint_name_version(request) #for legacy SR7 support
52 self.blueprint_name_version_uuid = utils.blueprint_name_version_uuid(request)
53 self.execution_timeout = utils.get_blueprint_timeout(request)
54 # onap/blueprints/deploy will be ephemeral now
55 self.blueprint_dir = self.BLUEPRINTS_DEPLOY_DIR + self.blueprint_name_version_uuid
56 # if the command matches “/opt/app/onap/blueprints/deploy/$cba_name/$cba_version/stuff_that_is_not_cba_uuid/”
57 # then prepend the $cba_uuid before “stuff_that_is_not_cba_uuid”
58 self.blueprint_tosca_meta_file = self.blueprint_dir + '/' + self.TOSCA_META_FILE
59 self.extra = utils.getExtraLogData(request)
60 self.installed = self.blueprint_dir + '/.installed'
61 self.prometheus_histogram = self.get_prometheus_histogram()
62 self.prometheus_counter = self.get_prometheus_counter()
63 self.start_prometheus_server()
65 def get_prometheus_histogram(self):
66 histogram = getattr(prometheus.REGISTRY, '_command_executor_histogram', None)
68 histogram = prometheus.Histogram('cds_ce_execution_duration_seconds',
69 'How many times CE actions (upload, prepare env and execute) got executed and how long it took to complete for each CBA python script.',
70 ['step', 'blueprint_name', 'blueprint_version', 'script_name'])
71 prometheus.REGISTRY._command_executor_histogram = histogram
74 def get_prometheus_counter(self):
75 counter = getattr(prometheus.REGISTRY, '_command_executor_counter', None)
77 counter = prometheus.Counter('cds_ce_execution_error_total',
78 'How many times CE actions (upload, prepare env and execute) got executed and failed for each CBA python script',
79 ['step', 'blueprint_name', 'blueprint_version', 'script_name'])
80 prometheus.REGISTRY._command_executor_counter = counter
83 def start_prometheus_server(self):
84 self.logger.info("PROMETHEUS_METRICS_ENABLED: {}".format(os.environ.get('PROMETHEUS_METRICS_ENABLED')), extra=self.extra)
85 if (os.environ.get('PROMETHEUS_METRICS_ENABLED')):
86 if not "PROMETHEUS_PORT" in os.environ:
87 err_msg = "ERROR: failed to start prometheus server, PROMETHEUS_PORT env variable is not found."
88 self.logger.error(err_msg, extra=self.extra)
89 return utils.build_ret_data(False, results_log=[], error=err_msg)
91 server_started = getattr(prometheus.REGISTRY, '_command_executor_prometheus_server_started', None)
92 if not server_started:
93 self.logger.info("PROMETHEUS_PORT: {}".format(os.environ.get('PROMETHEUS_PORT')), extra=self.extra)
94 prometheus.start_http_server(int(os.environ.get('PROMETHEUS_PORT')))
95 prometheus.REGISTRY._command_executor_prometheus_server_started = True
97 def is_installed(self):
98 return os.path.exists(self.installed)
100 def blueprint_dir_exists(self):
101 return os.path.exists(self.blueprint_dir)
103 # used to validate if the blueprint actually had a chace of getting uploaded
104 def blueprint_tosca_meta_file_exists(self):
105 return os.path.exists(self.blueprint_tosca_meta_file)
107 def err_exit(self, msg):
108 self.logger.error(msg, extra=self.extra)
109 return utils.build_ret_data(False, error=msg)
111 def is_valid_archive_type(self, archiveType):
112 return archiveType=="CBA_ZIP" or archiveType=="CBA_GZIP"
114 # Handle uploading blueprint request
115 # accept UploadBlueprintInput (CommandExecutor.proto) struct
116 # create dir blueprintName/BlueprintVersion/BlueprintUUID, and extract binData as either ZIP file or GZIP
117 # based on archiveType field...
118 def uploadBlueprint(self, request):
119 start_time = time.time()
120 archive_type = request.archiveType
121 compressed_cba_stream = io.BytesIO(request.binData)
122 ### request_copy = request.pop("binData") # Do not show binData of the uploaded compressed CBA in the log
123 ### self.logger.info("uploadBlueprint request\n{}".format(request_copy), extra=self.extra)
124 self.logger.info("uploadBlueprint request\n{}".format(request), extra=self.extra)
125 if not self.is_valid_archive_type(archive_type):
126 self.prometheus_counter.labels(self.PROMETHEUS_METRICS_UPLOAD_CBA_LABEL, self.blueprint_name, self.blueprint_version, None).inc()
127 return utils.build_grpc_blueprint_upload_response(self.request_id, self.sub_request_id, False, ["Archive type {} is not valid.".format(archive_type)])
129 # create the BP dir self.blueprint_dir
131 os.makedirs(name=self.blueprint_dir, mode=0o755, exist_ok=True)
132 except OSError as ex:
133 self.prometheus_counter.labels(self.PROMETHEUS_METRICS_UPLOAD_CBA_LABEL, self.blueprint_name, self.blueprint_version, None).inc()
134 err_msg = "Failed to create blueprint dir: {} exception message: {}".format(self.blueprint_dir, ex.strerror)
135 self.logger.error(err_msg, extra=self.extra)
136 return utils.build_grpc_blueprint_upload_response(self.request_id, self.sub_request_id, False, [err_msg])
137 if archive_type=="CBA_ZIP":
138 self.logger.info("Extracting ZIP data to dir {}".format(self.blueprint_dir), extra=self.extra)
140 with ZipFile(compressed_cba_stream,'r') as zipfile:
141 zipfile.extractall(self.blueprint_dir)
142 self.logger.info("Done extracting ZIP data to dir {}".format(self.blueprint_dir), extra=self.extra)
143 except (IOError, zipfile.error) as e:
144 self.prometheus_counter.labels(self.PROMETHEUS_METRICS_UPLOAD_CBA_LABEL, self.blueprint_name, self.blueprint_version, None).inc()
145 err_msg = "Error extracting ZIP data to dir {} exception: {}".format(self.blueprint_dir, e)
146 self.logger.error(err_msg, extra=self.extra)
147 return utils.build_grpc_blueprint_upload_response(self.request_id, self.sub_request_id, False, [err_msg])
148 # TODO with an actual test gzip cba...
149 elif archive_type=="CBA_GZIP":
150 self.prometheus_counter.labels(self.PROMETHEUS_METRICS_UPLOAD_CBA_LABEL, self.blueprint_name, self.blueprint_version, None).inc()
151 self.logger.error("CBA_GZIP TODO", extra=self.extra)
152 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)])
153 # Finally, everything is ok!
154 self.prometheus_histogram.labels(self.PROMETHEUS_METRICS_UPLOAD_CBA_LABEL, self.blueprint_name, self.blueprint_version, None).observe(time.time() - start_time)
155 return utils.build_grpc_blueprint_upload_response(self.request_id, self.sub_request_id, True, [])
157 def prepare_env(self, request):
159 start_time = time.time()
161 self.logger.info("prepare_env request {}".format(request), extra=self.extra)
162 # validate that the blueprint name in the request exists, if not, notify the caller
163 if not self.blueprint_dir_exists():
164 self.prometheus_counter.labels(self.PROMETHEUS_METRICS_PREP_ENV_LABEL, self.blueprint_name, self.blueprint_version, None).inc()
165 err_msg = "CBA directory {} not found on cmd-exec. CBA will be uploaded by BP proc.".format(self.blueprint_name_version_uuid)
166 self.logger.info(err_msg, extra=self.extra)
167 return utils.build_ret_data(False, results_log=results_log, error=err_msg, reupload_cba=True)
168 if not self.blueprint_tosca_meta_file_exists():
169 self.prometheus_counter.labels(self.PROMETHEUS_METRICS_PREP_ENV_LABEL, self.blueprint_name, self.blueprint_version, None).inc()
170 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)
171 self.logger.info(err_msg, extra=self.extra)
172 return utils.build_ret_data(False, results_log=results_log, error=err_msg, reupload_cba=True)
173 self.logger.info("CBA directory {} exists on cmd-exec.".format(self.blueprint_name_version_uuid), extra=self.extra)
175 if not self.is_installed():
176 create_venv_status = self.create_venv()
177 if not create_venv_status[utils.CDS_IS_SUCCESSFUL_KEY]:
178 self.prometheus_counter.labels(self.PROMETHEUS_METRICS_PREP_ENV_LABEL, self.blueprint_name, self.blueprint_version, None).inc()
179 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]))
181 # Upgrade pip - venv comes with PIP 18.1, which is too old.
182 if not self.upgrade_pip(results_log):
183 self.prometheus_counter.labels(self.PROMETHEUS_METRICS_PREP_ENV_LABEL, self.blueprint_name, self.blueprint_version, None).inc()
184 err_msg = "ERROR: failed to prepare environment for request {} due to error in upgrading pip.".format(self.blueprint_name_version_uuid)
185 return utils.build_ret_data(False, results_log=results_log, error=err_msg)
188 # NOTE: pip or ansible selection is done later inside 'install_packages'
189 with open(self.installed, "w+") as f:
190 if not self.install_packages(request, CommandExecutor_pb2.pip, f, results_log):
191 self.prometheus_counter.labels(self.PROMETHEUS_METRICS_PREP_ENV_LABEL, self.blueprint_name, self.blueprint_version, None).inc()
192 err_msg = "ERROR: failed to prepare environment for request {} during pip package install.".format(self.blueprint_name_version_uuid)
193 return utils.build_ret_data(False, results_log=results_log, error=err_msg)
194 f.write("\r\n") # TODO: is \r needed?
195 results_log.append("\n")
196 if not self.install_packages(request, CommandExecutor_pb2.ansible_galaxy, f, results_log):
197 self.prometheus_counter.labels(self.PROMETHEUS_METRICS_PREP_ENV_LABEL, self.blueprint_name, self.blueprint_version, None).inc()
198 err_msg = "ERROR: failed to prepare environment for request {} during Ansible install.".format(self.blueprint_name_version_uuid)
199 return utils.build_ret_data(False, results_log=results_log, error=err_msg)
200 except Exception as ex:
201 self.prometheus_counter.labels(self.PROMETHEUS_METRICS_PREP_ENV_LABEL, self.blueprint_name, self.blueprint_version, None).inc()
202 err_msg = "ERROR: failed to prepare environment for request {} during installing packages. Exception: {}".format(self.blueprint_name_version_uuid, ex)
203 self.logger.error(err_msg, extra=self.extra)
204 return utils.build_ret_data(False, error=err_msg)
207 self.logger.info(".installed file was found for request {}".format(self.blueprint_name_version_uuid), extra=self.extra)
208 with open(self.installed, "r") as f:
209 results_log.append(f.read())
210 except Exception as ex:
211 self.prometheus_counter.labels(self.PROMETHEUS_METRICS_PREP_ENV_LABEL, self.blueprint_name, self.blueprint_version, None).inc()
212 err_msg="ERROR: failed to prepare environment during reading 'installed' file {}. Exception: {}".format(self.installed, ex)
213 return utils.build_ret_data(False, error=err_msg)
215 # deactivate_venv(blueprint_id)
216 self.prometheus_histogram.labels(self.PROMETHEUS_METRICS_PREP_ENV_LABEL, self.blueprint_name, self.blueprint_version, None).observe(time.time() - start_time)
217 return utils.build_ret_data(True, results_log=results_log)
219 def execute_command(self, request):
220 start_time = time.time()
221 # STDOUT/STDERR output of the process
223 # encoded payload returned by the process
227 self.logger.info("execute_command request {}".format(request), extra=self.extra)
228 # workaround for when packages are not specified, we may not want to go through the install step
229 # can just call create_venv from here.
230 if not self.is_installed():
233 if not self.is_installed():
234 create_venv_status = self.create_venv
235 if not create_venv_status[utils.CDS_IS_SUCCESSFUL_KEY]:
236 self.prometheus_counter.labels(self.PROMETHEUS_METRICS_EXEC_COMMAND_LABEL, self.blueprint_name, self.blueprint_version, request.command).inc()
237 err_msg = "{} - Failed to execute command during venv creation. Original error: {}".format(self.blueprint_name_version_uuid, create_venv_status[utils.ERR_MSG_KEY])
238 return utils.build_ret_data(False, error=err_msg)
240 # touch blueprint dir to indicate this CBA was used recently
241 os.utime(self.blueprint_dir)
243 cmd = "cd " + self.blueprint_dir
245 ### if properties are defined we add them to the command
247 if request.properties is not None and len(request.properties) > 0:
248 properties = " " + re.escape(MessageToJson(request.properties)).replace('"','\\"')
250 # SR7/SR10 compatibility hack
251 # check if the path for the request.command does not contain UUID, then add it after cba_name/cba_version path.
252 updated_request_command = request.command
253 if self.blueprint_name_version in updated_request_command and self.blueprint_name_version_uuid not in updated_request_command:
254 updated_request_command = updated_request_command.replace(self.blueprint_name_version, self.blueprint_name_version_uuid)
256 if "ansible-playbook" in updated_request_command:
257 cmd = cmd + "; " + updated_request_command + " -e 'ansible_python_interpreter=" + self.blueprint_dir + "/bin/python'"
259 cmd = cmd + "; " + updated_request_command + properties
261 ### extract the original header request into sys-env variables
263 originator_id = request.originatorId
265 correlation_id = request.correlationId
266 request_id_map = {'CDS_REQUEST_ID':self.request_id, 'CDS_SUBREQUEST_ID':self.sub_request_id, 'CDS_ORIGINATOR_ID': originator_id, 'CDS_CORRELATION_ID': correlation_id}
267 updated_env = { **os.environ, **request_id_map }
268 # Prepare PATH and VENV_HOME
269 updated_env['PATH'] = self.blueprint_dir + "/bin/:" + os.environ['PATH']
270 updated_env['VIRTUAL_ENV'] = self.blueprint_dir
271 self.logger.info("Running blueprint {} with timeout: {}".format(self.blueprint_name_version_uuid, self.execution_timeout), extra=self.extra)
272 with tempfile.TemporaryFile(mode="w+") as tmp:
274 completed_subprocess = subprocess.run(cmd, stdout=tmp, stderr=subprocess.STDOUT, shell=True,
275 env=updated_env, timeout=self.execution_timeout)
276 except TimeoutExpired as timeout_ex:
277 self.prometheus_counter.labels(self.PROMETHEUS_METRICS_EXEC_COMMAND_LABEL, self.blueprint_name, self.blueprint_version, request.command).inc()
278 timeout_err_msg = "Running command {} failed due to timeout of {} seconds.".format(self.blueprint_name_version_uuid, self.execution_timeout)
279 self.logger.error(timeout_err_msg, extra=self.extra)
280 # In the time-out case, we will never get CBA's script err msg string.
281 utils.parse_cmd_exec_output(outputfile=tmp, logger=self.logger, payload_result=result, err_msg_result=script_err_msg, results_log=results_log, extra=self.extra)
282 return utils.build_ret_data(False, results_log=results_log, error=timeout_err_msg)
283 utils.parse_cmd_exec_output(outputfile=tmp, logger=self.logger, payload_result=result, err_msg_result=script_err_msg, results_log=results_log, extra=self.extra)
284 rc = completed_subprocess.returncode
285 except Exception as e:
286 self.prometheus_counter.labels(self.PROMETHEUS_METRICS_EXEC_COMMAND_LABEL, self.blueprint_name, self.blueprint_version, request.command).inc()
287 err_msg = "{} - Failed to execute command. Error: {}".format(self.blueprint_name_version_uuid, e)
288 result.update(utils.build_ret_data(False, results_log=results_log, error=err_msg))
291 # Since return code is only used to check if it's zero (success), we can just return success flag instead.
292 is_execution_successful = rc == 0
293 # Propagate error message in case rc is not 0
294 ret_err_msg = None if is_execution_successful or not script_err_msg else script_err_msg
295 result.update(utils.build_ret_data(is_execution_successful, results_log=results_log, error=ret_err_msg))
296 self.prometheus_histogram.labels(self.PROMETHEUS_METRICS_EXEC_COMMAND_LABEL, self.blueprint_name, self.blueprint_version, request.command).observe(time.time() - start_time)
300 def install_packages(self, request, type, f, results):
301 success = self.install_python_packages('UTILITY', results)
303 self.logger.error("Error installing 'UTILITY (cds_utils) package to CBA python environment!!!", extra=self.extra)
306 for package in request.packages:
307 if package.type == type:
308 f.write("Installed %s packages:\r\n" % CommandExecutor_pb2.PackageType.Name(type))
309 for p in package.package:
310 f.write(" %s\r\n" % p)
311 if package.type == CommandExecutor_pb2.pip:
312 success = self.install_python_packages(p, results)
314 success = self.install_ansible_packages(p, results)
317 os.remove(self.installed)
321 def upgrade_pip(self, results):
322 self.logger.info("{} - updating PIP (venv supplied pip is too old...)".format(self.blueprint_name_version_uuid), extra=self.extra)
323 full_path_to_pip = self.blueprint_dir + "/bin/pip"
324 command = [full_path_to_pip,"install","--upgrade","pip"]
325 env = dict(os.environ)
326 if "https_proxy" in os.environ:
327 env['https_proxy'] = os.environ['https_proxy']
328 self.logger.info("Using https_proxy: {}".format(env['https_proxy']), extra=self.extra)
330 results.append(subprocess.run(command, check=True, stdout=PIPE, stderr=PIPE, env=env).stdout.decode())
332 self.logger.info("upgrade_pip succeeded", extra=self.extra)
334 except CalledProcessError as e:
335 results.append(e.stderr.decode())
336 self.logger.error("upgrade_pip failed", extra=self.extra)
339 def install_python_packages(self, package, results):
340 self.logger.info( "{} - Install Python package({}) in Python Virtual Environment".format(self.blueprint_name_version_uuid, package), extra=self.extra)
341 pip_install_user_flag = 'PIP_INSTALL_USER_FLAG' in os.environ
343 if pip_install_user_flag:
344 self.logger.info("Note: PIP_INSTALL_USER_FLAG is set, 'pip install' will use '--user' flag.", extra=self.extra)
346 if REQUIREMENTS_TXT == package:
347 full_path_to_pip = self.blueprint_dir + "/bin/pip"
348 full_path_to_requirements_txt = self.blueprint_dir + "/Environments/" + REQUIREMENTS_TXT
349 command = [full_path_to_pip, "install", "--user", "-r", full_path_to_requirements_txt] if pip_install_user_flag else [full_path_to_pip, "install", "-r", full_path_to_requirements_txt]
350 elif package == 'UTILITY':
351 py_ver_maj = sys.version_info.major
352 py_ver_min = sys.version_info.minor
353 command = ["cp", "-r", "./cds_utils", "{}/lib/python{}.{}/site-packages/".format(self.blueprint_dir, py_ver_maj,py_ver_min)]
355 command = ["pip", "install", "--user", package] if pip_install_user_flag else ["pip", "install", package]
357 env = dict(os.environ)
358 if "https_proxy" in os.environ:
359 env['https_proxy'] = os.environ['https_proxy']
360 self.logger.info("Using https_proxy: {}".format(env['https_proxy']), extra=self.extra)
362 results.append(subprocess.run(command, check=True, stdout=PIPE, stderr=PIPE, env=env).stdout.decode())
364 self.logger.info("install_python_packages {} succeeded".format(package), extra=self.extra)
366 except CalledProcessError as e:
367 results.append(e.stderr.decode())
368 self.logger.error("install_python_packages {} failed".format(package), extra=self.extra)
371 def install_ansible_packages(self, package, results):
372 self.logger.info( "{} - Install Ansible Role package({}) in Python Virtual Environment".format(self.blueprint_name_version_uuid, package), extra=self.extra)
373 command = ["ansible-galaxy", "install", package, "-p", self.blueprint_dir + "/Scripts/ansible/roles"]
375 env = dict(os.environ)
376 if "http_proxy" in os.environ:
377 # ansible galaxy uses https_proxy environment variable, but requires it to be set with http proxy value.
378 env['https_proxy'] = os.environ['http_proxy']
380 results.append(subprocess.run(command, check=True, stdout=PIPE, stderr=PIPE, env=env).stdout.decode())
383 except CalledProcessError as e:
384 results.append(e.stderr.decode())
387 # Returns a map with 'status' and 'err_msg'.
388 # 'status' True indicates success.
389 # 'err_msg' indicates an error occurred. The presence of err_msg may not be fatal,
390 # status should be set to False for fatal errors.
391 def create_venv(self):
392 self.logger.info("{} - Create Python Virtual Environment".format(self.blueprint_name_version_uuid), extra=self.extra)
394 bin_dir = self.blueprint_dir + "/bin"
395 # check if CREATE_VENV_DISABLE_SITE_PACKAGES is set
396 venv_system_site_packages_disabled = 'CREATE_VENV_DISABLE_SITE_PACKAGES' in os.environ
397 if (venv_system_site_packages_disabled):
398 self.logger.info("Note: CREATE_VENV_DISABLE_SITE_PACKAGES env var is set - environment creation will have site packages disabled.", extra=self.extra)
399 venv.create(self.blueprint_dir, with_pip=True, system_site_packages=not venv_system_site_packages_disabled)
400 self.logger.info("{} - Creation of Python Virtual Environment finished.".format(self.blueprint_name_version_uuid), extra=self.extra)
401 return utils.build_ret_data(True)
402 except Exception as err:
403 err_msg = "{} - Failed to provision Python Virtual Environment. Error: {}".format(self.blueprint_name_version_uuid, err)
404 self.logger.info(err_msg, extra=self.extra)
405 return utils.build_ret_data(False, error=err_msg)