Beispiel #1
0
    def post(self, request):
        if ('shares' not in request.data):
            e_msg = ('Must provide share names')
            handle_exception(Exception(e_msg), request)
        shares = [
            self._validate_share(request, s) for s in request.data['shares']
        ]
        options = self._validate_input(request)
        custom_config = options['custom_config']
        del (options['custom_config'])
        for share in shares:
            if (SambaShare.objects.filter(share=share).exists()):
                e_msg = ('Share(%s) is already exported via Samba' %
                         share.name)
                handle_exception(Exception(e_msg), request)
        with self._handle_exception(request):
            for share in shares:
                mnt_pt = ('%s%s' % (settings.MNT_PT, share.name))
                options['share'] = share
                options['path'] = mnt_pt
                smb_share = SambaShare(**options)
                smb_share.save()
                for cc in custom_config:
                    cco = SambaCustomConfig(smb_share=smb_share,
                                            custom_config=cc)
                    cco.save()
                if (not is_share_mounted(share.name)):
                    mount_share(share, mnt_pt)

                admin_users = request.data.get('admin_users', [])
                if (admin_users is None): admin_users = []
                self._set_admin_users(admin_users, smb_share)
            refresh_smb_config(list(SambaShare.objects.all()))
            self._restart_samba()
            return Response(SambaShareSerializer(smb_share).data)
Beispiel #2
0
    def post(self, request):
        if ('shares' not in request.data):
            e_msg = ('Must provide share names')
            handle_exception(Exception(e_msg), request)
        shares = [self._validate_share(request, s) for s in request.data['shares']]
        options = self._validate_input(request)
        custom_config = options['custom_config']
        del(options['custom_config'])
        for share in shares:
            if (SambaShare.objects.filter(share=share).exists()):
                e_msg = ('Share(%s) is already exported via Samba' %
                         share.name)
                handle_exception(Exception(e_msg), request)
        with self._handle_exception(request):
            for share in shares:
                mnt_pt = ('%s%s' % (settings.MNT_PT, share.name))
                options['share'] = share
                options['path'] = mnt_pt
                smb_share = SambaShare(**options)
                smb_share.save()
                for cc in custom_config:
                    cco = SambaCustomConfig(smb_share=smb_share,
                                            custom_config=cc)
                    cco.save()
                if (not is_share_mounted(share.name)):
                    mount_share(share, mnt_pt)

                admin_users = request.data.get('admin_users', [])
                if (admin_users is None): admin_users = []
                self._set_admin_users(admin_users, smb_share)
            refresh_smb_config(list(SambaShare.objects.all()))
            self._restart_samba()
            return Response(SambaShareSerializer(smb_share).data)
    def post(self, request, sname):
        with self._handle_exception(request):
            share = Share.objects.get(name=sname)
            try:
                samba_o = SambaShare.objects.get(share=share)
                samba_serializer = SambaShareSerializer(samba_o)
                return Response(samba_serializer.data)
            except:
                options = {
                    'comment': ('samba for %s' % sname),
                    'browsable': 'yes',
                    'guest_ok': 'no',
                    'read_only': 'no',
                    'create_mask': '0755',
                    }
                if ('comment' in request.DATA):
                    options['comment'] = request.DATA['comment']
                if ('browsable' in request.DATA):
                    if (request.DATA['browsable'] != 'yes' and
                        request.DATA['browsable'] != 'no'):
                        e_msg = ('Invalid choice for browsable. Possible '
                                 'choices are yes or no.')
                        handle_exception(Exception(e_msg), request)
                    options['browsable'] = request.DATA['browsable']
                if ('guest_ok' in request.DATA):
                    if (request.DATA['guest_ok'] != 'yes' and
                        request.DATA['guest_ok'] != 'no'):
                        e_msg = ('Invalid choice for guest_ok. Possible '
                                 'options are yes or no.')
                        handle_exception(Exception(e_msg), request)
                    options['guest_ok'] = request.DATA['guest_ok']
                if ('read_only' in request.DATA):
                    if (request.DATA['read_only'] != 'yes' and
                        request.DATA['read_only'] != 'no'):
                        e_msg = ('Invalid choice for read_only. Possible '
                                 'options are yes or no.')
                        handle_exception(Exception(e_msg), request)
                    options['read_only'] = request.DATA['read_only']
                if ('create_mask' in request.DATA):
                    if (request.DATA['create_mask'] not in self.CREATE_MASKS):
                        e_msg = ('Invalid choice for create_mask. Possible '
                                 'options are: %s' % self.CREATE_MASKS)
                        handle_exception(Exception(e_msg), request)

            mnt_pt = ('%s%s' % (settings.MNT_PT, share.name))
            smb_share = SambaShare(share=share, path=mnt_pt,
                                   comment=options['comment'],
                                   browsable=options['browsable'],
                                   read_only=options['read_only'],
                                   guest_ok=options['guest_ok'],
                                   create_mask=options['create_mask'])
            smb_share.save()
            if (not is_share_mounted(share.name)):
                pool_device = Disk.objects.filter(pool=share.pool)[0].name
                mount_share(share.subvol_name, pool_device, mnt_pt)
            refresh_smb_config(list(SambaShare.objects.all()))
            restart_samba()
            samba_serializer = SambaShareSerializer(smb_share)
            return Response(samba_serializer.data)
