diff --git a/src/rockstor/storageadmin/views/__init__.py b/src/rockstor/storageadmin/views/__init__.py index 5a3ec688c..9b87f5812 100644 --- a/src/rockstor/storageadmin/views/__init__.py +++ b/src/rockstor/storageadmin/views/__init__.py @@ -20,8 +20,8 @@ from home import (login_page, login_submit, logout_user, home) from snapshot import SnapshotView from share import (ShareListView, ShareDetailView) +from disk import (DiskMixin, DiskListView, DiskDetailView) from pool import (PoolListView, PoolDetailView) -from disk import (DiskListView, DiskDetailView) from command import CommandView from share_iscsi import ShareIscsiView from appliances import (ApplianceListView, ApplianceDetailView) diff --git a/src/rockstor/storageadmin/views/disk.py b/src/rockstor/storageadmin/views/disk.py index bacc06320..b4e8bdd67 100644 --- a/src/rockstor/storageadmin/views/disk.py +++ b/src/rockstor/storageadmin/views/disk.py @@ -34,16 +34,12 @@ logger = logging.getLogger(__name__) -class DiskListView(rfc.GenericView): +class DiskMixin(object): serializer_class = DiskInfoSerializer - def get_queryset(self, *args, **kwargs): - #do rescan on get. - with self._handle_exception(self.request): - return Disk.objects.all().order_by('name') - + @staticmethod @transaction.atomic - def _scan(self): + def _update_disk_state(): disks = scan_disks(settings.MIN_DISK_SIZE) for d in disks: dob = None @@ -73,7 +69,15 @@ def _scan(self): else: dob.pool = None if (dob.pool is None and d.root is True): - self._create_root_pool(dob) + p = Pool(name=settings.ROOT_POOL, raid='single') + p.disk_set.add(d) + p.save() + d.pool = p + d.save() + p.size = pool_usage(mount_root(p))[0] + enable_quota(p) + p.uuid = btrfs_uuid(d.name) + p.save() dob.save() for do in Disk.objects.all(): if (do.name not in [d.name for d in disks]): @@ -89,22 +93,18 @@ def _scan(self): ds = DiskInfoSerializer(Disk.objects.all().order_by('name'), many=True) return Response(ds.data) - def _create_root_pool(self, d): - p = Pool(name=settings.ROOT_POOL, raid='single') - p.disk_set.add(d) - p.save() - d.pool = p - d.save() - p.size = pool_usage(mount_root(p))[0] - enable_quota(p) - p.uuid = btrfs_uuid(d.name) - p.save() - return p + +class DiskListView(DiskMixin, rfc.GenericView): + serializer_class = DiskInfoSerializer + + def get_queryset(self, *args, **kwargs): + with self._handle_exception(self.request): + return Disk.objects.all().order_by('name') def post(self, request, command, dname=None): with self._handle_exception(request): if (command == 'scan'): - return self._scan() + return self._update_disk_state() e_msg = ('Unsupported command(%s).' % command) handle_exception(Exception(e_msg), request) diff --git a/src/rockstor/storageadmin/views/pool.py b/src/rockstor/storageadmin/views/pool.py index 4e4a1aaf7..831175bc6 100644 --- a/src/rockstor/storageadmin/views/pool.py +++ b/src/rockstor/storageadmin/views/pool.py @@ -27,6 +27,7 @@ from django.db import transaction from storageadmin.serializers import PoolInfoSerializer from storageadmin.models import (Disk, Pool, Share, PoolBalance) +from storageadmin.views import DiskMixin from fs.btrfs import (add_pool, pool_usage, resize_pool, umount_root, btrfs_uuid, mount_root, remount, get_pool_info, pool_raid, start_balance) @@ -277,7 +278,7 @@ def post(self, request): return Response(PoolInfoSerializer(p).data) -class PoolDetailView(PoolMixin, rfc.GenericView): +class PoolDetailView(DiskMixin, PoolMixin, rfc.GenericView): def get(self, *args, **kwargs): try: pool = Pool.objects.get(name=self.kwargs['pname']) @@ -455,4 +456,8 @@ def delete(self, request, pname): pool_path = ('%s%s' % (settings.MNT_PT, pname)) umount_root(pool_path) pool.delete() + try: + self._update_disk_state() + except Exception, e: + logger.error('Exception while updating disk state: %s' % e.__str__()) return Response()