irods_testbench_training_py.../server_rule_example/ocf.re

119 lines
5.6 KiB
C++
Executable File

ocfUnzipDataObject(*objpath, *objtype) {
# use filename.zip to build string filename.csv
msiSplitPath(*objpath,*collection,*file)
*file_extension_elem = split(*file,".");
*file_extension_elem_size = size(*file_extension_elem);
#*file_extension = elem(*file_extension_elem,(*file_extension_elem_size -1));
*match_file = ""
# loop used to match filename in filename.x.y.z.zip
for (*i = 0 ; *i < (*file_extension_elem_size -1) ; *i = *i + 1) {
*filename_part = elem(*file_extension_elem,*i)
*newstr = "*match_file" ++ "*filename_part" ++ "."
*match_file = *newstr
}
# build filename file.csv from file.zip
*match_extension = "csv"
*newstr = "*match_file" ++ "*match_extension"
*match_file = *newstr
# find the physical path of filename.zip, this should be a local unix filesystem mounted as a Resource (or s3 Resource cached directory)
*query = SELECT DATA_PATH WHERE COLL_NAME = '*collection' AND DATA_NAME = '*file'
foreach(*row in *query) {
msiGetValByKey(*row,"DATA_PATH",*physpath)
}
# get contents of filename.zip, if contains filename.csv extract and register in iRODS
#
# link unzip and stat to iRODS executable directory for msiExecCmd
# ln -s /usr/bin/unzip /var/lib/irods/msiExecCmd_bin/unzip
# ln -s /usr/bin/stat /var/lib/irods/msiExecCmd_bin/stat
# ln -s /bin/mkdir /var/lib/irods/msiExecCmd_bin/mkdir
# ln -s /bin/rm /var/lib/irods/msiExecCmd_bin/rm
msiExecCmd("unzip","-Z1 *physpath","localhost","null","null",*result)
#msiGetStderrInExecCmdOut(*result,*stderr);
msiGetStdoutInExecCmdOut(*result,*stdout);
# check each line of stdout for a matching filename.csv
*stdout_elem = split(*stdout,"\n")
*stdout_elem_size = size(*stdout_elem)
for (*i = 0 ; *i < *stdout_elem_size ; *i = *i + 1) {
*zip_content_match = elem(*stdout_elem,*i)
# if filename.csv found in filename.zip
# where metadata is to be extracted from the file name (like date) this would be performed here
if (*zip_content_match == *match_file) then {
#writeLine("serverLog", "*check_match = *match_file")
# create temporary directory and unzip file
msiSplitPath(*physpath,*phys_dir,*phys_file)
msiGetSystemTime(*time, "")
*phys_tmp_dir = "*phys_dir" ++ "/" ++ "*time"
*tmp_dir = "*collection" ++ "/" ++ "*time"
*phys_tmp_path = *phys_tmp_dir ++ "/" ++ *match_file
*extract_objpath = *collection ++ "/" ++ *match_file
msiCollCreate(*tmp_dir,"",*result)
msiExecCmd("mkdir","*phys_tmp_dir","localhost","null","null",*result)
msiExecCmd("unzip","-j *physpath *match_file -d *phys_tmp_dir","localhost","null","null",*result)
# check file extracted correctly, move from temp directory to unzip directory, otherwise tidy up and error
if (errorcode(msiExecCmd("stat",*phys_tmp_path,"localhost","null","null",*result)) >=0) {
writeLine("serverLog", "*phys_tmp_path successfully extracted")
# register the extracted file in iCAT to allow it to be copied to unzip folder
# extracting the file directly to the unzip folder and registering (without copy) has odd behaviour on irm delete
msiPhyPathReg("*tmp_dir/*match_file","OCFResc",*phys_tmp_path,"null",*reg_stat)
msiDataObjCopy("*tmp_dir/*match_file",*extract_objpath,"destRescName=OCFResc++++forceFlag=",*result)
# The zip cannot be removed from the iCAT in the PEP activation without a client side error
# solution 1 - unlink/unregister file - works but client errors
#msiDataObjUnlink("objPath=*objpath++++forceFlag=++++unreg=",*status)
#msiDataObjUnlink("objPath=*objpath",*status)
# solution 2 - rename file to temp dir then remove temp dir - works but client errors
#*rename = *tmp_dir ++ "/" ++ *file
#msiDataObjRename(*objpath,*rename,"",*result)
# solution 3 - shell out and run irm - works but client errors
# ln -s /usr/bin/irm /var/lib/irods/msiExecCmd_bin/irm
#msiExecCmd("irm","-f *objpath","localhost","null","null",*result)
# solution 4 - use put microservice to move extracted file to the unzip folder
# this fails, the microservice only works as a client command not a server command
#msiDataObjPut(*extract_objpath,"OCFResc","",*put_status)
# solution 4 - run a bash script that deletes the file
#msiExecCmd("remove_obj.sh","*objpath","localhost","null","null",*result)
# solution 5 - try to trim all replicas
#msiDataObjTrim(*objpath,"OCFResc","0","null","null",*result)
# remove temp directory from iCAT and disk
msiRmColl(*tmp_dir,"forceFlag=",*result)
msiExecCmd("rm","-Rf *phys_tmp_dir","localhost","null","null",*result)
} else {
msiRmColl(*tmp_dir,"forceFlag=",*result)
msiExecCmd("rm","-Rf *phys_tmp_dir","localhost","null","null",*result)
}
}
}
}
# meta, object path, type -d
ocfTagDataObject(*str, *objpath, *objtype) {
msiString2KeyValPair(*str, *kvp);
msiAssociateKeyValuePairsToObj(*kvp, *objpath, *objtype);
writeLine("serverLog", "added metadata to *objpath");
#session variables also available here in native rules structure
#
#msiGetSessionVarValue("all", "server");
#*get_session_var=msiGetSessionVarValue("objPath", "server");
#*something = str(msiGetSessionVarValue("objPath", "client"));
#writeLine("serverLog", "*something");
}
getSessionVar(*name,*output) {
*output = eval("str($"++*name++")");
}