Skip to content

Commit 5c453b3

Browse files
authored
Merge pull request #6444 from DIRACGridBot/cherry-pick-2-b879c174d-integration
[sweep:integration] fix: remove warning messages when initializing ElasticDB parameters
2 parents 6588fc3 + 28195c1 commit 5c453b3

File tree

12 files changed

+139
-121
lines changed

12 files changed

+139
-121
lines changed

integration_tests.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,8 @@ def _make_config(modules, flags, release_var, editable):
621621
"DB_HOST": DB_HOST,
622622
"DB_PORT": DB_PORT,
623623
# ElasticSearch settings
624+
"NoSQLDB_USER": "elastic",
625+
"NoSQLDB_PASSWORD": "changeme",
624626
"NoSQLDB_HOST": "elasticsearch",
625627
"NoSQLDB_PORT": "9200",
626628
# Hostnames

src/DIRAC/ConfigurationSystem/Client/Utilities.py

Lines changed: 46 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -369,15 +369,6 @@ def getDBParameters(fullname):
369369
"""Retrieve Database parameters from CS
370370
371371
:param str fullname: should be of the form <System>/<DBname>
372-
defaultHost is the host to return if the option is not found in the CS.
373-
Not used as the method will fail if it cannot be found
374-
defaultPort is the port to return if the option is not found in the CS
375-
defaultUser is the user to return if the option is not found in the CS.
376-
Not usePassword is the password to return if the option is not found in the CS.
377-
Not used as the method will fail if it cannot be found
378-
defaultDB is the db to return if the option is not found in the CS.
379-
Not used as the method will fail if it cannot be found
380-
defaultQueueSize is the QueueSize to return if the option is not found in the CS
381372
382373
:return: S_OK(dict)/S_ERROR() - dictionary with the keys: 'host', 'port', 'user', 'password',
383374
'db' and 'queueSize'
@@ -386,6 +377,25 @@ def getDBParameters(fullname):
386377
cs_path = getDatabaseSection(fullname)
387378
parameters = {}
388379

380+
# Check mandatory parameters first: Password, User, Host and DBName
381+
result = gConfig.getOption(cs_path + "/Password")
382+
if not result["OK"]:
383+
# No individual password found, try at the common place
384+
result = gConfig.getOption("/Systems/Databases/Password")
385+
if not result["OK"]:
386+
return S_ERROR("Failed to get the configuration parameter: Password")
387+
dbPass = result["Value"]
388+
parameters["Password"] = dbPass
389+
390+
result = gConfig.getOption(cs_path + "/User")
391+
if not result["OK"]:
392+
# No individual user name found, try at the common place
393+
result = gConfig.getOption("/Systems/Databases/User")
394+
if not result["OK"]:
395+
return S_ERROR("Failed to get the configuration parameter: User")
396+
dbUser = result["Value"]
397+
parameters["User"] = dbUser
398+
389399
result = gConfig.getOption(cs_path + "/Host")
390400
if not result["OK"]:
391401
# No host name found, try at the common place
@@ -401,6 +411,13 @@ def getDBParameters(fullname):
401411
dbHost = "localhost"
402412
parameters["Host"] = dbHost
403413

414+
result = gConfig.getOption(cs_path + "/DBName")
415+
if not result["OK"]:
416+
return S_ERROR("Failed to get the configuration parameter: DBName")
417+
dbName = result["Value"]
418+
parameters["DBName"] = dbName
419+
420+
# Check optional parameters: Port
404421
# Mysql standard
405422
dbPort = 3306
406423
result = gConfig.getOption(cs_path + "/Port")
@@ -413,30 +430,6 @@ def getDBParameters(fullname):
413430
dbPort = int(result["Value"])
414431
parameters["Port"] = dbPort
415432

416-
result = gConfig.getOption(cs_path + "/User")
417-
if not result["OK"]:
418-
# No individual user name found, try at the common place
419-
result = gConfig.getOption("/Systems/Databases/User")
420-
if not result["OK"]:
421-
return S_ERROR("Failed to get the configuration parameter: User")
422-
dbUser = result["Value"]
423-
parameters["User"] = dbUser
424-
425-
result = gConfig.getOption(cs_path + "/Password")
426-
if not result["OK"]:
427-
# No individual password found, try at the common place
428-
result = gConfig.getOption("/Systems/Databases/Password")
429-
if not result["OK"]:
430-
return S_ERROR("Failed to get the configuration parameter: Password")
431-
dbPass = result["Value"]
432-
parameters["Password"] = dbPass
433-
434-
result = gConfig.getOption(cs_path + "/DBName")
435-
if not result["OK"]:
436-
return S_ERROR("Failed to get the configuration parameter: DBName")
437-
dbName = result["Value"]
438-
parameters["DBName"] = dbName
439-
440433
return S_OK(parameters)
441434

442435

@@ -451,6 +444,26 @@ def getElasticDBParameters(fullname):
451444
cs_path = getDatabaseSection(fullname)
452445
parameters = {}
453446

447+
# Check mandatory parameters first: Password, User
448+
result = gConfig.getOption(cs_path + "/Password")
449+
if not result["OK"]:
450+
# No individual password found, try at the common place
451+
result = gConfig.getOption("/Systems/NoSQLDatabases/Password")
452+
if not result["OK"]:
453+
return S_ERROR("Failed to get the configuration parameter: Password.")
454+
dbPass = result["Value"]
455+
parameters["Password"] = dbPass
456+
457+
result = gConfig.getOption(cs_path + "/User")
458+
if not result["OK"]:
459+
# No individual user name found, try at the common place
460+
result = gConfig.getOption("/Systems/NoSQLDatabases/User")
461+
if not result["OK"]:
462+
return S_ERROR("Failed to get the configuration parameter: User.")
463+
dbUser = result["Value"]
464+
parameters["User"] = dbUser
465+
466+
# Check optional parameters: Host, Port, SSL, CRT, ca_certs, client_key, client_cert
454467
result = gConfig.getOption(cs_path + "/Host")
455468
if not result["OK"]:
456469
# No host name found, try at the common place
@@ -484,36 +497,6 @@ def getElasticDBParameters(fullname):
484497
dbPort = int(result["Value"])
485498
parameters["Port"] = dbPort
486499

487-
result = gConfig.getOption(cs_path + "/User")
488-
if not result["OK"]:
489-
# No individual user name found, try at the common place
490-
result = gConfig.getOption("/Systems/NoSQLDatabases/User")
491-
if not result["OK"]:
492-
gLogger.warn(
493-
"Failed to get the configuration parameter: User. Assuming no user/password is provided/needed"
494-
)
495-
dbUser = None
496-
else:
497-
dbUser = result["Value"]
498-
else:
499-
dbUser = result["Value"]
500-
parameters["User"] = dbUser
501-
502-
result = gConfig.getOption(cs_path + "/Password")
503-
if not result["OK"]:
504-
# No individual password found, try at the common place
505-
result = gConfig.getOption("/Systems/NoSQLDatabases/Password")
506-
if not result["OK"]:
507-
gLogger.warn(
508-
"Failed to get the configuration parameter: Password. Assuming no user/password is provided/needed"
509-
)
510-
dbPass = None
511-
else:
512-
dbPass = result["Value"]
513-
else:
514-
dbPass = result["Value"]
515-
parameters["Password"] = dbPass
516-
517500
result = gConfig.getOption(cs_path + "/SSL")
518501
if not result["OK"]:
519502
# No SSL option found, try at the common place

src/DIRAC/Core/Base/ElasticDB.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ def __init__(self, dbname, fullName, indexPrefix="", parentLogger=None):
2727
dbParameters = result["Value"]
2828
self._dbHost = dbParameters["Host"]
2929
self._dbPort = dbParameters["Port"]
30-
# we can have db which does not have any authentication...
31-
self.__user = dbParameters.get("User", "")
32-
self.__dbPassword = dbParameters.get("Password", "")
30+
self.__user = dbParameters["User"]
31+
self.__dbPassword = dbParameters["Password"]
3332
self.__useSSL = dbParameters.get("SSL", True)
3433
self.__useCRT = dbParameters.get("CRT", True)
3534
self.__ca_certs = dbParameters.get("ca_certs", None)

src/DIRAC/Core/Utilities/ElasticSearchDB.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,22 +137,23 @@ def __init__(
137137
if user and password:
138138
sLog.debug("Specified username and password")
139139
if port:
140-
self.__url = "https://%s:%s@%s:%d" % (user, password, host, port)
140+
self.__url = f"://{user}:{password}@{host}:{port}"
141141
else:
142-
self.__url = f"https://{user}:{password}@{host}"
142+
self.__url = f"://{user}:{password}@{host}"
143143
else:
144144
sLog.debug("Username and password not specified")
145145
if port:
146-
self.__url = "http://%s:%d" % (host, port)
146+
self.__url = f"://{host}:{port}"
147147
else:
148-
self.__url = "http://%s" % host
148+
self.__url = f"://{host}"
149149

150150
if port:
151151
sLog.verbose(f"Connecting to {host}:{port}, useSSL = {useSSL}")
152152
else:
153153
sLog.verbose(f"Connecting to {host}, useSSL = {useSSL}")
154154

155155
if useSSL:
156+
self.__url = "https" + self.__url
156157
if ca_certs:
157158
casFile = ca_certs
158159
else:
@@ -169,6 +170,7 @@ def __init__(
169170
self.__url, timeout=self.__timeout, use_ssl=True, verify_certs=True, ca_certs=casFile
170171
)
171172
elif useCRT:
173+
self.__url = "https" + self.__url
172174
self.client = Elasticsearch(
173175
self.__url,
174176
timeout=self.__timeout,
@@ -179,6 +181,7 @@ def __init__(
179181
client_key=client_key,
180182
)
181183
else:
184+
self.__url = "http" + self.__url
182185
self.client = Elasticsearch(self.__url, timeout=self.__timeout)
183186

184187
# Before we use the database we try to connect
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
Provide uniform interface to backend for local and remote clients.return
3+
4+
There's a pretty big assumption here: that DB and Handler expose the same calls, with identical signatures.
5+
This is not always the case.
6+
"""
7+
8+
9+
def getDBOrClient(DB, serverName):
10+
"""Tries to instantiate the DB object and returns it if we manage to connect to the DB,
11+
otherwise returns a Client of the server
12+
"""
13+
from DIRAC import gLogger
14+
from DIRAC.Core.Base.Client import Client
15+
16+
try:
17+
database = DB()
18+
if database._connected:
19+
return database
20+
except Exception:
21+
pass
22+
23+
gLogger.info(f"Can not connect to DB will use {serverName}")
24+
return Client(url=serverName)

