def post(self, request): try: sname = request.DATA['sname'] if (Share.objects.filter(name=sname).exists()): e_msg = ('Share with name: %s already exists.' % sname) handle_exception(Exception(e_msg), request) pool_name = request.DATA['pool'] size = int(request.DATA['size']) #in KB self._validate_share_size(request, size) pool = None try: pool = Pool.objects.get(name=pool_name) except: e_msg = ('Pool with name: %s does not exist.' % pool_name) handle_exception(Exception(e_msg), request) disk = None try: disk = Disk.objects.filter(pool=pool)[0] except: e_msg = ('Pool with name: %s does not have any disks in it.' % pool_name) handle_exception(Exception(e_msg), request) add_share(pool_name, disk.name, sname) qgroup_id = self._update_quota(pool_name, disk.name, sname, size) s = Share(pool=pool, qgroup=qgroup_id, name=sname, size=size, subvol_name=sname) s.save() return Response(ShareSerializer(s).data) except RockStorAPIException: raise except Exception, e: handle_exception(e, request)
def post(self, request): with self._handle_exception(request): pool_name = request.data.get('pool', None) try: pool = Pool.objects.get(name=pool_name) except: e_msg = ('Pool(%s) does not exist.' % pool_name) handle_exception(Exception(e_msg), request) compression = self._validate_compression(request) size = self._validate_share_size(request, pool) sname = request.data.get('sname', None) if ((sname is None or re.match('%s$' % settings.SHARE_REGEX, sname) is None)): e_msg = ('Share name must start with a alphanumeric(a-z0-9) ' 'character and can be followed by any of the ' 'following characters: letter(a-z), digits(0-9), ' 'hyphen(-), underscore(_) or a period(.).') handle_exception(Exception(e_msg), request) if (Share.objects.filter(name=sname).exists()): e_msg = ('Share(%s) already exists. Choose a different name' % sname) handle_exception(Exception(e_msg), request) if (Pool.objects.filter(name=sname).exists()): e_msg = ( 'A Pool with this name(%s) exists. Share and Pool names ' 'must be distinct. Choose a different name' % sname) handle_exception(Exception(e_msg), request) disk = Disk.objects.filter(pool=pool)[0] replica = False if ('replica' in request.data): replica = request.data['replica'] if (type(replica) != bool): e_msg = ('replica must be a boolean, not %s' % type(replica)) handle_exception(Exception(e_msg), request) add_share(pool, disk.name, sname) qid = qgroup_id(pool, disk.name, sname) update_quota(pool, disk.name, qid, size * 1024) s = Share(pool=pool, qgroup=qid, name=sname, size=size, subvol_name=sname, replica=replica, compression_algo=compression) s.save() mnt_pt = '%s%s' % (settings.MNT_PT, sname) if (not is_share_mounted(sname)): disk = Disk.objects.filter(pool=pool)[0].name mount_share(s, disk, mnt_pt) if (compression != 'no'): set_property(mnt_pt, 'compression', compression) return Response(ShareSerializer(s).data)
def post(self, request): with self._handle_exception(request): pool_name = request.DATA.get('pool', None) try: pool = Pool.objects.get(name=pool_name) except: e_msg = ('Pool(%s) does not exist.' % pool_name) handle_exception(Exception(e_msg), request) compression = self._validate_compression(request) size = self._validate_share_size(request, pool) sname = request.DATA.get('sname', None) if ((sname is None or re.match('%s$' % settings.SHARE_REGEX, sname) is None)): e_msg = ('Share name must start with a letter(a-z) and can ' 'be followed by any of the following characters: ' 'letter(a-z), digits(0-9), hyphen(-), underscore' '(_) or a period(.).') handle_exception(Exception(e_msg), request) if (Share.objects.filter(name=sname).exists()): e_msg = ('Share(%s) already exists.' % sname) handle_exception(Exception(e_msg), request) try: disk = Disk.objects.filter(pool=pool)[0] except: e_msg = ('Pool(%s) does not have any disks in it.' % pool_name) handle_exception(Exception(e_msg), request) replica = False if ('replica' in request.DATA): replica = request.DATA['replica'] if (type(replica) != bool): e_msg = ('replica must be a boolean, not %s' % type(replica)) handle_exception(Exception(e_msg), request) add_share(pool, disk.name, sname) qgroup_id = self._update_quota(pool, disk.name, sname, size) s = Share(pool=pool, qgroup=qgroup_id, name=sname, size=size, subvol_name=sname, replica=replica, compression_algo=compression) s.save() mnt_pt = '%s%s' % (settings.MNT_PT, sname) if (not is_share_mounted(sname)): disk = Disk.objects.filter(pool=pool)[0].name mount_share(s, disk, mnt_pt) if (compression != 'no'): set_property(mnt_pt, 'compression', compression) return Response(ShareSerializer(s).data)
def post(self, request): try: sname = request.DATA['sname'] if (re.match('%s$' % settings.SHARE_REGEX, sname) is None): e_msg = ('Share name must start with a letter(a-z) and can' ' be followed by any of the following characters: ' 'letter(a-z), digits(0-9), hyphen(-), underscore' '(_) or a period(.).') handle_exception(Exception(e_msg), request) if (Share.objects.filter(name=sname).exists()): e_msg = ('Share with name: %s already exists.' % sname) handle_exception(Exception(e_msg), request) pool_name = request.DATA['pool'] size = int(request.DATA['size']) #in KB self._validate_share_size(request, size) pool = None try: pool = Pool.objects.get(name=pool_name) except: e_msg = ('Pool with name: %s does not exist.' % pool_name) handle_exception(Exception(e_msg), request) disk = None try: disk = Disk.objects.filter(pool=pool)[0] except: e_msg = ('Pool with name: %s does not have any disks in it.' % pool_name) handle_exception(Exception(e_msg), request) replica = False if ('replica' in request.DATA): replica = request.DATA['replica'] if (type(replica) != bool): e_msg = ('replica must be a boolean, not %s' % type(replica)) handle_exception(Exception(e_msg), request) add_share(pool_name, disk.name, sname) qgroup_id = self._update_quota(pool_name, disk.name, sname, size) s = Share(pool=pool, qgroup=qgroup_id, name=sname, size=size, subvol_name=sname, replica=replica) s.save() return Response(ShareSerializer(s).data) except RockStorAPIException: raise except Exception, e: handle_exception(e, request)
def post(self, request): with self._handle_exception(request): pool_name = request.data.get('pool', None) try: pool = Pool.objects.get(name=pool_name) except: e_msg = ('Pool(%s) does not exist.' % pool_name) handle_exception(Exception(e_msg), request) compression = self._validate_compression(request) size = self._validate_share_size(request, pool) sname = request.data.get('sname', None) if ((sname is None or re.match('%s$' % settings.SHARE_REGEX, sname) is None)): e_msg = ('Share name must start with a alphanumeric(a-z0-9) ' 'character and can be followed by any of the ' 'following characters: letter(a-z), digits(0-9), ' 'hyphen(-), underscore(_) or a period(.).') handle_exception(Exception(e_msg), request) if (Share.objects.filter(name=sname).exists()): e_msg = ('Share(%s) already exists. Choose a different name' % sname) handle_exception(Exception(e_msg), request) if (Pool.objects.filter(name=sname).exists()): e_msg = ('A Pool with this name(%s) exists. Share and Pool names ' 'must be distinct. Choose a different name' % sname) handle_exception(Exception(e_msg), request) disk = Disk.objects.filter(pool=pool)[0] replica = False if ('replica' in request.data): replica = request.data['replica'] if (type(replica) != bool): e_msg = ('replica must be a boolean, not %s' % type(replica)) handle_exception(Exception(e_msg), request) add_share(pool, disk.name, sname) qid = qgroup_id(pool, disk.name, sname) update_quota(pool, disk.name, qid, size * 1024) s = Share(pool=pool, qgroup=qgroup_id, name=sname, size=size, subvol_name=sname, replica=replica, compression_algo=compression) s.save() mnt_pt = '%s%s' % (settings.MNT_PT, sname) if (not is_share_mounted(sname)): disk = Disk.objects.filter(pool=pool)[0].name mount_share(s, disk, mnt_pt) if (compression != 'no'): set_property(mnt_pt, 'compression', compression) return Response(ShareSerializer(s).data)
def btrfs_add_share(share): pools = btrfs_pool_scan() share_s = {} for pool in pools: if pool["name"] == share["pool"]: pqid = qgroup_create(pool) add_share(pool, share["sname"], pqid) qid = qgroup_id(pool, share["sname"]) update_quota(pool, pqid, share["size"] * 1024) mnt_pt = '%s%s' % (MNT_PT, share["sname"]) replica = False share_s["name"] = share["sname"] share_s["subvol_name"] = share["sname"] share_s["size"] = share["size"] share_s["qgroup"] = qid share_s["pqgroup"] = pqid share_s["pool"] = pool share_s["replica"] = replica share_s["compression_algo"] = share["compression"] mount_share(share_s, mnt_pt) return share_s
def post(self, request): try: pool_name = request.DATA['pool'] share_name = request.DATA['name'] size = int(request.DATA['size']) pool = None for p in Pool.objects.all(): if (p.name == pool_name): pool = p break disk = Disk.objects.filter(pool=p)[0] add_share(pool_name, disk.name, share_name) sid = share_id(pool_name, disk.name, share_name) qgroup_id = '0/' + sid update_quota(pool_name, disk.name, qgroup_id, str(size)) cur_usage = int(share_usage(pool_name, disk.name, qgroup_id)) qgroup = Qgroup(uuid=qgroup_id) qgroup.save() s = Share(pool=pool, qgroup=qgroup, name=share_name, size=size, free=(size - cur_usage)) s.save() return Response(ShareSerializer(s).data) except Exception, e: handle_exception(e, request)
def post(self, request): # qgroup notes for shares. we need to create a qgroup prior to share # creation. qgroup ids 0/<subvol_id> automatically get created when a # subvolume(i.e., a Share or a Snapshot) is created. So let's create a # new qgroup: 2015/<some_number> whenever a Share is # created. <some_number> starts from 1 and is incremented as more # Shares are created. So, for the very first Share in a pool, it's # qgroup will be 1/1. 2015 is arbitrarily chose. # Before creating a new Share, we create the qgroup for it. And during # it's creation, we assign this qgroup to it. During it's creation a # 0/x qgroup will automatically be created, but it will become the # child of our explicitly-created qgroup(2015/x). # We will set the qgroup limit on our qgroup and it will enforce the # quota on every subvolume(i.e., Share and Snapshot) in that qgroup. # When a Share is deleted, we need to destroy two qgroups. One is it's # auto 0/x qgroup and the other is our explicitly-created 2015/y # qgroup. with self._handle_exception(request): pool_name = request.data.get('pool', None) try: pool = Pool.objects.get(name=pool_name) except: e_msg = 'Pool ({}) does not exist.'.format(pool_name) handle_exception(Exception(e_msg), request) compression = self._validate_compression(request) size = self._validate_share_size(request, pool) sname = request.data.get('sname', None) if ((sname is None or re.match('%s$' % settings.SHARE_REGEX, sname) is None)): e_msg = ('Invalid characters in share name. Following are ' 'allowed: letter(a-z or A-Z), digit(0-9), ' 'hyphen(-), underscore(_) or a period(.).') handle_exception(Exception(e_msg), request) if (len(sname) > 254): # btrfs subvolume names cannot exceed 254 characters. e_msg = ('Share name length cannot exceed 254 characters.') handle_exception(Exception(e_msg), request) if (Share.objects.filter(name=sname).exists()): # Note e_msg is consumed by replication/util.py create_share() e_msg = ('Share ({}) already exists. Choose a ' 'different name.').format(sname) handle_exception(Exception(e_msg), request) if (Pool.objects.filter(name=sname).exists()): e_msg = ('A pool with this name ({}) exists. Share ' 'and pool names must be distinct. Choose ' 'a different name.').format(sname) handle_exception(Exception(e_msg), request) replica = False if ('replica' in request.data): replica = request.data['replica'] if (type(replica) != bool): # TODO: confirm this 'type' call works as format parameter. e_msg = ('Replica must be a boolean, ' 'not ({}).').format(type(replica)) handle_exception(Exception(e_msg), request) pqid = qgroup_create(pool) add_share(pool, sname, pqid) qid = qgroup_id(pool, sname) s = Share(pool=pool, qgroup=qid, pqgroup=pqid, name=sname, size=size, subvol_name=sname, replica=replica, compression_algo=compression) # The following pool.save() was informed by test_share.py pool.save() s.save() if pqid is not PQGROUP_DEFAULT: update_quota(pool, pqid, size * 1024) share_pqgroup_assign(pqid, s) mnt_pt = '%s%s' % (settings.MNT_PT, sname) if not s.is_mounted: mount_share(s, mnt_pt) if (compression != 'no'): set_property(mnt_pt, 'compression', compression) return Response(ShareSerializer(s).data)
def post(self, request): #qgroup notes for shares. we need to create a qgroup prior to share #creation. qgroup ids 0/<subvol_id> automatically get created when a #subvolume(i.e., a Share or a Snapshot) is created. So let's create a #new qgroup: 2015/<some_number> whenever a Share is #created. <some_number> starts from 1 and is incremented as more Shares #are created. So, for the very first Share in a pool, it's qgroup will #be 1/1. 2015 is arbitrarily chose. #Before creating a new Share, we create the qgroup for it. And during #it's creation, we assign this qgroup to it. During it's creation a 0/x #qgroup will automatically be created, but it will become the child of #our explicitly-created qgroup(2015/x). #We will set the qgroup limit on our qgroup and it will enforce the #quota on every subvolume(i.e., Share and Snapshot) in that qgroup. #When a Share is deleted, we need to destroy two qgroups. One is it's #auto 0/x qgroup and the other is our explicitly-created 2015/y qgroup. with self._handle_exception(request): pool_name = request.data.get('pool', None) try: pool = Pool.objects.get(name=pool_name) except: e_msg = ('Pool(%s) does not exist.' % pool_name) handle_exception(Exception(e_msg), request) compression = self._validate_compression(request) size = self._validate_share_size(request, pool) sname = request.data.get('sname', None) if ((sname is None or re.match('%s$' % settings.SHARE_REGEX, sname) is None)): e_msg = ('Share name must start with a alphanumeric(a-z0-9) ' 'character and can be followed by any of the ' 'following characters: letter(a-z), digits(0-9), ' 'hyphen(-), underscore(_) or a period(.).') handle_exception(Exception(e_msg), request) if (len(sname) > 254): #btrfs subvolume names cannot exceed 254 characters. e_msg = ('Share name length cannot exceed 254 characters') handle_exception(Exception(e_msg), request) if (Share.objects.filter(name=sname).exists()): e_msg = ('Share(%s) already exists. Choose a different name' % sname) handle_exception(Exception(e_msg), request) if (Pool.objects.filter(name=sname).exists()): e_msg = ('A Pool with this name(%s) exists. Share and Pool names ' 'must be distinct. Choose a different name' % sname) handle_exception(Exception(e_msg), request) disk = Disk.objects.filter(pool=pool)[0] replica = False if ('replica' in request.data): replica = request.data['replica'] if (type(replica) != bool): e_msg = ('replica must be a boolean, not %s' % type(replica)) handle_exception(Exception(e_msg), request) pqid = qgroup_create(pool) add_share(pool, sname, pqid) qid = qgroup_id(pool, sname) update_quota(pool, pqid, size * 1024) s = Share(pool=pool, qgroup=qid, pqgroup=pqid, name=sname, size=size, subvol_name=sname, replica=replica, compression_algo=compression) s.save() mnt_pt = '%s%s' % (settings.MNT_PT, sname) if (not is_share_mounted(sname)): mount_share(s, mnt_pt) if (compression != 'no'): set_property(mnt_pt, 'compression', compression) return Response(ShareSerializer(s).data)
def post(self, request): #qgroup notes for shares. we need to create a qgroup prior to share #creation. qgroup ids 0/<subvol_id> automatically get created when a #subvolume(i.e., a Share or a Snapshot) is created. So let's create a #new qgroup: 2015/<some_number> whenever a Share is #created. <some_number> starts from 1 and is incremented as more Shares #are created. So, for the very first Share in a pool, it's qgroup will #be 1/1. 2015 is arbitrarily chose. #Before creating a new Share, we create the qgroup for it. And during #it's creation, we assign this qgroup to it. During it's creation a 0/x #qgroup will automatically be created, but it will become the child of #our explicitly-created qgroup(2015/x). #We will set the qgroup limit on our qgroup and it will enforce the #quota on every subvolume(i.e., Share and Snapshot) in that qgroup. #When a Share is deleted, we need to destroy two qgroups. One is it's #auto 0/x qgroup and the other is our explicitly-created 2015/y qgroup. with self._handle_exception(request): pool_name = request.data.get('pool', None) try: pool = Pool.objects.get(name=pool_name) except: e_msg = ('Pool(%s) does not exist.' % pool_name) handle_exception(Exception(e_msg), request) compression = self._validate_compression(request) size = self._validate_share_size(request, pool) sname = request.data.get('sname', None) if ((sname is None or re.match('%s$' % settings.SHARE_REGEX, sname) is None)): e_msg = ('Share name must start with a alphanumeric(a-z0-9) ' 'character and can be followed by any of the ' 'following characters: letter(a-z), digits(0-9), ' 'hyphen(-), underscore(_) or a period(.).') handle_exception(Exception(e_msg), request) if (len(sname) > 254): #btrfs subvolume names cannot exceed 254 characters. e_msg = ('Share name length cannot exceed 254 characters') handle_exception(Exception(e_msg), request) if (Share.objects.filter(name=sname).exists()): e_msg = ('Share(%s) already exists. Choose a different name' % sname) handle_exception(Exception(e_msg), request) if (Pool.objects.filter(name=sname).exists()): e_msg = ( 'A Pool with this name(%s) exists. Share and Pool names ' 'must be distinct. Choose a different name' % sname) handle_exception(Exception(e_msg), request) disk = Disk.objects.filter(pool=pool)[0] replica = False if ('replica' in request.data): replica = request.data['replica'] if (type(replica) != bool): e_msg = ('replica must be a boolean, not %s' % type(replica)) handle_exception(Exception(e_msg), request) pqid = qgroup_create(pool) add_share(pool, sname, pqid) qid = qgroup_id(pool, sname) update_quota(pool, pqid, size * 1024) s = Share(pool=pool, qgroup=qid, pqgroup=pqid, name=sname, size=size, subvol_name=sname, replica=replica, compression_algo=compression) s.save() mnt_pt = '%s%s' % (settings.MNT_PT, sname) if (not is_share_mounted(sname)): mount_share(s, mnt_pt) if (compression != 'no'): set_property(mnt_pt, 'compression', compression) return Response(ShareSerializer(s).data)