Beispiel #4
0
 def post(self, request):
     if isinstance(request.data, list):
         for se in request.data:
             smb_share = self.create_samba_share(se)
     else:
         smb_share = self.create_samba_share(request.data)
     refresh_smb_config(list(SambaShare.objects.all()))
     self._restart_samba()
     return Response(SambaShareSerializer(smb_share).data)
 def delete(self, request, sname):
     with self._handle_exception(request):
         share = Share.objects.get(name=sname)
         if (not SambaShare.objects.filter(share=share).exists()):
             e_msg = ('Share is not exported via Samba. Nothing to delete')
             handle_exception(Exception(e_msg), request)
         samba_share = SambaShare.objects.get(share=share)
         samba_share.delete()
         refresh_smb_config(list(SambaShare.objects.all()))
         restart_samba()
         return Response()
Beispiel #6
0
    def delete(self, request, smb_id):
        try:
            smbo = SambaShare.objects.get(id=smb_id)
            SambaCustomConfig.objects.filter(smb_share=smbo).delete()
            smbo.delete()
        except:
            e_msg = ('Samba export for the id(%s) does not exist' % smb_id)
            handle_exception(Exception(e_msg), request)

        with self._handle_exception(request):
            refresh_smb_config(list(SambaShare.objects.all()))
            self._restart_samba()
            return Response()
Beispiel #7
0
    def delete(self, request, smb_id):
        try:
            smbo = SambaShare.objects.get(id=smb_id)
            SambaCustomConfig.objects.filter(smb_share=smbo).delete()
            smbo.delete()
        except:
            e_msg = ('Samba export for the id(%s) does not exist' % smb_id)
            handle_exception(Exception(e_msg), request)

        with self._handle_exception(request):
            refresh_smb_config(list(SambaShare.objects.all()))
            self._restart_samba()
            return Response()
Beispiel #8
0
    def put(self, request, smb_id):
        with self._handle_exception(request):
            try:
                smbo = SambaShare.objects.get(id=smb_id)
            except:
                e_msg = ('Samba export for the id(%s) does not exist' % smb_id)
                handle_exception(Exception(e_msg), request)

            options = self._validate_input(request)
            custom_config = options['custom_config']
            del(options['custom_config'])
            smbo.__dict__.update(**options)
            admin_users = request.data.get('admin_users', None)
            if (admin_users is None):
                admin_users = []
            for uo in User.objects.filter(smb_shares=smbo):
                if (uo.username not in admin_users):
                    uo.smb_shares.remove(smbo)
            for u in admin_users:
                if (not User.objects.filter(username=u,
                                            smb_shares=smbo).exists()):
                    auo = User.objects.get(username=u)
                    auo.smb_shares.add(smbo)
            smbo.save()
            for cco in SambaCustomConfig.objects.filter(smb_share=smbo):
                if (cco.custom_config not in custom_config):
                    cco.delete()
                else:
                    custom_config.remove(cco.custom_config)
            for cc in custom_config:
                cco = SambaCustomConfig(smb_share=smbo, custom_config=cc)
                cco.save()
            for smb_o in SambaShare.objects.all():
                if (not is_share_mounted(smb_o.share.name)):
                    pool_device = Disk.objects.filter(
                        pool=smb_o.share.pool)[0].name
                    mnt_pt = ('%s%s' % (settings.MNT_PT, smb_o.share.name))
                    try:
                        mount_share(smb_o.share, pool_device, mnt_pt)
                    except Exception, e:
                        logger.exception(e)
                        if (smb_o.id == smbo.id):
                            e_msg = ('Failed to mount share(%s) due to a low '
                                     'level error.' % smb_o.share.name)
                            handle_exception(Exception(e_msg), request)

            refresh_smb_config(list(SambaShare.objects.all()))
            self._restart_samba()
            return Response(SambaShareSerializer(smbo).data)
