Exemplo n.º 1
0
    def _start_child_process(self):
        """
        Start a child process with the command.
        :return:
        """
        communicator = ClientCommunicator(
            self.master_uri,
            self.quarantine,
            verify_certs=self.verify_certs,
            get_files_auth=self.rapid_config.get_files_basic_auth)

        # Cleanup first
        self.workspace = OSUtil.path_join(
            self.workspace, str(self.work_request.action_instance_id))
        self.clean_workspace()

        env = self.get_environment()
        try:
            command = self.get_command(communicator)
            command.extend(self.get_arguments())

            # Check and download required files that will execute in command.
            try:
                self._download_required_files([self.work_request.executable],
                                              communicator, self.logger)
            except Exception as exception:
                self.logger.exception(exception)
                raise exception

            stdout = None
            stderr = None
            if self.logger:
                stdout = subprocess.PIPE
                stderr = subprocess.STDOUT

            self.child_process = subprocess.Popen(command,
                                                  cwd=self.workspace,
                                                  env=env,
                                                  stderr=stderr,
                                                  stdout=stdout)
            self.pid = self.child_process.pid

            StoreService.save_executor(self)

            # wait for the process to finish
            self.reading_thread = threading.Thread(
                target=self._read_process_output,
                args=(self.logger, self.child_process))
            self.reading_thread.daemon = True
            self.reading_thread.start()

            self.child_process.wait()

            self._finalize_run(communicator)
        except (BaseException, Exception) as exception:
            self.logger.exception(exception)
            communicator.send_done(self.work_request.action_instance_id,
                                   'FAILED', None, None, None, self.logger)
        finally:
            self.clean_workspace()
Exemplo n.º 2
0
    def __get_quarantined_items(self):
        items = []
        quarantine_directory = self.app.rapid_config.quarantine_directory
        if quarantine_directory:
            communicator = None
            if not os.path.isdir(quarantine_directory):
                os.makedirs(quarantine_directory)

            for item in os.listdir(quarantine_directory):
                if item in ['.', '..']:
                    continue
                try:
                    items.append({
                        'action_instance_id': int(item),
                        'pid': 'quarantined'
                    })
                    if communicator is None:
                        communicator = ClientCommunicator(
                            self.app.rapid_config.master_uri,
                            self.app.rapid_config.quarantine_directory,
                            verify_certs=self.app.rapid_config.verify_certs,
                            get_files_auth=self.app.rapid_config.
                            get_files_basic_auth)
                    try:
                        delete_file = False
                        with open("{}/{}".format(quarantine_directory, item),
                                  'r') as tmp_file:
                            data = pickle.loads(tmp_file.read())
                            try:
                                status = data['status']
                                parameters = data[
                                    'parameters'] if 'parameters' in data else None
                                stats = data[
                                    'stats'] if 'stats' in data else None
                                results = data[
                                    'results'] if 'results' in data else None

                                communicator.send_done(
                                    int(item),
                                    status,
                                    parameters,
                                    stats,
                                    results,
                                    logger,
                                    headers={'X-Rapid-Quarantine': 'true'})
                                delete_file = True
                            except Exception:
                                import traceback
                                traceback.print_exc()

                        if delete_file:
                            try:
                                os.remove("{}/{}".format(
                                    quarantine_directory, item))
                            except Exception:
                                logger.error("Couldn't remove.")
                    except Exception:
                        import traceback
                        traceback.print_exc()

                except Exception:
                    pass
        return items