| 19 | |
| 20 | |
| 21 | class ExecutionService: |
| 22 | def __init__(self, authorizer, id_generator, env_vars: EnvVariables): |
| 23 | |
| 24 | self._id_generator = id_generator |
| 25 | self._authorizer = authorizer # type: Authorizer |
| 26 | |
| 27 | self._executors = {} # type: Dict[str, ScriptExecutor] |
| 28 | self._execution_infos = {} # type: Dict[str, _ExecutionInfo] |
| 29 | |
| 30 | # active from user perspective: |
| 31 | # - either they are running |
| 32 | # - OR user haven't yet seen execution results |
| 33 | self._active_executor_ids = set() |
| 34 | |
| 35 | self._finish_listeners = [] |
| 36 | self._start_listeners = [] |
| 37 | self._env_vars = env_vars |
| 38 | |
| 39 | def get_active_executor(self, execution_id, user): |
| 40 | self.validate_execution_id(execution_id, user, only_active=False) |
| 41 | if execution_id not in self._active_executor_ids: |
| 42 | return None |
| 43 | |
| 44 | return self._executors.get(execution_id) |
| 45 | |
| 46 | def start_script(self, config, user: User): |
| 47 | audit_name = user.get_audit_name() |
| 48 | |
| 49 | executor = ScriptExecutor(config, self._env_vars) |
| 50 | execution_id = self._id_generator.next_id() |
| 51 | |
| 52 | audit_command = executor.get_secure_command() |
| 53 | LOGGER.info('Calling script #%s: %s', execution_id, audit_command) |
| 54 | |
| 55 | executor.start(execution_id) |
| 56 | self._executors[execution_id] = executor |
| 57 | self._execution_infos[execution_id] = _ExecutionInfo( |
| 58 | execution_id=execution_id, |
| 59 | owner_user=user, |
| 60 | audit_name=audit_name, |
| 61 | audit_command=audit_command, |
| 62 | config=config) |
| 63 | self._active_executor_ids.add(execution_id) |
| 64 | |
| 65 | self._fire_execution_started(execution_id, user) |
| 66 | |
| 67 | self._add_post_finish_handling(execution_id, executor, user) |
| 68 | |
| 69 | return execution_id |
| 70 | |
| 71 | def stop_script(self, execution_id, user): |
| 72 | self.validate_execution_id(execution_id, user) |
| 73 | |
| 74 | if execution_id in self._executors: |
| 75 | self._executors[execution_id].stop() |
| 76 | |
| 77 | def kill_script(self, execution_id, user): |
| 78 | self.validate_execution_id(execution_id, user) |