def _init_host(self):
        """Initialize Worker host

        Init db connection, load and validate processing
        hooks, runs periodic tasks.

        :returns None
        """
        db.init()

        try:
            hooks = plugins_base.validate_processing_hooks()
        except Exception as exc:
            LOG.critical(str(exc))
            sys.exit(1)

        LOG.info('Enabled processing hooks: %s', [h.name for h in hooks])

        if CONF.firewall.manage_firewall:
            firewall.init()

        periodic_update_ = periodics.periodic(
            spacing=CONF.firewall.firewall_update_period,
            enabled=CONF.firewall.manage_firewall)(periodic_update)
        periodic_clean_up_ = periodics.periodic(
            spacing=CONF.clean_up_period)(periodic_clean_up)

        self._periodics_worker = periodics.PeriodicWorker(
            callables=[(periodic_update_, None, None),
                       (periodic_clean_up_, None, None)],
            executor_factory=periodics.ExistingExecutor(utils.executor()))
        utils.executor().submit(self._periodics_worker.start)
示例#2
0
    def get_periodic_sync_task(self):
        """Get periodic sync task for the filter.

        The periodic task returned is casting the InvalidFilterDriverState
        to the periodics.NeverAgain exception to quit looping.

        :raises: periodics.NeverAgain
        :returns: a periodic task to be run in the background.
        """
        ironic = ir_utils.get_client()

        def periodic_sync_task():
            try:
                self.sync(ironic)
            except InvalidFilterDriverState as e:
                LOG.warning(
                    'Filter driver %s disabling periodic sync '
                    'task because of an invalid state.', self)
                raise periodics.NeverAgain(e)

        return periodics.periodic(
            # NOTE(milan): the periodic decorator doesn't support 0 as
            # a spacing value of (a switched off) periodic
            spacing=CONF.pxe_filter.sync_period or float('inf'),
            enabled=bool(CONF.pxe_filter.sync_period))(periodic_sync_task)
示例#3
0
def create_periodic(target, spacing, run_immediately=True, *args, **kwargs):
    p = periodics.PeriodicWorker.create(
        [], executor_factory=lambda: futures.ThreadPoolExecutor(max_workers=1))
    p.add(periodics.periodic(
        spacing=spacing, run_immediately=run_immediately)(
            lambda: target(*args, **kwargs)))
    return p
示例#4
0
    def _init_host(self):
        """Initialize Worker host

        Init db connection, load and validate processing
        hooks, runs periodic tasks.

        :returns None
        """
        db.init()

        try:
            hooks = plugins_base.validate_processing_hooks()
        except Exception as exc:
            LOG.critical(str(exc))
            sys.exit(1)

        LOG.info('Enabled processing hooks: %s', [h.name for h in hooks])

        driver = pxe_filter.driver()
        driver.init_filter()

        periodic_clean_up_ = periodics.periodic(
            spacing=CONF.clean_up_period)(periodic_clean_up)

        self._periodics_worker = periodics.PeriodicWorker(
            callables=[(driver.get_periodic_sync_task(), None, None),
                       (periodic_clean_up_, None, None)],
            executor_factory=periodics.ExistingExecutor(utils.executor()),
            on_failure=self._periodics_watchdog)
        utils.executor().submit(self._periodics_worker.start)
示例#5
0
文件: utils.py 项目: sapcc/ceilometer
def create_periodic(target, spacing, run_immediately=True, *args, **kwargs):
    p = periodics.PeriodicWorker.create(
        [], executor_factory=lambda: futures.ThreadPoolExecutor(max_workers=1))
    p.add(periodics.periodic(
        spacing=spacing, run_immediately=run_immediately)(
            lambda: target(*args, **kwargs)))
    return p
示例#6
0
    def init(self):
        if utils.get_auth_strategy() != 'noauth':
            utils.add_auth_middleware(app)
        else:
            LOG.warning(_LW('Starting unauthenticated, please check'
                            ' configuration'))

        if CONF.processing.store_data == 'none':
            LOG.warning(_LW('Introspection data will not be stored. Change '
                            '"[processing] store_data" option if this is not '
                            'the desired behavior'))
        elif CONF.processing.store_data == 'swift':
            LOG.info(_LI('Introspection data will be stored in Swift in the '
                         'container %s'), CONF.swift.container)

        utils.add_cors_middleware(app)

        db.init()

        try:
            hooks = [ext.name for ext in
                     plugins_base.processing_hooks_manager()]
        except KeyError as exc:
            # callback function raises MissingHookError derived from KeyError
            # on missing hook
            LOG.critical(_LC('Hook(s) %s failed to load or was not found'),
                         str(exc))
            sys.exit(1)

        LOG.info(_LI('Enabled processing hooks: %s'), hooks)

        if CONF.firewall.manage_firewall:
            firewall.init()

        periodic_update_ = periodics.periodic(
            spacing=CONF.firewall.firewall_update_period,
            enabled=CONF.firewall.manage_firewall
        )(periodic_update)
        periodic_clean_up_ = periodics.periodic(
            spacing=CONF.clean_up_period
        )(periodic_clean_up)

        self._periodics_worker = periodics.PeriodicWorker(
            callables=[(periodic_update_, None, None),
                       (periodic_clean_up_, None, None)],
            executor_factory=periodics.ExistingExecutor(utils.executor()))
        utils.executor().submit(self._periodics_worker.start)
