示例#1
0
    def __init__(self):
        """Standard Constructor"""

        try:
            section = getDatabaseSection("WorkloadManagement/ElasticJobParametersDB")
            indexPrefix = gConfig.getValue("%s/IndexPrefix" % section, CSGlobals.getSetup()).lower()

            # Connecting to the ES cluster
            super(ElasticJobParametersDB, self).__init__(name, "WorkloadManagement/ElasticJobParametersDB", indexPrefix)
        except Exception as ex:
            self.log.error("Can't connect to ElasticJobParametersDB", repr(ex))
            raise RuntimeError("Can't connect to ElasticJobParametersDB")

        self.indexName = "%s_%s" % (self.getIndexPrefix(), name.lower())
        # Verifying if the index is there, and if not create it
        res = self.existingIndex(self.indexName)
        if not res["OK"] or not res["Value"]:
            result = self.createIndex(self.indexName, mapping, period=None)
            if not result["OK"]:
                self.log.error(result["Message"])
                raise RuntimeError(result["Message"])
            self.log.always("Index created:", self.indexName)

        self.dslSearch = self._Search(self.indexName)
        self.dslSearch.extra(track_total_hits=True)
示例#2
0
 def __init__(self, name='Monitoring/MonitoringDB', readOnly=False):
   section = getDatabaseSection("Monitoring/MonitoringDB")
   indexPrefix = gConfig.getValue("%s/IndexPrefix" % section, CSGlobals.getSetup()).lower()
   super(MonitoringDB, self).__init__('MonitoringDB', name, indexPrefix)
   self.__readonly = readOnly
   self.__documents = {}
   self.__loadIndexes()
示例#3
0
def getElasticDBParameters(fullname):
    """
  Retrieve Database parameters from CS
  fullname should be of the form <System>/<DBname>

  """

    cs_path = getDatabaseSection(fullname)
    parameters = {}

    result = gConfig.getOption(cs_path + '/Host')
    if not result['OK']:
        # No host name found, try at the common place
        result = gConfig.getOption('/Systems/NoSQLDatabases/Host')
        if not result['OK']:
            return S_ERROR('Failed to get the configuration parameter: Host')
    dbHost = result['Value']
    # Check if the host is the local one and then set it to 'localhost' to use
    # a socket connection
    if dbHost != 'localhost':
        localHostName = socket.getfqdn()
        if localHostName == dbHost:
            dbHost = 'localhost'
    parameters['Host'] = dbHost

    # Elasticsearch standard port
    dbPort = 9200
    result = gConfig.getOption(cs_path + '/Port')
    if not result['OK']:
        # No individual port number found, try at the common place
        result = gConfig.getOption('/Systems/NoSQLDatabases/Port')
        if result['OK']:
            dbPort = int(result['Value'])
    else:
        dbPort = int(result['Value'])
    parameters['Port'] = dbPort

    result = gConfig.getOption(cs_path + '/User')
    if not result['OK']:
        # No individual user name found, try at the common place
        result = gConfig.getOption('/Systems/NoSQLDatabases/User')
        if not result['OK']:
            return S_ERROR('Failed to get the configuration parameter: User')
    dbUser = result['Value']
    parameters['User'] = dbUser

    result = gConfig.getOption(cs_path + '/Password')
    if not result['OK']:
        # No individual password found, try at the common place
        result = gConfig.getOption('/Systems/NoSQLDatabases/Password')
        if not result['OK']:
            return S_ERROR(
                'Failed to get the configuration parameter: Password')
    dbPass = result['Value']
    parameters['Password'] = dbPass

    return S_OK(parameters)
示例#4
0
    def __init__(self, name='Monitoring/MonitoringDB', readOnly=False):
        section = getDatabaseSection("Monitoring/MonitoringDB")
        indexPrefix = gConfig.getValue("%s/IndexPrefix" % section,
                                       CSGlobals.getSetup()).lower()
        super(MonitoringDB, self).__init__('MonitoringDB', name, indexPrefix)
        self.__readonly = readOnly
        self.documentTypes = {}

        # loads all monitoring indexes and types.
        objectsLoaded = TypeLoader('Monitoring').getTypes()

        # Load the files
        for pythonClassName in sorted(objectsLoaded):
            typeClass = objectsLoaded[pythonClassName]
            indexName = "%s_%s" % (self.getIndexPrefix(),
                                   typeClass()._getIndex())
            monitoringType = typeClass().__class__.__name__
            mapping = typeClass().mapping
            monfields = typeClass().monitoringFields
            period = typeClass().period
            self.documentTypes[monitoringType] = {
                'indexName': indexName,
                'mapping': mapping,
                'monitoringFields': monfields,
                'period': period
            }
            if self.__readonly:
                self.log.info("Read only mode is okay")
            else:
                if self.exists("%s-*" % indexName):
                    indexes = self.getIndexes()
                    if indexes:
                        actualIndexName = self.generateFullIndexName(
                            indexName, period)
                        if self.exists(actualIndexName):
                            self.log.info("The index exists:", actualIndexName)
                        else:
                            result = self.createIndex(
                                indexName,
                                self.documentTypes[monitoringType]['mapping'],
                                period)
                            if not result['OK']:
                                self.log.error(result['Message'])
                                raise RuntimeError(result['Message'])
                            self.log.info("The index is created",
                                          actualIndexName)
                else:
                    # in case the index does not exist
                    result = self.createIndex(
                        indexName,
                        self.documentTypes[monitoringType]['mapping'], period)
                    if not result['OK']:
                        self.log.error(result['Message'])
                        raise RuntimeError(result['Message'])
                    self.log.info("The index is created", indexName)
