def handle(self, *args, **options): istorage = IrodsStorage() # reset quota for data zone root_path = '/{}/home/{}'.format(settings.IRODS_ZONE, settings.IRODS_USERNAME) istorage.setAVU(root_path, 'resetQuotaDir', 1) # reset quota for user zone user_root_path = '/{}/home/{}'.format(settings.HS_USER_IRODS_ZONE, settings.HS_IRODS_PROXY_USER_IN_USER_ZONE) istorage.setAVU(user_root_path, 'resetQuotaDir', 1)
def download(request, path, *args, **kwargs): split_path_strs = path.split('/') if split_path_strs[0] == 'bags': res_id = os.path.splitext(split_path_strs[1])[0] else: res_id = split_path_strs[0] _, authorized, _ = authorize(request, res_id, needed_permission=Action_To_Authorize.VIEW_RESOURCE, raises_exception=False) if not authorized: response = HttpResponse() response.content = "<h1>You do not have permission to download this resource!</h1>" return response if 'environment' in kwargs: environment = int(kwargs['environment']) environment = m.RodsEnvironment.objects.get(pk=environment) session = Session("/tmp/django_irods", settings.IRODS_ICOMMANDS_PATH, session_id=uuid4()) session.create_environment(environment) session.run('iinit', None, environment.auth) elif getattr(settings, 'IRODS_GLOBAL_SESSION', False): session = GLOBAL_SESSION elif icommands.ACTIVE_SESSION: session = icommands.ACTIVE_SESSION else: raise KeyError('settings must have IRODS_GLOBAL_SESSION set if there is no environment object') # do on-demand bag creation istorage = IrodsStorage() bag_modified = "false" # needs to check whether res_id collection exists before getting/setting AVU on it to accommodate the case # where the very same resource gets deleted by another request when it is getting downloaded if istorage.exists(res_id): bag_modified = istorage.getAVU(res_id, 'bag_modified') if bag_modified == "true": create_bag_by_irods(res_id, istorage) if istorage.exists(res_id): istorage.setAVU(res_id, 'bag_modified', "false") # obtain mime_type to set content_type mtype = 'application-x/octet-stream' mime_type = mimetypes.guess_type(path) if mime_type[0] is not None: mtype = mime_type[0] # retrieve file size to set up Content-Length header stdout = session.run("ils", None, "-l", path)[0].split() flen = int(stdout[3]) options = ('-',) # we're redirecting to stdout. proc = session.run_safe('iget', None, path, *options) response = FileResponse(proc.stdout, content_type=mtype) response['Content-Disposition'] = 'attachment; filename="{name}"'.format(name=path.split('/')[-1]) response['Content-Length'] = flen return response
def resource_modified(resource, by_user=None, overwrite_bag=True): resource.last_changed_by = by_user resource.updated = now().isoformat() resource.save() if resource.metadata.dates.all().filter(type='modified'): res_modified_date = resource.metadata.dates.all().filter(type='modified')[0] resource.metadata.update_element('date', res_modified_date.id) if overwrite_bag: create_bag_files(resource) istorage = IrodsStorage() # set bag_modified-true AVU pair for the modified resource in iRODS to indicate # the resource is modified for on-demand bagging. istorage.setAVU(resource.short_id, "bag_modified", "true")
def resource_modified(resource, by_user=None, overwrite_bag=True): resource.last_changed_by = by_user resource.updated = now().isoformat() resource.save() if resource.metadata.dates.all().filter(type='modified'): res_modified_date = resource.metadata.dates.all().filter( type='modified')[0] resource.metadata.update_element('date', res_modified_date.id) if overwrite_bag: create_bag_files(resource) istorage = IrodsStorage() # set bag_modified-true AVU pair for the modified resource in iRODS to indicate # the resource is modified for on-demand bagging. istorage.setAVU(resource.short_id, "bag_modified", "true")
def handle(self, *args, **options): resource_counter = 0 storage = IrodsStorage() avu_list = ['bag_modified', 'metadata_dirty', 'isPublic', 'resourceType'] for resource in BaseResource.objects.all(): if resource.storage_type == 'user': # resource is in user zone, so migrate it to data zone # copy files from iRODS user zone to data zone try: src_coll = resource.root_path tgt_coll = resource.short_id if storage.exists(tgt_coll): storage.delete(tgt_coll) storage.copyFiles(src_coll, tgt_coll) # copy AVU over for the resource collection from iRODS user zone to data zone for avu_name in avu_list: value = storage.getAVU(src_coll, avu_name) # bag_modified AVU needs to be set to true for the new resource so the bag # can be regenerated in the data zone if avu_name == 'bag_modified': storage.setAVU(tgt_coll, avu_name, 'true') # everything else gets copied literally else: storage.setAVU(tgt_coll, avu_name, value) # Just to be on the safe side, it is better not to delete resources from user # zone after it is migrated over to data zone in case there are issues with # migration. A simple irm iRODS command can be issued to delete all resource # collections afterwards if all works well after some time. Commenting the # deletion statement below rather than deleting it to serve as a reminder # that additional cleanup to delete all resource collections in user zone # is needed after we can confirm migration is successfully. # delete the original resource from user zone # storage.delete(src_coll) path_migrated = False for res_file in resource.files.all(): if res_file.resource_file.name: print('The resource_file field should be empty for resource {} but ' 'have the value of {}'.format(resource.short_id, res_file.resource_file.name)) break file_path = res_file.fed_resource_file.name if not file_path: print('The fed_resource_file field should not be empty for ' 'resource {}'.format(resource.short_id)) break elif file_path.startswith(resource.resource_federation_path): file_path = file_path[len(resource.resource_federation_path)+1:] res_file.resource_file.name = file_path res_file.fed_resource_file.name = '' res_file.save() path_migrated = True else: res_file.resource_file.name = file_path res_file.fed_resource_file.name = '' res_file.save() path_migrated = True print('fed_resource_file field does not contain absolute federation ' 'path which is an exception but can work after migration. ' 'file_path is {}'.format(file_path)) if path_migrated or resource.files.count() == 0: # update resource federation path to point resource to data zone resource.resource_federation_path = '' resource.save() print("Resource {} has been moved from user zone to data zone " "successfully".format(resource.short_id)) resource_counter += 1 else: continue except SessionException as ex: print("Resource {} failed to move: {}".format(resource.short_id, ex.stderr)) print("{} resources have been moved from user zone to data zone successfully".format( resource_counter))