示例#7
0
    def init(self):
        if CONF.auth_strategy != 'noauth':
            utils.add_auth_middleware(app)
        else:
            LOG.warning('Starting unauthenticated, please check'
                        ' configuration')

        if CONF.processing.store_data == 'none':
            LOG.warning('Introspection data will not be stored. Change '
                        '"[processing] store_data" option if this is not '
                        'the desired behavior')
        elif CONF.processing.store_data == 'swift':
            LOG.info(
                'Introspection data will be stored in Swift in the '
                'container %s', CONF.swift.container)

        utils.add_cors_middleware(app)

        db.init()

        try:
            hooks = [
                ext.name for ext in plugins_base.processing_hooks_manager()
            ]
        except KeyError as exc:
            # callback function raises MissingHookError derived from KeyError
            # on missing hook
            LOG.critical('Hook(s) %s failed to load or was not found',
                         str(exc))
            sys.exit(1)

        LOG.info('Enabled processing hooks: %s', hooks)

        if CONF.firewall.manage_firewall:
            firewall.init()

        periodic_update_ = periodics.periodic(
            spacing=CONF.firewall.firewall_update_period,
            enabled=CONF.firewall.manage_firewall)(periodic_update)
        periodic_clean_up_ = periodics.periodic(
            spacing=CONF.clean_up_period)(periodic_clean_up)

        self._periodics_worker = periodics.PeriodicWorker(
            callables=[(periodic_update_, None, None),
                       (periodic_clean_up_, None, None)],
            executor_factory=periodics.ExistingExecutor(utils.executor()))
        utils.executor().submit(self._periodics_worker.start)
示例#8
0
    def init_host(self):
        """Initialize Worker host

        Init db connection, load and validate processing
        hooks, runs periodic tasks.

        :returns None
        """
        if CONF.processing.store_data == 'none':
            LOG.warning('Introspection data will not be stored. Change '
                        '"[processing] store_data" option if this is not '
                        'the desired behavior')
        else:
            LOG.info('Introspection data will be stored in the %s backend',
                     CONF.processing.store_data)

        db.init()

        try:
            hooks = plugins_base.validate_processing_hooks()
        except Exception as exc:
            LOG.critical(str(exc))
            sys.exit(1)
        LOG.info('Enabled processing hooks: %s', [h.name for h in hooks])

        driver = pxe_filter.driver()
        driver.init_filter()

        periodic_clean_up_ = periodics.periodic(
            spacing=CONF.clean_up_period)(periodic_clean_up)

        self._periodics_worker = periodics.PeriodicWorker(
            callables=[(driver.get_periodic_sync_task(), None, None),
                       (periodic_clean_up_, None, None)],
            executor_factory=periodics.ExistingExecutor(utils.executor()),
            on_failure=self._periodics_watchdog)
        utils.executor().submit(self._periodics_worker.start)

        if CONF.enable_mdns:
            endpoint = keystone.get_endpoint('service_catalog')
            self._zeroconf = mdns.Zeroconf()
            self._zeroconf.register_service('baremetal-introspection',
                                            endpoint)

        if not CONF.standalone:
            try:
                coordinator = coordination.get_coordinator(prefix='conductor')
                coordinator.start(heartbeat=True)
                coordinator.join_group()
            except tooz.ToozError:
                with excutils.save_and_reraise_exception():
                    LOG.critical('Failed when connecting to coordination '
                                 'backend.')
                    self.del_host()
            else:
                LOG.info('Successfully connected to coordination backend.')
示例#9
0
    def get_periodic_sync_task(self):
        """Get periodic sync task for the filter.

        :returns: a periodic task to be run in the background.
        """
        ironic = ir_utils.get_client()
        return periodics.periodic(
            # NOTE(milan): the periodic decorator doesn't support 0 as
            # a spacing value of (a switched off) periodic
            spacing=CONF.pxe_filter.sync_period or float('inf'),
            enabled=bool(
                CONF.pxe_filter.sync_period))(lambda: self.sync(ironic))
