Skip to content

Commit

Permalink
[kbss-cvut/fta-fmea-ui#278] Fix deployment of trig files to not clear…
Browse files Browse the repository at this point in the history
… whole repository
  • Loading branch information
blcham committed May 10, 2024
1 parent e354745 commit 53bf9f2
Showing 1 changed file with 73 additions and 29 deletions.
102 changes: 73 additions & 29 deletions deploy/shared/db-server/bin/rdf4j-deploy-context.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@ CONTENT_TYPE='text/turtle'
#CONTENT_TYPE='application/ld+json'

function print_usage() {
echo "Rdf4j server deploy script."
echo "Rdf4j server deploy script. By default it appends data to the server, for replacement of data use -R parameter."
echo "Parameters: "
echo -e "\t-R -- replace content of GRAPH_IRI (appends by default)"
echo -e "\t-R -- replace content of GRAPH_IRI (appends by default). If specified together with -C 'application/trig', the graphs to be cleared are inferred from the trig file."
echo -e "\t-c GRAPH_IRI -- specifies named-graph/context to replace/append. If -R is specified and -c is not defined, context to be cleared are inferred For trig files if it -c is not specified, it is inferred from the data, where all named graphs specified in trig file are used."
echo -e "\t-C CONTENT_TYPE -- content type of input FILES, i.e. text/turtle (default), application/rdf+xml ..."
echo "Usage: "
echo -e "\t$0 -R -C <CONTENT_TYPE> -s <RDF4J_SERVER> -r <REPO_ID> -c <GRAPH_IRI> <FILES>"
echo -e "\t$0 -R -C <CONTENT_TYPE> -u <RDF4J_REPOSITORY_URL> -c <GRAPH_IRI> <FILES>"
echo "Examples: "
echo -e "\tEXAMPLE-1 (append context): $0 -s http://onto.mondis.cz/openrdf-RDF4J -r ontomind_owlim -c http://onto.mondis.cz/resource/mdr-1.0-SNAPSHOT-temporary mdr.owl"
echo -e "\tEXAMPLE-1 (append context): $0 -s http://onto.mondis.cz/openrdf-RDF4J -r ontomind_owlim -c http://onto.mondis.cz/resource/mdr-1.0-SNAPSHOT-temporary mdr.owl"
echo -e "\tEXAMPLE-2 (replace context): $0 -R -C 'text/turtle' -s http://onto.fel.cvut.cz/rdf4j-server -r test -c http://vfn.cz/ontologies/fertility-saving-study study-model.ttl"
echo -e "\tEXAMPLE-3 (replace repository): $0 -R -C 'text/x-nquads' -s http://onto.fel.cvut.cz/rdf4j-server -r test fss-study-formgen.ng"
echo -e "\tEXAMPLE-4 (use of repository url): $0 -R -C 'text/turtle' -u http://onto.fel.cvut.cz/rdf4j-server/repositories/test -c http://vfn.cz/ontologies/fertility-saving-study study-model.ttl"
echo -e "\tEXAMPLE-5 (use of repository url): $0 -R -C 'application/trig' -u http://onto.fel.cvut.cz/rdf4j-server/repositories/test study-model.trig"
}

if [ "$#" -eq 0 ]; then
Expand Down Expand Up @@ -49,7 +51,7 @@ while getopts "h:s:r:u:c:RC:" arg; do
GRAPH=$(perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "<$OPTARG>")
;;
R)
APPEND=false
APPEND=false
;;
C)
CONTENT_TYPE=$OPTARG
Expand All @@ -62,40 +64,82 @@ FILES=$@

REPOSITORY_URL=${REPOSITORY_URL:-$RDF4J_SERVER/repositories/$REPOSITORY}

echo "INFO: *** PROVIDING REPOSITORY INFO ***"

function get_contexts_from_trig_file() {
FILE=$1
grep '\S\S*\s\s*{\s*' ${FILE} | sed 's/\s*{\s*//' | sort -u
}

function encode() {
while read GRAPH; do
perl -MURI::Escape -e "print uri_escape(q($GRAPH));"
echo
done
}

echo "INFO: *** RUNNING DEPLOY WITH PARAMETERS ***"
[ ! -z "$REPOSITORY_URL" ] && echo "INFO: - repository url: $REPOSITORY_URL"
[ ! -z "$GRAPH" ] && echo "INFO: - graph: $GRAPH"
echo "INFO: - files: $FILES"
echo "INFO: - method: $(if [ "$APPEND" = false ]; then echo "replace-data"; else echo "append-data"; fi)"