示例#5
0
    def __init__(self):
        """ Standard Constructor
    """

        section = getDatabaseSection(
            "WorkloadManagement/ElasticJobParametersDB")
        indexPrefix = gConfig.getValue("%s/IndexPrefix" % section,
                                       CSGlobals.getSetup()).lower()

        # Connecting to the ES cluster
        super(ElasticJobParametersDB,
              self).__init__(name, 'WorkloadManagement/ElasticJobParametersDB',
                             indexPrefix)

        self.indexName = "%s_%s" % (self.getIndexPrefix(), name.lower())
        # Verifying if the index is there, and if not create it
        if not self.exists(self.indexName):
            result = self.createIndex(self.indexName, mapping, period=None)
            if not result['OK']:
                self.log.error(result['Message'])
                raise RuntimeError(result['Message'])
            self.log.always("Index created:", self.indexName)

        self.dslSearch = self._Search(self.indexName)
示例#6
0
 def getCSOption(self, optionName, defaultValue=None):
   cs_path = getDatabaseSection(self.fullname)
   return gConfig.getValue("/%s/%s" % (cs_path, optionName), defaultValue)
示例#7
0
文件: DB.py 项目: DIRACGrid/DIRAC
 def getCSOption(self, optionName, defaultValue=None):
   cs_path = getDatabaseSection(self.fullname)
   return gConfig.getValue("/%s/%s" % (cs_path, optionName), defaultValue)
示例#8
0
    def __init__(self, name="Monitoring/MonitoringDB", readOnly=False):
        """Standard constructor"""

        try:
            section = getDatabaseSection("Monitoring/MonitoringDB")
            indexPrefix = gConfig.getValue("%s/IndexPrefix" % section,
                                           CSGlobals.getSetup()).lower()

            # Connecting to the ES cluster
            super(MonitoringDB, self).__init__(dbname=name.split("/")[1],
                                               fullName=name,
                                               indexPrefix=indexPrefix)
        except RuntimeError as ex:
            self.log.error("Can't connect to MonitoringDB", repr(ex))
            raise ex

        self.__readonly = readOnly
        self.documentTypes = {}

        # loads all monitoring indexes and types.
        objectsLoaded = TypeLoader("Monitoring").getTypes()

        # Load the files
        for pythonClassName in sorted(objectsLoaded):
            typeClass = objectsLoaded[pythonClassName]
            indexName = "%s_%s" % (self.getIndexPrefix(),
                                   typeClass()._getIndex())
            monitoringType = typeClass().__class__.__name__
            mapping = typeClass().mapping
            monfields = typeClass().monitoringFields
            period = self.getCSOption(
                "MonitoringTypes/%s/Period" % monitoringType,
                typeClass().period)
            self.documentTypes[monitoringType] = {
                "indexName": indexName,
                "mapping": mapping,
                "monitoringFields": monfields,
                "period": period,
            }
            if self.__readonly:
                self.log.info("Read only mode: no new index will be created")
            else:
                # Verifying if the index is there, and if not create it
                res = self.existingIndex("%s-*" %
                                         indexName)  # check with a wildcard
                if res["OK"] and res["Value"]:
                    actualIndexName = self.generateFullIndexName(
                        indexName, period)
                    res = self.existingIndex(
                        actualIndexName)  # check actual index
                    if not res["OK"] or not res["Value"]:
                        result = self.createIndex(
                            indexName,
                            self.documentTypes[monitoringType]["mapping"],
                            period)
                        if not result["OK"]:
                            self.log.error(result["Message"])
                            raise RuntimeError(result["Message"])
                        self.log.always("Index created", actualIndexName)
                else:
                    # in case the index pattern does not exist
                    result = self.createIndex(
                        indexName,
                        self.documentTypes[monitoringType]["mapping"], period)
                    if not result["OK"]:
                        self.log.error(result["Message"])
                        raise RuntimeError(result["Message"])
                    self.log.always("Index created", indexName)