示例#10
0
    def init_host(self):
        """Initialize Worker host

        Init db connection, load and validate processing
        hooks, runs periodic tasks.

        :returns None
        """
        if CONF.processing.store_data == 'none':
            LOG.warning('Introspection data will not be stored. Change '
                        '"[processing] store_data" option if this is not '
                        'the desired behavior')
        elif CONF.processing.store_data == 'swift':
            LOG.info(
                'Introspection data will be stored in Swift in the '
                'container %s', CONF.swift.container)

        db.init()

        try:
            hooks = plugins_base.validate_processing_hooks()
        except Exception as exc:
            LOG.critical(str(exc))
            sys.exit(1)
        LOG.info('Enabled processing hooks: %s', [h.name for h in hooks])

        driver = pxe_filter.driver()
        driver.init_filter()

        periodic_clean_up_ = periodics.periodic(
            spacing=CONF.clean_up_period)(periodic_clean_up)

        self._periodics_worker = periodics.PeriodicWorker(
            callables=[(driver.get_periodic_sync_task(), None, None),
                       (periodic_clean_up_, None, None)],
            executor_factory=periodics.ExistingExecutor(utils.executor()),
            on_failure=self._periodics_watchdog)
        utils.executor().submit(self._periodics_worker.start)

        if CONF.enable_mdns:
            endpoint = keystone.get_endpoint('service_catalog')
            self._zeroconf = mdns.Zeroconf()
            self._zeroconf.register_service('baremetal-introspection',
                                            endpoint)
示例#11
0
def driver_periodic_task(**kwargs):
    """Decorator for a driver-specific periodic task.

    Deprecated, please use futurist directly.
    Example::

        from futurist import periodics

        class MyDriver(base.BaseDriver):
            @periodics.periodic(spacing=42)
            def task(self, manager, context):
                # do some job

    :param kwargs: arguments to pass to @periodics.periodic
    """
    LOG.warning(
        _LW('driver_periodic_task decorator is deprecated, please '
            'use futurist.periodics.periodic directly'))
    # Previously we accepted more arguments, make a backward compatibility
    # layer for out-of-tree drivers.
    new_kwargs = {}
    for arg in ('spacing', 'enabled', 'run_immediately'):
        try:
            new_kwargs[arg] = kwargs.pop(arg)
        except KeyError:
            pass

    # NOTE(jroll) this is here to avoid a circular import when a module
    # imports ironic.common.service. Normally I would balk at this, but this
    # option is deprecared for removal and this code only runs at startup.
    CONF.import_opt('periodic_interval', 'ironic.common.service')
    new_kwargs.setdefault('spacing', CONF.periodic_interval)

    if kwargs:
        LOG.warning(
            _LW('The following arguments are not supported by '
                'futurist.periodics.periodic and are ignored: %s'),
            ', '.join(kwargs))

    return periodics.periodic(**new_kwargs)
示例#12
0
    def init_host(self):
        """Initialize Worker host

        Init db connection, load and validate processing
        hooks, runs periodic tasks.

        :returns None
        """
        if CONF.processing.store_data == 'none':
            LOG.warning('Introspection data will not be stored. Change '
                        '"[processing] store_data" option if this is not '
                        'the desired behavior')
        elif CONF.processing.store_data == 'swift':
            LOG.info('Introspection data will be stored in Swift in the '
                     'container %s', CONF.swift.container)

        db.init()

        try:
            hooks = plugins_base.validate_processing_hooks()
        except Exception as exc:
            LOG.critical(str(exc))
            sys.exit(1)
        LOG.info('Enabled processing hooks: %s', [h.name for h in hooks])

        driver = pxe_filter.driver()
        driver.init_filter()

        periodic_clean_up_ = periodics.periodic(
            spacing=CONF.clean_up_period
        )(periodic_clean_up)

        self._periodics_worker = periodics.PeriodicWorker(
            callables=[(driver.get_periodic_sync_task(), None, None),
                       (periodic_clean_up_, None, None)],
            executor_factory=periodics.ExistingExecutor(utils.executor()),
            on_failure=self._periodics_watchdog)
        utils.executor().submit(self._periodics_worker.start)
