def post(self, request, sname, command): with self._handle_exception(request): share = self._validate_share(request, sname) if (command == 'clone'): new_name = request.data.get('name', '') return create_clone(share, new_name, request, logger) if (command == 'rollback'): snap = self._validate_snapshot(request, share) if (NFSExport.objects.filter(share=share).exists()): e_msg = ('Share(%s) cannot be rolled back as it is ' 'exported via nfs. Delete nfs exports and ' 'try again' % sname) handle_exception(Exception(e_msg), request) if (SambaShare.objects.filter(share=share).exists()): e_msg = ('Share(%s) cannot be rolled back as it is shared' ' via Samba. Unshare and try again' % sname) handle_exception(Exception(e_msg), request) rollback_snap(snap.real_name, share.name, share.subvol_name, share.pool) update_quota(share.pool, snap.qgroup, share.size * 1024) share.qgroup = snap.qgroup share.save() snap.delete() return Response()
def post(self, request, sname, command): with self._handle_exception(request): share = self._validate_share(request, sname) if command == "clone": new_name = request.DATA.get("name", "") return create_clone(share, new_name, request, logger) if command == "rollback": snap = self._validate_snapshot(request, share) if NFSExport.objects.filter(share=share).exists(): e_msg = ( "Share(%s) cannot be rolled back as it is " "exported via nfs. Delete nfs exports and " "try again" % sname ) handle_exception(Exception(e_msg), request) if SambaShare.objects.filter(share=share).exists(): e_msg = ( "Share(%s) cannot be rolled back as it is shared" " via Samba. Unshare and try again" % sname ) handle_exception(Exception(e_msg), request) pool_device = Disk.objects.filter(pool=share.pool)[0].name rollback_snap(snap.real_name, share.name, share.subvol_name, share.pool, pool_device) update_quota(share.pool, pool_device, snap.qgroup, share.size * 1024) share.qgroup = snap.qgroup share.save() snap.delete() return Response() if command == "compress": algo = request.DATA.get("compress", None) if algo is None: e_msg = ( "Compression algorithm must be specified. Valid " "options are: %s" % settings.COMPRESSION_TYPES ) handle_exception(Exception(e_msg), request) if algo not in settings.COMPRESSION_TYPES: e_msg = ( "Compression algorithm(%s) is invalid. Valid " "options are: %s" % settings.COMPRESSION_TYPES ) handle_exception(Exception(e_msg), request) mnt_pt = "%s%s" % (settings.MNT_PT, share.name) if not is_share_mounted(share.name): disk = Disk.objects.filter(pool=share.pool)[0].name mount_share(share, disk, mnt_pt) share.compression_algo = algo share.save() if algo == "no": algo = "" set_property(mnt_pt, "compression", algo) return Response(ShareSerializer(share).data)
def post(self, request, sname, snap_name, command=None): with self._handle_exception(request): share = self._validate_share(sname, request) uvisible = request.data.get('uvisible', False) if (type(uvisible) != bool): e_msg = ('uvisible must be a boolean, not %s' % type(uvisible)) handle_exception(Exception(e_msg), request) snap_type = request.data.get('snap_type', 'admin') writable = request.data.get('writable', False) if (type(writable) != bool): e_msg = ('writable must be a boolean, not %s' % type(writable)) handle_exception(Exception(e_msg), request) if (command is None): ret = self._create(share, snap_name, request, uvisible=uvisible, snap_type=snap_type, writable=writable) if (uvisible): try: self._toggle_visibility(share, ret.data['real_name']) except Exception as e: msg = ('Failed to make the Snapshot(%s) visible. ' 'Exception: %s' % (snap_name, e.__str__())) logger.error(msg) logger.exception(e) try: toggle_sftp_visibility(share, ret.data['real_name']) except Exception as e: msg = ('Failed to make the Snapshot(%s) visible for ' 'SFTP. Exception: %s' % (snap_name, e.__str__())) logger.error(msg) logger.exception(e) return ret if (command == 'clone'): new_name = request.data.get('name', None) snapshot = Snapshot.objects.get(share=share, name=snap_name) return create_clone(share, new_name, request, logger, snapshot=snapshot) e_msg = ('Unknown command: %s' % command) handle_exception(Exception(e_msg), request)
def post(self, request, sname, command): try: share = Share.objects.get(name=sname) except: e_msg = ('Share: %s does not exist' % sname) handle_exception(Exception(e_msg), request) if (command == 'clone'): new_name = request.DATA['name'] if (Share.objects.filter(name=new_name).exists()): e_msg = ('Share with name: %s already exists.' % new_name) handle_exception(Exception(e_msg), request) pool_device = Disk.objects.filter(pool=share.pool)[0].name return create_clone(share, new_name, pool_device, request) elif (command == 'rollback'): snap_name = request.DATA['name'] try: snap = Snapshot.objects.get(share=share, name=snap_name) except: e_msg = ('Snapshot with name: %s does not exist for the ' 'share: %s' % (snap_name, share.name)) handle_exception(Exception(e_msg), request) if (NFSExport.objects.filter(share=share).exists()): e_msg = ('Share: %s cannot be rolled back as it is exported ' 'via nfs. Delete nfs exports and try again' % sname) handle_exception(Exception(e_msg), request) if (SambaShare.objects.filter(share=share).exists()): e_msg = ('Share: %s cannot be rolled back as it is shared ' ' via Samba. Unshare and try again' % sname) handle_exception(Exception(e_msg), request) try: pool_device = Disk.objects.filter(pool=share.pool)[0].name rollback_snap(snap.real_name, share.name, share.subvol_name, share.pool.name, pool_device) share.subvol_name = snap.real_name update_quota(share.pool.name, pool_device, snap.qgroup, share.size * 1024) share.qgroup = snap.qgroup share.save() snap.delete() return Response() except Exception, e: logger.exception(e) handle_exception(e, request)
def post(self, request, sname, command): try: share = Share.objects.get(name=sname) except: e_msg = ('Share: %s does not exist' % sname) handle_exception(Exception(e_msg), request) if (command == 'clone'): new_name = request.DATA['name'] if (Share.objects.filter(name=new_name).exists()): e_msg = ('Share with name: %s already exists.' % new_name) handle_exception(Exception(e_msg), request) pool_device = Disk.objects.filter(pool=share.pool)[0].name return create_clone(share, new_name, pool_device, request) elif (command == 'rollback'): snap_name = request.DATA['name'] try: snap = Snapshot.objects.get(share=share, name=snap_name) except: e_msg = ('Snapshot with name: %s does not exist for the ' 'share: %s' % (snap_name, share.name)) handle_exception(Exception(e_msg), request) if (NFSExport.objects.filter(share=share).exists()): e_msg = ('Share: %s cannot be rolled back as it is exported ' 'via nfs. Delete nfs exports and try again' % sname) handle_exception(Exception(e_msg), request) if (SambaShare.objects.filter(share=share).exists()): e_msg = ('Share: %s cannot be rolled back as it is shared ' ' via Samba. Unshare and try again' % sname) handle_exception(Exception(e_msg), request) try: pool_device = Disk.objects.filter(pool=share.pool)[0].name rollback_snap(snap.real_name, share.name, share.subvol_name, share.pool.name, pool_device) share.subvol_name = snap.real_name update_quota(share.pool.name, pool_device, snap.qgroup, share.size * 1024) share.qgroup = snap.qgroup share.save() snap.delete() return Response() except Exception, e: logger.exception(e) handle_exception(e, request)
def post(self, request, sname, snap_name, command=None): with self._handle_exception(request): share = self._validate_share(sname, request) uvisible = request.data.get('uvisible', False) if (type(uvisible) != bool): e_msg = ('uvisible must be a boolean, not %s' % type(uvisible)) handle_exception(Exception(e_msg), request) snap_type = request.data.get('snap_type', 'admin') writable = request.data.get('writable', False) if (type(writable) != bool): e_msg = ('writable must be a boolean, not %s' % type(writable)) handle_exception(Exception(e_msg), request) if (command is None): ret = self._create(share, snap_name, request, uvisible=uvisible, snap_type=snap_type, writable=writable) if (uvisible): try: self._toggle_visibility(share, ret.data['real_name']) except Exception as e: msg = ('Failed to make the Snapshot(%s) visible. ' 'Exception: %s' % (snap_name, e.__str__())) logger.error(msg) logger.exception(e) try: toggle_sftp_visibility(share, ret.data['real_name']) except Exception as e: msg = ('Failed to make the Snapshot(%s) visible for ' 'SFTP. Exception: %s' % (snap_name, e.__str__())) logger.error(msg) logger.exception(e) return ret if (command == 'clone'): new_name = request.data.get('name', None) snapshot = Snapshot.objects.get(share=share, name=snap_name) return create_clone(share, new_name, request, logger, snapshot=snapshot) e_msg = ('Unknown command: %s' % command) handle_exception(Exception(e_msg), request)
def post(self, request, sid, command): with self._handle_exception(request): share = self._validate_share(request, sid) if (command == 'clone'): new_name = request.data.get('name', '') return create_clone(share, new_name, request, logger) if (command == 'rollback'): snap = self._validate_snapshot(request, share) if (NFSExport.objects.filter(share=share).exists()): e_msg = ('Share ({}) cannot be rolled back as it is ' 'exported via NFS. Delete NFS exports and ' 'try again.').format(share.name) handle_exception(Exception(e_msg), request) if (SambaShare.objects.filter(share=share).exists()): e_msg = ('Share ({}) cannot be rolled back as it is ' 'shared via Samba. Unshare and ' 'try again.').format(share.name) handle_exception(Exception(e_msg), request) return create_repclone(share, request, logger, snap)
def post(self, request, sid, command): with self._handle_exception(request): share = self._validate_share(request, sid) if command == "clone": new_name = request.data.get("name", "") return create_clone(share, new_name, request, logger) if command == "rollback": snap = self._validate_snapshot(request, share) if NFSExport.objects.filter(share=share).exists(): e_msg = ("Share ({}) cannot be rolled back as it is " "exported via NFS. Delete NFS exports and " "try again.").format(share.name) handle_exception(Exception(e_msg), request) if SambaShare.objects.filter(share=share).exists(): e_msg = ("Share ({}) cannot be rolled back as it is " "shared via Samba. Unshare and " "try again.").format(share.name) handle_exception(Exception(e_msg), request) return create_repclone(share, request, logger, snap)
try: toggle_sftp_visibility(share, ret.data['real_name']) except Exception, e: msg = ('Failed to make the Snapshot(%s) visible for ' 'SFTP. Exception: %s' % (snap_name, e.__str__())) logger.error(msg) logger.exception(e) return ret if (command == 'clone'): new_name = request.data.get('name', None) snapshot = Snapshot.objects.get(share=share, name=snap_name) return create_clone(share, new_name, request, logger, snapshot=snapshot) e_msg = ('Unknown command: %s' % command) handle_exception(Exception(e_msg), request) @staticmethod def _validate_share(sname, request): try: return Share.objects.get(name=sname) except: e_msg = ('Share: %s does not exist' % sname) handle_exception(Exception(e_msg), request) @transaction.atomic def _delete_snapshot(self, request, sname, id=None, snap_name=None):
logger.error(msg) logger.exception(e) try: toggle_sftp_visibility(share, ret.data['real_name']) except Exception, e: msg = ('Failed to make the Snapshot(%s) visible for ' 'SFTP. Exception: %s' % (snap_name, e.__str__())) logger.error(msg) logger.exception(e) return ret if (command == 'clone'): new_name = request.data.get('name', None) snapshot = Snapshot.objects.get(share=share, name=snap_name) return create_clone(share, new_name, request, logger, snapshot=snapshot) e_msg = ('Unknown command: %s' % command) handle_exception(Exception(e_msg), request) @staticmethod def _validate_share(sname, request): try: return Share.objects.get(name=sname) except: e_msg = ('Share: %s does not exist' % sname) handle_exception(Exception(e_msg), request) @transaction.atomic def _delete_snapshot(self, request, sname, id=None, snap_name=None): share = self._validate_share(sname, request) try:
def post(self, request, sid, snap_name, command=None): with self._handle_exception(request): share = self._validate_share(sid, request) uvisible = request.data.get('uvisible', False) if (type(uvisible) != bool): # N.B. quote type important - test string involves ('unicode') e_msg = ("Element 'uvisible' must be a boolean, " "not ({}).").format(type(uvisible)) handle_exception(Exception(e_msg), request) snap_type = request.data.get('snap_type', 'admin') writable = request.data.get('writable', False) if (type(writable) != bool): e_msg = ('Element "writable" must be a boolean, ' 'not ({}).').format(type(writable)) handle_exception(Exception(e_msg), request) if (command is None): ret = self._create(share, snap_name, request, uvisible=uvisible, snap_type=snap_type, writable=writable) if (uvisible): try: self._toggle_visibility(share, ret.data['real_name'], ret.data['qgroup']) except Exception as e: msg = ('Failed to make the snapshot ({}) visible. ' 'Exception: ({}).').format(snap_name, e.__str__()) logger.error(msg) logger.exception(e) try: toggle_sftp_visibility(share, ret.data['real_name'], ret.data['qgroup']) except Exception as e: msg = ('Failed to make the snapshot ({}) visible for ' 'SFTP. Exception: ({}).').format(snap_name, e.__str__()) logger.error(msg) logger.exception(e) return ret if (command == 'clone'): new_name = request.data.get('name', None) snapshot = Snapshot.objects.get(share=share, name=snap_name) return create_clone(share, new_name, request, logger, snapshot=snapshot) if (command == 'repclone'): # When repclone is first called the oldest snapshot is # actually a share. This is an artifact of import_shares() # identifying the first snapshot incorrectly as a share. # TODO: As a temporary work around for this, and to avoid # TODO: modifying import_shares / import_snapshots for what is # TODO: currently stable behaviour we simply try for a share # TODO: if no snapshot is found, ie failing over to accommodate # TODO: for the currently quirky behaviour of the imports. # TODO: Note: this snap as share quirk is temporary as once a # TODO: replication cycle has completed it is removed. try: snapshot = Snapshot.objects.get(share=share, name=snap_name) except Snapshot.DoesNotExist: # Here we rely on the polymorphism of Share/Snap and that # this quirky share is located as per regular snapshots. # The subvol of our snap-as-share-quirk is as per a snap: # ".snapshots/sharename/snapname" quirk_snap_share = '.snapshots/{}/{}'.format(share.name, snap_name) logger.debug('Fail through for snap-as-share-quirk') snapshot = Share.objects.get(subvol_name=quirk_snap_share) return create_repclone(share, request, logger, snapshot=snapshot) e_msg = 'Unknown command: ({}).'.format(command) handle_exception(Exception(e_msg), request)
snap_name) logger.error(msg) logger.exception(e) try: toggle_sftp_visibility(share, ret.data['real_name']) except Exception, e: msg = ('Failed to make the Snapshot(%s) visible for SFTP.' % snap_name) logger.error(msg) logger.exception(e) return ret if (command == 'clone'): new_name = request.DATA['name'] return create_clone(share, new_name, request, logger) e_msg = ('Unknown command: %s' % command) handle_exception(Exception(e_msg), request) def _validate_share(self, sname, request): try: return Share.objects.get(name=sname) except: e_msg = ('Share: %s does not exist' % sname) handle_exception(Exception(e_msg), request) @transaction.commit_on_success def put(self, request): """ to make a snapshot writable etc.. """
logger.error(msg) logger.exception(e) try: toggle_sftp_visibility(share, ret.data['real_name']) except Exception, e: msg = ( 'Failed to make the Snapshot(%s) visible for SFTP.' % snap_name) logger.error(msg) logger.exception(e) return ret if (command == 'clone'): new_name = request.DATA['name'] return create_clone(share, new_name, request, logger) e_msg = ('Unknown command: %s' % command) handle_exception(Exception(e_msg), request) def _validate_share(self, sname, request): try: return Share.objects.get(name=sname) except: e_msg = ('Share: %s does not exist' % sname) handle_exception(Exception(e_msg), request) @transaction.commit_on_success def put(self, request): """ to make a snapshot writable etc.. """