diff --git a/src/rockstor/smart_manager/smd.py b/src/rockstor/smart_manager/smd.py index 9dcb60660..bc148975d 100644 --- a/src/rockstor/smart_manager/smd.py +++ b/src/rockstor/smart_manager/smd.py @@ -60,7 +60,7 @@ def main(): while True: try: set_token() - logger.debug('API token set. Moving on the boostrapping...') + logger.debug('API token set. Moving on to boostrapping...') break except Exception, e: e_msg = ('Exception while setting token: %s' % e.__str__()) diff --git a/src/rockstor/storageadmin/views/command.py b/src/rockstor/storageadmin/views/command.py index f7916adb3..00564477f 100644 --- a/src/rockstor/storageadmin/views/command.py +++ b/src/rockstor/storageadmin/views/command.py @@ -34,14 +34,13 @@ from system.services import (systemctl, join_winbind_domain, ads_join_status) from system.osi import (is_share_mounted, system_shutdown, system_reboot) from storageadmin.models import (Share, Disk, NFSExport, SFTP, Pool, Snapshot) -from smart_manager.models import ShareUsage from nfs_helpers import create_nfs_export_input from storageadmin.util import handle_exception from datetime import datetime from django.utils.timezone import utc from django.conf import settings from django.db import transaction -from share_helpers import sftp_snap_toggle +from share_helpers import (sftp_snap_toggle, import_shares, import_snapshots) from oauth2_provider.ext.rest_framework import OAuth2Authentication from system.pkg_mgmt import (auto_update, current_version, update_check, update_run, auto_update_status) @@ -246,92 +245,10 @@ def post(self, request, command): if (command == 'refresh-share-state'): for p in Pool.objects.all(): - disk = Disk.objects.filter(pool=p)[0].name - shares = [s.name for s in Share.objects.filter(pool=p)] - shares_d = shares_info('%s%s' % (settings.MNT_PT, p.name)) - for s in shares: - if (s not in shares_d): - Share.objects.get(pool=p, name=s).delete() - for s in shares_d: - if (s in shares): - share = Share.objects.get(name=s) - share.qgroup = shares_d[s] - rusage, eusage = share_usage(p, share.qgroup) - ts = datetime.utcnow().replace(tzinfo=utc) - if (rusage != share.rusage or eusage != share.eusage): - share.rusage = rusage - share.eusage = eusage - su = ShareUsage(name=s, r_usage=rusage, e_usage=eusage, - ts=ts) - su.save() - else: - try: - su = ShareUsage.objects.filter(name=s).latest('id') - su.ts = ts - su.count += 1 - except ShareUsage.DoesNotExist: - su = ShareUsage(name=s, r_usage=rusage, - e_usage=eusage, ts=ts) - finally: - su.save() - share.save() - continue - try: - cshare = Share.objects.get(name=s) - cshares_d = shares_info('%s%s' % (settings.MNT_PT, - cshare.pool.name)) - if (s in cshares_d): - e_msg = ('Another pool(%s) has a Share with this same ' - 'name(%s) as this pool(%s). This configuration is not supported.' - ' You can delete one of them manually with this command: ' - 'btrfs subvol delete %s[pool name]/%s' % - (cshare.pool.name, s, p.name, settings.MNT_PT, s)) - handle_exception(Exception(e_msg), self.request) - else: - cshare.pool = p - cshare.qgroup = shares_d[s] - cshare.size = p.size - cshare.subvol_name = s - cshare.rusage, cshare.eusage = share_usage(p, cshare.qgroup) - cshare.save() - except Share.DoesNotExist: - nso = Share(pool=p, qgroup=shares_d[s], name=s, size=p.size, - subvol_name=s) - nso.save() - mount_share(nso, '%s%s' % (settings.MNT_PT, s)) + import_shares(p) return Response() if (command == 'refresh-snapshot-state'): for share in Share.objects.all(): - snaps_d = snaps_info('%s%s' % (settings.MNT_PT, share.pool.name), - share.name) - disk = Disk.objects.filter(pool=share.pool)[0].name - snaps = [s.name for s in Snapshot.objects.filter(share=share)] - for s in snaps: - if (s not in snaps_d): - Snapshot.objects.get(share=share,name=s).delete() - for s in snaps_d: - if (s in snaps): - so = Snapshot.objects.get(share=share, name=s) - else: - so = Snapshot(share=share, name=s, real_name=s, - writable=snaps_d[s][1], qgroup=snaps_d[s][0]) - rusage, eusage = share_usage(share.pool, snaps_d[s][0]) - ts = datetime.utcnow().replace(tzinfo=utc) - if (rusage != so.rusage or eusage != so.eusage): - so.rusage = rusage - so.eusage = eusage - su = ShareUsage(name=s, r_usage=rusage, e_usage=eusage, ts=ts) - su.save() - else: - try: - su = ShareUsage.objects.filter(name=s).latest('id') - su.ts = ts - su.count += 1 - except ShareUsage.DoesNotExist: - su = ShareUsage(name=s, r_usage=rusage, e_usage=eusage, - ts=ts) - finally: - su.save() - so.save() + import_snapshots(share) return Response() diff --git a/src/rockstor/storageadmin/views/disk.py b/src/rockstor/storageadmin/views/disk.py index 6b307bd62..32b70d347 100644 --- a/src/rockstor/storageadmin/views/disk.py +++ b/src/rockstor/storageadmin/views/disk.py @@ -24,12 +24,13 @@ from rest_framework.response import Response from django.db import transaction -from storageadmin.models import (Disk, Pool) +from storageadmin.models import (Disk, Pool, Share) from fs.btrfs import (scan_disks, wipe_disk, blink_disk, enable_quota, btrfs_uuid, pool_usage, mount_root, get_pool_info, pool_raid) from storageadmin.serializers import DiskInfoSerializer from storageadmin.util import handle_exception +from share_helpers import (import_shares, import_snapshots) from django.conf import settings import rest_framework_custom as rfc from system import smart @@ -200,6 +201,9 @@ def _btrfs_disk_import(self, dname, request): po.raid = pool_raid('%s%s' % (settings.MNT_PT, po.name))['data'] po.size = pool_usage('%s%s' % (settings.MNT_PT, po.name))[0] po.save() + import_shares(po) + for share in Share.objects.filter(pool=po): + import_snapshots(share) return Response(DiskInfoSerializer(disk).data) except Exception, e: e_msg = ('Failed to import any pool on this device(%s). Error: %s' diff --git a/src/rockstor/storageadmin/views/share_helpers.py b/src/rockstor/storageadmin/views/share_helpers.py index 4f9f8a99d..8c2141924 100644 --- a/src/rockstor/storageadmin/views/share_helpers.py +++ b/src/rockstor/storageadmin/views/share_helpers.py @@ -16,10 +16,13 @@ along with this program. If not, see . """ +from datetime import datetime +from django.utils.timezone import utc from django.conf import settings from storageadmin.models import (Share, Disk, Snapshot, SFTP) +from smart_manager.models import ShareUsage from fs.btrfs import (mount_share, mount_snap, is_share_mounted, is_mounted, - umount_root) + umount_root, shares_info, share_usage, snaps_info) from storageadmin.util import handle_exception import logging @@ -65,3 +68,93 @@ def toggle_sftp_visibility(share, snap_name, on=True): mount_snap(share, snap_name, pool_device, mnt_pt) else: umount_root(mnt_pt) + + +def import_shares(pool): + disk = Disk.objects.filter(pool=pool)[0].name + shares = [s.name for s in Share.objects.filter(pool=pool)] + shares_d = shares_info('%s%s' % (settings.MNT_PT, pool.name)) + for s in shares: + if (s not in shares_d): + Share.objects.get(pool=pool, name=s).delete() + for s in shares_d: + if (s in shares): + share = Share.objects.get(name=s) + share.qgroup = shares_d[s] + rusage, eusage = share_usage(pool, share.qgroup) + ts = datetime.utcnow().replace(tzinfo=utc) + if (rusage != share.rusage or eusage != share.eusage): + share.rusage = rusage + share.eusage = eusage + su = ShareUsage(name=s, r_usage=rusage, e_usage=eusage, + ts=ts) + su.save() + else: + try: + su = ShareUsage.objects.filter(name=s).latest('id') + su.ts = ts + su.count += 1 + except ShareUsage.DoesNotExist: + su = ShareUsage(name=s, r_usage=rusage, + e_usage=eusage, ts=ts) + finally: + su.save() + share.save() + continue + try: + cshare = Share.objects.get(name=s) + cshares_d = shares_info('%s%s' % (settings.MNT_PT, + cshare.pool.name)) + if (s in cshares_d): + e_msg = ('Another pool(%s) has a Share with this same ' + 'name(%s) as this pool(%s). This configuration is not supported.' + ' You can delete one of them manually with this command: ' + 'btrfs subvol delete %s[pool name]/%s' % + (cshare.pool.name, s, pool.name, settings.MNT_PT, s)) + handle_exception(Exception(e_msg), self.request) + else: + cshare.pool = pool + cshare.qgroup = shares_d[s] + cshare.size = pool.size + cshare.subvol_name = s + cshare.rusage, cshare.eusage = share_usage(pool, cshare.qgroup) + cshare.save() + except Share.DoesNotExist: + nso = Share(pool=pool, qgroup=shares_d[s], name=s, size=pool.size, + subvol_name=s) + nso.save() + mount_share(nso, '%s%s' % (settings.MNT_PT, s)) + + +def import_snapshots(share): + snaps_d = snaps_info('%s%s' % (settings.MNT_PT, share.pool.name), + share.name) + disk = Disk.objects.filter(pool=share.pool)[0].name + snaps = [s.name for s in Snapshot.objects.filter(share=share)] + for s in snaps: + if (s not in snaps_d): + Snapshot.objects.get(share=share,name=s).delete() + for s in snaps_d: + if (s in snaps): + so = Snapshot.objects.get(share=share, name=s) + else: + so = Snapshot(share=share, name=s, real_name=s, + writable=snaps_d[s][1], qgroup=snaps_d[s][0]) + rusage, eusage = share_usage(share.pool, snaps_d[s][0]) + ts = datetime.utcnow().replace(tzinfo=utc) + if (rusage != so.rusage or eusage != so.eusage): + so.rusage = rusage + so.eusage = eusage + su = ShareUsage(name=s, r_usage=rusage, e_usage=eusage, ts=ts) + su.save() + else: + try: + su = ShareUsage.objects.filter(name=s).latest('id') + su.ts = ts + su.count += 1 + except ShareUsage.DoesNotExist: + su = ShareUsage(name=s, r_usage=rusage, e_usage=eusage, + ts=ts) + finally: + su.save() + so.save()