Exemple #1
0
    def test_get_default_queue_logger(self, queueloggerClass):
        queueloggerClass.return_value = Mock()

        # These two should be the same Mock object
        defaultQueueLogger = log.getDefaultQueueLogger()
        secondQueueLogger = log.getDefaultQueueLogger()

        self.assertTrue(defaultQueueLogger == secondQueueLogger)
        defaultQueueLogger.start_logging.assert_any_call()
Exemple #2
0
def exit(code, status=None):
    """
    exits with the code provided, properly disposing of resources

    If status is not None, use sd_notify to report the status to systemd
    """
    if status is not None:
        sd_notify("STATUS=%s" % status)

    if virtWho:
        virtWho.terminate()
    queueLogger = log.getDefaultQueueLogger()
    if queueLogger:
        queueLogger.terminate()
    sys.exit(code)
Exemple #3
0
def exit(code, status=None):
    """
    exits with the code provided, properly disposing of resources

    If status is not None, use sd_notify to report the status to systemd
    """
    if status is not None:
        sd_notify("STATUS=%s" % status)

    if virtWho:
        virtWho.terminate()
    queueLogger = log.getDefaultQueueLogger()
    if queueLogger:
        queueLogger.terminate()
    sys.exit(code)
Exemple #4
0
    def run(self):
        self.queue_logger = log.getDefaultQueueLogger()

        self.reloading = False
        if not self.options.oneshot:
            self.logger.debug(
                "Starting infinite loop with %d seconds interval" %
                self.options.interval)

        # Queue for getting events from virt backends
        if self.queue is None:
            self.queue = Queue()

        # Run the virtualization backends
        self.virts = []
        for config in self.configManager.configs:
            try:
                logger = log.getLogger(self.options, config)
                virt = Virt.fromConfig(logger, config)
            except Exception as e:
                self.logger.error('Unable to use configuration "%s": %s' %
                                  (config.name, str(e)))
                continue
            # Run the process
            virt.start(self.queue, self.terminate_event, self.options.interval,
                       self.options.oneshot)
            self.virts.append(virt)
        if self.options.oneshot:
            oneshot_remaining = set(virt.config.name for virt in self.virts)

        if len(self.virts) == 0:
            self.logger.error("No suitable virt backend found")
            return

        result = {}
        report = None
        report_sent = None
        while not self.terminate_event.is_set():
            # Wait for incoming report from virt backend
            try:
                report = self.queue.get(block=True, timeout=self.queue_timeout)
            except Empty:
                report = None
            except IOError:
                continue

            # Read all the reports from the queue in order to remove obsoleted
            # reports from same virt
            reports = [report]
            while True:
                try:
                    report = self.queue.get(block=False)
                    reports.append(report)
                except Empty:
                    break
            reports = self._remove_obsolete(reports)

            try:
                # Run all jobs that have been queued as a result of sending last
                # time
                self.runJobs()
                if self.options.oneshot and not oneshot_remaining and not self.jobs:
                    break
            except Empty:
                pass
            except IOError:
                pass

            report_sent = None
            for report in reports:
                if report == "exit":
                    break
                if report == "reload":
                    self.stop_virts()
                    raise ReloadRequest()
                if isinstance(report, ErrorReport):
                    if self.options.oneshot:
                        # Don't hang on the failed backend
                        oneshot_remaining.remove(report.config.name)
                        self.logger.warn(
                            'Unable to collect report for config "%s"' %
                            report.config.name)
                # Send the report
                if not self.options.print_ and not isinstance(
                        report, ErrorReport):
                    self.update_report_to_send(report)
            # Check to see if it is time to send a report
            try:
                if time.time() >= self.send_after:
                    # It is time, send the current report
                    config, report_sent = self.send_current_report()
                else:
                    # It's not time update our queue_timeout to make sure we
                    # don't wait too long
                    wait_time = self.send_after - time.time()
                    self.queue_timeout = max(0, wait_time)
                    self.logger.debug('Waiting to send report')
            except ManagerFatalError:
                # System not register (probably), stop the backends
                self.stop_virts()
                continue

            if self.options.print_:
                report_sent = report

            if (self.options.oneshot and report_sent) or self.options.print_:
                oneshot_remaining.remove(report_sent.config.name)
                if not isinstance(report_sent, ErrorReport):
                    if self.options.print_:
                        result[report_sent.config] = report_sent
                for virt in self.virts:
                    if virt.config.name == report_sent.config.name:
                        virt.stop()
                if not oneshot_remaining and not self.jobs:
                    break
        self.queue = None
        self.jobs = None
        self.stop_virts()

        self.virt = []
        if self.options.print_:
            return result
