mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-05-20 10:34:30 +00:00
backups: Clarify two separate uses of name create_repository
It is being used to mean initialize the borg repository as well as creating an instance of the repository class object. Use 'initialize' for former and 'get_instance' for latter. Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
parent
6c14e34875
commit
5b01689a92
@ -189,7 +189,7 @@ class BaseBorgRepository(abc.ABC):
|
|||||||
archive_path = self._get_archive_path(archive_name)
|
archive_path = self._get_archive_path(archive_name)
|
||||||
self.run(['delete-archive', '--path', archive_path])
|
self.run(['delete-archive', '--path', archive_path])
|
||||||
|
|
||||||
def create_repository(self, encryption):
|
def initialize(self, encryption):
|
||||||
"""Initialize / create a borg repository."""
|
"""Initialize / create a borg repository."""
|
||||||
if encryption not in SUPPORTED_BORG_ENCRYPTION:
|
if encryption not in SUPPORTED_BORG_ENCRYPTION:
|
||||||
raise ValueError('Unsupported encryption: %s' % encryption)
|
raise ValueError('Unsupported encryption: %s' % encryption)
|
||||||
@ -439,14 +439,14 @@ class SshBorgRepository(BaseBorgRepository):
|
|||||||
|
|
||||||
def get_repositories():
|
def get_repositories():
|
||||||
"""Get all repositories of a given storage type."""
|
"""Get all repositories of a given storage type."""
|
||||||
repositories = [create_repository(ROOT_REPOSITORY_UUID)]
|
repositories = [get_instance(ROOT_REPOSITORY_UUID)]
|
||||||
for uuid in store.get_storages():
|
for uuid in store.get_storages():
|
||||||
repositories.append(create_repository(uuid))
|
repositories.append(get_instance(uuid))
|
||||||
|
|
||||||
return sorted(repositories, key=lambda x: x.sort_order)
|
return sorted(repositories, key=lambda x: x.sort_order)
|
||||||
|
|
||||||
|
|
||||||
def create_repository(uuid):
|
def get_instance(uuid):
|
||||||
"""Create a local or SSH repository object instance."""
|
"""Create a local or SSH repository object instance."""
|
||||||
if uuid == ROOT_REPOSITORY_UUID:
|
if uuid == ROOT_REPOSITORY_UUID:
|
||||||
return RootBorgRepository(path=ROOT_REPOSITORY)
|
return RootBorgRepository(path=ROOT_REPOSITORY)
|
||||||
|
|||||||
@ -89,7 +89,7 @@ def test_create_unencrypted_repository(backup_directory):
|
|||||||
"""Test creating an unencrypted repository."""
|
"""Test creating an unencrypted repository."""
|
||||||
path = backup_directory / 'borgbackup'
|
path = backup_directory / 'borgbackup'
|
||||||
repository = BorgRepository(str(path))
|
repository = BorgRepository(str(path))
|
||||||
repository.create_repository()
|
repository.initialize()
|
||||||
info = repository.get_info()
|
info = repository.get_info()
|
||||||
assert 'encryption' in info
|
assert 'encryption' in info
|
||||||
|
|
||||||
@ -106,7 +106,7 @@ def test_create_export_delete_archive(data_directory, backup_directory):
|
|||||||
path = backup_directory / repo_name
|
path = backup_directory / repo_name
|
||||||
|
|
||||||
repository = BorgRepository(str(path))
|
repository = BorgRepository(str(path))
|
||||||
repository.create_repository()
|
repository.initialize()
|
||||||
archive_path = "::".join([str(path), archive_name])
|
archive_path = "::".join([str(path), archive_name])
|
||||||
actions.superuser_run('backups', [
|
actions.superuser_run('backups', [
|
||||||
'create-archive', '--path', archive_path, '--paths',
|
'create-archive', '--path', archive_path, '--paths',
|
||||||
|
|||||||
@ -42,7 +42,7 @@ from . import (SESSION_PATH_VARIABLE, api, forms, get_known_hosts_path,
|
|||||||
is_ssh_hostkey_verified, split_path)
|
is_ssh_hostkey_verified, split_path)
|
||||||
from .decorators import delete_tmp_backup_file
|
from .decorators import delete_tmp_backup_file
|
||||||
from .errors import BorgRepositoryDoesNotExistError
|
from .errors import BorgRepositoryDoesNotExistError
|
||||||
from .repository import (BorgRepository, SshBorgRepository, create_repository,
|
from .repository import (BorgRepository, SshBorgRepository, get_instance,
|
||||||
get_repositories)
|
get_repositories)
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@ -81,7 +81,7 @@ class CreateArchiveView(SuccessMessageMixin, FormView):
|
|||||||
|
|
||||||
def form_valid(self, form):
|
def form_valid(self, form):
|
||||||
"""Create the archive on valid form submission."""
|
"""Create the archive on valid form submission."""
|
||||||
repository = create_repository(form.cleaned_data['repository'])
|
repository = get_instance(form.cleaned_data['repository'])
|
||||||
if repository.flags.get('mountable'):
|
if repository.flags.get('mountable'):
|
||||||
repository.mount()
|
repository.mount()
|
||||||
|
|
||||||
@ -99,7 +99,7 @@ class DeleteArchiveView(SuccessMessageMixin, TemplateView):
|
|||||||
"""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'] = _('Delete Archive')
|
context['title'] = _('Delete Archive')
|
||||||
repository = create_repository(self.kwargs['uuid'])
|
repository = get_instance(self.kwargs['uuid'])
|
||||||
context['archive'] = repository.get_archive(self.kwargs['name'])
|
context['archive'] = repository.get_archive(self.kwargs['name'])
|
||||||
if context['archive'] is None:
|
if context['archive'] is None:
|
||||||
raise Http404
|
raise Http404
|
||||||
@ -108,7 +108,7 @@ class DeleteArchiveView(SuccessMessageMixin, TemplateView):
|
|||||||
|
|
||||||
def post(self, request, uuid, name):
|
def post(self, request, uuid, name):
|
||||||
"""Delete the archive."""
|
"""Delete the archive."""
|
||||||
repository = create_repository(uuid)
|
repository = get_instance(uuid)
|
||||||
repository.delete_archive(name)
|
repository.delete_archive(name)
|
||||||
messages.success(request, _('Archive deleted.'))
|
messages.success(request, _('Archive deleted.'))
|
||||||
return redirect('backups:index')
|
return redirect('backups:index')
|
||||||
@ -217,12 +217,12 @@ class RestoreArchiveView(BaseRestoreView):
|
|||||||
"""Save some data used to instantiate the form."""
|
"""Save some data used to instantiate the form."""
|
||||||
name = unquote(self.kwargs['name'])
|
name = unquote(self.kwargs['name'])
|
||||||
uuid = self.kwargs['uuid']
|
uuid = self.kwargs['uuid']
|
||||||
repository = create_repository(uuid)
|
repository = get_instance(uuid)
|
||||||
return repository.get_archive_apps(name)
|
return repository.get_archive_apps(name)
|
||||||
|
|
||||||
def form_valid(self, form):
|
def form_valid(self, form):
|
||||||
"""Restore files from the archive on valid form submission."""
|
"""Restore files from the archive on valid form submission."""
|
||||||
repository = create_repository(self.kwargs['uuid'])
|
repository = get_instance(self.kwargs['uuid'])
|
||||||
selected_apps = form.cleaned_data['selected_apps']
|
selected_apps = form.cleaned_data['selected_apps']
|
||||||
repository.restore_archive(self.kwargs['name'], selected_apps)
|
repository.restore_archive(self.kwargs['name'], selected_apps)
|
||||||
return super().form_valid(form)
|
return super().form_valid(form)
|
||||||
@ -232,7 +232,7 @@ class DownloadArchiveView(View):
|
|||||||
"""View to export and download an archive as stream."""
|
"""View to export and download an archive as stream."""
|
||||||
|
|
||||||
def get(self, request, uuid, name):
|
def get(self, request, uuid, name):
|
||||||
repository = create_repository(uuid)
|
repository = get_instance(uuid)
|
||||||
filename = f'{name}.tar.gz'
|
filename = f'{name}.tar.gz'
|
||||||
|
|
||||||
response = StreamingHttpResponse(
|
response = StreamingHttpResponse(
|
||||||
@ -268,7 +268,7 @@ class AddRepositoryView(SuccessMessageMixin, FormView):
|
|||||||
try:
|
try:
|
||||||
repository.get_info()
|
repository.get_info()
|
||||||
except BorgRepositoryDoesNotExistError:
|
except BorgRepositoryDoesNotExistError:
|
||||||
repository.create_repository(encryption)
|
repository.initialize(encryption)
|
||||||
repository.save(store_credentials=True, verified=True)
|
repository.save(store_credentials=True, verified=True)
|
||||||
return super().form_valid(form)
|
return super().form_valid(form)
|
||||||
|
|
||||||
@ -329,7 +329,7 @@ class VerifySshHostkeyView(SuccessMessageMixin, FormView):
|
|||||||
def _get_repository(self):
|
def _get_repository(self):
|
||||||
"""Fetch the repository data from DB only once."""
|
"""Fetch the repository data from DB only once."""
|
||||||
if not self.repository:
|
if not self.repository:
|
||||||
self.repository = create_repository(self.kwargs['uuid'])
|
self.repository = get_instance(self.kwargs['uuid'])
|
||||||
|
|
||||||
return self.repository
|
return self.repository
|
||||||
|
|
||||||
@ -393,7 +393,7 @@ class VerifySshHostkeyView(SuccessMessageMixin, FormView):
|
|||||||
messages.error(self.request, _('Repository removed.'))
|
messages.error(self.request, _('Repository removed.'))
|
||||||
# Remove the repository so that the user can have another go at
|
# Remove the repository so that the user can have another go at
|
||||||
# creating it.
|
# creating it.
|
||||||
create_repository(uuid).remove()
|
get_instance(uuid).remove()
|
||||||
return redirect(reverse_lazy('backups:add-remote-repository'))
|
return redirect(reverse_lazy('backups:add-remote-repository'))
|
||||||
|
|
||||||
|
|
||||||
@ -430,7 +430,7 @@ def _create_remote_repository(repository, encryption, dir_contents):
|
|||||||
if dir_contents:
|
if dir_contents:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
repository.create_repository(encryption)
|
repository.initialize(encryption)
|
||||||
|
|
||||||
return repository
|
return repository
|
||||||
|
|
||||||
@ -456,12 +456,12 @@ class RemoveRepositoryView(SuccessMessageMixin, TemplateView):
|
|||||||
"""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'] = _('Remove Repository')
|
context['title'] = _('Remove Repository')
|
||||||
context['repository'] = create_repository(uuid)
|
context['repository'] = get_instance(uuid)
|
||||||
return context
|
return context
|
||||||
|
|
||||||
def post(self, request, uuid):
|
def post(self, request, uuid):
|
||||||
"""Delete the repository on confirmation."""
|
"""Delete the repository on confirmation."""
|
||||||
repository = create_repository(uuid)
|
repository = get_instance(uuid)
|
||||||
repository.remove()
|
repository.remove()
|
||||||
messages.success(request,
|
messages.success(request,
|
||||||
_('Repository removed. Backups were not deleted.'))
|
_('Repository removed. Backups were not deleted.'))
|
||||||
@ -482,7 +482,7 @@ def umount_repository(request, uuid):
|
|||||||
def mount_repository(request, uuid):
|
def mount_repository(request, uuid):
|
||||||
"""View to mount a remote SSH repository."""
|
"""View to mount a remote SSH repository."""
|
||||||
# Do not mount unverified ssh repositories. Prompt for verification.
|
# Do not mount unverified ssh repositories. Prompt for verification.
|
||||||
if not create_repository(uuid).is_usable():
|
if not get_instance(uuid).is_usable():
|
||||||
return redirect('backups:verify-ssh-hostkey', uuid=uuid)
|
return redirect('backups:verify-ssh-hostkey', uuid=uuid)
|
||||||
|
|
||||||
repository = SshBorgRepository(uuid=uuid)
|
repository = SshBorgRepository(uuid=uuid)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user