class ObjectLoaderMainSuccessScenario(unittest.TestCase): def setUp(self): self.ol = ObjectLoader() def __check(self, result): if not result['OK']: self.fail(result['Message']) return result['Value'] def test_load(self): self.__check(self.ol.loadObject("Core.Utilities.List", 'fromChar')) self.__check(self.ol.loadObject("Core.Utilities.ObjectLoader", "ObjectLoader")) dataFilter = self.__check(self.ol.getObjects("WorkloadManagementSystem.Service", ".*Handler")) dataClass = self.__check(self.ol.getObjects("WorkloadManagementSystem.Service", parentClass=RequestHandler)) self.assertEqual(sorted(dataFilter), sorted(dataClass))
class ObjectLoaderMainSuccessScenario( unittest.TestCase ): def setUp( self ): self.ol = ObjectLoader() def __check( self, result ): if not result[ 'OK' ]: self.fail( result[ 'Message' ] ) return result[ 'Value' ] def test_load( self ): self.__check( self.ol.loadObject( "Core.Utilities.List", 'fromChar' ) ) self.__check( self.ol.loadObject( "Core.Utilities.ObjectLoader", "ObjectLoader" ) ) dataFilter = self.__check( self.ol.getObjects( "WorkloadManagementSystem.Service", ".*Handler" ) ) dataClass = self.__check( self.ol.getObjects( "WorkloadManagementSystem.Service", parentClass = RequestHandler ) ) self.assertEqual( sorted( dataFilter.keys() ), sorted( dataClass.keys() ) )
def __calculateRoutes( self ): """ Load all handlers and generate the routes """ ol = ObjectLoader( [ 'WebAppDIRAC' ] ) origin = "WebApp.handler" result = ol.getObjects( origin, parentClass = WebHandler, recurse = True ) if not result[ 'OK' ]: return result self.__handlers = result[ 'Value' ] staticPaths = self.getPaths( "static" ) self.log.verbose( "Static paths found:\n - %s" % "\n - ".join( staticPaths ) ) self.__routes = [] for pattern in ( ( r"/static/(.*)", r"/(favicon\.ico)", r"/(robots\.txt)" ) ): if self.__baseURL: pattern = "/%s%s" % ( self.__baseURL, pattern ) self.__routes.append( ( pattern, StaticHandler, dict( pathList = staticPaths ) ) ) for hn in self.__handlers: self.log.info( "Found handler %s" % hn ) handler = self.__handlers[ hn ] #CHeck it has AUTH_PROPS if type( handler.AUTH_PROPS ) == None: return S_ERROR( "Handler %s does not have AUTH_PROPS defined. Fix it!" % hn ) #Get the root for the handler if handler.LOCATION: handlerRoute = handler.LOCATION.strip( "/") else: handlerRoute = hn[ len( origin ): ].replace( ".", "/" ).replace( "Handler", "" ) #Add the setup group RE before baseRoute = self.__setupGroupRE #IF theres a base url like /DIRAC add it if self.__baseURL: baseRoute = "/%s%s" % ( self.__baseURL, baseRoute ) #Set properly the LOCATION after calculating where it is with helpers to add group and setup later handler.LOCATION = handlerRoute handler.PATH_RE = re.compile( "%s(%s/.*)" % ( baseRoute, handlerRoute ) ) handler.URLSCHEMA = "/%s%%(setup)s%%(group)s%%(location)s/%%(action)s" % ( self.__baseURL ) #Look for methods that are exported for mName, mObj in inspect.getmembers( handler ): if inspect.ismethod( mObj ) and mName.find( "web_" ) == 0: if mName == "web_index": #Index methods have the bare url self.log.verbose( " - Route %s -> %s.web_index" % ( handlerRoute, hn ) ) route = "%s(%s/)" % ( baseRoute, handlerRoute ) self.__routes.append( ( route, handler ) ) self.__routes.append( ( route.rstrip( "/" ), CoreHandler, dict( action = 'addSlash' ) ) ) else: #Normal methods get the method appeded without web_ self.log.verbose( " - Route %s/%s -> %s.%s" % ( handlerRoute, mName[4:], hn, mName ) ) route = "%s(%s/%s)" % ( baseRoute, handlerRoute, mName[4:] ) self.__routes.append( ( route, handler ) ) self.log.debug( " * %s" % route ) #Send to root self.__routes.append( ( "%s(/?)" % self.__setupGroupRE, CoreHandler, dict( action = "sendToRoot" ) ) ) if self.__baseURL: self.__routes.append( ( "/%s%s()" % ( self.__baseURL, self.__setupGroupRE ), CoreHandler, dict( action = "sendToRoot" ) ) ) return S_OK()
def initializeOptimizer(cls): objLoader = ObjectLoader() result = objLoader.getObjects("WorkloadManagementSystem.Splitters", reFilter=".*Splitter", parentClass=BaseSplitter) if not result['OK']: return result data = result['Value'] cls.__splitters = {} for k in data: spClass = data[k] spName = k.split(".")[-1][:-8] cls.__splitters[spName] = spClass cls.log.notice("Found %s splitter" % spName) cls.ex_setOption("FailedStatus", "Invalid split") return S_OK()
def initializeOptimizer( cls ): objLoader = ObjectLoader() result = objLoader.getObjects( "WorkloadManagementSystem.Splitters", reFilter = ".*Splitter", parentClass = BaseSplitter ) if not result[ 'OK' ]: return result data = result[ 'Value' ] cls.__splitters = {} for k in data: spClass = data[k] spName = k.split(".")[-1][:-8] cls.__splitters[ spName ] = spClass cls.log.notice( "Found %s splitter" % spName ) cls.ex_setOption( "FailedStatus", "Invalid split" ) return S_OK()
def bootstrap( self ): gLogger.always( "\n === Bootstrapping REST Server === \n" ) ol = ObjectLoader( [ 'DIRAC', 'RESTDIRAC' ] ) result = ol.getObjects( "RESTSystem.API", parentClass = RESTHandler, recurse = True ) if not result[ 'OK' ]: return result self.__handlers = result[ 'Value' ] if not self.__handlers: return S_ERROR( "No handlers found" ) self.__routes = [ ( self.__handlers[ k ].getRoute(), self.__handlers[k] ) for k in self.__handlers if self.__handlers[ k ].getRoute() ] gLogger.info( "Routes found:" ) for t in sorted( self.__routes ): gLogger.info( " - %s : %s" % ( t[0], t[1].__name__ ) ) balancer = RESTConf.balancer() kw = dict( debug = RESTConf.debug(), log_function = self._logRequest ) if balancer and RESTConf.numProcesses not in ( 0, 1 ): process.fork_processes( RESTConf.numProcesses(), max_restarts = 0 ) kw[ 'debug' ] = False if kw[ 'debug' ]: gLogger.always( "Starting in debug mode" ) self.__app = web.Application( self.__routes, **kw ) port = RESTConf.port() if balancer: gLogger.notice( "Configuring REST HTTP service for balancer %s on port %s" % ( balancer, port ) ) self.__sslops = False else: gLogger.notice( "Configuring REST HTTPS service on port %s" % port ) self.__sslops = dict( certfile = RESTConf.cert(), keyfile = RESTConf.key(), cert_reqs = ssl.CERT_OPTIONAL, ca_certs = RESTConf.generateCAFile() ) self.__httpSrv = httpserver.HTTPServer( self.__app, ssl_options = self.__sslops ) self.__httpSrv.listen( port ) return S_OK()
def __calculateRoutes(self): """ Load all handlers and generate the routes """ ol = ObjectLoader() origin = "WebApp.handler" result = ol.getObjects(origin, parentClass=WebHandler, recurse=True) if not result['OK']: return result self.__handlers = result['Value'] staticPaths = self.getPaths("static") self.log.verbose("Static paths found:\n - %s" % "\n - ".join(staticPaths)) self.__routes = [] # Add some standard paths for static files for stdir in ['defaults', 'demo']: pattern = '/%s/(.*)' % stdir self.__routes.append( (pattern, StaticHandler, dict(pathList=['%s/webRoot/www/%s' % (rootPath, stdir)]))) self.log.debug(" - Static route: %s" % pattern) for pattern in ((r"/static/(.*)", r"/(favicon\.ico)", r"/(robots\.txt)")): pattern = r"%s%s" % (self.__shySetupGroupRE, pattern) if self.__baseURL: pattern = "/%s%s" % (self.__baseURL, pattern) self.__routes.append( (pattern, StaticHandler, dict(pathList=staticPaths))) self.log.debug(" - Static route: %s" % pattern) for hn in self.__handlers: self.log.info("Found handler %s" % hn) handler = self.__handlers[hn] #CHeck it has AUTH_PROPS if type(handler.AUTH_PROPS) == None: return S_ERROR( "Handler %s does not have AUTH_PROPS defined. Fix it!" % hn) #Get the root for the handler if handler.LOCATION: handlerRoute = handler.LOCATION.strip("/") else: handlerRoute = hn[len(origin):].replace(".", "/").replace( "Handler", "") #Add the setup group RE before baseRoute = self.__setupGroupRE #IF theres a base url like /DIRAC add it if self.__baseURL: baseRoute = "/%s%s" % (self.__baseURL, baseRoute) #Set properly the LOCATION after calculating where it is with helpers to add group and setup later handler.LOCATION = handlerRoute handler.PATH_RE = re.compile("%s(%s/.*)" % (baseRoute, handlerRoute)) handler.URLSCHEMA = "/%s%%(setup)s%%(group)s%%(location)s/%%(action)s" % ( self.__baseURL) if issubclass(handler, WebSocketHandler): handler.PATH_RE = re.compile("%s(%s)" % (baseRoute, handlerRoute)) route = "%s(%s)" % (baseRoute, handlerRoute) self.__routes.append((route, handler)) self.log.verbose(" - WebSocket %s -> %s" % (handlerRoute, hn)) self.log.debug(" * %s" % route) continue #Look for methods that are exported for mName, mObj in inspect.getmembers(handler): if inspect.ismethod(mObj) and mName.find("web_") == 0: if mName == "web_index": #Index methods have the bare url self.log.verbose(" - Route %s -> %s.web_index" % (handlerRoute, hn)) route = "%s(%s/)" % (baseRoute, handlerRoute) self.__routes.append((route, handler)) self.__routes.append( ("%s(%s)" % (baseRoute, handlerRoute), CoreHandler, dict(action='addSlash'))) else: #Normal methods get the method appeded without web_ self.log.verbose(" - Route %s/%s -> %s.%s" % (handlerRoute, mName[4:], hn, mName)) route = "%s(%s/%s)" % (baseRoute, handlerRoute, mName[4:]) self.__routes.append((route, handler)) self.log.debug(" * %s" % route) #Send to root self.__routes.append(("%s(/?)" % self.__setupGroupRE, CoreHandler, dict(action="sendToRoot"))) if self.__baseURL: self.__routes.append( ("/%s%s()" % (self.__baseURL, self.__setupGroupRE), CoreHandler, dict(action="sendToRoot"))) return S_OK()