示例#9
0
def getElasticDBParameters(fullname):
  """
  Retrieve Database parameters from CS
  fullname should be of the form <System>/<DBname>

  """

  cs_path = getDatabaseSection(fullname)
  parameters = {}

  result = gConfig.getOption(cs_path + '/Host')
  if not result['OK']:
    # No host name found, try at the common place
    result = gConfig.getOption('/Systems/NoSQLDatabases/Host')
    if not result['OK']:
      gLogger.warn("Failed to get the configuration parameter: Host. Using localhost")
      dbHost = 'localhost'
    else:
      dbHost = result['Value']
  else:
    dbHost = result['Value']
  # Check if the host is the local one and then set it to 'localhost' to use
  # a socket connection
  if dbHost != 'localhost':
    localHostName = socket.getfqdn()
    if localHostName == dbHost:
      dbHost = 'localhost'
  parameters['Host'] = dbHost

  # Elasticsearch standard port
  result = gConfig.getOption(cs_path + '/Port')
  if not result['OK']:
    # No individual port number found, try at the common place
    result = gConfig.getOption('/Systems/NoSQLDatabases/Port')
    if not result['OK']:
      gLogger.warn("Failed to get the configuration parameter: Port. Using 9200")
      dbPort = 9200
    else:
      dbPort = int(result['Value'])
  else:
    dbPort = int(result['Value'])
  parameters['Port'] = dbPort

  result = gConfig.getOption(cs_path + '/User')
  if not result['OK']:
    # No individual user name found, try at the common place
    result = gConfig.getOption('/Systems/NoSQLDatabases/User')
    if not result['OK']:
      gLogger.warn("Failed to get the configuration parameter: User. Assuming no user/password is provided/needed")
      dbUser = None
    else:
      dbUser = result['Value']
  else:
    dbUser = result['Value']
  parameters['User'] = dbUser

  result = gConfig.getOption(cs_path + '/Password')
  if not result['OK']:
    # No individual password found, try at the common place
    result = gConfig.getOption('/Systems/NoSQLDatabases/Password')
    if not result['OK']:
      gLogger.warn("Failed to get the configuration parameter: Password. Assuming no user/password is provided/needed")
      dbPass = None
    else:
      dbPass = result['Value']
  else:
    dbPass = result['Value']
  parameters['Password'] = dbPass

  result = gConfig.getOption(cs_path + '/SSL')
  if not result['OK']:
    # No SSL option found, try at the common place
    result = gConfig.getOption('/Systems/NoSQLDatabases/SSL')
    if not result['OK']:
      gLogger.warn("Failed to get the configuration parameter: SSL. Assuming SSL is needed")
      ssl = True
    else:
      ssl = False if result['Value'].lower() in ('false', 'no', 'n') else True
  else:
    ssl = False if result['Value'].lower() in ('false', 'no', 'n') else True
  parameters['SSL'] = ssl

  return S_OK(parameters)
示例#10
0
def getDBParameters(fullname):
  """
  Retrieve Database parameters from CS
  fullname should be of the form <System>/<DBname>
  defaultHost is the host to return if the option is not found in the CS.
  Not used as the method will fail if it cannot be found
  defaultPort is the port to return if the option is not found in the CS
  defaultUser is the user to return if the option is not found in the CS.
  Not usePassword is the password to return if the option is not found in
  the CS.
  Not used as the method will fail if it cannot be found
  defaultDB is the db to return if the option is not found in the CS.
  Not used as the method will fail if it cannot be found
  defaultQueueSize is the QueueSize to return if the option is not found in the
  CS

  Returns a dictionary with the keys: 'host', 'port', 'user', 'password',
  'db' and 'queueSize'
  """

  cs_path = getDatabaseSection(fullname)
  parameters = {}

  result = gConfig.getOption(cs_path + '/Host')
  if not result['OK']:
    # No host name found, try at the common place
    result = gConfig.getOption('/Systems/Databases/Host')
    if not result['OK']:
      return S_ERROR('Failed to get the configuration parameter: Host')
  dbHost = result['Value']
  # Check if the host is the local one and then set it to 'localhost' to use
  # a socket connection
  if dbHost != 'localhost':
    localHostName = socket.getfqdn()
    if localHostName == dbHost:
      dbHost = 'localhost'
  parameters['Host'] = dbHost

  # Mysql standard
  dbPort = 3306
  result = gConfig.getOption(cs_path + '/Port')
  if not result['OK']:
    # No individual port number found, try at the common place
    result = gConfig.getOption('/Systems/Databases/Port')
    if result['OK']:
      dbPort = int(result['Value'])
  else:
    dbPort = int(result['Value'])
  parameters['Port'] = dbPort

  result = gConfig.getOption(cs_path + '/User')
  if not result['OK']:
    # No individual user name found, try at the common place
    result = gConfig.getOption('/Systems/Databases/User')
    if not result['OK']:
      return S_ERROR('Failed to get the configuration parameter: User')
  dbUser = result['Value']
  parameters['User'] = dbUser

  result = gConfig.getOption(cs_path + '/Password')
  if not result['OK']:
    # No individual password found, try at the common place
    result = gConfig.getOption('/Systems/Databases/Password')
    if not result['OK']:
      return S_ERROR('Failed to get the configuration parameter: Password')
  dbPass = result['Value']
  parameters['Password'] = dbPass

  result = gConfig.getOption(cs_path + '/DBName')
  if not result['OK']:
    return S_ERROR('Failed to get the configuration parameter: DBName')
  dbName = result['Value']
  parameters['DBName'] = dbName

  return S_OK(parameters)
