def post(self, request): if ('shares' not in request.DATA): e_msg = ('Must provide share names') handle_exception(Exception(e_msg), request) shares = [validate_share(s, request) for s in request.DATA['shares']] editable = 'rw' if ('read_only' in request.DATA and request.DATA['read_only'] is True): editable = 'ro' try: mnt_map = sftp_mount_map(settings.SFTP_MNT_ROOT) input_list = [] for share in shares: if (SFTP.objects.filter(share=share).exists()): e_msg = ('Share(%s) is already exported via SFTP' % share.name) handle_exception(Exception(e_msg), request) if (share.owner == 'root'): e_msg = ('Share(%s) is owned by root. It cannot be ' 'exported via SFTP with root ownership' % share.name) handle_exception(Exception(e_msg), request) for share in shares: sftpo = SFTP(share=share, editable=editable) sftpo.save() # mount if not already mounted helper_mount_share(share) # bindmount if not already sftp_mount(share, settings.MNT_PT, settings.SFTP_MNT_ROOT, mnt_map, editable) sftp_snap_toggle(share) chroot_loc = ('%s%s' % (settings.SFTP_MNT_ROOT, share.owner)) rsync_for_sftp(chroot_loc) input_list.append({ 'user': share.owner, 'dir': chroot_loc, }) for sftpo in SFTP.objects.all(): if (sftpo.share not in shares): input_list.append({ 'user': sftpo.share.owner, 'dir': ('%s%s' % (settings.SFTP_MNT_ROOT, sftpo.share.owner)), }) update_sftp_config(input_list) return Response() except RockStorAPIException: raise except Exception, e: handle_exception(e, request)
def setUpClass(cls): super(SFTPTests, cls).setUpClass() # post mocks cls.patch_is_share_mounted = patch('storageadmin.views.sftp.' 'is_share_mounted') cls.mock_is_share_mounted = cls.patch_is_share_mounted.start() cls.mock_is_share_mounted.return_value = True cls.patch_helper_mount_share = patch('storageadmin.views.sftp.' 'helper_mount_share') cls.mock_helper_mount_share = cls.patch_helper_mount_share.start() cls.mock_helper_mount_share.return_value = True cls.patch_sftp_mount = patch('storageadmin.views.sftp.sftp_mount') cls.mock_sftp_mount = cls.patch_sftp_mount.start() cls.mock_sftp_mount.return_value = True # all values as per fixture cls.temp_pool = Pool(id=10, name='rock-pool', size=5242880) # the following line fails with: "Share matching query does not exist." # cls.temp_share_sftp = Share.objects.get(pk=18) cls.temp_share_sftp = Share(id=18, name='share-sftp', pool=cls.temp_pool, owner='admin', group='admin') cls.temp_share_root_owned = Share(id=19, name='share-root-owned', pool=cls.temp_pool) cls.temp_share_user_owned = Share(id=20, name='share-user-owned', pool=cls.temp_pool, owner='admin', group='admin') cls.temp_sftp = SFTP(id=1, share=cls.temp_share_sftp)
def post(self, request): if ('shares' not in request.DATA): e_msg = ('Must provide share names') handle_exception(Exception(e_msg), request) shares = [validate_share(s, request) for s in request.DATA['shares']] editable = 'rw' if ('read_only' in request.DATA and request.DATA['read_only'] is True): editable = 'ro' try: mnt_map = sftp_mount_map(settings.SFTP_MNT_ROOT) input_list = [] for share in shares: if (SFTP.objects.filter(share=share).exists()): e_msg = ('Share(%s) is already exported via SFTP' % share.name) handle_exception(Exception(e_msg), request) if (share.owner == 'root'): e_msg = ('Share(%s) is owned by root. It cannot be ' 'exported via SFTP with root ownership' % share.name) handle_exception(Exception(e_msg), request) for share in shares: sftpo = SFTP(share=share, editable=editable) sftpo.save() # mount if not already mounted helper_mount_share(share) # bindmount if not already sftp_mount(share, settings.MNT_PT, settings.SFTP_MNT_ROOT, mnt_map, editable) sftp_snap_toggle(share) chroot_loc = ('%s%s' % (settings.SFTP_MNT_ROOT, share.owner)) rsync_for_sftp(chroot_loc) input_list.append({'user': share.owner, 'dir': chroot_loc, }) for sftpo in SFTP.objects.all(): if (sftpo.share not in shares): input_list.append({'user': sftpo.share.owner, 'dir': ('%s%s' % (settings.SFTP_MNT_ROOT, sftpo.share.owner)), }) update_sftp_config(input_list) return Response() except RockStorAPIException: raise except Exception, e: handle_exception(e, request)
def post(self, request): with self._handle_exception(request): if "shares" not in request.data: e_msg = "Must provide share names." handle_exception(Exception(e_msg), request) shares = [ validate_share(s, request) for s in request.data["shares"] ] editable = "rw" if "read_only" in request.data and request.data[ "read_only"] is True: editable = "ro" mnt_map = sftp_mount_map(settings.SFTP_MNT_ROOT) input_map = {} for share in shares: if SFTP.objects.filter(share=share).exists(): e_msg = ( "Share ({}) is already exported via SFTP.").format( share.name) handle_exception(Exception(e_msg), request) if share.owner == "root": e_msg = ("Share ({}) is owned by root. It cannot be " "exported via SFTP with " "root ownership.").format(share.name) handle_exception(Exception(e_msg), request) for share in shares: sftpo = SFTP(share=share, editable=editable) sftpo.save() # mount if not already mounted helper_mount_share(share) # bindmount if not already sftp_mount(share, settings.MNT_PT, settings.SFTP_MNT_ROOT, mnt_map, editable) sftp_snap_toggle(share) chroot_loc = "{}{}".format(settings.SFTP_MNT_ROOT, share.owner) rsync_for_sftp(chroot_loc) input_map[share.owner] = chroot_loc for sftpo in SFTP.objects.all(): if sftpo.share not in shares: input_map[sftpo.share.owner] = "{}{}".format( settings.SFTP_MNT_ROOT, sftpo.share.owner, ) update_sftp_config(input_map) return Response()
def post(self, request): with self._handle_exception(request): if ('shares' not in request.data): e_msg = ('Must provide share names') handle_exception(Exception(e_msg), request) shares = [ validate_share(s, request) for s in request.data['shares'] ] editable = 'rw' if ('read_only' in request.data and request.data['read_only'] is True): editable = 'ro' mnt_map = sftp_mount_map(settings.SFTP_MNT_ROOT) input_map = {} for share in shares: if (SFTP.objects.filter(share=share).exists()): e_msg = ('Share(%s) is already exported via SFTP' % share.name) handle_exception(Exception(e_msg), request) if (share.owner == 'root'): e_msg = ('Share(%s) is owned by root. It cannot be ' 'exported via SFTP with root ownership' % share.name) handle_exception(Exception(e_msg), request) for share in shares: sftpo = SFTP(share=share, editable=editable) sftpo.save() # mount if not already mounted helper_mount_share(share) # bindmount if not already sftp_mount(share, settings.MNT_PT, settings.SFTP_MNT_ROOT, mnt_map, editable) sftp_snap_toggle(share) chroot_loc = ('%s%s' % (settings.SFTP_MNT_ROOT, share.owner)) rsync_for_sftp(chroot_loc) input_map[share.owner] = chroot_loc for sftpo in SFTP.objects.all(): if (sftpo.share not in shares): input_map[sftpo.share.owner] = ( '%s%s' % (settings.SFTP_MNT_ROOT, sftpo.share.owner)) update_sftp_config(input_map) return Response()
def post(self, request): with self._handle_exception(request): if ('shares' not in request.data): e_msg = ('Must provide share names') handle_exception(Exception(e_msg), request) shares = [validate_share(s, request) for s in request.data['shares']] editable = 'rw' if ('read_only' in request.data and request.data['read_only'] is True): editable = 'ro' mnt_map = sftp_mount_map(settings.SFTP_MNT_ROOT) input_map = {} for share in shares: if (SFTP.objects.filter(share=share).exists()): e_msg = ('Share(%s) is already exported via SFTP' % share.name) handle_exception(Exception(e_msg), request) if (share.owner == 'root'): e_msg = ('Share(%s) is owned by root. It cannot be ' 'exported via SFTP with root ownership' % share.name) handle_exception(Exception(e_msg), request) for share in shares: sftpo = SFTP(share=share, editable=editable) sftpo.save() # mount if not already mounted helper_mount_share(share) # bindmount if not already sftp_mount(share, settings.MNT_PT, settings.SFTP_MNT_ROOT, mnt_map, editable) sftp_snap_toggle(share) chroot_loc = ('%s%s' % (settings.SFTP_MNT_ROOT, share.owner)) rsync_for_sftp(chroot_loc) input_map[share.owner] = chroot_loc for sftpo in SFTP.objects.all(): if (sftpo.share not in shares): input_map[sftpo.share.owner] = ( '%s%s' % (settings.SFTP_MNT_ROOT, sftpo.share.owner)) update_sftp_config(input_map) return Response()