Пример #1
0
    def __init__(self, configs):
        logging.info('Initializing ServerCore ...')

        self.initialized = True
        self.configs = configs
        try:
            self.database = Database(configs)
        except:
            logging.critical('Failed to initialize ServerCore. Shutting down.',
                             exc_info=True)
            self.initialized = False
            return
        
        if self.database.database is None:
            logging.critical('Failed to initialize ServerCore. Shutting down.')
            self.initialized = False
            return
        self.shell = ServerShell(self)
        self.database.shell = self.shell

        self.modules = {}
        self._load_modules()
        
        self.user_system = UserSystem(self.database)
        self.server_interface = ServerInterface(self.configs,
                                                self.user_system, self.shell)
        self.shell.server_interface = self.server_interface

        # Initialize the Notification system
        Notification.registered_classes[Notification.get_name()] = Notification

        # Save the configs
        self.configs.save_settings()

        logging.info('Initialized ServerCore')
Пример #2
0
class ServerCore:

    def __init__(self, configs):
        logging.info('Initializing ServerCore ...')

        self.initialized = True
        self.configs = configs
        try:
            self.database = Database(configs)
        except:
            logging.critical('Failed to initialize ServerCore. Shutting down.',
                             exc_info=True)
            self.initialized = False
            return
        
        if self.database.database is None:
            logging.critical('Failed to initialize ServerCore. Shutting down.')
            self.initialized = False
            return
        self.shell = ServerShell(self)
        self.database.shell = self.shell

        self.modules = {}
        self._load_modules()
        
        self.user_system = UserSystem(self.database)
        self.server_interface = ServerInterface(self.configs,
                                                self.user_system, self.shell)
        self.shell.server_interface = self.server_interface

        # Initialize the Notification system
        Notification.registered_classes[Notification.get_name()] = Notification

        # Save the configs
        self.configs.save_settings()

        logging.info('Initialized ServerCore')


    def set_admin_password(self, password):
        if not self.initialized:
            return False
        self.user_system.set_admin_password(password)
        return True


    def _load_modules(self):
        logging.info('Loading modules ...')
        modules_objects =\
            umit.inventory.common.load_modules_from_target('server',
                self.configs, self.shell)
        for module_object in modules_objects:
            self.modules[module_object.get_name()] = module_object
        logging.info('Loaded modules')


    def _activate_modules(self):
        # Init subscriptions. This is done now because all modules must be
        # loaded and initialized before the subscribtions are done.
        logging.info('Initializing modules subscriptions ...')
        for module in self.modules.values():
            if not module.is_enabled():
                continue
            if isinstance(module, SubscriberServerModule):
                module.subscribe()
        logging.info('Initialized modules subscriptions.')

        # Init the database operations for each module.
        logging.info('Initializing modules database operations ...')
        for module in self.modules.values():
            module.init_database_operations()
        logging.info('Done initializing modules database operations.')

        # Activate the modules
        logging.info('Activating modules ...')
        for module in self.modules.values():
            if module.is_enabled():
                module.activate()
        logging.info('Done activating modules.')


    def update_configs(self, configs):
        try:
            logging.info('Updating configurations.\n%s',
                         json.dumps(configs, sort_keys=True, indent=4))
        except:
            logging.warning('Invalid configuration change request',
                            exc_info=True)

        try:
            sections = configs.keys()
            for section in sections:
                options = configs[section]
                for option_name in options.keys():
                    option_value = options[option_name]
                    self.configs.set(section, option_name, option_value)

                # If a module, refresh it's settings and activate if needed
                if section in self.modules.keys():
                    module = self.modules[section]
                    if not module.is_enabled() and\
                       self.configs.get(section, InventoryConfig.module_enabled):
                        module.activate()
                    module.refresh_settings()

            # Save the settings
            self.configs.save_settings()
        except:
            logging.error('Changing server configurations failed',
                          exc_info=True)


    def run(self):
        if not self.initialized:
            return
        
        self._activate_modules()

        # Call the modules which implement ListenerServerModule so they
        # will start listening.
        for module in self.modules.values():
            if not module.is_enabled():
                continue
            if isinstance(module, ListenerServerModule):
                module.listen()
        self.server_interface.listen()
        
        logging.info('Starting the Twisted Reactor')
        reactor.run()


    def shutdown(self):
        for module in self.modules.values():
            if module.is_enabled():
                module.deactivate()
            module.shutdown()
        reactor.stop()