src/DIRAC/MonitoringSystem/Client/ServerUtils.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,7 @@
33
It always try to insert the records directly. In case of failure the monitoring client is used...
44
"""
55

6-
7-
def getDBOrClient(DB, serverName):
8-
"""Tries to instantiate the DB object and returns it if we manage to connect to the DB,
9-
otherwise returns a Client of the server
10-
"""
11-
from DIRAC import gLogger
12-
from DIRAC.Core.Base.Client import Client
13-
14-
try:
15-
database = DB()
16-
if database._connected:
17-
return database
18-
except Exception:
19-
pass
20-
21-
gLogger.info(f"Can not connect to DB will use {serverName}")
22-
return Client(url=serverName)
6+
from DIRAC.Core.Utilities.ServerUtils import getDBOrClient
237

248

259
def getMonitoringDB():

src/DIRAC/WorkloadManagementSystem/Client/ServerUtils.py

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,9 @@
11
"""
2-
Provide uniform interface to backend for local and remote clients.return
3-
4-
There's a pretty big assumption here: that DB and Handler expose the same calls, with identical signatures.
5-
This is not exactly the case for WMS DBs and services.
2+
This module is used to create an appropriate object which can be used to insert records to the WMS.
3+
It always try to insert the records directly. In case of failure a WMS client is used...
64
"""
75

8-
9-
def getDBOrClient(DB, serverName):
10-
"""Tries to instantiate the DB object
11-
and returns it if we manage to connect to the DB,
12-
otherwise returns a Client of the server
13-
"""
14-
from DIRAC import gLogger
15-
from DIRAC.Core.Base.Client import Client
16-
17-
try:
18-
myDB = DB()
19-
if myDB._connected:
20-
return myDB
21-
except Exception:
22-
pass
23-
24-
gLogger.info("Can not connect to DB will use %s" % serverName)
25-
return Client(url=serverName)
6+
from DIRAC.Core.Utilities.ServerUtils import getDBOrClient
267

278

289
def getPilotAgentsDB():

tests/CI/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ services:
2222
- 9200:9200
2323
env_file: "${ES_VER}.env"
2424
healthcheck:
25-
test: ["CMD", "curl", "-f", "http://localhost:9200"]
25+
test: ["CMD", "curl", "-f", "-u", "elastic:changeme", "http://localhost:9200"]
2626
interval: 5s
2727
timeout: 2s
2828
retries: 15

tests/CI/envs/elasticsearch:7.9.1.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ cluster.routing.allocation.disk.threshold_enabled=true
33
cluster.routing.allocation.disk.watermark.flood_stage=200mb
44
cluster.routing.allocation.disk.watermark.low=500mb
55
cluster.routing.allocation.disk.watermark.high=300mb
6+
xpack.security.enabled=true
7+
ELASTIC_PASSWORD="changeme"
68
# Elasticsearch allocates 1GB of memory by default. As resources are limited
79
# and elasticsearch performance isn't critical in CI, limit this to 256MB
810
"ES_JAVA_OPTS=-Xms256m -Xmx256m"

0 commit comments

Comments
 (0)