|
| 1 | +import logging |
| 2 | +import time |
| 3 | +import uuid |
| 4 | + |
| 5 | +from alerts.alerts_service import AlertsService |
| 6 | +from execution.executor import ScriptExecutor |
| 7 | +from execution.logging import ExecutionLoggingService |
| 8 | +from react.observable import Observable |
| 9 | +from utils.date_utils import get_current_millis |
| 10 | + |
| 11 | +LOGGER = logging.getLogger('script_server.execution_service') |
| 12 | + |
| 13 | + |
| 14 | +class ExecutionService: |
| 15 | + def __init__(self, execution_logging_service, alerts_service, id_generator): |
| 16 | + self._execution_logging_service = execution_logging_service # type: ExecutionLoggingService |
| 17 | + self._alerts_service = alerts_service # type: AlertsService |
| 18 | + self._id_generator = id_generator |
| 19 | + |
| 20 | + self._running_scripts = {} |
| 21 | + |
| 22 | + def get_active_executor(self, execution_id): |
| 23 | + return self._running_scripts.get(execution_id) |
| 24 | + |
| 25 | + def start_script(self, config, values, audit_name): |
| 26 | + script_name = config.name |
| 27 | + |
| 28 | + executor = ScriptExecutor(config, values, audit_name) |
| 29 | + execution_id = self._id_generator.next_id() |
| 30 | + |
| 31 | + audit_command = executor.get_secure_command() |
| 32 | + LOGGER.info('Calling script #%s: %s', execution_id, audit_command) |
| 33 | + |
| 34 | + executor.start() |
| 35 | + self._running_scripts[execution_id] = executor |
| 36 | + |
| 37 | + secure_output_stream = executor.get_secure_output_stream() |
| 38 | + |
| 39 | + self._execution_logging_service.start_logging( |
| 40 | + execution_id, audit_name, script_name, audit_command, secure_output_stream, self) |
| 41 | + |
| 42 | + self.subscribe_fail_alerter( |
| 43 | + script_name, |
| 44 | + self._alerts_service, |
| 45 | + audit_name, |
| 46 | + executor, |
| 47 | + secure_output_stream) |
| 48 | + |
| 49 | + return execution_id |
| 50 | + |
| 51 | + def stop_script(self, execution_id): |
| 52 | + if execution_id in self._running_scripts: |
| 53 | + self._running_scripts[execution_id].stop() |
| 54 | + |
| 55 | + def get_exit_code(self, execution_id): |
| 56 | + executor = self._running_scripts.get(execution_id) # type: ScriptExecutor |
| 57 | + if executor is None: |
| 58 | + return None |
| 59 | + |
| 60 | + return executor.get_return_code() |
| 61 | + |
| 62 | + def is_running(self, execution_id): |
| 63 | + executor = self._running_scripts.get(execution_id) # type: ScriptExecutor |
| 64 | + if executor is None: |
| 65 | + return False |
| 66 | + |
| 67 | + return not executor.is_finished() |
| 68 | + |
| 69 | + @staticmethod |
| 70 | + def subscribe_fail_alerter( |
| 71 | + script_name, |
| 72 | + alerts_service, |
| 73 | + audit_name, |
| 74 | + executor: ScriptExecutor, |
| 75 | + output_stream: Observable): |
| 76 | + |
| 77 | + class Alerter(object): |
| 78 | + def finished(self): |
| 79 | + return_code = executor.get_return_code() |
| 80 | + |
| 81 | + if return_code != 0: |
| 82 | + script = str(script_name) |
| 83 | + |
| 84 | + title = script + ' FAILED' |
| 85 | + body = 'The script "' + script + '", started by ' + audit_name + \ |
| 86 | + ' exited with return code ' + str(return_code) + '.' + \ |
| 87 | + ' Usually this means an error, occurred during the execution.' + \ |
| 88 | + ' Please check the corresponding logs' |
| 89 | + |
| 90 | + output_stream.wait_close() |
| 91 | + script_output = ''.join(output_stream.get_old_data()) |
| 92 | + |
| 93 | + alerts_service.send_alert(title, body, script_output) |
| 94 | + |
| 95 | + executor.add_finish_listener(Alerter()) |
0 commit comments