mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-09-19 04:59:01 +00:00
backups: Implement hostname property on SSH repository
Use repository class instances instead of custom ad-hoc data structures such as repo_data. Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
parent
0eb2db3f9a
commit
6c14e34875
@ -32,7 +32,7 @@ from plinth.errors import ActionError
|
|||||||
|
|
||||||
from . import (ROOT_REPOSITORY, ROOT_REPOSITORY_NAME, ROOT_REPOSITORY_UUID,
|
from . import (ROOT_REPOSITORY, ROOT_REPOSITORY_NAME, ROOT_REPOSITORY_UUID,
|
||||||
_backup_handler, api, get_known_hosts_path,
|
_backup_handler, api, get_known_hosts_path,
|
||||||
restore_archive_handler, store)
|
restore_archive_handler, split_path, store)
|
||||||
from .errors import BorgError, BorgRepositoryDoesNotExistError, SshfsError
|
from .errors import BorgError, BorgRepositoryDoesNotExistError, SshfsError
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@ -369,6 +369,12 @@ class SshBorgRepository(BaseBorgRepository):
|
|||||||
"""
|
"""
|
||||||
return self._mountpoint
|
return self._mountpoint
|
||||||
|
|
||||||
|
@property
|
||||||
|
def hostname(self):
|
||||||
|
"""Return hostname from the remote path."""
|
||||||
|
_, hostname, _ = split_path(self._path)
|
||||||
|
return hostname.split('%')[0] # XXX: Likely incorrect to split
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _mountpoint(self):
|
def _mountpoint(self):
|
||||||
"""Return the local mount point where repository is to be mounted."""
|
"""Return the local mount point where repository is to be mounted."""
|
||||||
|
|||||||
@ -311,37 +311,27 @@ class VerifySshHostkeyView(SuccessMessageMixin, FormView):
|
|||||||
form_class = forms.VerifySshHostkeyForm
|
form_class = forms.VerifySshHostkeyForm
|
||||||
template_name = 'verify_ssh_hostkey.html'
|
template_name = 'verify_ssh_hostkey.html'
|
||||||
success_url = reverse_lazy('backups:index')
|
success_url = reverse_lazy('backups:index')
|
||||||
repo_data = {}
|
repository = None
|
||||||
|
|
||||||
def get_form_kwargs(self):
|
def get_form_kwargs(self):
|
||||||
"""Pass additional keyword args for instantiating the form."""
|
"""Pass additional keyword args for instantiating the form."""
|
||||||
kwargs = super().get_form_kwargs()
|
kwargs = super().get_form_kwargs()
|
||||||
hostname = self._get_hostname()
|
kwargs['hostname'] = self._get_repository().hostname
|
||||||
kwargs['hostname'] = hostname
|
|
||||||
return kwargs
|
return kwargs
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
"""Return additional context for rendering the template."""
|
"""Return additional context for rendering the template."""
|
||||||
context = super().get_context_data(**kwargs)
|
context = super().get_context_data(**kwargs)
|
||||||
context['title'] = _('Verify SSH hostkey')
|
context['title'] = _('Verify SSH hostkey')
|
||||||
context['hostname'] = self._get_hostname()
|
context['hostname'] = self._get_repository().hostname
|
||||||
return context
|
return context
|
||||||
|
|
||||||
def _get_repo_data(self):
|
def _get_repository(self):
|
||||||
"""Fetch the repository data from DB only once."""
|
"""Fetch the repository data from DB only once."""
|
||||||
if not self.repo_data:
|
if not self.repository:
|
||||||
uuid = self.kwargs['uuid']
|
self.repository = create_repository(self.kwargs['uuid'])
|
||||||
self.repo_data = store.get(uuid)
|
|
||||||
|
|
||||||
return self.repo_data
|
return self.repository
|
||||||
|
|
||||||
def _get_hostname(self):
|
|
||||||
"""Get the hostname of the repository.
|
|
||||||
|
|
||||||
Network interface information is stripped out.
|
|
||||||
"""
|
|
||||||
_, hostname, _ = split_path(self._get_repo_data()['path'])
|
|
||||||
return hostname.split('%')[0] # XXX: Likely incorrect to split
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _add_ssh_hostkey(ssh_public_key):
|
def _add_ssh_hostkey(ssh_public_key):
|
||||||
@ -355,7 +345,7 @@ class VerifySshHostkeyView(SuccessMessageMixin, FormView):
|
|||||||
|
|
||||||
def get(self, *args, **kwargs):
|
def get(self, *args, **kwargs):
|
||||||
"""Skip this view if host is already verified."""
|
"""Skip this view if host is already verified."""
|
||||||
if is_ssh_hostkey_verified(self._get_hostname()):
|
if is_ssh_hostkey_verified(self._get_repository().hostname):
|
||||||
messages.success(self.request, _('SSH host already verified.'))
|
messages.success(self.request, _('SSH host already verified.'))
|
||||||
return self._add_remote_repository()
|
return self._add_remote_repository()
|
||||||
|
|
||||||
@ -370,19 +360,16 @@ class VerifySshHostkeyView(SuccessMessageMixin, FormView):
|
|||||||
|
|
||||||
def _add_remote_repository(self):
|
def _add_remote_repository(self):
|
||||||
"""On successful verification of host, add repository."""
|
"""On successful verification of host, add repository."""
|
||||||
repo_data = self._get_repo_data()
|
repository = self._get_repository()
|
||||||
path = repo_data['path']
|
|
||||||
credentials = repo_data['credentials']
|
|
||||||
uuid = self.kwargs['uuid']
|
uuid = self.kwargs['uuid']
|
||||||
encryption = 'none'
|
encryption = 'none'
|
||||||
if 'encryption_passphrase' in credentials and \
|
if 'encryption_passphrase' in repository.credentials and \
|
||||||
credentials['encryption_passphrase']:
|
repository.credentials['encryption_passphrase']:
|
||||||
encryption = 'repokey'
|
encryption = 'repokey'
|
||||||
|
|
||||||
try:
|
try:
|
||||||
dir_contents = _list_remote_directory(path, credentials)
|
dir_contents = _list_remote_directory(repository.path,
|
||||||
repository = SshBorgRepository(uuid=uuid, path=path,
|
repository.credentials)
|
||||||
credentials=credentials)
|
|
||||||
repository.mount()
|
repository.mount()
|
||||||
repository = _create_remote_repository(repository, encryption,
|
repository = _create_remote_repository(repository, encryption,
|
||||||
dir_contents)
|
dir_contents)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user