2023-05-29 15:34:26 +00:00
|
|
|
import datetime
|
|
|
|
import typing
|
|
|
|
import strawberry
|
|
|
|
from strawberry.types import Info
|
|
|
|
|
|
|
|
from selfprivacy_api.graphql import IsAuthenticated
|
|
|
|
from selfprivacy_api.graphql.mutations.mutation_interface import (
|
|
|
|
GenericMutationReturn,
|
2023-06-21 03:46:56 +00:00
|
|
|
GenericJobMutationReturn,
|
2023-05-29 15:34:26 +00:00
|
|
|
MutationReturnInterface,
|
|
|
|
)
|
|
|
|
from selfprivacy_api.graphql.queries.backup import BackupConfiguration
|
2023-05-29 18:24:38 +00:00
|
|
|
from selfprivacy_api.graphql.queries.backup import Backup
|
2023-05-29 15:34:26 +00:00
|
|
|
from selfprivacy_api.graphql.queries.providers import BackupProvider
|
2023-06-07 14:11:22 +00:00
|
|
|
from selfprivacy_api.graphql.common_types.jobs import job_to_api_job
|
2023-07-07 13:24:05 +00:00
|
|
|
from selfprivacy_api.graphql.common_types.backup import RestoreStrategy
|
2023-05-29 15:34:26 +00:00
|
|
|
|
|
|
|
from selfprivacy_api.backup import Backups
|
2023-05-31 11:30:09 +00:00
|
|
|
from selfprivacy_api.services import get_all_services, get_service_by_id
|
2023-06-01 14:03:26 +00:00
|
|
|
from selfprivacy_api.backup.tasks import start_backup, restore_snapshot
|
2023-06-14 09:52:44 +00:00
|
|
|
from selfprivacy_api.backup.jobs import add_backup_job, add_restore_job
|
2023-05-29 15:34:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
@strawberry.input
|
|
|
|
class InitializeRepositoryInput:
|
|
|
|
"""Initialize repository input"""
|
|
|
|
|
|
|
|
provider: BackupProvider
|
|
|
|
# The following field may become optional for other providers?
|
|
|
|
# Backblaze takes bucket id and name
|
|
|
|
location_id: str
|
|
|
|
location_name: str
|
|
|
|
# Key ID and key for Backblaze
|
|
|
|
login: str
|
|
|
|
password: str
|
|
|
|
|
|
|
|
|
|
|
|
@strawberry.type
|
|
|
|
class GenericBackupConfigReturn(MutationReturnInterface):
|
|
|
|
"""Generic backup config return"""
|
|
|
|
|
|
|
|
configuration: typing.Optional[BackupConfiguration]
|
|
|
|
|
|
|
|
|
|
|
|
@strawberry.type
|
|
|
|
class BackupMutations:
|
|
|
|
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
|
|
|
def initialize_repository(
|
|
|
|
self, repository: InitializeRepositoryInput
|
|
|
|
) -> GenericBackupConfigReturn:
|
|
|
|
"""Initialize a new repository"""
|
2023-06-16 13:43:41 +00:00
|
|
|
Backups.set_provider(
|
2023-06-23 09:40:10 +00:00
|
|
|
kind=repository.provider,
|
2023-05-29 15:34:26 +00:00
|
|
|
login=repository.login,
|
|
|
|
key=repository.password,
|
|
|
|
location=repository.location_name,
|
|
|
|
repo_id=repository.location_id,
|
|
|
|
)
|
|
|
|
Backups.init_repo()
|
2023-06-16 13:43:41 +00:00
|
|
|
return GenericBackupConfigReturn(
|
2023-06-23 09:40:10 +00:00
|
|
|
success=True,
|
|
|
|
message="",
|
2023-06-23 11:36:16 +00:00
|
|
|
code=200,
|
2023-06-23 09:40:10 +00:00
|
|
|
configuration=Backup().configuration(),
|
2023-06-16 13:43:41 +00:00
|
|
|
)
|
2023-05-29 18:24:38 +00:00
|
|
|
|
2023-05-31 11:30:09 +00:00
|
|
|
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
|
|
|
def remove_repository(self) -> GenericBackupConfigReturn:
|
|
|
|
"""Remove repository"""
|
|
|
|
Backups.reset()
|
2023-06-16 14:33:48 +00:00
|
|
|
return GenericBackupConfigReturn(
|
2023-06-23 09:40:10 +00:00
|
|
|
success=True,
|
|
|
|
message="",
|
2023-06-23 11:36:16 +00:00
|
|
|
code=200,
|
2023-06-23 09:40:10 +00:00
|
|
|
configuration=Backup().configuration(),
|
2023-06-16 14:33:48 +00:00
|
|
|
)
|
2023-05-31 11:30:09 +00:00
|
|
|
|
2023-06-05 11:49:37 +00:00
|
|
|
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
|
|
|
def set_autobackup_period(
|
|
|
|
self, period: typing.Optional[int] = None
|
|
|
|
) -> GenericBackupConfigReturn:
|
|
|
|
"""Set autobackup period. None is to disable autobackup"""
|
2023-06-19 13:28:02 +00:00
|
|
|
if period is not None:
|
|
|
|
Backups.set_autobackup_period_minutes(period)
|
|
|
|
else:
|
|
|
|
Backups.set_autobackup_period_minutes(0)
|
|
|
|
|
2023-06-16 14:39:00 +00:00
|
|
|
return GenericBackupConfigReturn(
|
2023-06-23 09:40:10 +00:00
|
|
|
success=True,
|
|
|
|
message="",
|
2023-06-23 11:36:16 +00:00
|
|
|
code=200,
|
2023-06-23 09:40:10 +00:00
|
|
|
configuration=Backup().configuration(),
|
2023-06-16 14:39:00 +00:00
|
|
|
)
|
2023-06-05 11:49:37 +00:00
|
|
|
|
2023-05-31 11:30:09 +00:00
|
|
|
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
2023-06-21 03:46:56 +00:00
|
|
|
def start_backup(self, service_id: str) -> GenericJobMutationReturn:
|
2023-06-07 14:11:22 +00:00
|
|
|
"""Start backup"""
|
|
|
|
|
|
|
|
service = get_service_by_id(service_id)
|
|
|
|
if service is None:
|
2023-06-21 03:46:56 +00:00
|
|
|
return GenericJobMutationReturn(
|
2023-06-07 14:11:22 +00:00
|
|
|
success=False,
|
|
|
|
code=300,
|
|
|
|
message=f"nonexistent service: {service_id}",
|
|
|
|
job=None,
|
|
|
|
)
|
2023-06-14 09:52:44 +00:00
|
|
|
|
|
|
|
job = add_backup_job(service)
|
2023-06-07 14:11:22 +00:00
|
|
|
start_backup(service)
|
|
|
|
|
2023-06-21 03:46:56 +00:00
|
|
|
return GenericJobMutationReturn(
|
2023-06-14 09:52:44 +00:00
|
|
|
success=True,
|
|
|
|
code=200,
|
|
|
|
message="Backup job queued",
|
2023-06-23 09:40:10 +00:00
|
|
|
job=job_to_api_job(job),
|
2023-06-07 14:11:22 +00:00
|
|
|
)
|
2023-06-01 14:03:26 +00:00
|
|
|
|
|
|
|
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
2023-07-07 13:24:05 +00:00
|
|
|
def restore_backup(
|
|
|
|
self,
|
|
|
|
snapshot_id: str,
|
|
|
|
strategy: RestoreStrategy = RestoreStrategy.DOWNLOAD_VERIFY_OVERWRITE,
|
|
|
|
) -> GenericJobMutationReturn:
|
2023-06-01 14:03:26 +00:00
|
|
|
"""Restore backup"""
|
|
|
|
snap = Backups.get_snapshot_by_id(snapshot_id)
|
2023-06-07 14:11:22 +00:00
|
|
|
if snap is None:
|
2023-06-21 03:46:56 +00:00
|
|
|
return GenericJobMutationReturn(
|
2023-06-07 14:11:22 +00:00
|
|
|
success=False,
|
2023-06-23 09:40:10 +00:00
|
|
|
code=404,
|
2023-06-07 14:11:22 +00:00
|
|
|
message=f"No such snapshot: {snapshot_id}",
|
|
|
|
job=None,
|
|
|
|
)
|
|
|
|
|
2023-06-23 09:40:10 +00:00
|
|
|
service = get_service_by_id(snap.service_name)
|
|
|
|
if service is None:
|
|
|
|
return GenericJobMutationReturn(
|
|
|
|
success=False,
|
|
|
|
code=404,
|
|
|
|
message=f"nonexistent service: {snap.service_name}",
|
|
|
|
job=None,
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
job = add_restore_job(snap)
|
|
|
|
except ValueError as e:
|
|
|
|
return GenericJobMutationReturn(
|
|
|
|
success=False,
|
|
|
|
code=400,
|
|
|
|
message=str(e),
|
|
|
|
job=None,
|
|
|
|
)
|
|
|
|
|
2023-07-07 13:24:05 +00:00
|
|
|
restore_snapshot(snap, strategy)
|
2023-06-01 14:03:26 +00:00
|
|
|
|
2023-06-21 03:46:56 +00:00
|
|
|
return GenericJobMutationReturn(
|
2023-06-07 14:11:22 +00:00
|
|
|
success=True,
|
|
|
|
code=200,
|
|
|
|
message="restore job created",
|
2023-06-23 09:40:10 +00:00
|
|
|
job=job_to_api_job(job),
|
2023-06-07 14:11:22 +00:00
|
|
|
)
|
2023-06-01 16:12:32 +00:00
|
|
|
|
|
|
|
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
|
|
|
def force_snapshots_reload(self) -> GenericMutationReturn:
|
|
|
|
"""Force snapshots reload"""
|
2023-06-26 19:20:49 +00:00
|
|
|
Backups.force_snapshot_cache_reload()
|
2023-06-16 14:39:00 +00:00
|
|
|
return GenericMutationReturn(
|
|
|
|
success=True,
|
|
|
|
code=200,
|
|
|
|
message="",
|
|
|
|
)
|