From 661a00198e896e2be6b509ba9c6664d6de4bc406 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Mon, 24 Jun 2019 16:46:23 -0700 Subject: [PATCH] backups: Minor styling changes - Add docstrings. - Add blank lines for extra readability. - Use instead of tag for icon in template. Signed-off-by: Sunil Mohan Adapa Reviewed-by: Joseph Nuthalapati --- plinth/modules/backups/__init__.py | 3 +++ plinth/modules/backups/forms.py | 19 +++++++++++++------ plinth/modules/backups/repository.py | 1 + plinth/modules/backups/views.py | 11 ++++++++++- plinth/templates/clients.html | 3 ++- static/themes/default/css/plinth.css | 2 +- 6 files changed, 30 insertions(+), 9 deletions(-) diff --git a/plinth/modules/backups/__init__.py b/plinth/modules/backups/__init__.py index 3c8d48695..d518ddc78 100644 --- a/plinth/modules/backups/__init__.py +++ b/plinth/modules/backups/__init__.py @@ -148,7 +148,9 @@ def restore_from_upload(path, apps=None): def is_ssh_hostkey_verified(hostname): """Check whether SSH Hostkey has already been verified. + hostname: Domain name or IP address of the host + """ known_hosts_path = cfg.known_hosts if not os.path.exists(known_hosts_path): @@ -164,5 +166,6 @@ def split_path(path): Network interface information is kept in the hostname if provided. e.g. fe80::2078:6c26:498a:1fa5%wlp1s0 + """ return re.findall(r'^(.*)@([^/]*):(.*)$', path)[0] diff --git a/plinth/modules/backups/forms.py b/plinth/modules/backups/forms.py index 2a5bba9e5..c89cdc32c 100644 --- a/plinth/modules/backups/forms.py +++ b/plinth/modules/backups/forms.py @@ -59,6 +59,7 @@ def _get_repository_choices(): for storage in storages.values(): if storage['verified']: choices += [(storage['uuid'], storage['path'])] + return choices @@ -99,6 +100,7 @@ class UploadForm(forms.Form): def repository_validator(path): + """Validate an SSH repository path.""" if not ('@' in path and ':' in path): raise ValidationError(_('Repository path format incorrect.')) @@ -125,6 +127,7 @@ def repository_validator(path): class AddRepositoryForm(forms.Form): + """Form to add new SSH remote repository.""" repository = forms.CharField( label=_('SSH Repository Path'), strip=True, help_text=_('Path of a new or existing repository. Example: ' @@ -161,19 +164,23 @@ class AddRepositoryForm(forms.Form): return self.cleaned_data def clean_repository(self): + """Validate repository form field.""" path = self.cleaned_data.get('repository') # Avoid creation of duplicate ssh remotes self._check_if_duplicate_remote(path) return path - def _check_if_duplicate_remote(self, path): - for ns in network_storage.get_storages().values(): - if ns['path'] == path: + @staticmethod + def _check_if_duplicate_remote(path): + """Raise validation error if given path is a stored remote.""" + for storage in network_storage.get_storages().values(): + if storage['path'] == path: raise forms.ValidationError( _('Remote backup repository already exists.')) class VerifySshHostkeyForm(forms.Form): + """Form to verify the SSH public key for a host.""" ssh_public_key = forms.ChoiceField( label=_('Select verified SSH public key'), widget=forms.RadioSelect) @@ -184,9 +191,9 @@ class VerifySshHostkeyForm(forms.Form): self.fields['ssh_public_key'].choices = self._get_all_public_keys( hostname) - def _get_all_public_keys(self, hostname): - """Use ssh-keyscan to get all the SSH public keys of the - given hostname.""" + @staticmethod + def _get_all_public_keys(hostname): + """Use ssh-keyscan to get all the SSH public keys of a host.""" # Fetch public keys of ssh remote res1 = subprocess.run(['ssh-keyscan', hostname], stdout=subprocess.PIPE, diff --git a/plinth/modules/backups/repository.py b/plinth/modules/backups/repository.py index 3f15fb8a4..7f4d8a38b 100644 --- a/plinth/modules/backups/repository.py +++ b/plinth/modules/backups/repository.py @@ -311,6 +311,7 @@ class SshBorgRepository(BorgRepository): """Initialize / create a borg repository.""" if encryption not in SUPPORTED_BORG_ENCRYPTION: raise ValueError('Unsupported encryption: %s' % encryption) + self.run( ['init', '--path', self.repo_path, '--encryption', encryption]) diff --git a/plinth/modules/backups/views.py b/plinth/modules/backups/views.py index 6e3fa31fd..64987cd2c 100644 --- a/plinth/modules/backups/views.py +++ b/plinth/modules/backups/views.py @@ -264,7 +264,7 @@ class DownloadArchiveView(View): class AddRepositoryView(SuccessMessageMixin, FormView): - """View to verify the SSH Hostkey of the server and save repository.""" + """View to create a new remote backup repository.""" form_class = forms.AddRepositoryForm template_name = 'backups_repository_add.html' success_url = reverse_lazy('backups:index') @@ -319,6 +319,7 @@ class VerifySshHostkeyView(SuccessMessageMixin, FormView): if not self.repo_data: uuid = self.kwargs['uuid'] self.repo_data = network_storage.get(uuid) + return self.repo_data def _get_hostname(self): @@ -346,6 +347,7 @@ class VerifySshHostkeyView(SuccessMessageMixin, FormView): known_hosts_file.write('\n') def get(self, *args, **kwargs): + """Skip this view if host is already verified.""" if is_ssh_hostkey_verified(self._get_hostname()): self._add_remote_repository() messages.success(self.request, @@ -363,6 +365,7 @@ class VerifySshHostkeyView(SuccessMessageMixin, FormView): return super().form_valid(form) def _add_remote_repository(self): + """On successful verification of host, add repository.""" repo_data = self._get_repo_data() path = repo_data['path'] credentials = repo_data['credentials'] @@ -476,21 +479,26 @@ class RemoveRepositoryView(SuccessMessageMixin, TemplateView): messages.success( request, _('Repository removed. The remote backup itself was not deleted.')) + return redirect('backups:index') def umount_repository(request, uuid): + """View to unmount a remote SSH repository.""" repository = SshBorgRepository(uuid=uuid) repository.umount() if repository.is_mounted: messages.error(request, _('Unmounting failed!')) + return redirect('backups:index') def mount_repository(request, uuid): + """View to mount a remote SSH repository.""" # Do not mount unverified ssh repositories. Prompt for verification. if not network_storage.get(uuid).get('verified'): return redirect('backups:verify-ssh-hostkey', uuid=uuid) + repository = SshBorgRepository(uuid=uuid) try: repository.mount() @@ -500,4 +508,5 @@ def mount_repository(request, uuid): else: if not repository.is_mounted: messages.error(request, _('Mounting failed')) + return redirect('backups:index') diff --git a/plinth/templates/clients.html b/plinth/templates/clients.html index 4a9a09c8e..003c8bac1 100644 --- a/plinth/templates/clients.html +++ b/plinth/templates/clients.html @@ -25,7 +25,8 @@

diff --git a/static/themes/default/css/plinth.css b/static/themes/default/css/plinth.css index cf213a4d0..d0a9106f5 100644 --- a/static/themes/default/css/plinth.css +++ b/static/themes/default/css/plinth.css @@ -244,7 +244,7 @@ footer { content: "\f054"; } -/* No-JS fallbacks for collapsible content in clients.html */ +/* No-JS fallbacks for collapsible content */ .no-js .collapse { display: block; }