Exemple #5
0
    def run(self):
        self.queue_logger = log.getDefaultQueueLogger()

        self.reloading = False
        if not self.options.oneshot:
            self.logger.debug("Starting infinite loop with %d seconds interval" % self.options.interval)

        # Queue for getting events from virt backends
        if self.queue is None:
            self.queue = Queue()

        # Run the virtualization backends
        self.virts = []
        for config in self.configManager.configs:
            try:
                logger = log.getLogger(self.options, config)
                virt = Virt.fromConfig(logger, config)
            except Exception as e:
                self.logger.error('Unable to use configuration "%s": %s' % (config.name, str(e)))
                continue
            # Run the process
            virt.start(self.queue, self.terminate_event, self.options.interval, self.options.oneshot)
            self.virts.append(virt)
        if self.options.oneshot:
            oneshot_remaining = set(virt.config.name for virt in self.virts)

        if len(self.virts) == 0:
            self.logger.error("No suitable virt backend found")
            return

        result = {}
        report = None
        report_sent = None
        while not self.terminate_event.is_set():
            # Wait for incoming report from virt backend
            try:
                report = self.queue.get(block=True, timeout=self.queue_timeout)
            except Empty:
                report = None
            except IOError:
                continue

            # Read all the reports from the queue in order to remove obsoleted
            # reports from same virt
            reports = [report]
            while True:
                try:
                    report = self.queue.get(block=False)
                    reports.append(report)
                except Empty:
                    break
            reports = self._remove_obsolete(reports)

            try:
                # Run all jobs that have been queued as a result of sending last
                # time
                self.runJobs()
                if self.options.oneshot and not oneshot_remaining and not self.jobs:
                    break
            except Empty:
                pass
            except IOError:
                pass

            report_sent = None
            for report in reports:
                if report == "exit":
                    break
                if report == "reload":
                    self.stop_virts()
                    raise ReloadRequest()
                if isinstance(report, ErrorReport):
                    if self.options.oneshot:
                        # Don't hang on the failed backend
                        oneshot_remaining.remove(report.config.name)
                        self.logger.warn('Unable to collect report for config "%s"' % report.config.name)
                # Send the report
                if not self.options.print_ and not isinstance(report, ErrorReport):
                    self.update_report_to_send(report)
            # Check to see if it is time to send a report
            try:
                if time.time() >= self.send_after:
                    # It is time, send the current report
                    config, report_sent = self.send_current_report()
                else:
                    # It's not time update our queue_timeout to make sure we
                    # don't wait too long
                    wait_time = self.send_after - time.time()
                    self.queue_timeout = max(0, wait_time)
                    self.logger.debug('Waiting to send report')
            except ManagerFatalError:
                # System not register (probably), stop the backends
                self.stop_virts()
                continue

            if self.options.print_:
                report_sent = report

            if (self.options.oneshot and report_sent) or self.options.print_:
                oneshot_remaining.remove(report_sent.config.name)
                if not isinstance(report_sent, ErrorReport):
                    if self.options.print_:
                        result[report_sent.config] = report_sent
                for virt in self.virts:
                    if virt.config.name == report_sent.config.name:
                        virt.stop()
                if not oneshot_remaining and not self.jobs:
                    break
        self.queue = None
        self.jobs = None
        self.stop_virts()

        self.virt = []
        if self.options.print_:
            return result