-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
69361dd
commit 7f96a17
Showing
44 changed files
with
824 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
boto3<2 | ||
deepdiff>=6.2.0,<7 | ||
dimi >=1.3.0,< 2 | ||
django-bootstrap5 >=24.2,<25 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .backend import BackupBackend | ||
from .backupers import Backuper, GitBackuper, S3Backuper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from contextlib import contextmanager | ||
from tempfile import TemporaryDirectory | ||
from typing import TYPE_CHECKING | ||
|
||
|
||
if TYPE_CHECKING: | ||
from validity.models import BackupPoint | ||
from .backupers import Backuper | ||
|
||
|
||
class BackupBackend: | ||
def __init__(self, backupers: dict[str, "Backuper"]): | ||
self.backupers = backupers | ||
|
||
@contextmanager | ||
def _datasource_in_filesytem(self, backup_point: "BackupPoint"): | ||
with TemporaryDirectory() as datasource_dir: | ||
for file in backup_point.data_source.datafiles.all(): | ||
if not backup_point.ignore_file(file.path): | ||
file.write_to_disk(datasource_dir, overwrite=True) | ||
yield datasource_dir | ||
|
||
def __call__(self, backup_point: "BackupPoint") -> None: | ||
backuper = self.backupers[backup_point.method] | ||
with self._datasource_in_filesytem(backup_point) as datasource_dir: | ||
backuper(backup_point.url, backup_point.parameters, datasource_dir) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import shutil | ||
from abc import ABC, abstractmethod | ||
from dataclasses import dataclass | ||
from pathlib import Path | ||
from tempfile import TemporaryDirectory | ||
from typing import Any, ClassVar | ||
|
||
from pydantic import BaseModel | ||
|
||
from validity.integrations.git import GitClient | ||
from validity.integrations.s3 import S3Client | ||
from validity.utils.filesystem import merge_directories | ||
from .entities import RemoteGitRepo | ||
from .parameters import GitParams, S3Params | ||
|
||
|
||
class Backuper(ABC): | ||
parameters_cls: type[BaseModel] | ||
|
||
def __call__(self, url: str, parameters: dict[str, Any], datasource_dir: Path) -> None: | ||
validated_params = self.parameters_cls.model_validate(parameters) | ||
self._do_backup(url, validated_params, datasource_dir) | ||
|
||
@abstractmethod | ||
def _do_backup(self, url: str, parameters: BaseModel, datasource_dir: Path) -> None: ... | ||
|
||
|
||
@dataclass | ||
class GitBackuper(Backuper): | ||
message: str | ||
author_username: str | ||
author_email: str | ||
git_client: GitClient | ||
|
||
parameters_cls: ClassVar[type[BaseModel]] = GitParams | ||
|
||
def _do_backup(self, url: str, parameters: GitParams, datasource_dir: Path) -> None: | ||
with TemporaryDirectory() as repo_dir: | ||
repo = RemoteGitRepo( | ||
local_path=repo_dir, | ||
remote_url=url, | ||
active_branch=parameters.branch, | ||
username=parameters.username, | ||
password=parameters.password, | ||
client=self.git_client, | ||
) | ||
repo.download() | ||
merge_directories(datasource_dir, repo.local_path) | ||
repo.save_changes(self.author_username, self.author_email, message=self.message) | ||
repo.upload() | ||
|
||
|
||
@dataclass | ||
class S3Backuper(Backuper): | ||
s3_client: S3Client | ||
|
||
parameters_cls: ClassVar[type[BaseModel]] = S3Params | ||
|
||
def _backup_archive(self, url: str, parameters: S3Params, datasource_dir: Path) -> None: | ||
with TemporaryDirectory() as backup_dir: | ||
archive = Path(backup_dir) / "a.zip" | ||
shutil.make_archive(archive, "zip", datasource_dir) | ||
self.s3_client.upload_file(archive, url, parameters.aws_access_key_id, parameters.aws_secret_access_key) | ||
|
||
def _backup_dir(self, url: str, parameters: S3Params, datasource_dir: Path) -> None: | ||
self.s3_client.upload_folder( | ||
datasource_dir, url, parameters.aws_access_key_id, parameters.aws_secret_access_key | ||
) | ||
|
||
def _do_backup(self, url: str, parameters: S3Params, datasource_dir: Path): | ||
if parameters.archive: | ||
self._backup_archive(url, parameters, datasource_dir) | ||
else: | ||
self._backup_dir(url, parameters, datasource_dir) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from dataclasses import dataclass | ||
|
||
from validity.integrations.git import GitClient | ||
|
||
|
||
@dataclass(slots=True, kw_only=True) | ||
class RemoteGitRepo: | ||
local_path: str | ||
remote_url: str | ||
active_branch: str | ||
username: str = "" | ||
password: str = "" | ||
client: GitClient | ||
|
||
def save_changes(self, author_username: str, author_email: str, message: str = ""): | ||
self.client.stage_all(self.local_path) | ||
self.client.commit(self.local_path, author_username, author_email, message) | ||
|
||
def download(self): | ||
self.client.clone(self.local_path, self.remote_url, self.active_branch, self.username, self.password, depth=1) | ||
|
||
def upload(self): | ||
self.client.push(self.local_path, self.remote_url, self.active_branch, self.username, self.password) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from pydantic import BaseModel | ||
|
||
|
||
class GitParams(BaseModel): | ||
username: str | ||
password: str | ||
branch: str | None = None | ||
|
||
|
||
class S3Params(BaseModel): | ||
aws_access_key_id: str | ||
aws_secret_access_key: str | ||
archive: bool |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.