示例#13
0
def driver_periodic_task(**kwargs):
    """Decorator for a driver-specific periodic task.

    Deprecated, please use futurist directly.
    Example::

        from futurist import periodics

        class MyDriver(base.BaseDriver):
            @periodics.periodic(spacing=42)
            def task(self, manager, context):
                # do some job

    :param kwargs: arguments to pass to @periodics.periodic
    """
    LOG.warning(_LW('driver_periodic_task decorator is deprecated, please '
                    'use futurist.periodics.periodic directly'))
    # Previously we accepted more arguments, make a backward compatibility
    # layer for out-of-tree drivers.
    new_kwargs = {}
    for arg in ('spacing', 'enabled', 'run_immediately'):
        try:
            new_kwargs[arg] = kwargs.pop(arg)
        except KeyError:
            pass

    # NOTE(jroll) this is here to avoid a circular import when a module
    # imports ironic.common.service. Normally I would balk at this, but this
    # option is deprecared for removal and this code only runs at startup.
    CONF.import_opt('periodic_interval', 'ironic.common.service')
    new_kwargs.setdefault('spacing', CONF.periodic_interval)

    if kwargs:
        LOG.warning(_LW('The following arguments are not supported by '
                        'futurist.periodics.periodic and are ignored: %s'),
                    ', '.join(kwargs))

    return periodics.periodic(**new_kwargs)
示例#14
0
    def get_periodic_sync_task(self):
        """Get periodic sync task for the filter.

        The periodic task returned is casting the InvalidFilterDriverState
        to the periodics.NeverAgain exception to quit looping.

        :raises: periodics.NeverAgain
        :returns: a periodic task to be run in the background.
        """
        ironic = ir_utils.get_client()

        def periodic_sync_task():
            try:
                self.sync(ironic)
            except InvalidFilterDriverState as e:
                LOG.warning('Filter driver %s disabling periodic sync '
                            'task because of an invalid state.', self)
                raise periodics.NeverAgain(e)

        return periodics.periodic(
            # NOTE(milan): the periodic decorator doesn't support 0 as
            # a spacing value of (a switched off) periodic
            spacing=CONF.pxe_filter.sync_period or float('inf'),
            enabled=bool(CONF.pxe_filter.sync_period))(periodic_sync_task)
示例#15
0
    def init_host(self):
        """Initialize Worker host

        Init db connection, load and validate processing
        hooks, runs periodic tasks.

        :returns None
        """
        if CONF.processing.store_data == 'none':
            LOG.warning('Introspection data will not be stored. Change '
                        '"[processing] store_data" option if this is not '
                        'the desired behavior')
        else:
            LOG.info('Introspection data will be stored in the %s backend',
                     CONF.processing.store_data)

        db.init()

        self.coordinator = None
        try:
            self.coordinator = coordination.get_coordinator(prefix='conductor')
            self.coordinator.start(heartbeat=True)
            self.coordinator.join_group()
        except Exception as exc:
            if CONF.standalone:
                LOG.info(
                    'Coordination backend cannot be started, assuming '
                    'no other instances are running. Error: %s', exc)
                self.coordinator = None
            else:
                with excutils.save_and_reraise_exception():
                    LOG.critical(
                        'Failure when connecting to coordination '
                        'backend',
                        exc_info=True)
                    self.del_host()
        else:
            LOG.info('Successfully connected to coordination backend.')

        try:
            hooks = plugins_base.validate_processing_hooks()
        except Exception as exc:
            LOG.critical(str(exc))
            sys.exit(1)
        LOG.info('Enabled processing hooks: %s', [h.name for h in hooks])

        driver = pxe_filter.driver()
        driver.init_filter()

        periodic_clean_up_ = periodics.periodic(
            spacing=CONF.clean_up_period,
            enabled=(CONF.clean_up_period != 0))(periodic_clean_up)

        sync_with_ironic_ = periodics.periodic(
            spacing=CONF.clean_up_period,
            enabled=(CONF.clean_up_period != 0))(sync_with_ironic)

        callables = [(periodic_clean_up_, None, None),
                     (sync_with_ironic_, (self, ), None)]

        driver_task = driver.get_periodic_sync_task()
        if driver_task is not None:
            callables.append((driver_task, None, None))

        # run elections periodically if we have a coordinator
        # that we were able to start
        if (self.coordinator and self.coordinator.started):
            periodic_leader_election_ = periodics.periodic(
                spacing=CONF.leader_election_interval)(
                    periodic_leader_election)
            callables.append((periodic_leader_election_, (self, ), None))

        self._periodics_worker = periodics.PeriodicWorker(
            callables=callables,
            executor_factory=periodics.ExistingExecutor(utils.executor()),
            on_failure=self._periodics_watchdog)

        utils.executor().submit(self._periodics_worker.start)

        if CONF.enable_mdns:
            endpoint = keystone.get_endpoint('service_catalog')
            self._zeroconf = mdns.Zeroconf()
            self._zeroconf.register_service('baremetal-introspection',
                                            endpoint)