Esempio n. 1
0
    def simulate_logging(self,
                         execution_id=None,
                         user_name=None,
                         user_id=None,
                         script_name=None,
                         command=None,
                         log_lines=None,
                         start_time_millis=None,
                         exit_code=0,
                         write_post_execution_info=True):

        output_stream = Observable()

        execution_id = self.start_logging(command=command,
                                          execution_id=execution_id,
                                          output_stream=output_stream,
                                          script_name=script_name,
                                          start_time_millis=start_time_millis,
                                          user_id=user_id,
                                          user_name=user_name)

        if log_lines:
            for line in log_lines:
                output_stream.push(line + '\n')

        output_stream.close()

        if write_post_execution_info:
            self.logging_service.write_post_execution_info(execution_id, exit_code)
    def simulate_logging(self,
                         execution_id=None,
                         user='******',
                         script_name='my_script',
                         command='cmd',
                         log_lines=None,
                         start_time_millis=None):
        if not execution_id:
            execution_id = str(uuid.uuid1())

        output_stream = Observable()

        all_audit_names = {}
        all_audit_names[audit_utils.AUTH_USERNAME] = user

        self.logging_service.start_logging(
            execution_id,
            user,
            script_name,
            command,
            output_stream,
            self.post_info_provider,
            all_audit_names,
            start_time_millis)

        if log_lines:
            for line in log_lines:
                output_stream.push(line + '\n')

        output_stream.close()
    def simulate_logging(self,
                         execution_id=None,
                         user_name=None,
                         user_id=None,
                         script_name=None,
                         command=None,
                         log_lines=None,
                         start_time_millis=None,
                         exit_code=0,
                         write_post_execution_info=True):

        output_stream = Observable()

        execution_id = self.start_logging(command=command,
                                          execution_id=execution_id,
                                          output_stream=output_stream,
                                          script_name=script_name,
                                          start_time_millis=start_time_millis,
                                          user_id=user_id,
                                          user_name=user_name)

        if log_lines:
            for line in log_lines:
                output_stream.push(line + '\n')

        output_stream.close()

        if write_post_execution_info:
            self.logging_service.write_post_execution_info(execution_id, exit_code)
Esempio n. 4
0
    def test_write_post_execution_info_before_log_closed(self):
        output_stream = Observable()

        execution_id = '999'
        self.start_logging(output_stream, execution_id=execution_id)

        output_stream.push('abcde')

        self.logging_service.write_post_execution_info(execution_id, 255)

        old_entry = self.logging_service.find_history_entry(execution_id, 'userX')
        self.validate_history_entry(old_entry, id=execution_id, exit_code=None)

        output_stream.close()
        new_entry = self.logging_service.find_history_entry(execution_id, 'userX')
        self.validate_history_entry(new_entry, id=execution_id, exit_code=255)
    def test_write_post_execution_info_before_log_closed(self):
        output_stream = Observable()

        execution_id = '999'
        self.start_logging(output_stream, execution_id=execution_id)

        output_stream.push('abcde')

        self.logging_service.write_post_execution_info(execution_id, 255)

        old_entry = self.logging_service.find_history_entry(execution_id)
        self.validate_history_entry(old_entry, id=execution_id, exit_code=None)

        output_stream.close()
        new_entry = self.logging_service.find_history_entry(execution_id)
        self.validate_history_entry(new_entry, id=execution_id, exit_code=255)
Esempio n. 6
0
class ProcessWrapper(metaclass=abc.ABCMeta):
    def __init__(self, command, working_directory):
        self.process = None

        self.working_directory = working_directory
        self.command = command

        self.finish_listeners = []

        self.output_stream = Observable()

    def start(self):
        self.start_execution(self.command, self.working_directory)

        read_output_thread = threading.Thread(target=self.pipe_process_output)
        read_output_thread.start()

        notify_finish_thread = threading.Thread(target=self.notify_finished)
        notify_finish_thread.start()

    @abc.abstractmethod
    def pipe_process_output(self):
        pass

    @abc.abstractmethod
    def start_execution(self, command, working_directory):
        pass

    @abc.abstractmethod
    def write_to_input(self, value):
        pass

    @abc.abstractmethod
    def wait_finish(self):
        pass

    def _get_process_id(self):
        return self.process.pid

    def is_finished(self):
        return self.process.poll() is not None

    def get_return_code(self):
        return self.process.returncode

    def _write_script_output(self, text):
        self.output_stream.push(text)

    def stop(self):
        if not self.is_finished():
            if not os_utils.is_win():
                group_id = os.getpgid(self._get_process_id())
                os.killpg(group_id, signal.SIGTERM)

                class KillChildren(object):
                    def finished(self):
                        try:
                            os.killpg(group_id, signal.SIGKILL)
                        except ProcessLookupError:
                            # probably there are no children left
                            pass

                self.add_finish_listener(KillChildren())

            else:
                self.process.terminate()

            self._write_script_output('\n>> STOPPED BY USER\n')

    def kill(self):
        if not self.is_finished():
            if not os_utils.is_win():
                group_id = os.getpgid(self._get_process_id())
                os.killpg(group_id, signal.SIGKILL)
                self._write_script_output('\n>> KILLED\n')
            else:
                subprocess.Popen("taskkill /F /T /PID " + self._get_process_id())

    def add_finish_listener(self, listener):
        self.finish_listeners.append(listener)

        if self.is_finished():
            self.notify_finished()

    def notify_finished(self):
        self.wait_finish()

        for listener in self.finish_listeners:
            listener.finished()