Allow configuration and data directory to be read from environment variables
Signed-off-by: Jörg Behrmann <behrmann@physik.fu-berlin.de>
This commit is contained in:
parent
1abc5a39ba
commit
be1a100314
|
@ -413,6 +413,13 @@ class RootConfig:
|
||||||
if hasattr(config, func_name):
|
if hasattr(config, func_name):
|
||||||
getattr(config, func_name)(*args, **kwargs)
|
getattr(config, func_name)(*args, **kwargs)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def paths_from_environment(envvar: str) -> List[str]:
|
||||||
|
envval = os.environ.get(envvar)
|
||||||
|
if not envval:
|
||||||
|
return []
|
||||||
|
return envval.split(":")
|
||||||
|
|
||||||
def generate_config(
|
def generate_config(
|
||||||
self,
|
self,
|
||||||
config_dir_path: str,
|
config_dir_path: str,
|
||||||
|
@ -601,7 +608,9 @@ class RootConfig:
|
||||||
action="append",
|
action="append",
|
||||||
metavar="CONFIG_FILE",
|
metavar="CONFIG_FILE",
|
||||||
help="Specify config file. Can be given multiple times and"
|
help="Specify config file. Can be given multiple times and"
|
||||||
" may specify directories containing *.yaml files.",
|
" may specify directories containing *.yaml files."
|
||||||
|
" Also uses the value of the CONFIGURATION_DIRECTORY environment"
|
||||||
|
" variable, where multiple values can be delimited by colons.",
|
||||||
)
|
)
|
||||||
|
|
||||||
# we nest the mutually-exclusive group inside another group so that the help
|
# we nest the mutually-exclusive group inside another group so that the help
|
||||||
|
@ -666,7 +675,8 @@ class RootConfig:
|
||||||
metavar="DIRECTORY",
|
metavar="DIRECTORY",
|
||||||
help=(
|
help=(
|
||||||
"Specify where data such as the media store and database file should be"
|
"Specify where data such as the media store and database file should be"
|
||||||
" stored. Defaults to the current working directory."
|
" stored. Defaults to the first entry of the STATE_DIRECTORY environment"
|
||||||
|
" variable or the current working directory in that order."
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
generate_group.add_argument(
|
generate_group.add_argument(
|
||||||
|
@ -681,7 +691,10 @@ class RootConfig:
|
||||||
cls.invoke_all_static("add_arguments", parser)
|
cls.invoke_all_static("add_arguments", parser)
|
||||||
config_args = parser.parse_args(argv)
|
config_args = parser.parse_args(argv)
|
||||||
|
|
||||||
config_files = find_config_files(search_paths=config_args.config_path)
|
config_paths_env = cls.paths_from_environment("CONFIGURATION_DIRECTORY")
|
||||||
|
config_files = find_config_files(
|
||||||
|
search_paths=config_paths_env + config_args.config_path
|
||||||
|
)
|
||||||
|
|
||||||
if not config_files:
|
if not config_files:
|
||||||
parser.error(
|
parser.error(
|
||||||
|
@ -695,7 +708,8 @@ class RootConfig:
|
||||||
else:
|
else:
|
||||||
config_dir_path = os.path.dirname(config_files[-1])
|
config_dir_path = os.path.dirname(config_files[-1])
|
||||||
config_dir_path = os.path.abspath(config_dir_path)
|
config_dir_path = os.path.abspath(config_dir_path)
|
||||||
data_dir_path = os.getcwd()
|
data_dir_path_env = cls.paths_from_environment("STATE_DIRECTORY")
|
||||||
|
data_dir_path = data_dir_path_env[0] if data_dir_path_env else os.getcwd()
|
||||||
|
|
||||||
obj = cls(config_files)
|
obj = cls(config_files)
|
||||||
|
|
||||||
|
@ -713,10 +727,7 @@ class RootConfig:
|
||||||
if not path_exists(config_path):
|
if not path_exists(config_path):
|
||||||
print("Generating config file %s" % (config_path,))
|
print("Generating config file %s" % (config_path,))
|
||||||
|
|
||||||
if config_args.data_directory:
|
data_dir_path = config_args.data_directory or data_dir_path
|
||||||
data_dir_path = config_args.data_directory
|
|
||||||
else:
|
|
||||||
data_dir_path = os.getcwd()
|
|
||||||
data_dir_path = os.path.abspath(data_dir_path)
|
data_dir_path = os.path.abspath(data_dir_path)
|
||||||
|
|
||||||
server_name = config_args.server_name
|
server_name = config_args.server_name
|
||||||
|
|
Loading…
Reference in New Issue