def initialize(): """Load configuration. Usage:: from DIRAC import initialize initialize() # Initialize configuration :return: S_OK()/S_ERROR() """ from DIRAC.ConfigurationSystem.Client.LocalConfiguration import LocalConfiguration return LocalConfiguration().initialize()
# $HeadURL$ # File : dirac-executor # Author : Adria Casajus ######################################################################## __RCSID__ = "$Id$" """ This is a script to launch DIRAC executors """ import sys import DIRAC from DIRAC.ConfigurationSystem.Client.LocalConfiguration import LocalConfiguration from DIRAC import gLogger, gConfig from DIRAC.Core.Base.ExecutorReactor import ExecutorReactor localCfg = LocalConfiguration() positionalArgs = localCfg.getPositionalArguments() if len( positionalArgs ) == 0: gLogger.fatal( "You must specify which executor to run!" ) sys.exit( 1 ) if len( positionalArgs ) == 1 and positionalArgs[0].find( "/" ) > -1: mainName = positionalArgs[0] else: mainName = "Framework/MultiExecutor" localCfg.setConfigurationForExecutor( mainName ) localCfg.addMandatoryEntry( "/DIRAC/Setup" ) localCfg.addDefaultEntry( "/DIRAC/Security/UseServerCertificate", "yes" ) localCfg.addDefaultEntry( "LogLevel", "INFO" )
def main(): localCfg = LocalConfiguration() localCfg.setUsageMessage(__doc__) positionalArgs = localCfg.getPositionalArguments() if len(positionalArgs) == 0: gLogger.fatal("You must specify which agent to run!") sys.exit(1) agentName = positionalArgs[0] localCfg.setConfigurationForAgent(agentName) localCfg.addMandatoryEntry("/DIRAC/Setup") localCfg.addDefaultEntry("/DIRAC/Security/UseServerCertificate", "yes") localCfg.addDefaultEntry("LogLevel", "INFO") localCfg.addDefaultEntry("LogColor", True) resultDict = localCfg.loadUserData() if not resultDict['OK']: gLogger.error("There were errors when loading configuration", resultDict['Message']) sys.exit(1) includeExtensionErrors() agentReactor = AgentReactor(positionalArgs[0]) result = agentReactor.loadAgentModules(positionalArgs) if result['OK']: agentReactor.go() else: gLogger.error("Error while loading agent module", result['Message']) sys.exit(2)
""" This is the guy that you should use when you develop a script that interacts with DIRAC And don't forget to call parseCommandLine() """ import sys import os.path from DIRAC.ConfigurationSystem.Client.LocalConfiguration import LocalConfiguration from DIRAC.FrameworkSystem.Client.Logger import gLogger from DIRAC.FrameworkSystem.Client.MonitoringClient import gMonitor from DIRAC.Core.Utilities.DErrno import includeExtensionErrors __RCSID__ = "$Id$" localCfg = LocalConfiguration() scriptName = os.path.basename(sys.argv[0]).replace('.py', '') gIsAlreadyInitialized = False def parseCommandLine(script=False, ignoreErrors=False, initializeMonitor=False): if gIsAlreadyInitialized: return False gLogger.showHeaders(False) return initialize(script, ignoreErrors, initializeMonitor, True) def initialize(script=False,
And don't forget to call parseCommandLine() """ __RCSID__ = "$Id$" import sys import os.path import inspect from DIRAC.ConfigurationSystem.Client.LocalConfiguration import LocalConfiguration from DIRAC.FrameworkSystem.Client.Logger import gLogger from DIRAC.FrameworkSystem.Client.MonitoringClient import gMonitor from DIRAC.Core.Utilities.DErrno import includeExtensionErrors localCfg = LocalConfiguration() caller = inspect.currentframe().f_back.f_globals['__name__'] i = 0 if caller != '__main__': # This is for the case when, for example, you run a DIRAC script from within pytest gLogger.debug("Called from module", caller) for arg in sys.argv: if os.path.basename(arg).replace('.py', '') == caller.split('.')[-1]: break i += 1 i = 0 if i == len(sys.argv) else i scriptName = os.path.basename(sys.argv[i]).replace('.py', '') localCfg.firstOptionIndex = i + 1
#!/usr/bin/env python import sys import tornado from DIRAC.Core.Base import Script from DIRAC.ConfigurationSystem.Client.LocalConfiguration import LocalConfiguration from DIRAC.FrameworkSystem.Client.Logger import gLogger from WebAppDIRAC.Core.App import App if __name__ == "__main__": localCfg = LocalConfiguration() localCfg.setConfigurationForWeb( "WebApp" ) localCfg.addMandatoryEntry( "/DIRAC/Setup" ) localCfg.addDefaultEntry( "/DIRAC/Security/UseServerCertificate", "yes" ) localCfg.addDefaultEntry( "LogLevel", "INFO" ) localCfg.addDefaultEntry( "LogColor", True ) result = localCfg.loadUserData() if not result[ 'OK' ]: gLogger.initialize( serverName, "/" ) gLogger.fatal( "There were errors when loading configuration", result[ 'Message' ] ) sys.exit( 1 ) app = App() result = app.bootstrap() if not result[ 'OK' ]: gLogger.fatal( result[ 'Message' ] ) sys.exit( 1 ) app.run()
def main(): localCfg = LocalConfiguration() localCfg.setUsageMessage(__doc__) positionalArgs = localCfg.getPositionalArguments() if len(positionalArgs) == 0: gLogger.fatal("You must specify which server to run!") sys.exit(1) serverName = positionalArgs[0] localCfg.setConfigurationForServer(serverName) localCfg.addMandatoryEntry("Port") # localCfg.addMandatoryEntry( "HandlerPath" ) localCfg.addMandatoryEntry("/DIRAC/Setup") localCfg.addDefaultEntry("/DIRAC/Security/UseServerCertificate", "yes") localCfg.addDefaultEntry("LogLevel", "INFO") localCfg.addDefaultEntry("LogColor", True) resultDict = localCfg.loadUserData() if not resultDict['OK']: gLogger.initialize(serverName, "/") gLogger.error("There were errors when loading configuration", resultDict['Message']) sys.exit(1) includeExtensionErrors() serverToLaunch = ServiceReactor() result = serverToLaunch.initialize(positionalArgs) if not result['OK']: gLogger.error(result['Message']) sys.exit(1) result = serverToLaunch.serve() if not result['OK']: gLogger.error(result['Message']) sys.exit(1)
def main(): localCfg = LocalConfiguration() localCfg.setUsageMessage(__doc__) positionalArgs = localCfg.getPositionalArguments() if len(positionalArgs) == 0: gLogger.fatal("You must specify which executor to run!") sys.exit(1) if len(positionalArgs) == 1 and positionalArgs[0].find("/") > -1: mainName = positionalArgs[0] else: mainName = "Framework/MultiExecutor" localCfg.setConfigurationForExecutor(mainName) localCfg.addMandatoryEntry("/DIRAC/Setup") localCfg.addDefaultEntry("/DIRAC/Security/UseServerCertificate", "yes") localCfg.addDefaultEntry("LogLevel", "INFO") localCfg.addDefaultEntry("LogColor", True) resultDict = localCfg.loadUserData() if not resultDict['OK']: gLogger.fatal("There were errors when loading configuration", resultDict['Message']) sys.exit(1) includeExtensionErrors() executorReactor = ExecutorReactor() result = executorReactor.loadModules(positionalArgs) if not result['OK']: gLogger.fatal("Error while loading executor", result['Message']) sys.exit(1) result = executorReactor.go() if not result['OK']: gLogger.fatal(result['Message']) sys.exit(1) gLogger.notice("Graceful exit. Bye!") sys.exit(0)
######################################################################## # $HeadURL$ # File : dirac-agent # Author : Adria Casajus, Andrei Tsaregorodtsev, Stuart Paterson ######################################################################## __RCSID__ = "$Id$" """ This is a script to launch DIRAC agents """ import sys import DIRAC from DIRAC.ConfigurationSystem.Client.LocalConfiguration import LocalConfiguration from DIRAC import gLogger, gConfig from DIRAC.Core.Base.AgentReactor import AgentReactor localCfg = LocalConfiguration() positionalArgs = localCfg.getPositionalArguments() if len(positionalArgs) == 0: gLogger.initialize("NOT SPECIFIED", "/") gLogger.fatal("You must specify which agent to run!") sys.exit(1) agentName = positionalArgs[0] localCfg.setConfigurationForAgent(agentName) localCfg.addMandatoryEntry("/DIRAC/Setup") localCfg.addDefaultEntry("/DIRAC/Security/UseServerCertificate", "yes") localCfg.addDefaultEntry("LogLevel", "INFO") resultDict = localCfg.loadUserData() if not resultDict['OK']: gLogger.error("There were errors when loading configuration",
def main(): # Must be defined BEFORE any dirac import os.environ['DIRAC_USE_TORNADO_IOLOOP'] = "True" from DIRAC import gConfig from DIRAC.ConfigurationSystem.Client import PathFinder from DIRAC.ConfigurationSystem.Client.ConfigurationData import gConfigurationData from DIRAC.ConfigurationSystem.Client.LocalConfiguration import LocalConfiguration from DIRAC.Core.Tornado.Server.TornadoServer import TornadoServer from DIRAC.Core.Utilities.DErrno import includeExtensionErrors from DIRAC.FrameworkSystem.Client.Logger import gLogger # We check if there is no configuration server started as master # If you want to start a master CS you should use Configuration_Server.cfg and # use tornado-start-CS.py key = '/Systems/Configuration/%s/Services/Server/Protocol' % PathFinder.getSystemInstance('Configuration') if gConfigurationData.isMaster() and gConfig.getValue(key, 'dips').lower() == 'https': gLogger.fatal("You can't run the CS and services in the same server!") sys.exit(0) localCfg = LocalConfiguration() localCfg.setConfigurationForServer('Tornado/Tornado') localCfg.addMandatoryEntry("/DIRAC/Setup") localCfg.addDefaultEntry("/DIRAC/Security/UseServerCertificate", "yes") localCfg.addDefaultEntry("LogLevel", "INFO") localCfg.addDefaultEntry("LogColor", True) resultDict = localCfg.loadUserData() if not resultDict['OK']: gLogger.initialize("Tornado", "/") gLogger.error("There were errors when loading configuration", resultDict['Message']) sys.exit(1) includeExtensionErrors() gLogger.initialize('Tornado', "/") serverToLaunch = TornadoServer() serverToLaunch.startTornado()
def main(): # Must be defined BEFORE any dirac import os.environ['DIRAC_USE_TORNADO_IOLOOP'] = "True" from DIRAC.ConfigurationSystem.Client.PathFinder import getServiceSection from DIRAC.ConfigurationSystem.Client.ConfigurationData import gConfigurationData from DIRAC.ConfigurationSystem.Client.LocalConfiguration import LocalConfiguration from DIRAC.ConfigurationSystem.private.Refresher import gRefresher from DIRAC.Core.Utilities.DErrno import includeExtensionErrors from DIRAC.Core.Tornado.Server.TornadoServer import TornadoServer from DIRAC.FrameworkSystem.Client.Logger import gLogger if gConfigurationData.isMaster(): gRefresher.disable() localCfg = LocalConfiguration() localCfg.addMandatoryEntry("/DIRAC/Setup") localCfg.addDefaultEntry("/DIRAC/Security/UseServerCertificate", "yes") localCfg.addDefaultEntry("LogLevel", "INFO") localCfg.addDefaultEntry("LogColor", True) resultDict = localCfg.loadUserData() if not resultDict['OK']: gLogger.initialize("Tornado-CS", "/") gLogger.error("There were errors when loading configuration", resultDict['Message']) sys.exit(1) includeExtensionErrors() gLogger.initialize('Tornado-CS', "/") # get the specific master CS port try: csPort = int( gConfigurationData.extractOptionFromCFG( '%s/Port' % getServiceSection('Configuration/Server'))) except TypeError: csPort = None serverToLaunch = TornadoServer(services='Configuration/Server', port=csPort) serverToLaunch.startTornado()
class DIRACScript(object): """Decorator for providing command line executables All console-scripts entrypoints in DIRAC and downstream extensions should be wrapped in this decorator to allow extensions to override any entry_point. """ scriptName = None alreadyInitialized = False localCfg = LocalConfiguration() def __call__(self, func=None): """Set the wrapped function or call the script This function is either called with a decorator or directly to call the underlying function. When running with Python 2 the raw function will always be called however in Python 3 the priorities will be applied from the dirac.extension_metadata entry_point. """ # If func is provided then the decorator is being applied to a function if func is not None: self._func = func # Find the name of the command and its documentation DIRACScript.localCfg.setUsageMessage(func.__globals__["__doc__"]) DIRACScript.scriptName = os.path.basename( func.__globals__["__file__"])[:-3].replace("_", "-") return functools.wraps(func)(self) # Setuptools based installations aren't supported with Python 2 if six.PY2: return self._func() # pylint: disable=not-callable # This is only available in Python 3.8+ so it has to be here for now from importlib import metadata # pylint: disable=no-name-in-module # Iterate through all known entry_points looking for self.scriptName matches = [ ep for ep in metadata.entry_points()["console_scripts"] if ep.name == self.scriptName ] if not matches: # TODO: This should an error once the integration tests modified to use pip install return self._func() # pylint: disable=not-callable # raise NotImplementedError("Something is very wrong") # Call the entry_point from the extension with the highest priority rankedExtensions = extensionsByPriority() entrypoint = min( matches, key=lambda e: rankedExtensions.index(entrypointToExtension(e)), ) entrypointFunc = entrypoint.load() # Check if entrypoint is DIRACScript if not isinstance(entrypointFunc, DIRACScript): raise ImportError( "Invalid dirac- console_scripts entry_point: " + repr(entrypoint) + "\n" + "All dirac- console_scripts should be wrapped in the DiracScript " + "decorator to ensure extension overlays are applied correctly." ) return entrypointFunc._func() @classmethod def parseCommandLine(cls, script=False, ignoreErrors=False, initializeMonitor=False): """Parse command line :param str script: script name :param bool ignoreErrors: ignore errors when loading configuration :param bool initializeMonitor: to use monitoring """ if not cls.alreadyInitialized: gLogger.showHeaders(False) cls.initialize(script, ignoreErrors, initializeMonitor, True) return (cls.localCfg.getUnprocessedSwitches(), cls.localCfg.getPositionalArguments()) @classmethod def initialize(cls, script=False, ignoreErrors=False, initializeMonitor=False, enableCommandLine=False): """initialization :param str script: script name :param bool ignoreErrors: ignore errors when loading configuration :param bool initializeMonitor: to use monitoring :param bool enableCommandLine: enable parse command line """ # Please do not call initialize in every file if cls.alreadyInitialized: return False userDisabled = not cls.localCfg.isCSEnabled() cls.alreadyInitialized = True if not userDisabled: cls.localCfg.disableCS() if not enableCommandLine: cls.localCfg.disableParsingCommandLine() if script: cls.scriptName = script cls.localCfg.setConfigurationForScript(cls.scriptName) if not ignoreErrors: cls.localCfg.addMandatoryEntry("/DIRAC/Setup") resultDict = cls.localCfg.loadUserData() if not ignoreErrors and not resultDict["OK"]: gLogger.error("There were errors when loading configuration", resultDict["Message"]) sys.exit(1) if not userDisabled: cls.localCfg.enableCS() if initializeMonitor: gMonitor.setComponentType(gMonitor.COMPONENT_SCRIPT) gMonitor.setComponentName(cls.scriptName) gMonitor.setComponentLocation("script") gMonitor.initialize() else: gMonitor.disable() includeExtensionErrors() return True @classmethod def showHelp(cls, dummy=False, exitCode=0): """See :func:`~DIRAC.ConfigurationSystem.Client.LocalConfiguration.LocalConfiguration.showHelp`.""" return cls.localCfg.showHelp(dummy=dummy, exitCode=exitCode) @classmethod def registerSwitches(cls, switches): """Register switches :param list switches: switches """ for switch in switches: cls.registerSwitch(*switch) @classmethod def registerSwitch(cls, showKey, longKey, helpString, callback=False): """See :func:`~DIRAC.ConfigurationSystem.Client.LocalConfiguration.LocalConfiguration.registerCmdOpt`.""" cls.localCfg.registerCmdOpt(showKey, longKey, helpString, callback) @classmethod def registerArgument(cls, description, mandatory=True, values=None, default=None): """See :func:`~DIRAC.ConfigurationSystem.Client.LocalConfiguration.LocalConfiguration.registerCmdArg`.""" cls.localCfg.registerCmdArg(description, mandatory, values, default) @classmethod def getPositionalArgs(cls, group=False): """See :func:`~DIRAC.ConfigurationSystem.Client.LocalConfiguration.LocalConfiguration.getPositionalArguments`.""" return cls.localCfg.getPositionalArguments(group) @classmethod def getExtraCLICFGFiles(cls): """See :func:`~DIRAC.ConfigurationSystem.Client.LocalConfiguration.LocalConfiguration.getExtraCLICFGFiles`.""" return cls.localCfg.getExtraCLICFGFiles() @classmethod def getUnprocessedSwitches(cls): """See :func:`~DIRAC.ConfigurationSystem.Client.LocalConfiguration.LocalConfiguration.getUnprocessedSwitches`.""" return cls.localCfg.getUnprocessedSwitches() @classmethod def addDefaultOptionValue(cls, option, value): """See :func:`~DIRAC.ConfigurationSystem.Client.LocalConfiguration.LocalConfiguration.addDefaultEntry`.""" cls.localCfg.addDefaultEntry(option, value) @classmethod def setUsageMessage(cls, usageMessage): """See :func:`~DIRAC.ConfigurationSystem.Client.LocalConfiguration.LocalConfiguration.setUsageMessage`.""" cls.localCfg.setUsageMessage(usageMessage) @classmethod def disableCS(cls): """See :func:`~DIRAC.ConfigurationSystem.Client.LocalConfiguration.LocalConfiguration.disableCS`.""" cls.localCfg.disableCS() @classmethod def enableCS(cls): """See :func:`~DIRAC.ConfigurationSystem.Client.LocalConfiguration.LocalConfiguration.enableCS`.""" return cls.localCfg.enableCS()
And don't forget to call parseCommandLine() """ __RCSID__ = "$Id$" import sys import os.path import inspect from DIRAC.ConfigurationSystem.Client.LocalConfiguration import LocalConfiguration from DIRAC.FrameworkSystem.Client.Logger import gLogger from DIRAC.FrameworkSystem.Client.MonitoringClient import gMonitor from DIRAC.Core.Utilities.DErrno import includeExtensionErrors localCfg = LocalConfiguration() caller = inspect.currentframe().f_back.f_globals['__name__'] # There are several ways to call DIRAC scripts: # * dirac-do-something # * dirac-do-something.py # * python dirac-do-something.py # * pytest test-dirac-do-something.py # * pytest --option test-dirac-do-something.py # * pytest test-dirac-do-something.py::class::method # The following lines attempt to keep only what is necessary to DIRAC, leaving the rest to pytest or whatever is before i = 0 if caller != '__main__':
#!/usr/bin/env python import sys import tornado from DIRAC.Core.Base import Script from DIRAC.ConfigurationSystem.Client.LocalConfiguration import LocalConfiguration from DIRAC.FrameworkSystem.Client.Logger import gLogger from WebAppDIRAC.Core.App import App if __name__ == "__main__": localCfg = LocalConfiguration() localCfg.setConfigurationForWeb("WebApp") localCfg.addMandatoryEntry("/DIRAC/Setup") localCfg.addDefaultEntry("/DIRAC/Security/UseServerCertificate", "yes") localCfg.addDefaultEntry("LogLevel", "INFO") localCfg.addDefaultEntry("LogColor", True) result = localCfg.loadUserData() if not result['OK']: gLogger.initialize(serverName, "/") gLogger.fatal("There were errors when loading configuration", result['Message']) sys.exit(1) app = App() result = app.bootstrap() if not result['OK']: gLogger.fatal(result['Message']) sys.exit(1)
import sys import tornado from DIRAC import gConfig, S_OK from DIRAC.Core.Base import Script from DIRAC.ConfigurationSystem.Client.LocalConfiguration import LocalConfiguration from DIRAC.FrameworkSystem.Client.Logger import gLogger from WebAppDIRAC.Core.App import App if __name__ == "__main__": def disableDevMode( op ): gConfig.setOptionValue( "/WebApp/DevelopMode", "False" ) return S_OK() localCfg = LocalConfiguration() localCfg.setConfigurationForWeb( "WebApp" ) localCfg.addMandatoryEntry( "/DIRAC/Setup" ) localCfg.addDefaultEntry( "/DIRAC/Security/UseServerCertificate", "yes" ) localCfg.addDefaultEntry( "LogLevel", "INFO" ) localCfg.addDefaultEntry( "LogColor", True ) localCfg.registerCmdOpt( "p", "production", "Enable production mode", disableDevMode ) result = localCfg.loadUserData() if not result[ 'OK' ]: gLogger.initialize( "WebApp", "/" ) gLogger.fatal( "There were errors when loading configuration", result[ 'Message' ] ) sys.exit( 1 ) app = App()
#!/usr/bin/env python ######################################################################## # $HeadURL$ # File : dirac-service # Author : Adria Casajus ######################################################################## __RCSID__ = "$Id$" import sys import DIRAC from DIRAC.ConfigurationSystem.Client.LocalConfiguration import LocalConfiguration from DIRAC.FrameworkSystem.Client.Logger import gLogger from DIRAC.Core.DISET.ServiceReactor import ServiceReactor localCfg = LocalConfiguration() positionalArgs = localCfg.getPositionalArguments() if len( positionalArgs ) == 0: gLogger.initialize( "NOT SPECIFIED", "/" ) gLogger.fatal( "You must specify which server to run!" ) sys.exit( 1 ) serverName = positionalArgs[0] localCfg.setConfigurationForServer( serverName ) localCfg.addMandatoryEntry( "Port" ) #localCfg.addMandatoryEntry( "HandlerPath" ) localCfg.addMandatoryEntry( "/DIRAC/Setup" ) localCfg.addDefaultEntry( "/DIRAC/Security/UseServerCertificate", "yes" ) resultDict = localCfg.loadUserData() if not resultDict[ 'OK' ]: gLogger.initialize( serverName, "/" )