示例#11
0
文件: DB.py 项目: vfalbor/DIRAC
  def __init__( self, dbname, fullname, maxQueueSize ):

    self.database_name = dbname
    self.fullname = fullname
    self.cs_path = getDatabaseSection( fullname )

    self.log = gLogger.getSubLogger( self.database_name )

    self.dbHost = ''
    result = gConfig.getOption( self.cs_path + '/Host' )
    if not result['OK']:
      self.log.fatal( 'Failed to get the configuration parameters: Host' )
      return
    self.dbHost = result['Value']
    # Check if the host is the local one and then set it to 'localhost' to use
    # a socket connection
    if self.dbHost != 'localhost':
      localHostName = socket.getfqdn()
      if localHostName == self.dbHost:
        self.dbHost = 'localhost'
    self.dbUser = ''
    result = gConfig.getOption( self.cs_path + '/User' )
    if not result['OK']:
      # No individual user name found, try at the common place
      result = gConfig.getOption( '/Systems/Databases/User' )
      if not result['OK']:
        self.log.fatal( 'Failed to get the configuration parameters: User' )
        return
    self.dbUser = result['Value']
    self.dbPass = ''
    result = gConfig.getOption( self.cs_path + '/Password' )
    if not result['OK']:
      # No individual password found, try at the common place
      result = gConfig.getOption( '/Systems/Databases/Password' )
      if not result['OK']:
        self.log.fatal( 'Failed to get the configuration parameters: Password' )
        return
    self.dbPass = result['Value']
    self.dbName = ''
    result = gConfig.getOption( self.cs_path + '/DBName' )
    if not result['OK']:
      self.log.fatal( 'Failed to get the configuration parameters: DBName' )
      return
    self.dbName = result['Value']
    self.maxQueueSize = maxQueueSize
    result = gConfig.getOption( self.cs_path + '/MaxQueueSize' )
    if result['OK']:
      self.maxQueueSize = int( result['Value'] )

    MySQL.__init__( self, self.dbHost, self.dbUser, self.dbPass,
                   self.dbName, maxQueueSize = maxQueueSize )

    if not self._connected:
      err = 'Can not connect to DB, exiting...'
      self.log.fatal( err )
      sys.exit( err )


    self.log.info( "==================================================" )
    #self.log.info("SystemInstance: "+self.system)
    self.log.info( "User:           "******"Host:           " + self.dbHost )
    #self.log.info("Password:       "******"DBName:         " + self.dbName )
    self.log.info( "MaxQueue:       " + str(self.maxQueueSize) )
    self.log.info( "==================================================" )
示例#12
0
def getElasticDBParameters(fullname):
    """
  Retrieve Database parameters from CS
  fullname should be of the form <System>/<DBname>

  """

    cs_path = getDatabaseSection(fullname)
    parameters = {}

    result = gConfig.getOption(cs_path + '/Host')
    if not result['OK']:
        # No host name found, try at the common place
        result = gConfig.getOption('/Systems/NoSQLDatabases/Host')
        if not result['OK']:
            gLogger.warn(
                "Failed to get the configuration parameter: Host. Using localhost"
            )
            dbHost = 'localhost'
        else:
            dbHost = result['Value']
    else:
        dbHost = result['Value']
    # Check if the host is the local one and then set it to 'localhost' to use
    # a socket connection
    if dbHost != 'localhost':
        localHostName = socket.getfqdn()
        if localHostName == dbHost:
            dbHost = 'localhost'
    parameters['Host'] = dbHost

    # Elasticsearch standard port
    result = gConfig.getOption(cs_path + '/Port')
    if not result['OK']:
        # No individual port number found, try at the common place
        result = gConfig.getOption('/Systems/NoSQLDatabases/Port')
        if not result['OK']:
            gLogger.warn(
                "No configuration parameter set for Port, assuming URL points to right location"
            )
            dbPort = None
        else:
            dbPort = int(result['Value'])
    else:
        dbPort = int(result['Value'])
    parameters['Port'] = dbPort

    result = gConfig.getOption(cs_path + '/User')
    if not result['OK']:
        # No individual user name found, try at the common place
        result = gConfig.getOption('/Systems/NoSQLDatabases/User')
        if not result['OK']:
            gLogger.warn(
                "Failed to get the configuration parameter: User. Assuming no user/password is provided/needed"
            )
            dbUser = None
        else:
            dbUser = result['Value']
    else:
        dbUser = result['Value']
    parameters['User'] = dbUser

    result = gConfig.getOption(cs_path + '/Password')
    if not result['OK']:
        # No individual password found, try at the common place
        result = gConfig.getOption('/Systems/NoSQLDatabases/Password')
        if not result['OK']:
            gLogger.warn(
                "Failed to get the configuration parameter: Password. Assuming no user/password is provided/needed"
            )
            dbPass = None
        else:
            dbPass = result['Value']
    else:
        dbPass = result['Value']
    parameters['Password'] = dbPass

    result = gConfig.getOption(cs_path + '/SSL')
    if not result['OK']:
        # No SSL option found, try at the common place
        result = gConfig.getOption('/Systems/NoSQLDatabases/SSL')
        if not result['OK']:
            gLogger.warn(
                "Failed to get the configuration parameter: SSL. Assuming SSL is needed"
            )
            ssl = True
        else:
            ssl = False if result['Value'].lower() in ('false', 'no',
                                                       'n') else True
    else:
        ssl = False if result['Value'].lower() in ('false', 'no',
                                                   'n') else True
    parameters['SSL'] = ssl

    return S_OK(parameters)
