Add configuration construction tooling

This commit is contained in:
Ethan Paul 2023-10-16 22:34:23 -04:00
parent 4cf26b861b
commit 891009014d
Signed by: enpaul
GPG Key ID: 07F53B438281D181

105
drone.py
View File

@ -19,7 +19,7 @@ class Config:
repo_init: bool = True repo_init: bool = True
repo_init_append_only: bool = False repo_init_append_only: bool = False
repo_init_quota: Optional[str] = None repo_init_quota: Optional[str] = None
repo_init_encryption: str = "" repo_init_encryption: Optional[str] = None
repo_init_overwrite_key: bool = False repo_init_overwrite_key: bool = False
repo_key_file: pathlib.Path = pathlib.Path("/keys") repo_key_file: pathlib.Path = pathlib.Path("/keys")
repo_passphrase: Optional[str] = None repo_passphrase: Optional[str] = None
@ -42,14 +42,103 @@ class Config:
@classmethod @classmethod
def build(cls): def build(cls):
parameters = {} prefix = "BORGDRONE_"
for conf, field in cls.__dataclass_fields__.items():
env = f"BORGDRONE_{conf.upper()}"
if env in os.environ:
if field.type == bool:
parameters[conf] = cls._parse_bool(env, os.environ[env])
return cls(**parameters) try:
return cls(
repo_path=pathlib.Path(os.environ[f"{prefix}REPO_PATH"]),
archive_path=[
pathlib.Path(item.strip())
for item in os.environ[f"{prefix}ARCHIVE_PATH"].split(",")
if item
],
compact=os.environ.get(f"{prefix}COMPACT", str(cls.compact)).lower()
== "true",
check=os.environ.get(f"{prefix}CHECK", str(cls.check)).lower()
== "true",
repo_init=os.environ.get(
f"{prefix}REPO_INIT", str(cls.repo_init)
).lower()
== "true",
repo_init_append_only=os.environ.get(
f"{prefix}REPO_INIT_APPEND_ONLY", str(cls.repo_init_append_only)
).lower()
== "true",
repo_init_quota=os.environ[f"{prefix}REPO_INIT_QUOTA"]
if f"{prefix}REPO_INIT_QUOTA" in os.environ
else cls.repo_init_quota,
repo_init_encryption=os.environ[f"{prefix}REPO_INIT_ENCRYPTION"]
if f"{prefix}REPO_INIT_ENCRYPTION" in os.environ
else cls.repo_init_encryption,
repo_init_overwrite_key=os.environ.get(
f"{prefix}REPO_INIT_OVERWRITE_KEY", str(cls.repo_init_overwrite_key)
).lower()
== "true",
repo_key_file=pathlib.Path(os.environ[f"{prefix}REPO_KEY_FILE"])
if f"{prefix}REPO_KEY_FILE" in os.environ
else cls.repo_key_file,
repo_passphrase=os.environ[f"{prefix}REPO_PASSPHRASE"]
if f"{prefix}REPO_PASSPHRASE" in os.environ
else cls.repo_passphrase,
repo_passphrase_file=pathlib.Path(
os.environ[f"{prefix}REPO_PASSPHRASE_FILE"]
)
if f"{prefix}REPO_PASSPHRASE_FILE" in os.environ
else cls.repo_passphrase_file,
prune=os.environ.get(f"{prefix}PRUNE", str(cls.prune)).lower()
== "true",
prune_interval=datetime.timedelta(
seconds=int(os.environ[f"{prefix}PRUNE_INTERVAL"])
)
if f"{prefix}PRUNE_INTERVAL" in os.environ
else cls.prune_interval,
prune_secondly=int(
os.environ.get(f"{prefix}PRUNE_SECONDLY", cls.prune_secondly)
),
prune_minutely=int(
os.environ.get(f"{prefix}PRUNE_MINUTELY", cls.prune_minutely)
),
prune_hourly=int(
os.environ.get(f"{prefix}PRUNE_HOURLY", cls.prune_hourly)
),
prune_daily=int(
os.environ.get(f"{prefix}PRUNE_DAILY", cls.prune_daily)
),
prune_weekly=int(
os.environ.get(f"{prefix}PRUNE_WEEKLY", cls.prune_weekly)
),
prune_monthly=int(
os.environ.get(f"{prefix}PRUNE_MONTHLY", cls.prune_monthly)
),
prune_yearly=int(
os.environ.get(f"{prefix}PRUNE_YEARLY", cls.prune_yearly)
),
archive_name=os.environ[f"{prefix}ARCHIVE_NAME"]
if f"{prefix}ARCHIVE_NAME" in os.environ
else cls.archive_name,
archive_comment=os.environ[f"{prefix}ARCHIVE_COMMENT"]
if f"{prefix}ARCHIVE_COMMENT" in os.environ
else cls.archive_comment,
archive_exclude_pattern=os.environ[f"{prefix}ARCHIVE_EXCLUDE_PATTERN"]
if f"{prefix}ARCHIVE_EXCLUDE_PATTERN" in os.environ
else cls.archive_exclude_pattern,
archive_exclude_caches=os.environ.get(
f"{prefix}ARCHIVE_EXCLUDE_CACHES", str(cls.archive_exclude_caches)
).lower()
== "true",
archive_exclude_if_present=os.environ[
f"{prefix}ARCHIVE_EXCLUDE_IF_PRESENT"
]
if f"{prefix}ARCHIVE_EXCLUDE_IF_PRESENT" in os.environ
else cls.archive_exclude_if_present,
archive_keep_exclude_tags=os.environ.get(
f"{prefix}ARCHIVE_KEEP_EXCLUDE_TAGS",
str(cls.archive_keep_exclude_tags),
).lower()
== "true",
)
except KeyError as err:
raise RuntimeError(f"Required configuration parameter {err} not provided")
@staticmethod @staticmethod
def _parse_bool(env: str, value: str) -> bool: def _parse_bool(env: str, value: str) -> bool: