Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environ config list #659

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions quetz/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ def _set_user_roles(db: Session, config: Config):
]

default_role = config.users_default_role

for users, role in role_map:
for username in users:
try:
Expand All @@ -186,8 +185,8 @@ def _set_user_roles(db: Session, config: Config):
# use github as default provider
raise ValueError(
"could not parse the users setting, please provide users in"
"the format 'PROVIDER:USERNAME' where PROVIDER is one of"
"'google', 'github', 'dummy', etc."
" the format 'PROVIDER:USERNAME' where PROVIDER is one of"
" 'google', 'github', 'dummy', etc."
)
logger.info(f"create user {username} with role {role}")
user = (
Expand Down Expand Up @@ -462,6 +461,23 @@ def create(
)
raise typer.Abort()

if copy_conf:
if not os.path.exists(copy_conf):
typer.echo(f'Config file to copy does not exist {copy_conf}.', err=True)
raise typer.Abort()
typer.echo(f"Copying config file from {copy_conf} to {config_file}")
shutil.copyfile(copy_conf, config_file)

if not config_file.exists() and create_conf:
https = 'false' if dev else 'true'
conf = create_config(https=https)
with open(config_file, 'w') as f:
f.write(conf)

os.environ[_env_prefix + _env_config_file] = str(config_file.resolve())
config = Config(str(config_file))

# Overwrites the config variable
if not config_file.exists() and not create_conf and not copy_conf:
try:
# If no config file is provided or created, try to get config
Expand All @@ -480,23 +496,6 @@ def create(
)
raise typer.Abort()

if copy_conf:
if not os.path.exists(copy_conf):
typer.echo(f'Config file to copy does not exist {copy_conf}.', err=True)
raise typer.Abort()

typer.echo(f"Copying config file from {copy_conf} to {config_file}")
shutil.copyfile(copy_conf, config_file)

if not config_file.exists() and create_conf:
https = 'false' if dev else 'true'
conf = create_config(https=https)
with open(config_file, 'w') as f:
f.write(conf)

os.environ[_env_prefix + _env_config_file] = str(config_file.resolve())
config = Config(str(config_file))

deployment_folder.joinpath('channels').mkdir(exist_ok=True)

with working_directory(db_path):
Expand Down
31 changes: 29 additions & 2 deletions quetz/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright 2020 QuantStack
# Distributed under the terms of the Modified BSD License.

import json
import logging
import logging.config
import os
Expand Down Expand Up @@ -383,6 +384,29 @@ def _find_first_level_config(
return item
return None

def _correct_environ_config_list_value(self, value: str) -> Union[str, List[str]]:
"""Correct a value from environ that should be a list.

Parameters
----------
value : str
The env variable value to correct.

Returns
-------
corrected_value : Union[str, List[str]]
Original value if no correction needed, else the corrected list of
strings value.
"""
corrected_value: Union[str, List[str]] = value
if isinstance(value, str):
if "[" in value:
corrected_value = json.loads(value)
elif "," in value and "[" not in value:
corrected_value = value.split(",")

return corrected_value

def _get_environ_config(self) -> Dict[str, Any]:
"""Looks into environment variables if some matches with config_map.

Expand All @@ -400,6 +424,7 @@ def _get_environ_config(self) -> Dict[str, Any]:
if key.startswith(_env_prefix)
}
for var, value in quetz_var.items():
parsed_value = self._correct_environ_config_list_value(value)
splitted_key = var.split('_')
config_key = splitted_key[1].lower()
idx = 2
Expand All @@ -419,7 +444,7 @@ def _get_environ_config(self) -> Dict[str, Any]:
continue
# the first level is an entry, add it to the config.
if isinstance(first_level, ConfigEntry):
config[first_level.name] = value
config[first_level.name] = parsed_value
# the first level is a section.
elif isinstance(first_level, ConfigSection):
entry = "_".join(splitted_key[idx:]).lower()
Expand All @@ -431,7 +456,9 @@ def _get_environ_config(self) -> Dict[str, Any]:
# add the entry to the config.
if first_level.name not in config:
config[first_level.name]: Dict[str, Any] = {}
config[first_level.name]["_".join(splitted_key[idx:]).lower()] = value
config[first_level.name][
"_".join(splitted_key[idx:]).lower()
] = parsed_value

return config

Expand Down
28 changes: 28 additions & 0 deletions quetz/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,34 @@ def test_config_extend_require(config):
config._config_map.pop()


@pytest.fixture()
def quetz_environ_config(database_url) -> None:
environ_values = {
"QUETZ_SESSION_HTTPS_ONLY": "False",
"QUETZ_LOGGING_LEVEL": "DEBUG",
"QUETZ_USERS_MEMBERS": '["dummy:happyuser"]',
"QUETZ_USERS_ADMINS": '["dummy:happyadmin"]',
"QUETZ_SESSION_SECRET": "test",
"QUETZ_SQLALCHEMY_DATABASE_URL": database_url,
}
for key, value in environ_values.items():
os.environ[key] = value
yield
for key in environ_values.keys():
if key in os.environ:
del os.environ[key]


def test_config_without_input(
quetz_environ_config,
):
c = Config()
assert c.session_https_only is False
assert c.logging_level == "DEBUG"
assert c.users_members == ["dummy:happyuser"]
assert c.users_admins == ["dummy:happyadmin"]


@pytest.mark.parametrize(
"config_extra", ["[extra_plugin]\nsome=\"testvalue\"\nconfig=\"othervalue\"\n"]
)
Expand Down
Loading