示例#13
0
    def __init__(self, dbname, fullname, maxQueueSize, debug=False):

        self.database_name = dbname
        self.fullname = fullname
        self.cs_path = getDatabaseSection(fullname)

        self.log = gLogger.getSubLogger(self.database_name)

        self.dbHost = ""
        result = gConfig.getOption(self.cs_path + "/Host")
        if not result["OK"]:
            raise RuntimeError("Failed to get the configuration parameters: Host")
        self.dbHost = result["Value"]
        # Check if the host is the local one and then set it to 'localhost' to use
        # a socket connection
        if self.dbHost != "localhost":
            localHostName = socket.getfqdn()
            if localHostName == self.dbHost:
                self.dbHost = "localhost"

        self.dbPort = 3306
        result = gConfig.getOption(self.cs_path + "/Port")
        if not result["OK"]:
            # No individual port number found, try at the common place
            result = gConfig.getOption("/Systems/Databases/Port")
            if result["OK"]:
                self.dbPort = int(result["Value"])
        else:
            self.dbPort = int(result["Value"])

        self.dbUser = ""
        result = gConfig.getOption(self.cs_path + "/User")
        if not result["OK"]:
            # No individual user name found, try at the common place
            result = gConfig.getOption("/Systems/Databases/User")
            if not result["OK"]:
                raise RuntimeError("Failed to get the configuration parameters: User")
        self.dbUser = result["Value"]
        self.dbPass = ""
        result = gConfig.getOption(self.cs_path + "/Password")
        if not result["OK"]:
            # No individual password found, try at the common place
            result = gConfig.getOption("/Systems/Databases/Password")
            if not result["OK"]:
                raise RuntimeError("Failed to get the configuration parameters: Password")
        self.dbPass = result["Value"]
        self.dbName = ""
        result = gConfig.getOption(self.cs_path + "/DBName")
        if not result["OK"]:
            raise RuntimeError("Failed to get the configuration parameters: DBName")
        self.dbName = result["Value"]
        self.maxQueueSize = maxQueueSize
        result = gConfig.getOption(self.cs_path + "/MaxQueueSize")
        if result["OK"]:
            self.maxQueueSize = int(result["Value"])

        MySQL.__init__(
            self,
            self.dbHost,
            self.dbUser,
            self.dbPass,
            self.dbName,
            self.dbPort,
            maxQueueSize=maxQueueSize,
            debug=debug,
        )

        if not self._connected:
            raise RuntimeError("Can not connect to DB %s, exiting..." % dbname)

        self.log.info("==================================================")
        # self.log.info("SystemInstance: "+self.system)
        self.log.info("User:           "******"Host:           " + self.dbHost)
        self.log.info("Port:           " + str(self.dbPort))
        # self.log.info("Password:       "******"DBName:         " + self.dbName)
        self.log.info("MaxQueue:       " + str(self.maxQueueSize))
        self.log.info("==================================================")
