|
@@ -41,6 +41,9 @@ class CustomServiceOrchestrator():
|
|
|
SCRIPT_TYPE_PYTHON = "PYTHON"
|
|
|
CUSTOM_ACTION_COMMAND = 'ACTIONEXECUTE'
|
|
|
|
|
|
+ PRE_HOOK_PREFIX="before"
|
|
|
+ POST_HOOK_PREFIX="after"
|
|
|
+
|
|
|
def __init__(self, config):
|
|
|
self.config = config
|
|
|
self.tmp_dir = config.get('agent', 'prefix')
|
|
@@ -56,27 +59,47 @@ class CustomServiceOrchestrator():
|
|
|
command_name = command['roleCommand']
|
|
|
timeout = int(command['commandParams']['command_timeout'])
|
|
|
task_id = command['taskId']
|
|
|
+
|
|
|
if command_name == self.CUSTOM_ACTION_COMMAND:
|
|
|
base_dir = self.config.get('python', 'custom_actions_dir')
|
|
|
script_path = os.path.join(base_dir, script)
|
|
|
+ hook_dir = None
|
|
|
else:
|
|
|
stack_name = command['hostLevelParams']['stack_name']
|
|
|
stack_version = command['hostLevelParams']['stack_version']
|
|
|
+ hook_dir = self.file_cache.get_hook_base_dir(stack_name, stack_version)
|
|
|
metadata_folder = command['commandParams']['service_metadata_folder']
|
|
|
base_dir = self.file_cache.get_service_base_dir(
|
|
|
stack_name, stack_version, metadata_folder, component_name)
|
|
|
script_path = self.resolve_script_path(base_dir, script, script_type)
|
|
|
|
|
|
- tmpstrucoutfile = os.path.join(self.tmp_dir, "structured-out-{0}.json".
|
|
|
- format(task_id))
|
|
|
- if script_type.upper() == self.SCRIPT_TYPE_PYTHON:
|
|
|
- json_path = self.dump_command_to_json(command)
|
|
|
- script_params = [command_name, json_path, base_dir]
|
|
|
- ret = self.python_executor.run_file(script_path, script_params,
|
|
|
- tmpoutfile, tmperrfile, timeout, tmpstrucoutfile)
|
|
|
- else:
|
|
|
+ tmpstrucoutfile = os.path.join(self.tmp_dir,
|
|
|
+ "structured-out-{0}.json".format(task_id))
|
|
|
+ if script_type.upper() != self.SCRIPT_TYPE_PYTHON:
|
|
|
+ # We don't support anything else yet
|
|
|
message = "Unknown script type {0}".format(script_type)
|
|
|
raise AgentException(message)
|
|
|
+ # Execute command using proper interpreter
|
|
|
+ json_path = self.dump_command_to_json(command)
|
|
|
+ script_params = [command_name, json_path, base_dir]
|
|
|
+ pre_hook = self.resolve_hook_script_path(hook_dir,
|
|
|
+ self.PRE_HOOK_PREFIX, command_name, script_type)
|
|
|
+ post_hook = self.resolve_hook_script_path(hook_dir,
|
|
|
+ self.POST_HOOK_PREFIX, command_name, script_type)
|
|
|
+ py_file_list = [pre_hook, script_path, post_hook]
|
|
|
+ # filter None values
|
|
|
+ filtered_py_file_list = [i for i in py_file_list if i]
|
|
|
+ # Executing hooks and script
|
|
|
+ ret = None
|
|
|
+ for py_file in filtered_py_file_list:
|
|
|
+ ret = self.python_executor.run_file(py_file, script_params,
|
|
|
+ tmpoutfile, tmperrfile, timeout, tmpstrucoutfile)
|
|
|
+ if ret['exitcode'] != 0:
|
|
|
+ break
|
|
|
+
|
|
|
+ if not ret: # Something went wrong
|
|
|
+ raise AgentException("No script has been executed")
|
|
|
+
|
|
|
except Exception: # We do not want to let agent fail completely
|
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
|
message = "Catched an exception while executing "\
|
|
@@ -102,6 +125,21 @@ class CustomServiceOrchestrator():
|
|
|
return path
|
|
|
|
|
|
|
|
|
+ def resolve_hook_script_path(self, hook_base_dir, prefix, command_name, script_type):
|
|
|
+ """
|
|
|
+ Returns a path to hook script according to string prefix
|
|
|
+ and command name. If script does not exist, returns None
|
|
|
+ """
|
|
|
+ if not hook_base_dir:
|
|
|
+ return None
|
|
|
+ script_file = "{0}-{1}.py".format(prefix, command_name)
|
|
|
+ hook_script_path = os.path.join(hook_base_dir, script_file)
|
|
|
+ if not os.path.isfile(hook_script_path):
|
|
|
+ logger.debug("Hook script {0} not found, skipping".format(hook_script_path))
|
|
|
+ return None
|
|
|
+ return hook_script_path
|
|
|
+
|
|
|
+
|
|
|
def dump_command_to_json(self, command):
|
|
|
"""
|
|
|
Converts command to json file and returns file path
|