Skip to content

Commit

Permalink
Refactor config file with better class target model names
Browse files Browse the repository at this point in the history
  • Loading branch information
rafsaf committed Jul 21, 2023
1 parent e6171ae commit 0ced1f5
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 110 deletions.
56 changes: 26 additions & 30 deletions backuper/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import re
from enum import StrEnum
from pathlib import Path
from typing import Any, TypeVar
from typing import Any, TypeVar, Self

from croniter import croniter
from pydantic import BaseModel, SecretStr, field_validator, model_validator
Expand Down Expand Up @@ -95,6 +95,8 @@ def logging_config(log_level: str) -> None:

log = logging.getLogger(__name__)

log.info("Start configuring backuper")


class BackupProviderEnum(StrEnum):
LOCAL_FILES = "local"
Expand Down Expand Up @@ -124,7 +126,7 @@ class BackupTargetEnum(StrEnum):
DISCORD_FAIL_WEBHOOK_URL: str = os.environ.get("DISCORD_FAIL_WEBHOOK_URL", "")


class BackupTarget(BaseModel):
class TargetModel(BaseModel):
env_name: str
type: BackupTargetEnum
cron_rule: str
Expand All @@ -148,7 +150,7 @@ def env_name_is_valid(cls, env_name: str) -> str:
return env_name


class PostgreSQLBackupTarget(BackupTarget):
class PostgreSQLTargetModel(TargetModel):
user: str = "postgres"
host: str = "localhost"
port: int = 5432
Expand All @@ -157,7 +159,7 @@ class PostgreSQLBackupTarget(BackupTarget):
type: BackupTargetEnum = BackupTargetEnum.POSTGRESQL


class MySQLBackupTarget(BackupTarget):
class MySQLTargetModel(TargetModel):
user: str = "root"
host: str = "localhost"
port: int = 3306
Expand All @@ -166,7 +168,7 @@ class MySQLBackupTarget(BackupTarget):
type: BackupTargetEnum = BackupTargetEnum.MYSQL


class MariaDBBackupTarget(BackupTarget):
class MariaDBTargetModel(TargetModel):
user: str = "root"
host: str = "localhost"
port: int = 3306
Expand All @@ -175,12 +177,12 @@ class MariaDBBackupTarget(BackupTarget):
type: BackupTargetEnum = BackupTargetEnum.MARIADB


class FileBackupTarget(BackupTarget):
class FileTargetModel(TargetModel):
abs_path: Path
type: BackupTargetEnum = BackupTargetEnum.FILE

