def esure_subclass(cls, step): if step.get('execute'): ecls = importutils.import_class(step.get('execute')) if not reflection.is_subclass(ecls, cls.ECLS): raise TypeError('Scheduler execute is enable subclass of %s' % cls.__name__) if step.get('revert'): rcls = importutils.import_class(step.get('revert')) if not reflection.is_subclass(rcls, cls.RCLS): raise TypeError('Scheduler revert is enable subclass of %s' % cls.__name__)
def builder(cls, name, jobstep, **kwargs): ecls = importutils.import_class(jobstep.execute)(get_http()) method = jobstep.method execute = getattr(ecls, method) provides = safe_load(jobstep.provides) rebind = safe_load(jobstep.rebind) revert = None if jobstep.revert: revert = importutils.import_class(jobstep.revert)(ecls, method) return cls(name, jobstep, execute, provides, rebind, revert, **kwargs)
def pre_start(self, external_objects): super(RpcServerManager, self).pre_start(external_objects) for executer in self.conf.executers: LOG.debug('Loading executer %s', executer) cls = importutils.import_class('goperation.manager.rpc.server.executer.%s.Executer' % executer) self.executers[executer] = cls for condition in self.conf.conditions: LOG.debug('Loading condition %s', condition) cls = importutils.import_class('goperation.manager.rpc.server.condition.%s.Condition' % condition) self.conditions[condition] = cls
def impl_cls(api, impl): try: return IMPLMAP[impl]() except KeyError: with goperation.lock.get('gopdb-impl-map'): if impl not in IMPLMAP: cls_string = 'gopdb.api.%s.impl.%s.DatabaseManager' % (api, impl) cls = importutils.import_class(cls_string) IMPLMAP.setdefault(impl, cls) return cls() return IMPLMAP[impl]()
def __init__(self, manager, user='******', gourp='root', **kwargs): plugin_threadpool = kwargs.pop('plugin_threadpool', None) if isinstance(manager, basestring): self.manager = importutils.import_class(manager)(**kwargs) else: self.manager = manager if not isinstance(manager, ManagerBase): raise RuntimeError('Manager type error') super(LauncheRpcServiceBase, self).__init__(self.manager.__class__.__name__.lower(), user=user, group=gourp, plugin_threadpool=plugin_threadpool) self.messageservice = None
def find_subclasses(locations, base_cls, exclude_hidden=True): """Finds subclass types in the given locations. This will examines the given locations for types which are subclasses of the base class type provided and returns the found subclasses (or fails with exceptions if this introspection can not be accomplished). If a string is provided as one of the locations it will be imported and examined if it is a subclass of the base class. If a module is given, all of its members will be examined for attributes which are subclasses of the base class. If a type itself is given it will be examined for being a subclass of the base class. """ derived = set() for item in locations: module = None if isinstance(item, six.string_types): try: pkg, cls = item.split(':') except ValueError: module = importutils.import_module(item) else: obj = importutils.import_class('%s.%s' % (pkg, cls)) if not reflection.is_subclass(obj, base_cls): raise TypeError("Object '%s' (%s) is not a '%s' subclass" % (item, type(item), base_cls)) derived.add(obj) elif isinstance(item, types.ModuleType): module = item elif reflection.is_subclass(item, base_cls): derived.add(item) else: raise TypeError("Object '%s' (%s) is an unexpected type" % (item, type(item))) # If it's a module derive objects from it if we can. if module is not None: for (name, obj) in inspect.getmembers(module): if name.startswith("_") and exclude_hidden: continue if reflection.is_subclass(obj, base_cls): derived.add(obj) return derived
def __init__(self): super(RpcAgentManager, self).__init__(target=target_server(self.agent_type, CONF.host, fanout=True)) interface, self.ipnetwork = get_network(self.local_ip) if not self.ipnetwork: raise RuntimeError('can not find local ip netmask') LOG.debug('Local ip %s/%s on interface %s' % (self.local_ip, self.ipnetwork.netmask, interface)) global DISK DISK = psutil.disk_usage(self.work_path).total/(1024*1024) # agent id self._agent_id = None # port and port and disk space info conf = CONF[manager_common.AGENT] self.conf = conf self.ports_range = validators['type:ports_range_list'](conf.ports_range) if conf.ports_range else [] # zone mark self.zone = conf.zone self.dnsnames = conf.dnsnames # key: port, value endpoint name self.allocked_ports = {} # left ports self.left_ports = set() for p_range in self.ports_range: down, up = map(int, p_range.split('-')) if down < 1024: raise ValueError('Port 1-1024 is not allowed') for port in range(down, up): self.left_ports.add(port) # init metadata self._metadata = super(RpcAgentManager, self).metadata self._metadata.setdefault('agent_type', self.agent_type) self._metadata.setdefault('zone', self.zone) self._metadata.setdefault('dnsnames', conf.dnsnames) # init httpclient self.client = AgentManagerClient(httpclient=get_http()) # init filemanager self.filemanager = FileManager(conf=conf, threadpool=threadpool, infoget=lambda x: self.client.file_show(x)['data'][0]) # init endpoint if conf.endpoints: # endpoint class must be singleton for endpoint in conf.endpoints: endpoint_group = cfg.OptGroup(endpoint.lower(), title='endpopint of %s' % endpoint) CONF.register_group(endpoint_group) CONF.register_opts(rpc_endpoint_opts, endpoint_group) endpoint_class = '%s.%s' % (CONF[endpoint_group.name].module, self.agent_type.capitalize()) try: cls = importutils.import_class(endpoint_class) except Exception: LOG.error('Import class %s of %s fail' % (endpoint_class, endpoint_group.name)) raise else: obj = cls(manager=self) if not isinstance(obj, RpcAgentEndpointBase): raise TypeError('Endpoint string %s not base from RpcEndpointBase') self.endpoints.add(obj) self.endpoint_lock = lockutils.Semaphores() self.websockets = dict() self.reporter = OnlinTaskReporter(self)
def _fetch_factory(factory_name): try: return importutils.import_class(factory_name) except (ImportError, ValueError) as e: raise ImportError("Could not import factory %r: %s" % (factory_name, e))