示例#14
0
  def __init__( self, dbname, fullname, maxQueueSize ):

    self.database_name = dbname
    self.fullname = fullname
    self.cs_path = getDatabaseSection( fullname )

    self.log = gLogger.getSubLogger( self.database_name )

    self.dbHost = ''
    result = gConfig.getOption( self.cs_path + '/Host' )
    if not result['OK']:
      self.log.fatal( 'Failed to get the configuration parameters: Host' )
      return
    self.dbHost = result['Value']
    # Check if the host is the local one and then set it to 'localhost' to use
    # a socket connection
    if self.dbHost != 'localhost':
      localHostName = socket.getfqdn()
      if localHostName == self.dbHost:
        self.dbHost = 'localhost'
        
    self.dbPort = 3306
    result = gConfig.getOption( self.cs_path + '/Port' )
    if not result['OK']:
      # No individual port number found, try at the common place
      result = gConfig.getOption( '/Systems/Databases/Port' )
      if result['OK']:
        self.dbPort = int(result['Value'])
    else:
      self.dbPort = int(result['Value'])
        
    self.dbUser = ''
    result = gConfig.getOption( self.cs_path + '/User' )
    if not result['OK']:
      # No individual user name found, try at the common place
      result = gConfig.getOption( '/Systems/Databases/User' )
      if not result['OK']:
        self.log.fatal( 'Failed to get the configuration parameters: User' )
        return
    self.dbUser = result['Value']
    self.dbPass = ''
    result = gConfig.getOption( self.cs_path + '/Password' )
    if not result['OK']:
      # No individual password found, try at the common place
      result = gConfig.getOption( '/Systems/Databases/Password' )
      if not result['OK']:
        self.log.fatal( 'Failed to get the configuration parameters: Password' )
        return
    self.dbPass = result['Value']
    self.dbName = ''
    result = gConfig.getOption( self.cs_path + '/DBName' )
    if not result['OK']:
      self.log.fatal( 'Failed to get the configuration parameters: DBName' )
      return
    self.dbName = result['Value']
    self.maxQueueSize = maxQueueSize
    result = gConfig.getOption( self.cs_path + '/MaxQueueSize' )
    if result['OK']:
      self.maxQueueSize = int( result['Value'] )

    MySQL.__init__( self, self.dbHost, self.dbUser, self.dbPass,
                   self.dbName, self.dbPort, maxQueueSize = maxQueueSize )

    if not self._connected:
      err = 'Can not connect to DB, exiting...'
      self.log.fatal( err )
      sys.exit( err )


    self.log.info( "==================================================" )
    #self.log.info("SystemInstance: "+self.system)
    self.log.info( "User:           "******"Host:           " + self.dbHost )
    self.log.info( "Port:           " + str(self.dbPort) )
    #self.log.info("Password:       "******"DBName:         " + self.dbName )
    self.log.info( "MaxQueue:       " + str(self.maxQueueSize) )
    self.log.info( "==================================================" )
示例#15
0
文件: ProxyDB.py 项目: radonys/DIRAC
 def getFromAddr( self ):
   """ Get the From address to use in proxy expiry e-mails. """
   cs_path = getDatabaseSection( self.fullname )
   opt_path = "/%s/%s" % ( cs_path, "FromAddr" )
   return gConfig.getValue( opt_path, "*****@*****.**" )
示例#16
0
def getDBParameters(fullname):
    """Retrieve Database parameters from CS

    :param str fullname: should be of the form <System>/<DBname>
           defaultHost is the host to return if the option is not found in the CS.
           Not used as the method will fail if it cannot be found
           defaultPort is the port to return if the option is not found in the CS
           defaultUser is the user to return if the option is not found in the CS.
           Not usePassword is the password to return if the option is not found in the CS.
           Not used as the method will fail if it cannot be found
           defaultDB is the db to return if the option is not found in the CS.
           Not used as the method will fail if it cannot be found
           defaultQueueSize is the QueueSize to return if the option is not found in the CS

    :return: S_OK(dict)/S_ERROR() - dictionary with the keys: 'host', 'port', 'user', 'password',
                                    'db' and 'queueSize'
    """

    cs_path = getDatabaseSection(fullname)
    parameters = {}

    result = gConfig.getOption(cs_path + "/Host")
    if not result["OK"]:
        # No host name found, try at the common place
        result = gConfig.getOption("/Systems/Databases/Host")
        if not result["OK"]:
            return S_ERROR("Failed to get the configuration parameter: Host")
    dbHost = result["Value"]
    # Check if the host is the local one and then set it to 'localhost' to use
    # a socket connection
    if dbHost != "localhost":
        localHostName = socket.getfqdn()
        if localHostName == dbHost:
            dbHost = "localhost"
    parameters["Host"] = dbHost

    # Mysql standard
    dbPort = 3306
    result = gConfig.getOption(cs_path + "/Port")
    if not result["OK"]:
        # No individual port number found, try at the common place
        result = gConfig.getOption("/Systems/Databases/Port")
        if result["OK"]:
            dbPort = int(result["Value"])
    else:
        dbPort = int(result["Value"])
    parameters["Port"] = dbPort

    result = gConfig.getOption(cs_path + "/User")
    if not result["OK"]:
        # No individual user name found, try at the common place
        result = gConfig.getOption("/Systems/Databases/User")
        if not result["OK"]:
            return S_ERROR("Failed to get the configuration parameter: User")
    dbUser = result["Value"]
    parameters["User"] = dbUser

    result = gConfig.getOption(cs_path + "/Password")
    if not result["OK"]:
        # No individual password found, try at the common place
        result = gConfig.getOption("/Systems/Databases/Password")
        if not result["OK"]:
            return S_ERROR(
                "Failed to get the configuration parameter: Password")
    dbPass = result["Value"]
    parameters["Password"] = dbPass

    result = gConfig.getOption(cs_path + "/DBName")
    if not result["OK"]:
        return S_ERROR("Failed to get the configuration parameter: DBName")
    dbName = result["Value"]
    parameters["DBName"] = dbName

    return S_OK(parameters)
