mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-05-20 10:34:30 +00:00
backups: repository: Simplify handling of remote repo properties
- Simplify save() method such that a simple load(uuid).save() does not destroy data. Currently, the verified parameter is lost. - Drop unused store_credentials argument to save(). - Make verified a property of the SSH repository and instantiate with it properly. Tests performed: - Adding a new remote repository works with SSH verification from unknown and known hosts. - Mounting and unmounting works. Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
parent
1170e438a3
commit
8c161431ba
@ -80,11 +80,7 @@ class BaseBorgRepository(abc.ABC):
|
|||||||
known_credentials = []
|
known_credentials = []
|
||||||
|
|
||||||
def __init__(self, path, credentials=None, uuid=None, **kwargs):
|
def __init__(self, path, credentials=None, uuid=None, **kwargs):
|
||||||
"""Instantiate a new repository.
|
"""Instantiate a new repository."""
|
||||||
|
|
||||||
If only a uuid is given, load the values from kvstore.
|
|
||||||
|
|
||||||
"""
|
|
||||||
self._path = path
|
self._path = path
|
||||||
self.credentials = credentials or {}
|
self.credentials = credentials or {}
|
||||||
self.uuid = uuid or str(uuid1())
|
self.uuid = uuid or str(uuid1())
|
||||||
@ -295,28 +291,22 @@ class BaseBorgRepository(abc.ABC):
|
|||||||
create_subvolume=False, backup_file=archive_path,
|
create_subvolume=False, backup_file=archive_path,
|
||||||
encryption_passphrase=passphrase)
|
encryption_passphrase=passphrase)
|
||||||
|
|
||||||
def _get_storage_format(self, store_credentials, verified):
|
def _get_storage_format(self):
|
||||||
|
"""Return a dict representing the repository."""
|
||||||
storage = {
|
storage = {
|
||||||
'path': self._path,
|
'path': self._path,
|
||||||
'storage_type': self.storage_type,
|
'storage_type': self.storage_type,
|
||||||
'added_by_module': 'backups',
|
'added_by_module': 'backups',
|
||||||
'verified': verified
|
'credentials': self.credentials,
|
||||||
}
|
}
|
||||||
if self.uuid:
|
if self.uuid:
|
||||||
storage['uuid'] = self.uuid
|
storage['uuid'] = self.uuid
|
||||||
|
|
||||||
if store_credentials:
|
|
||||||
storage['credentials'] = self.credentials
|
|
||||||
|
|
||||||
return storage
|
return storage
|
||||||
|
|
||||||
def save(self, store_credentials=True, verified=False):
|
def save(self):
|
||||||
"""Save the repository in store (kvstore).
|
"""Save the repository in store (kvstore)."""
|
||||||
|
storage = self._get_storage_format()
|
||||||
- store_credentials: Boolean whether credentials should be stored.
|
|
||||||
|
|
||||||
"""
|
|
||||||
storage = self._get_storage_format(store_credentials, verified)
|
|
||||||
self.uuid = store.update_or_add(storage)
|
self.uuid = store.update_or_add(storage)
|
||||||
|
|
||||||
|
|
||||||
@ -364,9 +354,21 @@ class SshBorgRepository(BaseBorgRepository):
|
|||||||
sort_order = 30
|
sort_order = 30
|
||||||
flags = {'removable': True, 'mountable': True}
|
flags = {'removable': True, 'mountable': True}
|
||||||
|
|
||||||
|
def __init__(self, path, credentials=None, uuid=None, schedule=None,
|
||||||
|
verified=None, **kwargs):
|
||||||
|
"""Instantiate a new repository."""
|
||||||
|
super().__init__(path, credentials, uuid, schedule, **kwargs)
|
||||||
|
self.verified = verified or False
|
||||||
|
|
||||||
|
def _get_storage_format(self):
|
||||||
|
"""Return a dict representing the repository."""
|
||||||
|
storage = super()._get_storage_format()
|
||||||
|
storage['verified'] = self.verified
|
||||||
|
return storage
|
||||||
|
|
||||||
def is_usable(self):
|
def is_usable(self):
|
||||||
"""Return whether repository is usable."""
|
"""Return whether repository is usable."""
|
||||||
return self.kwargs.get('verified')
|
return self.verified
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def borg_path(self):
|
def borg_path(self):
|
||||||
|
|||||||
@ -289,7 +289,8 @@ class AddRemoteRepositoryView(SuccessMessageMixin, FormView):
|
|||||||
'encryption_passphrase': encryption_passphrase
|
'encryption_passphrase': encryption_passphrase
|
||||||
}
|
}
|
||||||
repository = SshBorgRepository(path, credentials)
|
repository = SshBorgRepository(path, credentials)
|
||||||
repository.save(verified=False)
|
repository.verfied = False
|
||||||
|
repository.save()
|
||||||
messages.success(self.request, _('Added new remote SSH repository.'))
|
messages.success(self.request, _('Added new remote SSH repository.'))
|
||||||
|
|
||||||
url = reverse('backups:verify-ssh-hostkey', args=[repository.uuid])
|
url = reverse('backups:verify-ssh-hostkey', args=[repository.uuid])
|
||||||
@ -359,7 +360,8 @@ def _save_repository(request, repository):
|
|||||||
"""Initialize and save a repository. Convert errors to messages."""
|
"""Initialize and save a repository. Convert errors to messages."""
|
||||||
try:
|
try:
|
||||||
repository.initialize()
|
repository.initialize()
|
||||||
repository.save(verified=True)
|
repository.verified = True
|
||||||
|
repository.save()
|
||||||
return True
|
return True
|
||||||
except paramiko.BadHostKeyException:
|
except paramiko.BadHostKeyException:
|
||||||
message = _('SSH host public key could not be verified.')
|
message = _('SSH host public key could not be verified.')
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user