Beispiel #9
0
    def put(self, request, smb_id):
        with self._handle_exception(request):
            try:
                smbo = SambaShare.objects.get(id=smb_id)
            except:
                e_msg = ('Samba export for the id(%s) does not exist' % smb_id)
                handle_exception(Exception(e_msg), request)

            options = self._validate_input(request, smbo=smbo)
            custom_config = options['custom_config']
            del (options['custom_config'])
            smbo.__dict__.update(**options)
            admin_users = request.data.get('admin_users', None)
            if (admin_users is None):
                admin_users = []
            for uo in User.objects.filter(smb_shares=smbo):
                if (uo.username not in admin_users):
                    uo.smb_shares.remove(smbo)
            for u in admin_users:
                if (not User.objects.filter(username=u,
                                            smb_shares=smbo).exists()):
                    auo = User.objects.get(username=u)
                    auo.smb_shares.add(smbo)
            smbo.save()
            for cco in SambaCustomConfig.objects.filter(smb_share=smbo):
                if (cco.custom_config not in custom_config):
                    cco.delete()
                else:
                    custom_config.remove(cco.custom_config)
            for cc in custom_config:
                cco = SambaCustomConfig(smb_share=smbo, custom_config=cc)
                cco.save()
            for smb_o in SambaShare.objects.all():
                if (not is_share_mounted(smb_o.share.name)):
                    pool_device = Disk.objects.filter(
                        pool=smb_o.share.pool)[0].name
                    mnt_pt = ('%s%s' % (settings.MNT_PT, smb_o.share.name))
                    try:
                        mount_share(smb_o.share, pool_device, mnt_pt)
                    except Exception, e:
                        logger.exception(e)
                        if (smb_o.id == smbo.id):
                            e_msg = ('Failed to mount share(%s) due to a low '
                                     'level error.' % smb_o.share.name)
                            handle_exception(Exception(e_msg), request)

            refresh_smb_config(list(SambaShare.objects.all()))
            self._restart_samba()
            return Response(SambaShareSerializer(smbo).data)
Beispiel #10
0
    def put(self, request, smb_id):
        with self._handle_exception(request):
            try:
                smbo = SambaShare.objects.get(id=smb_id)
            except:
                e_msg = ("Samba export for the id ({}) does not exist."
                         ).format(smb_id)
                handle_exception(Exception(e_msg), request)

            options = self._validate_input(request.data, smbo=smbo)
            custom_config = options["custom_config"]
            del options["custom_config"]
            smbo.__dict__.update(**options)
            admin_users = request.data.get("admin_users", [])
            if admin_users is None:
                admin_users = []
            for uo in User.objects.filter(smb_shares=smbo):
                if uo.username not in admin_users:
                    uo.smb_shares.remove(smbo)
            self._set_admin_users(admin_users, smbo)
            smbo.save()
            for cco in SambaCustomConfig.objects.filter(smb_share=smbo):
                if cco.custom_config not in custom_config:
                    cco.delete()
                else:
                    custom_config.remove(cco.custom_config)
            for cc in custom_config:
                cco = SambaCustomConfig(smb_share=smbo, custom_config=cc)
                cco.save()
            for smb_o in SambaShare.objects.all():
                if not smb_o.share.is_mounted:
                    mnt_pt = "%s%s" % (settings.MNT_PT, smb_o.share.name)
                    try:
                        mount_share(smb_o.share, mnt_pt)
                    except Exception as e:
                        logger.exception(e)
                        if smb_o.id == smbo.id:
                            e_msg = ("Failed to mount share ({}) due to a low "
                                     "level error.").format(smb_o.share.name)
                            handle_exception(Exception(e_msg), request)

            refresh_smb_config(list(SambaShare.objects.all()))
            refresh_smb_discovery(list(SambaShare.objects.all()))
            self._restart_samba()
            return Response(SambaShareSerializer(smbo).data)
Beispiel #11
0
    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']]
        options = self._validate_input(request)
        custom_config = options['custom_config']
        del(options['custom_config'])
        for share in shares:
            if (SambaShare.objects.filter(share=share).exists()):
                e_msg = ('Share(%s) is already exported via Samba' %
                         share.name)
                handle_exception(Exception(e_msg), request)
        with self._handle_exception(request):
            for share in shares:
                mnt_pt = ('%s%s' % (settings.MNT_PT, share.name))
                options['share'] = share
                options['path'] = mnt_pt
                smb_share = SambaShare(**options)
                smb_share.save()
                for cc in custom_config:
                    cco = SambaCustomConfig(smb_share=smb_share,
                                            custom_config=cc)
                    cco.save()
                if (not is_share_mounted(share.name)):
                    pool_device = Disk.objects.filter(pool=share.pool)[0].name
                    mount_share(share, pool_device, mnt_pt)

                admin_users = request.DATA.get('admin_users', None)
                if (admin_users is None):
                    admin_users = []
                for au in admin_users:
                    auo = User.objects.get(username=au)
                    auo.smb_shares.add(smb_share)
            refresh_smb_config(list(SambaShare.objects.all()))
            self._restart_samba()
            return Response(SambaShareSerializer(smb_share).data)