@model_validator(mode="after") # type: ignore [arg-type]
def abs_path_is_valid(self) -> "FileBackupTarget":
def abs_path_is_valid(self) -> Self:
if not self.abs_path.is_file() or not self.abs_path.exists():
raise ValueError(
f"Path {self.abs_path} is not a file or does not exist\n "
Expand All @@ -189,12 +191,12 @@ def abs_path_is_valid(self) -> "FileBackupTarget":
return self


class FolderBackupTarget(BackupTarget):
class FolderTargetModel(TargetModel):
abs_path: Path
type: BackupTargetEnum = BackupTargetEnum.FOLDER

@model_validator(mode="after") # type: ignore [arg-type]
def abs_path_is_valid(self) -> "FolderBackupTarget":
def abs_path_is_valid(self) -> Self:
if not self.abs_path.is_dir() or not self.abs_path.exists():
raise ValueError(
f"Path {self.abs_path} is not a dir or does not exist\n "
Expand All @@ -203,10 +205,10 @@ def abs_path_is_valid(self) -> "FolderBackupTarget":
return self


_BT = TypeVar("_BT", bound=BackupTarget)
_BT = TypeVar("_BT", bound=TargetModel)


def _validate_backup_target(env_name: str, env_value: str, target: type[_BT]) -> _BT:
def _validate_target_model(env_name: str, env_value: str, target: type[_BT]) -> _BT:
target_type: str = target.__name__.lower()
log.info("validating %s variable: `%s`", target_type, env_name)
log.debug("%s=%s", target_type, env_value)
Expand All @@ -232,46 +234,41 @@ def _validate_backup_target(env_name: str, env_value: str, target: type[_BT]) ->
return validated_target


_targets_lst = list[
PostgreSQLBackupTarget
| MySQLBackupTarget
| FileBackupTarget
| FolderBackupTarget
| MariaDBBackupTarget
_target_models_lst = list[
PostgreSQLTargetModel
| MySQLTargetModel
| FileTargetModel
| FolderTargetModel
| MariaDBTargetModel
]


def create_backup_targets() -> _targets_lst:
targets: _targets_lst = []
def create_target_models() -> _target_models_lst:
targets: _target_models_lst = []
for env_name, env_value in os.environ.items():
env_name = env_name.lower()
if env_name.startswith(BackupTargetEnum.POSTGRESQL):
targets.append(
_validate_backup_target(env_name, env_value, PostgreSQLBackupTarget)
_validate_target_model(env_name, env_value, PostgreSQLTargetModel)
)
elif env_name.startswith(BackupTargetEnum.MYSQL):
targets.append(
_validate_backup_target(env_name, env_value, MySQLBackupTarget)
_validate_target_model(env_name, env_value, MySQLTargetModel)
)
elif env_name.startswith(BackupTargetEnum.FILE):
targets.append(
_validate_backup_target(env_name, env_value, FileBackupTarget)
)
targets.append(_validate_target_model(env_name, env_value, FileTargetModel))
elif env_name.startswith(BackupTargetEnum.FOLDER):
targets.append(
_validate_backup_target(env_name, env_value, FolderBackupTarget)
_validate_target_model(env_name, env_value, FolderTargetModel)
)
elif env_name.startswith(BackupTargetEnum.MARIADB):
targets.append(
_validate_backup_target(env_name, env_value, MariaDBBackupTarget)
_validate_target_model(env_name, env_value, MariaDBTargetModel)
)

return targets


BACKUP_TARGETS = create_backup_targets()


def runtime_configuration() -> None:
allowed_logs_levels = ["DEBUG", "INFO", "WARNING", "ERROR"]
if LOG_LEVEL not in allowed_logs_levels:
Expand Down Expand Up @@ -299,7 +296,6 @@ def runtime_configuration() -> None:
raise RuntimeError(
f"For provider: `{BACKUP_PROVIDER}` you must use environment variable `GOOGLE_BUCKET_UPLOAD_PATH`"
)
log.error(GOOGLE_BUCKET_UPLOAD_PATH)
if ZIP_ARCHIVE_PASSWORD and not CONST_ZIP_BIN_7ZZ_PATH.exists():
raise RuntimeError(
f"`{ZIP_ARCHIVE_PASSWORD}` is set but `{CONST_ZIP_BIN_7ZZ_PATH}` binary does not exists, did you forget to create it?"
Expand Down
47 changes: 24 additions & 23 deletions backuper/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,57 +43,58 @@ def backup_provider() -> BaseBackupProvider:

def backup_targets() -> list[BaseBackupTarget]:
targets: list[BaseBackupTarget] = []
if not config.BACKUP_TARGETS:
models = config.create_target_models()
if not models:
raise RuntimeError("Found 0 backup targets, at least 1 is required.")
for target in config.BACKUP_TARGETS:
if target.type == config.BackupTargetEnum.POSTGRESQL:
for target_model in models:
if target_model.type == config.BackupTargetEnum.POSTGRESQL:
log.info(
"initializing postgres target, connecting to database: `%s`",
target.env_name,
target_model.env_name,
)
pg_target = PostgreSQL(**target.model_dump())
pg_target = PostgreSQL(**target_model.model_dump())
targets.append(pg_target)
log.info(
"success initializing postgres target db version is %s: `%s`",
pg_target.db_version,
target.env_name,
target_model.env_name,
)
elif target.type == config.BackupTargetEnum.FILE:
log.info("initializing file target: `%s`", target.env_name)
targets.append(File(**target.model_dump()))
log.info("success initializing file target: `%s`", target.env_name)
elif target.type == config.BackupTargetEnum.FOLDER:
log.info("initializing folder target: `%s`", target.env_name)
targets.append(Folder(**target.model_dump()))
log.info("success initializing folder target: `%s`", target.env_name)
elif target.type == config.BackupTargetEnum.MYSQL:
elif target_model.type == config.BackupTargetEnum.FILE:
log.info("initializing file target: `%s`", target_model.env_name)
targets.append(File(**target_model.model_dump()))
log.info("success initializing file target: `%s`", target_model.env_name)
elif target_model.type == config.BackupTargetEnum.FOLDER:
log.info("initializing folder target: `%s`", target_model.env_name)
targets.append(Folder(**target_model.model_dump()))
log.info("success initializing folder target: `%s`", target_model.env_name)
elif target_model.type == config.BackupTargetEnum.MYSQL:
log.info(
"initializing mysql target, connecting to database: `%s`",
target.env_name,
target_model.env_name,
)
mysql_target = MySQL(**target.model_dump())
mysql_target = MySQL(**target_model.model_dump())
targets.append(mysql_target)
log.info(
"success initializing mysql target db version is %s: `%s`",
mysql_target.db_version,
target.env_name,
target_model.env_name,
)
elif target.type == config.BackupTargetEnum.MARIADB:
elif target_model.type == config.BackupTargetEnum.MARIADB:
log.info(
"initializing mariadb target, connecting to database: `%s`",
target.env_name,
target_model.env_name,
)
maria_target = MariaDB(**target.model_dump())
maria_target = MariaDB(**target_model.model_dump())
targets.append(maria_target)
log.info(
"success initializing mariadb target db version is %s: `%s`",
maria_target.db_version,
target.env_name,
target_model.env_name,
)
else: # pragma: no cover
raise RuntimeError(
"panic!!! unsupported backup target",
target.model_dump(),
target_model.model_dump(),
)
return targets

Expand Down
42 changes: 21 additions & 21 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,28 @@
from backuper import config
from backuper.config import (
BackupTargetEnum,
FileBackupTarget,
FolderBackupTarget,
MariaDBBackupTarget,
MySQLBackupTarget,
PostgreSQLBackupTarget,
FileTargetModel,
FolderTargetModel,
MariaDBTargetModel,
MySQLTargetModel,
PostgreSQLTargetModel,
)

DOCKER_TESTS: bool = os.environ.get("DOCKER_TESTS", None) is not None
CONST_TOKEN_URLSAFE = "mock"
FILE_1 = FileBackupTarget(
FILE_1 = FileTargetModel(
env_name="singlefile_1",
cron_rule="* * * * *",
type=BackupTargetEnum.FILE,
abs_path=Path(__file__).absolute().parent / "const/testfile.txt",
)
FOLDER_1 = FolderBackupTarget(
FOLDER_1 = FolderTargetModel(
env_name="directory_1",
cron_rule="* * * * *",
type=BackupTargetEnum.FOLDER,
abs_path=Path(__file__).absolute().parent / "const/testfolder",
)
POSTGRES_15 = PostgreSQLBackupTarget(
POSTGRES_15 = PostgreSQLTargetModel(
env_name="postgresql_db_15",
type=BackupTargetEnum.POSTGRESQL,
cron_rule="* * * * *",
Expand All @@ -41,7 +41,7 @@
db="database-_-12!@#$%^&*()/;><.,]}{[",
user="user-_-12!@#$%^&*()/;><.,]}{[",
)
POSTGRES_14 = PostgreSQLBackupTarget(
POSTGRES_14 = PostgreSQLTargetModel(
env_name="postgresql_db_14",
type=BackupTargetEnum.POSTGRESQL,
cron_rule="* * * * *",
Expand All @@ -51,7 +51,7 @@
db="database-_-12!@#$%^&*()/;><.,]}{[",
user="user-_-12!@#$%^&*()/;><.,]}{[",
)
POSTGRES_13 = PostgreSQLBackupTarget(
POSTGRES_13 = PostgreSQLTargetModel(
env_name="postgresql_db_13",
type=BackupTargetEnum.POSTGRESQL,
cron_rule="* * * * *",
Expand All @@ -61,7 +61,7 @@
db="database-_-12!@#$%^&*()/;><.,]}{[",
user="user-_-12!@#$%^&*()/;><.,]}{[",
)
POSTGRES_12 = PostgreSQLBackupTarget(
POSTGRES_12 = PostgreSQLTargetModel(
env_name="postgresql_db_12",
type=BackupTargetEnum.POSTGRESQL,
cron_rule="* * * * *",
Expand All @@ -71,7 +71,7 @@
db="database-_-12!@#$%^&*()/;><.,]}{[",
user="user-_-12!@#$%^&*()/;><.,]}{[",
)
POSTGRES_11 = PostgreSQLBackupTarget(
POSTGRES_11 = PostgreSQLTargetModel(
env_name="postgresql_db_11",
type=BackupTargetEnum.POSTGRESQL,
cron_rule="* * * * *",
Expand All @@ -81,7 +81,7 @@
db="database-_-12!@#$%^&*()/;><.,]}{[",
user="user-_-12!@#$%^&*()/;><.,]}{[",
)
MYSQL_57 = MySQLBackupTarget(
MYSQL_57 = MySQLTargetModel(
env_name="mysql_db_57",
type=BackupTargetEnum.MYSQL,
cron_rule="* * * * *",
Expand All @@ -91,7 +91,7 @@
db="database-_-12!@#$%^&*()/;><.,]}{[",
user="user-_-12!@#$%^&*()/;><.,]}{[",
)
MYSQL_80 = MySQLBackupTarget(
MYSQL_80 = MySQLTargetModel(
env_name="mysql_db_80",
type=BackupTargetEnum.MYSQL,
cron_rule="* * * * *",
Expand All @@ -101,7 +101,7 @@
db="database-_-12!@#$%^&*()/;><.,]}{[",
user="user-_-12!@#$%^&*()/;><.,]}{[",
)
MARIADB_1011 = MariaDBBackupTarget(
MARIADB_1011 = MariaDBTargetModel(
env_name="mariadb_1011",
type=BackupTargetEnum.MARIADB,
cron_rule="* * * * *",
Expand All @@ -111,7 +111,7 @@
db="database-_-12!@#$%^&*()/;><.,]}{[",
user="user-_-12!@#$%^&*()/;><.,]}{[",
)
MARIADB_1006 = MariaDBBackupTarget(
MARIADB_1006 = MariaDBTargetModel(
env_name="mariadb_1006",
type=BackupTargetEnum.MARIADB,
cron_rule="* * * * *",
Expand All @@ -121,7 +121,7 @@
db="database-_-12!@#$%^&*()/;><.,]}{[",
user="user-_-12!@#$%^&*()/;><.,]}{[",
)
MARIADB_1005 = MariaDBBackupTarget(
MARIADB_1005 = MariaDBTargetModel(
env_name="mariadb_1005",
type=BackupTargetEnum.MARIADB,
cron_rule="* * * * *",
Expand All @@ -131,7 +131,7 @@
db="database-_-12!@#$%^&*()/;><.,]}{[",
user="user-_-12!@#$%^&*()/;><.,]}{[",
)
MARIADB_1004 = MariaDBBackupTarget(
MARIADB_1004 = MariaDBTargetModel(
env_name="mariadb_1004",
type=BackupTargetEnum.MARIADB,
cron_rule="* * * * *",
Expand All @@ -155,18 +155,18 @@
"mariadb_1005": "10.5.19",
"mariadb_1004": "10.4.28",
}
ALL_POSTGRES_DBS_TARGETS: list[PostgreSQLBackupTarget] = [
ALL_POSTGRES_DBS_TARGETS: list[PostgreSQLTargetModel] = [
POSTGRES_11,
POSTGRES_12,
POSTGRES_13,
POSTGRES_14,
POSTGRES_15,
]
ALL_MYSQL_DBS_TARGETS: list[MySQLBackupTarget] = [
ALL_MYSQL_DBS_TARGETS: list[MySQLTargetModel] = [
MYSQL_57,
MYSQL_80,
]
ALL_MARIADB_DBS_TARGETS: list[MariaDBBackupTarget] = [
ALL_MARIADB_DBS_TARGETS: list[MariaDBTargetModel] = [
MARIADB_1011,
MARIADB_1006,
MARIADB_1005,
Expand Down
Loading

0 comments on commit 0ced1f5

Please sign in to comment.