Skip to content

Commit

Permalink
Client: Allow client to initialize without a set config file rucio#6410
Browse files Browse the repository at this point in the history
  • Loading branch information
voetberg committed Jan 18, 2024
1 parent 436ecc9 commit 2e78fd7
Show file tree
Hide file tree
Showing 3 changed files with 245 additions and 47 deletions.
15 changes: 8 additions & 7 deletions lib/rucio/client/baseclient.py
Expand Up @@ -111,8 +111,10 @@ def __init__(self, rucio_host=None, auth_host=None, account=None, ca_cert=None,
self.host = config_get('client', 'rucio_host')
if self.auth_host is None:
self.auth_host = config_get('client', 'auth_host')
except (NoOptionError, NoSectionError) as error:
raise MissingClientParameter('Section client and Option \'%s\' cannot be found in config file' % error.args[0])
except (NoOptionError, NoSectionError):
self.host = "https://rucio:443"
self.auth_host = "https://rucio:443"
self.logger.debug(f"Section client and Option rucio_host, auth_host cannot be found in config file, using {self.host} as host")

try:
self.trace_host = config_get('trace', 'trace_host')
Expand Down Expand Up @@ -145,8 +147,9 @@ def __init__(self, rucio_host=None, auth_host=None, account=None, ca_cert=None,
else:
try:
self.auth_type = config_get('client', 'auth_type')
except (NoOptionError, NoSectionError) as error:
raise MissingClientParameter('Option \'%s\' cannot be found in config file' % error.args[0])
except (NoOptionError, NoSectionError):
self.auth_type = "userpass"
self.logger.debug(f'Option client/auth_type cannot be found in config file, using {self.auth_type}.')

if self.auth_type == 'oidc':
if not self.creds:
Expand Down Expand Up @@ -282,9 +285,7 @@ def __init__(self, rucio_host=None, auth_host=None, account=None, ca_cert=None,
self.__authenticate()

try:
self.request_retries = config_get_int('client', 'request_retries')
except (NoOptionError, RuntimeError):
LOG.debug('request_retries not specified in config file. Taking default.')
self.request_retries = config_get_int('client', 'request_retries', raise_exception=False, default=self.request_retries)
except ValueError:
self.logger.debug('request_retries must be an integer. Taking default.')

Expand Down
32 changes: 27 additions & 5 deletions lib/rucio/common/config.py
Expand Up @@ -19,17 +19,22 @@
import json
import os
from collections.abc import Callable
from typing import TYPE_CHECKING, overload, Optional, TypeVar, Union
from typing import TYPE_CHECKING, Any, overload, Optional, TypeVar, Union

from rucio.common import exception
from rucio.common.exception import ConfigNotFound, DatabaseException

import logging
import tempfile

_T = TypeVar('_T')
_U = TypeVar('_U')

if TYPE_CHECKING:
from sqlalchemy.orm import Session

LoggerFunction = Callable[..., Any]


def convert_to_any_type(value) -> Union[bool, int, float, str]:
if value.lower() in ['true', 'yes', 'on']:
Expand Down Expand Up @@ -777,7 +782,7 @@ class Config:
The configuration class reading the config file on init, located by using
get_config_dirs or the use of the RUCIO_CONFIG environment variable.
"""
def __init__(self):
def __init__(self, logger: "LoggerFunction" = logging.log):
self.parser = configparser.ConfigParser()

if 'RUCIO_CONFIG' in os.environ:
Expand All @@ -786,9 +791,26 @@ def __init__(self):
configs = [os.path.join(confdir, 'rucio.cfg') for confdir in get_config_dirs()]
self.configfile = next(iter(filter(os.path.exists, configs)), None)
if self.configfile is None:
raise RuntimeError('Could not load Rucio configuration file. '
'Rucio looked in the following paths for a configuration file, in order:'
'\n\t' + '\n\t'.join(configs))
if ('RUCIO_CLIENT_MODE' not in os.environ) or (not os.environ['RUCIO_CLIENT_MODE']):
raise RuntimeError(
'Could not load configuration file. '
'A configuration file is required to run in server mode. '
'If trying to run in client mode, be sure to set RUCIO_CLIENT_MODE=True in your envoriment. '
'Rucio looked in the following paths for a configuration file, in order: '
'\n\t' + '\n\t'.join(configs))

logger(
level=30,
msg='Could not load Rucio configuration file. '
'Rucio looked in the following paths for a configuration file, in order: '
'\n\t' + '\n\t'.join(configs) + ''
'\n\t Using empty configuration.')

# Make a temp cfg file
self.configfile = tempfile.NamedTemporaryFile(suffix=".cfg").name
with open(self.configfile, 'w') as f:
f.write("") # File must have some content
f.close()

if not self.parser.read(self.configfile) == [self.configfile]:
raise RuntimeError('Could not load Rucio configuration file. '
Expand Down

0 comments on commit 2e78fd7

Please sign in to comment.