示例#17
0
def getDBParameters(fullname):
    """
  Retrieve Database parameters from CS
  fullname should be of the form <System>/<DBname>
  defaultHost is the host to return if the option is not found in the CS.
  Not used as the method will fail if it cannot be found
  defaultPort is the port to return if the option is not found in the CS
  defaultUser is the user to return if the option is not found in the CS.
  Not usePassword is the password to return if the option is not found in
  the CS.
  Not used as the method will fail if it cannot be found
  defaultDB is the db to return if the option is not found in the CS.
  Not used as the method will fail if it cannot be found
  defaultQueueSize is the QueueSize to return if the option is not found in the
  CS

  Returns a dictionary with the keys: 'host', 'port', 'user', 'password',
  'db' and 'queueSize'
  """

    cs_path = getDatabaseSection(fullname)
    parameters = {}

    result = gConfig.getOption(cs_path + '/Host')
    if not result['OK']:
        # No host name found, try at the common place
        result = gConfig.getOption('/Systems/Databases/Host')
        if not result['OK']:
            return S_ERROR('Failed to get the configuration parameter: Host')
    dbHost = result['Value']
    # Check if the host is the local one and then set it to 'localhost' to use
    # a socket connection
    if dbHost != 'localhost':
        localHostName = socket.getfqdn()
        if localHostName == dbHost:
            dbHost = 'localhost'
    parameters['Host'] = dbHost

    # Mysql standard
    dbPort = 3306
    result = gConfig.getOption(cs_path + '/Port')
    if not result['OK']:
        # No individual port number found, try at the common place
        result = gConfig.getOption('/Systems/Databases/Port')
        if result['OK']:
            dbPort = int(result['Value'])
    else:
        dbPort = int(result['Value'])
    parameters['Port'] = dbPort

    result = gConfig.getOption(cs_path + '/User')
    if not result['OK']:
        # No individual user name found, try at the common place
        result = gConfig.getOption('/Systems/Databases/User')
        if not result['OK']:
            return S_ERROR('Failed to get the configuration parameter: User')
    dbUser = result['Value']
    parameters['User'] = dbUser

    result = gConfig.getOption(cs_path + '/Password')
    if not result['OK']:
        # No individual password found, try at the common place
        result = gConfig.getOption('/Systems/Databases/Password')
        if not result['OK']:
            return S_ERROR(
                'Failed to get the configuration parameter: Password')
    dbPass = result['Value']
    parameters['Password'] = dbPass

    result = gConfig.getOption(cs_path + '/DBName')
    if not result['OK']:
        return S_ERROR('Failed to get the configuration parameter: DBName')
    dbName = result['Value']
    parameters['DBName'] = dbName

    return S_OK(parameters)
