Sunil Mohan Adapa 10b46f1968
storage: Use UDisks information as primary source
Rename get_disks() to get_mounts() and use it in for backups and samba shares.

Create a new get_disks() similar to get_mounts() but use df information only for
showing free space. This inverts the importance of 'df' and UDisks. Use UDisks
as primary source of information for showing list of disks and then use df to
fill in the free space information.

- Retrieve all the mount points of a device and return them as part of
get_disks() in an extra 'mount_points' property.

- For storage listing, this fixes showing up of /.snapshots as separate disk and
showing of vboxsf, network mounts etc. Only shows mounts that are related to
block devices.

- Update various uses of get_disks() within storage module to use
'mounts_points' instead of 'mount_point' to be accurate in cases where there are
multiple mounts for a given device. Use get_mounts() where appropriate instead.

- Display all the mount points against a devices in multiple lines.

- Also show devices that are not currently mounted.

Tests performed:

- Filling up a disk shows a disk space warning properly. Warning contains the
  free disk space correctly.

- Calling get_root_device(get_disks()) return the correct root device.

- In Deluge, the download directory contains a list of all samba current shares.
  If a disk with samba share is unmouted, it does not show up in the list.

- In the Samba app page, all disks are shown properly. Root disk is shown as
  'disk'. All other mount points such as .snapshots and /vagrant also show up.

- In the Samba app page, unavailable shares list shows up when a disk with a
  share is unmounted.

- Upload a backup, warning on the form shows available disk space properly.

- When adding a backup location. The list includes all mount points. Duplicated
  mount points are not shown. Root disk is not shown in the list. When all the
  disks are used up for backup location, a warning that no additional disks are
  available is shown.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2020-06-24 07:23:29 -04:00

97 lines
3.0 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-or-later
"""
Views for samba module.
"""
import logging
import urllib.parse
from collections import defaultdict
from django.contrib import messages
from django.shortcuts import redirect
from django.urls import reverse
from django.utils.translation import ugettext as _
from django.views.decorators.http import require_POST
from plinth import views
from plinth.errors import ActionError
from plinth.modules import samba, storage
logger = logging.getLogger(__name__)
class SambaAppView(views.AppView):
"""Samba sharing basic configuration."""
app_id = 'samba'
template_name = 'samba.html'
def get_context_data(self, *args, **kwargs):
"""Return template context data."""
context = super().get_context_data(*args, **kwargs)
disks = storage.get_mounts()
shares = samba.get_shares()
for disk in disks:
disk['name'] = samba.disk_name(disk['mount_point'])
context['disks'] = disks
shared_mounts = defaultdict(list)
for share in shares:
shared_mounts[share['mount_point']].append(share['share_type'])
context['shared_mounts'] = shared_mounts
context['share_types'] = [('open', _('Open Share')),
('group', _('Group Share')),
('home', _('Home Share'))]
unavailable_shares = []
for share in shares:
for disk in disks:
if share['mount_point'] == disk['mount_point']:
break
else:
unavailable_shares.append(share)
context['unavailable_shares'] = unavailable_shares
context['users'] = samba.get_users()
return context
@require_POST
def share(request, mount_point):
"""Enable sharing, given its root path.
mount_point is urlquoted.
"""
mount_point = urllib.parse.unquote(mount_point)
filesystem = request.POST.get('filesystem_type', '')
share_types = ['open', 'group', 'home']
for share_type in share_types:
action = request.POST.get(share_type + '_share', '')
if action == 'enable':
try:
samba.add_share(mount_point, share_type, filesystem)
messages.success(request, _('Share enabled.'))
except ActionError as exception:
logger.exception('Error enabling share')
messages.error(
request,
_('Error enabling share: {error_message}').format(
error_message=exception))
elif action == 'disable':
try:
samba.delete_share(mount_point, share_type)
messages.success(request, _('Share disabled.'))
except ActionError as exception:
logger.exception('Error disabling share')
messages.error(
request,
_('Error disabling share: {error_message}').format(
error_message=exception))
return redirect(reverse('samba:index'))