예제 #1
0
    def create_samba_share(self, rdata):
        if "shares" not in rdata:
            e_msg = "Must provide share names."
            handle_exception(Exception(e_msg), rdata)
        shares = [self._validate_share(rdata, s) for s in rdata["shares"]]
        options = self._validate_input(rdata)
        custom_config = options["custom_config"]
        del options["custom_config"]
        with self._handle_exception(rdata):
            for share in shares:
                if SambaShare.objects.filter(share=share).exists():
                    e_msg = (
                        "Share ({}) is already exported via Samba.").format(
                            share.name)
                    logger.error(e_msg)
                    smb_share = SambaShare.objects.get(share=share)
                    # handle_exception(Exception(e_msg), rdata)
                    continue
                mnt_pt = "{}{}".format(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 share.is_mounted:
                    mount_share(share, mnt_pt)

                admin_users = rdata.get("admin_users", [])
                if admin_users is None:
                    admin_users = []
                self._set_admin_users(admin_users, smb_share)
        return smb_share
예제 #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)
예제 #3
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)
예제 #4
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)
예제 #5
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)
예제 #6
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)
예제 #7
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)