def get_configs(self, app='beehive'): """Get server configuration. :param app: app name [default=cloudapi] :return: Config instance list :rtype: Config :raises ApiManagerError: if query empty return error. """ try: manager = ConfigDbManager() confs = manager.get(app=app) res = [] for c in confs: res.append({'type': c.group, 'name': c.name, 'value': c.value}) self.logger.debug('Get server configuration: %s' % truncate(res)) return res except (TransactionError, Exception) as ex: self.logger.error(ex) raise ApiManagerError(ex)
def __configure(self, config, update=True): """ """ msgs = [] try: # create api manager params = {u'api_id':u'server-01', u'api_name':config[u'api_system'], u'api_subsystem':config[u'api_subsystem'], u'database_uri':config[u'db_uri'], u'api_module':[u'beehive.module.process.mod.ConfigModule'], u'api_plugin':[]} manager = ApiManager(params) # remove and create scchema if update is False: ConfigDbManager.remove_table(config[u'db_uri']) ConfigDbManager.create_table(config[u'db_uri']) self.logger.info(u'Create config DB %s' % (u'')) msgs.append(u'Create config DB %s' % (u'')) # create session operation.session = manager.get_session() #operation.perms = perms #operation.user = authuser # create config db manager db_manager = ConfigDbManager() # set configurations # # populate configs # for item in config[u'config']: value = item[u'value'] if isinstance(value, dict): value = json.dumps(value) res = db_manager.add(config[u'api_system'], item[u'group'], item[u'name'], value) self.logger.info(u'Add configuration %s' % (res)) msgs.append(u'Add configuration %s' % (res)) except Exception as ex: self.logger.error(ex, exc_info=1) raise finally: # release session manager.release_session(operation.session) operation.session = None return msgs
def __init__(self, module): ApiController.__init__(self, module) self.manager = ConfigDbManager() self.child_classes = [Config]
class ConfigController(ApiController): """Basic Module controller. """ version = 'v1.0' def __init__(self, module): ApiController.__init__(self, module) self.manager = ConfigDbManager() self.child_classes = [Config] def init_object(self): """Register object types, objects and permissions related to module. Call this function when initialize system first time. """ # register all child class for child_class in self.child_classes: child_class(self).init_object() # # base configuration # @trace(entity='Config', op='view') def get_configs(self, app=None, group=None, name=None): """Get generic configuration. :param app: app name [optional] :param group: group name [optional] :param name: name of the configuration [optional] :return: Config instance :rtype: Config :raises ApiManagerError: if query empty return error. """ #params = {'app':app, 'group':group, 'name':name} # verify permissions self.can('view', Config.objtype, definition=Config.objdef) try: if app is not None or group is not None: confs = self.manager.get(app=app, group=group) elif name is not None: confs = self.manager.get(name=name) else: confs = self.manager.get() res = [] for c in confs: try: value = json.loads(c.value) except: value = c.value res.append( Config(self, oid=c.id, app=c.app, group=c.group, name=c.name, value=value, model=c)) self.logger.debug('Get generic configuration: %s' % res) #Config(self).send_event('view', params=params) return res except (TransactionError, Exception) as ex: #Config(self).send_event('view', params=params, exception=ex) self.logger.error(ex) raise ApiManagerError(ex) @trace(entity='Config', op='insert') def add_config(self, app, group, name, value): """Add generic configuration. :param app: app name :param group: group name :param name: property name :param value: property value :return: :rtype: :raises ApiManagerError: if query empty return error. """ #params = {'app':app, 'group':group, 'name':name} # verify permissions self.can('insert', Config.objtype, definition=Config.objdef) try: c = self.manager.add(app, group, name, value) res = Config(self, oid=c.id, app=c.app, group=c.group, name=c.name, value=c.value, model=c) self.logger.debug('Add generic configuration : %s' % res) #Config(self).send_event('view', params=params) return res except (TransactionError, Exception) as ex: #Config(self).send_event('view', params=params, exception=ex) self.logger.error(ex) raise ApiManagerError(ex) '''
def __init_subsystem(self, config, update=True): """Init beehive subsystem :param dict config: subsystem configuration :param update: if update is True don't replace database schema :return: trace of execution """ msgs = [] try: # create api manager params = {u'api_id':u'server-01', u'api_name':config[u'api_system'], u'api_subsystem':config[u'api_subsystem'], u'database_uri':config[u'db_uri'], u'api_module':config[u'api_modules'], u'api_plugin':config[u'api_plugins']} manager = ApiManager(params) manager.configure() manager.register_modules() # create config db manager config_db_manager = ConfigDbManager() for db_manager_class in config[u'db_managers']: db_manager = import_class(db_manager_class) # remove and create/update scchema if update is False: db_manager.remove_table(config[u'db_uri']) db_manager.create_table(config[u'db_uri']) self.logger.info(u'Create DB %s' % (db_manager_class)) msgs.append(u'Create DB %s' % (db_manager_class)) except Exception as ex: self.logger.error(ex, exc_info=1) raise self.set_permissions(classes=self.classes) # create module for item in config[u'api_modules']: try: self.logger.info(u'Load module %s' % (item)) module = manager.modules[item.split(u'.')[-1]] controller = module.get_controller() # create session operation.session = manager.get_session() # init module module.init_object() self.logger.info(u'Init module %s' % (module)) msgs.append(u'Init module %s' % (module)) # create system users and roles if module.name == u'AuthModule': res = self.__create_main_users(controller, config, config_db_manager) controller.set_superadmin_permissions() msgs.extend(res) elif module.name == u'Oauth2Module': controller.set_superadmin_permissions() elif module.name == u'BasicModule': controller.set_superadmin_permissions() elif module.name == u'CatalogModule': res = self.__create_main_catalogs(controller, config, config_db_manager) controller.set_superadmin_permissions() msgs.extend(res) except Exception as ex: self.logger.error(ex, exc_info=1) raise finally: # release session module.release_session(operation.session) operation.session = None self.logger.info(u'Init subsystem %s' % (config[u'api_subsystem'])) msgs.append(u'Init subsystem %s' % (config[u'api_subsystem'])) return msgs
def __configure(self, config, update=True): """Main configuration steps :param config: subsystem configuration :param update: if update is True don't replace database schema :return: """ msgs = [] manager = None try: # create api manager params = {'api_id': 'server-01', 'api_name': config['api_system'], 'api_subsystem': config['api_subsystem'], 'database_uri': config['db_uri'], 'api_module': 1, 'api_module.1': 'beehive.module.process.mod.ConfigModule', 'api_plugin': 0} manager = ApiManager(params) # remove and create scchema if update is False: ConfigDbManager.remove_table(config['db_uri']) ConfigDbManager.create_table(config['db_uri']) self.logger.info('Create config DB %s' % ('')) msgs.append('Create config DB %s' % ('')) # create session operation.session = manager.get_session() # create config db manager db_manager = ConfigDbManager() # set configurations # # populate configs # for item in config['config']: # check if config already exists value = item['value'] if isinstance(value, dict): value = json.dumps(value) try: res = db_manager.get(app=config['api_system'], group=item['group'], name=item['name']) self.logger.warning('Configuration %s %s %s already exist' % (config['api_system'], item['group'], item['name'])) msgs.append('Configuration %s %s %s already exist' % (config['api_system'], item['group'], item['name'])) except QueryError as ex: res = db_manager.add(config['api_system'], item['group'], item['name'], value) self.logger.info('Add configuration %s' % res) msgs.append('Add configuration %s' % res) except Exception as ex: self.logger.error(ex, exc_info=True) raise finally: # release session if manager is not None: manager.release_session() return msgs
def __init_subsystem(self, config, update=True): """Init beehive subsystem :param config: subsystem configuration :param update: if update is True don't replace database schema :return: trace of execution """ msgs = [] try: # create api manager params = {'api_id': 'server-01', 'api_name': config['api_system'], 'api_subsystem': config['api_subsystem'], 'database_uri': config['db_uri'], 'redis_identity_uri': config['redis_identity_uri'], 'api_module': config['api_module'], 'api_plugin': config['api_plugin'], 'api_endpoint': config['api_endpoint'], 'api_catalog': config['api_catalog']} for i in range(1, params['api_module']+1): params['api_module.%s' % i] = config['api_module.%s' % i] if config['api_plugin'] > 0: for i in range(1, params['api_plugin']+1): params['api_plugin.%s' % i] = config['api_plugin.%s' % i] manager = ApiManager(params) manager.configure() manager.register_modules() # create config db manager config_db_manager = ConfigDbManager() for db_manager_class in config['db_managers']: db_manager = import_class(db_manager_class) # remove and create/update scchema if update is False: db_manager.remove_table(config['db_uri']) db_manager.create_table(config['db_uri']) self.logger.info('Create DB %s' % db_manager_class) msgs.append('Create DB %s' % db_manager_class) except Exception as ex: self.logger.error(ex, exc_info=True) raise self.set_permissions(classes=self.classes) # create module for i in range(1, config['api_module']+1): item = config['api_module.%s' % i] try: self.logger.info('Load module %s' % item) module = manager.modules[item.split('.')[-1]] controller = module.get_controller() # create session operation.session = manager.get_session() # init module module.init_object() self.logger.info('Init module %s' % module) msgs.append('Init module %s' % module) # create system users and roles if module.name == 'AuthModule': res = self.__create_main_users(controller, config, config_db_manager, update) controller.set_superadmin_permissions() msgs.extend(res) elif module.name == 'Oauth2Module': controller.set_superadmin_permissions() elif module.name == 'BasicModule': controller.set_superadmin_permissions() elif module.name == 'CatalogModule': res = self.__create_main_catalogs(controller, config, config_db_manager) controller.set_superadmin_permissions() msgs.extend(res) elif module.name == 'ServiceModule': controller.populate(config['db_uri']) msgs.extend('Populate service database') except Exception as ex: self.logger.error(ex, exc_info=True) raise finally: # release session module.release_session() self.logger.info('Init subsystem %s' % (config['api_subsystem'])) msgs.append('Init subsystem %s' % (config['api_subsystem'])) return msgs