set_defaults() assigned os.getenv() results directly, which overwrote the argparse defaults with None whenever a variable was unset. A default container therefore ran "seaf-cli config -k delete_confirm_threshold -v None", and the same for upload_limit and download_limit: none of the three documented defaults were ever applied. SEAFILE_UID and SEAFILE_GID had a second problem. argparse type=int applies only to command line flags, never to set_defaults, so both stayed strings. The check in setup_uid compares them against pwd integers, and a string is never equal to one, so usermod ran on every start. dsc/config.py reads each variable with its documented default and rejects a malformed or negative value at startup, naming the variable, instead of passing it on. An empty variable counts as unset. Booleans are now rendered as true and false rather than Python's True and False. The daemon reads disable_verify_certificate with its boolean accessor, which does not recognise the capitalised form. I could not run the daemon to confirm the previous value's effect, since the AppImage is x86_64 only. seaf-cli is no longer given a None value for any option.
66 lines
1.9 KiB
Python
66 lines
1.9 KiB
Python
"""Typed reading of the container's environment.
|
|
|
|
Everything in the environment is text, including values the client later
|
|
compares as numbers or passes to seaf-cli. Reading them directly leaves two
|
|
failures that are silent until much later: an unset variable becomes None and
|
|
reaches seaf-cli as the string "None", and a numeric setting stays a string
|
|
that never equals the integer it is compared against.
|
|
"""
|
|
|
|
import os
|
|
|
|
from dsc.errors import ConfigError
|
|
|
|
_TRUE_VALUES = {"true", "1", "yes", "on"}
|
|
_FALSE_VALUES = {"false", "0", "no", "off"}
|
|
|
|
|
|
def env_str(name: str, default: str = None) -> str:
|
|
"""Return a text setting. An empty variable counts as unset."""
|
|
value = os.getenv(name)
|
|
if value is None or value == "":
|
|
return default
|
|
return value
|
|
|
|
|
|
def env_int(name: str, default: int, minimum: int = 0) -> int:
|
|
"""
|
|
Return a numeric setting as an int, never as text and never as None.
|
|
|
|
Values below ``minimum`` are rejected: a negative speed limit or deletion
|
|
threshold is not something to pass on to seaf-cli and hope for the best.
|
|
"""
|
|
raw = env_str(name)
|
|
if raw is None:
|
|
return default
|
|
|
|
stripped = raw.strip()
|
|
try:
|
|
value = int(stripped, 10)
|
|
except ValueError:
|
|
raise ConfigError(
|
|
f"{name} must be a whole number, got {stripped!r}"
|
|
)
|
|
|
|
if value < minimum:
|
|
raise ConfigError(
|
|
f"{name} must be {minimum} or greater, got {stripped!r}"
|
|
)
|
|
return value
|
|
|
|
|
|
def env_bool(name: str, default: bool = False) -> bool:
|
|
"""Return a boolean setting, accepting the usual spellings."""
|
|
raw = env_str(name)
|
|
if raw is None:
|
|
return default
|
|
|
|
value = raw.strip().lower()
|
|
if value in _TRUE_VALUES:
|
|
return True
|
|
if value in _FALSE_VALUES:
|
|
return False
|
|
|
|
accepted = ", ".join(sorted(_TRUE_VALUES | _FALSE_VALUES))
|
|
raise ConfigError(f"{name} must be one of {accepted}, got {raw.strip()!r}")
|