echo "INFO: *** PROVIDING INFO ABOUT INITIAL STATE OF REPOSITORY ***"
TEMP_FILE=`mktemp`
REPOSITORY_SIZE=`curl $REPOSITORY_URL/size 2> $TEMP_FILE`
echo "INFO: - old size of the repository is $REPOSITORY_SIZE"
echo "INFO: - initial size of the repository is $REPOSITORY_SIZE"

echo "INFO: *** DEPLOYING ***"
[ ! -z "$REPOSITORY_URL" ] && echo "INFO: - repository url: $REPOSITORY_URL"
[ ! -z "$GRAPH" ] && echo "INFO: - graph: $GRAPH"
echo "INFO: - files: $FILES"
echo "INFO: - method: $(if [ "$APPEND" = false ]; then echo "replace-data"; else echo "append-data"; fi)"
if [ "$APPEND" = false ]; then
echo "INFO: *** CLEARING DATA ***"

for FILE in $FILES; do
TEMP_FILE=`mktemp`

echo "INFO: *** SENDING DATA ***"
for FILE in $FILES
do
echo INFO: " -- sending $FILE";
TEMP_FILE=`mktemp`
QUERY_PARAMS=""

QUERY_PARAMS="context=$GRAPH"
if [ -z "$GRAPH" ]; then
QUERY_PARAMS=""
fi
if [ -n "$GRAPH" ]; then
QUERY_PARAMS="context=$GRAPH"

elif [ "$CONTENT_TYPE" = 'application/trig' ]; then
GRAPHS_TO_ENCODE=$(get_contexts_from_trig_file $FILE)
GRAPHS_LOG=$(echo $GRAPHS_TO_ENCODE | tr " " "\n" | sed 's/^/INFO: - /')
echo -e "INFO: - inferred contexts to clear:\n$GRAPHS_LOG";
QUERY_PARAMS=$(echo $GRAPHS_TO_ENCODE | tr " " "\n" | encode | sed 's/^/\&context=/' | tr -d '\n' | sed 's/^.//')

HTTP_METHOD="POST"
if [ "$APPEND" = false ]; then
HTTP_METHOD="PUT"
fi

if [ -n "$QUERY_PARAMS" ]; then
curl $REPOSITORY_URL/statements?$QUERY_PARAMS -v -X DELETE &> $TEMP_FILE
cat $TEMP_FILE | grep "HTTP/1.1 204" &>/dev/null && echo 'INFO: - clearing graphs was successful'
cat $TEMP_FILE | grep "HTTP/1.1 204" &>/dev/null || ( echo 'ERROR: - clearing graphs failed. Output of the process : '; cat $TEMP_FILE )

else
echo "INFO: - nothing to clear"
fi

done

echo "INFO: *** VALIDATING 'CLEARING DATA' ***"
TEMP_FILE=`mktemp`
REPOSITORY_SIZE=`curl $REPOSITORY_URL/size 2> $TEMP_FILE`
echo "INFO: - size of the repository after clearing is $REPOSITORY_SIZE"
fi

echo "INFO: *** ADDING DATA ***"
for FILE in $FILES; do
echo "INFO: - sending $FILE";
TEMP_FILE=`mktemp`

QUERY_PARAMS=""
if [ -n "$GRAPH" ]; then
QUERY_PARAMS="context=$GRAPH"
fi

curl -X $HTTP_METHOD -H "Content-Type: $CONTENT_TYPE" --data-binary "@$FILE" -o - -v "$REPOSITORY_URL/statements?$QUERY_PARAMS" 2> $TEMP_FILE
cat $TEMP_FILE | grep "HTTP/1.1 204" &>/dev/null && echo 'INFO: sending data was sucessfull'
cat $TEMP_FILE | grep "HTTP/1.1 204" &>/dev/null || ( echo 'ERROR: sending data failed. Output of the process : '; cat $TEMP_FILE )
done;
curl -X POST -H "Content-Type: $CONTENT_TYPE" --data-binary "@$FILE" -o - -v "$REPOSITORY_URL/statements?$QUERY_PARAMS" 2> $TEMP_FILE
cat $TEMP_FILE | grep "HTTP/1.1 204" &>/dev/null && echo 'INFO: - sending data was successful'
cat $TEMP_FILE | grep "HTTP/1.1 204" &>/dev/null || ( echo 'ERROR: - sending data failed. Output of the process : '; cat $TEMP_FILE )
done

echo "INFO: *** VALIDATING RESULT ***"
echo "INFO: *** VALIDATING 'ADDING DATA' ***"
TEMP_FILE=`mktemp`
REPOSITORY_SIZE=`curl $REPOSITORY_URL/size 2> $TEMP_FILE`
echo "INFO: - new size of the repository is $REPOSITORY_SIZE"
echo "INFO: - new size of the repository is $REPOSITORY_SIZE"

0 comments on commit 53bf9f2

Please sign in to comment.