示例#18
0
def getElasticDBParameters(fullname):
    """Retrieve Database parameters from CS

    :param str fullname: should be of the form <System>/<DBname>

    :return: S_OK(dict)/S_ERROR()
    """

    cs_path = getDatabaseSection(fullname)
    parameters = {}

    result = gConfig.getOption(cs_path + "/Host")
    if not result["OK"]:
        # No host name found, try at the common place
        result = gConfig.getOption("/Systems/NoSQLDatabases/Host")
        if not result["OK"]:
            gLogger.warn(
                "Failed to get the configuration parameter: Host. Using localhost"
            )
            dbHost = "localhost"
        else:
            dbHost = result["Value"]
    else:
        dbHost = result["Value"]
    # Check if the host is the local one and then set it to 'localhost' to use
    # a socket connection
    if dbHost != "localhost":
        localHostName = socket.getfqdn()
        if localHostName == dbHost:
            dbHost = "localhost"
    parameters["Host"] = dbHost

    # Elasticsearch standard port
    result = gConfig.getOption(cs_path + "/Port")
    if not result["OK"]:
        # No individual port number found, try at the common place
        result = gConfig.getOption("/Systems/NoSQLDatabases/Port")
        if not result["OK"]:
            gLogger.warn(
                "No configuration parameter set for Port, assuming URL points to right location"
            )
            dbPort = None
        else:
            dbPort = int(result["Value"])
    else:
        dbPort = int(result["Value"])
    parameters["Port"] = dbPort

    result = gConfig.getOption(cs_path + "/User")
    if not result["OK"]:
        # No individual user name found, try at the common place
        result = gConfig.getOption("/Systems/NoSQLDatabases/User")
        if not result["OK"]:
            gLogger.warn(
                "Failed to get the configuration parameter: User. Assuming no user/password is provided/needed"
            )
            dbUser = None
        else:
            dbUser = result["Value"]
    else:
        dbUser = result["Value"]
    parameters["User"] = dbUser

    result = gConfig.getOption(cs_path + "/Password")
    if not result["OK"]:
        # No individual password found, try at the common place
        result = gConfig.getOption("/Systems/NoSQLDatabases/Password")
        if not result["OK"]:
            gLogger.warn(
                "Failed to get the configuration parameter: Password. Assuming no user/password is provided/needed"
            )
            dbPass = None
        else:
            dbPass = result["Value"]
    else:
        dbPass = result["Value"]
    parameters["Password"] = dbPass

    result = gConfig.getOption(cs_path + "/SSL")
    if not result["OK"]:
        # No SSL option found, try at the common place
        result = gConfig.getOption("/Systems/NoSQLDatabases/SSL")
        if not result["OK"]:
            gLogger.warn(
                "Failed to get the configuration parameter: SSL. Assuming SSL is needed"
            )
            ssl = True
        else:
            ssl = False if result["Value"].lower() in ("false", "no",
                                                       "n") else True
    else:
        ssl = False if result["Value"].lower() in ("false", "no",
                                                   "n") else True
    parameters["SSL"] = ssl

    # Elasticsearch use certs
    result = gConfig.getOption(cs_path + "/CRT")
    if not result["OK"]:
        # No CRT option found, try at the common place
        result = gConfig.getOption("/Systems/NoSQLDatabases/CRT")
        if not result["OK"]:
            gLogger.warn(
                "Failed to get the configuration parameter: CRT. Using False")
            certs = False
        else:
            certs = result["Value"]
    else:
        certs = result["Value"]
    parameters["CRT"] = certs

    # Elasticsearch ca_certs
    result = gConfig.getOption(cs_path + "/ca_certs")
    if not result["OK"]:
        # No CA certificate found, try at the common place
        result = gConfig.getOption("/Systems/NoSQLDatabases/ca_certs")
        if not result["OK"]:
            gLogger.warn(
                "Failed to get the configuration parameter: ca_certs. Using None"
            )
            ca_certs = None
        else:
            ca_certs = result["Value"]
    else:
        ca_certs = result["Value"]
    parameters["ca_certs"] = ca_certs

    # Elasticsearch client_key
    result = gConfig.getOption(cs_path + "/client_key")
    if not result["OK"]:
        # No client private key found, try at the common place
        result = gConfig.getOption("/Systems/NoSQLDatabases/client_key")
        if not result["OK"]:
            gLogger.warn(
                "Failed to get the configuration parameter: client_key. Using None"
            )
            client_key = None
        else:
            client_key = result["Value"]
    else:
        client_key = result["Value"]
    parameters["client_key"] = client_key

    # Elasticsearch client_cert
    result = gConfig.getOption(cs_path + "/client_cert")
    if not result["OK"]:
        # No cient certificate found, try at the common place
        result = gConfig.getOption("/Systems/NoSQLDatabases/client_cert")
        if not result["OK"]:
            gLogger.warn(
                "Failed to get the configuration parameter: client_cert. Using None"
            )
            client_cert = None
        else:
            client_cert = result["Value"]
    else:
        client_cert = result["Value"]
    parameters["client_cert"] = client_cert

    return S_OK(parameters)
示例#19
0
文件: DIRACDB.py 项目: pmusset/DIRAC
 def getCSOption(self, optionName, defaultValue=None):
     cs_path = getDatabaseSection(self.fullname)  # pylint: disable=no-member
     return gConfig.getValue("/%s/%s" % (cs_path, optionName), defaultValue)
示例#20
0
def getElasticDBParameters( fullname ):
  """
  Retrieve Database parameters from CS
  fullname should be of the form <System>/<DBname>

  """

  cs_path = getDatabaseSection( fullname )
  parameters = {}

  result = gConfig.getOption( cs_path + '/Host' )
  if not result['OK']:
    # No host name found, try at the common place
    result = gConfig.getOption( '/Systems/NoSQLDatabases/Host' )
    if not result['OK']:
      return S_ERROR( 'Failed to get the configuration parameter: Host' )
  dbHost = result['Value']
  # Check if the host is the local one and then set it to 'localhost' to use
  # a socket connection
  if dbHost != 'localhost':
    localHostName = socket.getfqdn()
    if localHostName == dbHost:
      dbHost = 'localhost'
  parameters[ 'Host' ] = dbHost

  # Elasticsearch standard port
  dbPort = 9200
  result = gConfig.getOption( cs_path + '/Port' )
  if not result['OK']:
    # No individual port number found, try at the common place
    result = gConfig.getOption( '/Systems/NoSQLDatabases/Port' )
    if result['OK']:
      dbPort = int( result['Value'] )
  else:
    dbPort = int( result['Value'] )
  parameters[ 'Port' ] = dbPort

  dbuserName = None
  result = gConfig.getOption( cs_path + '/User' )
  if not result['OK']:
    # No individual port number found, try at the common place
    result = gConfig.getOption( '/Systems/NoSQLDatabases/User' )
    if result['OK']:
      dbuserName = result['Value']
  else:
    dbuserName = result['Value']
    
  if dbuserName:
    parameters[ 'User' ] = dbuserName

  dbPassword = None
  result = gConfig.getOption( cs_path + '/Password' )
  if not result['OK']:
    # No individual port number found, try at the common place
    result = gConfig.getOption( '/Systems/NoSQLDatabases/Password' )
    if result['OK']:
      dbPassword = result['Value']
  else:
    dbPassword = result['Value']
  
  if dbPassword:
    parameters[ 'Password' ] = dbPassword

  return S_OK( parameters )