Esempio n. 1
0
    def bot_run(self,
                bot_id,
                run_subcommand=None,
                console_type=None,
                message_action_kind=None,
                dryrun=None,
                msg=None,
                show_sent=None,
                loglevel=None):
        pid = self.__check_pid(bot_id)
        module = self._runtime_configuration[bot_id]['module']
        status = self.__status_process(pid, module, bot_id) if pid else False
        if pid and status is True:
            self._logger.info(
                "Main instance of the bot is running in the background and will be stopped; "
                "when finished, we try to relaunch it again. "
                "You may want to launch: 'intelmqctl stop {}' to prevent this message."
                .format(bot_id))
            paused = True
            self.bot_stop(bot_id)
        elif status is False:
            paused = False
        else:
            self._logger.error(status)
            return 1

        self._log_bot_message('starting', bot_id)
        filename = self.PIDFILE.format(bot_id)
        with open(filename, 'w') as fp:
            fp.write(str(os.getpid()))

        output = ""
        try:
            bd = BotDebugger(self._runtime_configuration[bot_id],
                             bot_id,
                             run_subcommand,
                             console_type,
                             message_action_kind,
                             dryrun,
                             msg,
                             show_sent,
                             loglevel=loglevel)
            output = bd.run()
            retval = 0
        except KeyboardInterrupt:
            print('Keyboard interrupt.')
            retval = 0
        except SystemExit as exc:
            print('Bot exited with code %s.' % exc.code)
            retval = exc.code

        self.__remove_pidfile(bot_id)
        if paused:
            self.bot_start(bot_id)

        return retval, output
Esempio n. 2
0
    def bot_run(self,
                bot_id,
                run_subcommand=None,
                console_type=None,
                message_action_kind=None,
                dryrun=None,
                msg=None,
                show_sent=None,
                loglevel=None):
        paused = False
        state = self._get_process_state(bot_id)
        if state in (self.ProcessState.STARTING, self.ProcessState.RUNNING,
                     self.ProcessState.BACKOFF):
            self.__logger.warning(
                "Main instance of the bot is running in the background and will be stopped; "
                "when finished, we try to relaunch it again. "
                "You may want to launch: 'intelmqctl stop {}' to prevent this message."
                .format(bot_id))
            paused = True
            self.bot_stop(bot_id)

        self._log_bot_message("starting", bot_id)

        output = ""
        try:
            bd = BotDebugger(self._runtime_configuration[bot_id],
                             bot_id,
                             run_subcommand,
                             console_type,
                             message_action_kind,
                             dryrun,
                             msg,
                             show_sent,
                             loglevel=loglevel)
            output = bd.run()
            retval = 0
        except KeyboardInterrupt:
            print("Keyboard interrupt.")
            retval = 0
        except SystemExit as exc:
            print("Bot exited with code %s." % exc.code)
            retval = exc.code

        if paused:
            self.bot_start(bot_id)

        return retval, output
Esempio n. 3
0
    def bot_run(self,
                bot_id,
                run_subcommand=None,
                console_type=None,
                message_action_kind=None,
                dryrun=None,
                msg=None):
        pid = self.__read_pidfile(bot_id)
        module = self.__runtime_configuration[bot_id]['module']
        if pid and self.__status_process(pid, module):
            self.logger.warning(
                "Main instance of the bot is running in the background and will be stopped; "
                "when finished, we try to relaunch it again. "
                "You may want to launch: 'intelmqctl stop {}' to prevent this message."
                .format(bot_id))
            paused = True
            self.bot_stop(bot_id)
        else:
            paused = False

        log_bot_message('starting', bot_id)
        filename = self.PIDFILE.format(bot_id)
        with open(filename, 'w') as fp:
            fp.write(str(os.getpid()))

        try:
            BotDebugger(self.__runtime_configuration[bot_id], bot_id,
                        run_subcommand, console_type, dryrun,
                        message_action_kind, msg)
            retval = 0
        except KeyboardInterrupt:
            print('Keyboard interrupt.')
            retval = 0
        except SystemExit as exc:
            print('Bot exited with code %s.' % exc)
            retval = exc

        self.__remove_pidfile(bot_id)
        if paused:
            self.bot_start(bot_id)
        return retval