mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2025-01-11 18:39:30 +00:00
feature(backups): restore strategies enum
This commit is contained in:
parent
53bb5cc4e2
commit
c74b3df32c
|
@ -8,11 +8,12 @@ from selfprivacy_api.utils import ReadUserData, WriteUserData
|
||||||
from selfprivacy_api.services import get_service_by_id
|
from selfprivacy_api.services import get_service_by_id
|
||||||
from selfprivacy_api.services.service import Service
|
from selfprivacy_api.services.service import Service
|
||||||
|
|
||||||
from selfprivacy_api.jobs import Jobs, JobStatus
|
from selfprivacy_api.jobs import Jobs, JobStatus, Job
|
||||||
|
|
||||||
from selfprivacy_api.graphql.queries.providers import (
|
from selfprivacy_api.graphql.queries.providers import (
|
||||||
BackupProvider as BackupProviderEnum,
|
BackupProvider as BackupProviderEnum,
|
||||||
)
|
)
|
||||||
|
from selfprivacy_api.graphql.common_types.backup import RestoreStrategy
|
||||||
|
|
||||||
from selfprivacy_api.models.backup.snapshot import Snapshot
|
from selfprivacy_api.models.backup.snapshot import Snapshot
|
||||||
|
|
||||||
|
@ -207,42 +208,36 @@ class Backups:
|
||||||
return snapshot
|
return snapshot
|
||||||
|
|
||||||
### Restoring
|
### Restoring
|
||||||
|
@staticmethod
|
||||||
|
def _ensure_active_restore_job(service, snapshot) -> Job:
|
||||||
|
job = get_restore_job(service)
|
||||||
|
if job is None:
|
||||||
|
job = add_restore_job(snapshot)
|
||||||
|
|
||||||
|
Jobs.update(job, status=JobStatus.RUNNING)
|
||||||
|
return job
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def restore_snapshot(snapshot: Snapshot):
|
def restore_snapshot(
|
||||||
|
snapshot: Snapshot, strategy=RestoreStrategy.DOWNLOAD_VERIFY_OVERWRITE
|
||||||
|
):
|
||||||
service = get_service_by_id(snapshot.service_name)
|
service = get_service_by_id(snapshot.service_name)
|
||||||
|
|
||||||
if service is None:
|
if service is None:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"snapshot has a nonexistent service: {snapshot.service_name}"
|
f"snapshot has a nonexistent service: {snapshot.service_name}"
|
||||||
)
|
)
|
||||||
|
|
||||||
job = get_restore_job(service)
|
job = Backups._ensure_active_restore_job(service, snapshot)
|
||||||
if job is None:
|
|
||||||
job = add_restore_job(snapshot)
|
|
||||||
|
|
||||||
Jobs.update(
|
|
||||||
job,
|
|
||||||
status=JobStatus.RUNNING,
|
|
||||||
)
|
|
||||||
try:
|
try:
|
||||||
Backups._assert_restorable(snapshot)
|
Backups._assert_restorable(snapshot)
|
||||||
Backups._restore_service_from_snapshot(
|
Backups._restore_service_from_snapshot(service, snapshot.id)
|
||||||
service,
|
|
||||||
snapshot.id,
|
|
||||||
)
|
|
||||||
service.post_restore()
|
service.post_restore()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
Jobs.update(
|
Jobs.update(job, status=JobStatus.ERROR)
|
||||||
job,
|
|
||||||
status=JobStatus.ERROR,
|
|
||||||
)
|
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
Jobs.update(
|
Jobs.update(job, status=JobStatus.FINISHED)
|
||||||
job,
|
|
||||||
status=JobStatus.FINISHED,
|
|
||||||
)
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _assert_restorable(snapshot: Snapshot):
|
def _assert_restorable(snapshot: Snapshot):
|
||||||
|
|
10
selfprivacy_api/graphql/common_types/backup.py
Normal file
10
selfprivacy_api/graphql/common_types/backup.py
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
"""Backup"""
|
||||||
|
# pylint: disable=too-few-public-methods
|
||||||
|
import strawberry
|
||||||
|
from enum import Enum
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry.enum
|
||||||
|
class RestoreStrategy(Enum):
|
||||||
|
INPLACE = "INPLACE"
|
||||||
|
DOWNLOAD_VERIFY_OVERWRITE = "DOWNLOAD_VERIFY_OVERWRITE"
|
Loading…
Reference in a new issue