def register_resource_plugin(logger, db_uri, plugin_class_name): try: # create api manager params = {'api_name':'beehive', 'api_id':'process', 'database_uri':db_uri, 'api_module':['gibbonbeehive.module.resource.mod.ResourceModule'], 'api_plugin':[], 'api_subsystem':'resource'} manager = ApiManager(params) manager.configure() manager.register_modules() # create module module = manager.modules['ResourceModule'] # import plugin operation.session = module.get_session() operation.perms = perms operation.user = authuser plugin_class = import_class(plugin_class_name) plugin = plugin_class(module) plugin.init() except: msg = traceback.format_exc() logger.error(msg) finally: # release session module.release_session(operation.session) operation.session = None
def __init__(self, connection, api_manager, event_handlers=[]): self.logger = logging.getLogger(self.__class__.__module__ + '.' + self.__class__.__name__) self.connection = connection self.api_manager = api_manager self.db_manager = self.api_manager.db_manager self._continue = None self.id = id_gen() self.manager = EventDbManager() self.event_handlers = [] for event_handler in event_handlers: handler = import_class(event_handler) self.event_handlers.append(handler(self.api_manager)) self.redis_uri = self.api_manager.redis_event_uri self.redis_exchange = self.api_manager.redis_event_exchange self.exchange = Exchange(self.redis_exchange, type='direct', delivery_mode=1, durable=False) self.queue_name = '%s.queue' % self.redis_exchange self.routing_key = '%s.key' % self.redis_exchange self.queue = Queue(self.queue_name, self.exchange, routing_key=self.routing_key, delivery_mode=1, durable=False) self.event_producer = EventProducerRedis(self.redis_uri, self.redis_exchange + '.sub', framework='simple') self.conn = Connection(self.redis_uri)
def init_resource(logger, db_uri): try: from gibbonbeehive.module.resource.model import ResourceDbManager # create api manager params = {'api_name':'beehive', 'api_id':'process', 'database_uri':db_uri, 'api_module':['gibbonbeehive.module.resource.mod.ResourceModule'], 'api_plugin':[], 'api_subsystem':'resource'} manager = ApiManager(params) manager.configure() manager.register_modules() # remove and create scchema ResourceDbManager.remove_table(db_uri) ResourceDbManager.create_table(db_uri) # create module #from gibbonbeehive.module.resource.mod import ResourceModule #from gibbonbeehive.module.resource.plugins.cloudstack import CloudstackPlugin #resource_module = ResourceModule(manager) #CloudstackPlugin(resource_module).register() resource_module = manager.modules['ResourceModule'] controller = resource_module.get_controller() # create session operation.session = resource_module.get_session() operation.perms = perms operation.user = authuser # init module resource_module.init_object() controller = resource_module.get_controller() for item in containers: container_class = import_class(item['class']) container = container_class(controller, objid=id_gen(), name=item['name'], desc=item['desc'], active=True, connection=json.dumps(item['conn'])) controller.add_container(container) except: msg = traceback.format_exc() logger.error(msg) finally: # release session resource_module.release_session(operation.session) operation.session = None
def decorated_view(task, params, *args, **kwargs): entity_class = import_class(params[0]) if entity_class.module is not None: mod = task.app.api_manager.modules[entity_class.module] task_local.controller = mod.get_controller() elif module is not None: mod = task.app.api_manager.modules[module] task_local.controller = mod.get_controller() task_local.entity_class = entity_class task_local.objid = params[1] task_local.op = params[2] task_local.opid = params[3] task_local.delta = params[5] task_local.api_id = params[7] operation.perms = [] operation.user = params[6] operation.id = params[7] operation.session = None operation.transaction = None operation.authorize = False operation.cache = False operation.encryption_key = task.app.api_manager.app_fernet_key res = None # task.update('STARTED', start_time=time(), msg='Start %s:%s' % (task.name, task.request.id)) task.update('STARTED', msg='START - %s:%s' % (task.name, task.request.id)) if synchronous: try: res = fn(task, params, *args, **kwargs) except: raise finally: task.release_session() else: try: res = fn(task, params, *args, **kwargs) except Exception as e: msg = 'FAIL - %s:%s caused by %s' % (task.name, task.request.id, e) task.on_failure(e, task.request.id, args, kwargs, ExceptionInfo()) logger.error(msg) finally: task.release_session() return res
def decorated_view(*args, **kwargs): task = args[0] params = args[1] if module != None: mod = task.app.api_manager.modules[module] task_local.controller = mod.get_controller() task_local.entity_class = import_class(params[0]) task_local.objid = params[1] task_local.op = params[2] task_local.opid = params[3] #task_local.start = params[4] task_local.delta = params[5] #task_local.inner_type = 'JOBTASK' operation.perms = [] operation.user = params[6] operation.session = None #task.update(u'PROGRESS') #logger.debug(u'Job %s - child task %s.%s' % (task_local.op, # task_local.opid, # task.request.id)) # record PENDING task and set start-time status = u'PROGRESS' task.update(status, start_time=time(), msg=u'Start jobtask %s.%s' % (task.name, task.request.id)) '''start_time = time() TaskResult.store(task.request.id, status=u'STARTED', retval=None, start_time=start_time, traceback=None, inner_type=u'JOBTASK')''' res = fn(*args, **kwargs) return res
def get_entity(entity): if entity is None: return inst else: eclass = import_class('%s.%s' % (inst.__module__, entity)) return eclass(inst)
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 __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
def get_tasks(self, *args, **kvargs): """Get tasks. :param entity_class: entity_class owner of the tasks to query :param task_id: task id :param page: users list page to show [default=0] :param size: number of users to show in list per page [default=0] :param order: sort order [default=DESC] :param field: sort field [default=id] :return: List of :class:`Task` :raises ApiManagerError: raise :class:`ApiManagerError` """ res = [] tags = [] objdef = None objtype = None entity_class = kvargs.pop('entity_class') if entity_class is not None: entity_class = import_class(entity_class) objdef = entity_class.objdef objtype = entity_class.objtype if operation.authorize is True: try: # use base permission over task manager - admin self.verify_permisssions('view') with_perm_tag = False except: if entity_class is None: raise ApiManagerError('entity_class must be specified') # use permission for a specific objtype:objdef - user with_perm_tag = True # verify permissions objs = self.controller.can(u'view', objtype=objtype, definition=objdef) # create permission tags for entity_def, ps in objs.items(): for p in ps: tags.append( self.manager.hash_from_permission( entity_def.lower(), p)) self.logger.debug(u'Permission tags to apply: %s' % truncate(tags)) else: with_perm_tag = False self.logger.debug(u'Auhtorization disabled for command') try: entities, total = self.manager.get_tasks( tags=tags, with_perm_tag=with_perm_tag, *args, **kvargs) for entity in entities: obj = Task(self.controller, oid=entity.id, objid=entity.objid, name=entity.name, model=entity) res.append(obj) self.logger.info(u'Get tasks (total:%s): %s' % (total, truncate(res))) return res, total except QueryError as ex: self.logger.warn(ex, exc_info=1) return [], 0