fix(backups): Handle orphaned snapshots

pull/35/head
Inex Code 2023-06-14 02:52:10 +03:00
parent 421c92d12e
commit 93b98cd4fd
4 changed files with 33 additions and 16 deletions

View File

@ -38,7 +38,7 @@ class LocalBackupSecret:
@staticmethod @staticmethod
def exists() -> bool: def exists() -> bool:
return redis.exists(REDIS_KEY) return redis.exists(REDIS_KEY) == 1
@staticmethod @staticmethod
def _generate() -> str: def _generate() -> str:

View File

@ -1,9 +0,0 @@
import datetime
import strawberry
@strawberry.type
class SnapshotInfo:
id: str
service_name: str
created_at: datetime.datetime

View File

@ -3,7 +3,6 @@ import typing
import strawberry import strawberry
import datetime import datetime
from selfprivacy_api.graphql.common_types.dns import DnsRecord from selfprivacy_api.graphql.common_types.dns import DnsRecord
from selfprivacy_api.graphql.common_types.backup_snapshot import SnapshotInfo
from selfprivacy_api.services import get_service_by_id, get_services_by_location from selfprivacy_api.services import get_service_by_id, get_services_by_location
from selfprivacy_api.services import Service as ServiceInterface from selfprivacy_api.services import Service as ServiceInterface
@ -104,14 +103,14 @@ class Service:
return get_storage_usage(self) return get_storage_usage(self)
@strawberry.field @strawberry.field
def backup_snapshots(self) -> typing.Optional[typing.List[SnapshotInfo]]: def backup_snapshots(self) -> typing.Optional[typing.List["SnapshotInfo"]]:
return None return None
@strawberry.type @strawberry.type
class SnapshotInfo: class SnapshotInfo:
id: str id: str
service: "Service" service: Service
created_at: datetime.datetime created_at: datetime.datetime

View File

@ -7,13 +7,20 @@ import strawberry
from selfprivacy_api.backup import Backups from selfprivacy_api.backup import Backups
from selfprivacy_api.backup.local_secret import LocalBackupSecret from selfprivacy_api.backup.local_secret import LocalBackupSecret
from selfprivacy_api.graphql.queries.providers import BackupProvider from selfprivacy_api.graphql.queries.providers import BackupProvider
from selfprivacy_api.graphql.common_types.service import SnapshotInfo from selfprivacy_api.graphql.common_types.service import (
Service,
ServiceStatusEnum,
SnapshotInfo,
service_to_graphql_service,
)
from selfprivacy_api.services import get_service_by_id
@strawberry.type @strawberry.type
class BackupConfiguration: class BackupConfiguration:
provider: BackupProvider provider: BackupProvider
# When server is lost, the app should have the key to decrypt backups on a new server # When server is lost, the app should have the key to decrypt backups
# on a new server
encryption_key: str encryption_key: str
# False when repo is not initialized and not ready to be used # False when repo is not initialized and not ready to be used
is_initialized: bool is_initialized: bool
@ -39,11 +46,31 @@ class Backup:
@strawberry.field @strawberry.field
def all_snapshots(self) -> typing.List[SnapshotInfo]: def all_snapshots(self) -> typing.List[SnapshotInfo]:
if not Backups.is_initted():
return []
result = [] result = []
snapshots = Backups.get_all_snapshots() snapshots = Backups.get_all_snapshots()
for snap in snapshots: for snap in snapshots:
service = get_service_by_id(snap.service_name)
if service is None:
service = Service(
id=snap.service_name,
display_name=f"{snap.service_name} (Orphaned)",
description="",
svg_icon="",
is_movable=False,
is_required=False,
is_enabled=False,
status=ServiceStatusEnum.OFF,
url=None,
dns_records=None,
)
else:
service = service_to_graphql_service(service)
graphql_snap = SnapshotInfo( graphql_snap = SnapshotInfo(
id=snap.id, service=snap.service_name, created_at=snap.created_at id=snap.id,
service=service,
created_at=snap.created_at,
) )
result.append(graphql_snap) result.append(graphql_snap)
return result return result