From 3d175cdc34341c92a003cff4f4d24b963ef39934 Mon Sep 17 00:00:00 2001 From: FelixFrizzy <39409092+FelixFrizzy@users.noreply.github.com> Date: Fri, 10 Jan 2025 15:42:59 +0100 Subject: [PATCH] Fix BASEHREF handling in Docker Compose - envsubst would overwrite existing skosmos config on every startup - basehref now written to config.ttl with bash script --- docker-compose.yml | 10 ++-- helperscripts/replace-basehref.sh | 77 +++++++++++++++++++++++++++++++ skosmos-dev/config-template.ttl | 51 -------------------- skosmos-dev/config.ttl | 5 +- skosmos-live/config-template.ttl | 50 -------------------- skosmos-live/config.ttl | 6 +-- 6 files changed, 86 insertions(+), 113 deletions(-) create mode 100755 helperscripts/replace-basehref.sh delete mode 100644 skosmos-dev/config-template.ttl delete mode 100644 skosmos-live/config-template.ttl diff --git a/docker-compose.yml b/docker-compose.yml index c2c4e0b..b359acb 100755 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -153,7 +153,7 @@ services: dockerfile: dockerfiles/Dockerfile.ubuntu hostname: skosmos-dev environment: - - BASEHREFDEV="http://${PUBLICURL:-localhost}:${PROXY_PORT:-9000}/skosmos-dev/" + - BASEHREFDEV=http://${PUBLICURL:-localhost}:${PROXY_PORT:-9000}/skosmos-dev/ depends_on: - fuseki-dev networks: @@ -162,11 +162,12 @@ services: volumes: - ./skosmos-dev/config-template.ttl:/var/www/html/config-template.ttl - ./skosmos-dev/config.ttl:/var/www/html/config.ttl + - ./helperscripts/replace-basehref.sh:/home/replace-basehref.sh - static-skosmos-dev:/var/www/html/resource restart: unless-stopped command: > /bin/bash -c "export BASEHREFDEV && - envsubst '$$BASEHREFDEV' < /var/www/html/config-template.ttl > /var/www/html/config.ttl && + /home/replace-basehref.sh /var/www/html/config.ttl $$BASEHREFDEV && /usr/sbin/apache2ctl -D FOREGROUND" healthcheck: test: ["CMD-SHELL", "curl -f http://localhost:80 || exit 1"] @@ -182,7 +183,7 @@ services: dockerfile: dockerfiles/Dockerfile.ubuntu hostname: skosmos-live environment: - - BASEHREFLIVE="http://${PUBLICURL:-localhost}:${PROXY_PORT:-9000}/skosmos-live/" + - BASEHREFLIVE=http://${PUBLICURL:-localhost}:${PROXY_PORT:-9000}/skosmos-live/ depends_on: - fuseki-live networks: @@ -191,11 +192,12 @@ services: volumes: - ./skosmos-live/config-template.ttl:/var/www/html/config-template.ttl - ./skosmos-live/config.ttl:/var/www/html/config.ttl + - ./helperscripts/replace-basehref.sh:/home/replace-basehref.sh - static-skosmos-live:/var/www/html/resource restart: unless-stopped command: > /bin/bash -c "export BASEHREFLIVE && - envsubst '$$BASEHREFLIVE' < /var/www/html/config-template.ttl > /var/www/html/config.ttl && + /home/replace-basehref.sh /var/www/html/config.ttl $$BASEHREFLIVE && /usr/sbin/apache2ctl -D FOREGROUND" healthcheck: test: ["CMD-SHELL", "curl -f http://localhost:80 || exit 1"] diff --git a/helperscripts/replace-basehref.sh b/helperscripts/replace-basehref.sh new file mode 100755 index 0000000..a1ef981 --- /dev/null +++ b/helperscripts/replace-basehref.sh @@ -0,0 +1,77 @@ +#!/bin/bash + + +# Replaces the skosmos:baseHref value in the specified file with the provided basehref value. +# Args: +# file_path (str): The path to the file to be updated. +# basehref (str): The new baseHref value to be set in the file. +# Raises: +# ValueError: If the file content or the updated content is empty. +# Exception: If any error occurs during the file operations. + +replace() { + local file_path="$1" + local basehref="$2" + + # Get the current timestamp for the backup file + local timestamp=$(date +"%Y%m%d_%H%M%S") + local backup_path="${file_path}.${timestamp}.backup" + + # Check if the file exists + if [[ ! -f "$file_path" ]]; then + echo "File '$file_path' does not exist. Aborting." + exit 1 + fi + + # Read the content of the file + local content + content=$(cat "$file_path") + + # Validate that the file is not empty + if [[ -z "${content// /}" ]]; then + echo "config.ttl seems to be empty. Aborting operation to avoid overwriting." + exit 1 + fi + + # Replace the skosmos:baseHref value using | as the delimiter + local updated_content + updated_content=$(echo "$content" | sed -E 's|skosmos:baseHref[[:space:]]*[^;]*;|skosmos:baseHref "'"$basehref"'" ;|') + # Validate the updated content is not empty + if [[ -z "${updated_content// /}" ]]; then + echo "Updated content of config.ttl is empty. Aborting operation to avoid overwriting." + exit 1 + fi + + # Create a backup of the original file + echo "$content" > "$backup_path" + + # Write the updated content to a temporary file first + local temp_file_path="${file_path}.temp" + echo "$updated_content" > "$temp_file_path" + + # Replace the original file with the temporary file's content + if ! mv "$temp_file_path" "$file_path"; then + echo "File is busy, attempting to copy instead." + if cp "$temp_file_path" "$file_path"; then + rm "$temp_file_path" + echo "File '$file_path' updated successfully using copy." + else + echo "Failed to update the file using copy. Aborting." + exit 1 + fi + else + echo "File '$file_path' updated successfully." + fi + +} + +# Main script logic +if [[ "$#" -ne 2 ]]; then + echo "Usage: $0 " + exit 1 +fi + +file_path="$1" +basehref="$2" + +replace "$file_path" "$basehref" diff --git a/skosmos-dev/config-template.ttl b/skosmos-dev/config-template.ttl deleted file mode 100644 index d5778cf..0000000 --- a/skosmos-dev/config-template.ttl +++ /dev/null @@ -1,51 +0,0 @@ -@prefix : . -@prefix dc: . -@prefix rdf: . -@prefix rdfs: . -@prefix skosmos: . -@prefix void: . -@prefix xsd: . - -:config a skosmos:Configuration ; - skosmos:baseHref ${BASEHREFDEV} ; - skosmos:customCss "resource/css/stylesheet.css" ; - skosmos:feedbackAddress "" ; - skosmos:feedbackEnvelopeSender "" ; - skosmos:feedbackSender "" ; - skosmos:globalPlugins () ; - skosmos:httpTimeout 5 ; - skosmos:languages ( [ rdfs:label "en" ; - rdf:value "ar_AE.utf8" ] [ rdfs:label "da" ; - rdf:value "da_DK.utf8" ] [ rdfs:label "de" ; - rdf:value "de_DE.utf8" ] [ rdfs:label "en" ; - rdf:value "en_GB.utf8" ] [ rdfs:label "en_US" ; - rdf:value "en_US.utf8" ] [ rdfs:label "es" ; - rdf:value "es_ES.utf8" ] [ rdfs:label "fa" ; - rdf:value "fa_IR.utf8" ] [ rdfs:label "fi" ; - rdf:value "fi_FI.utf8" ] [ rdfs:label "fr" ; - rdf:value "fr_FR.utf8" ] [ rdfs:label "it" ; - rdf:value "it_IT.utf8" ] [ rdfs:label "nb" ; - rdf:value "nb_NO.utf8" ] [ rdfs:label "nl" ; - rdf:value "nl_NL.utf8" ] [ rdfs:label "nn" ; - rdf:value "nn_NO.utf8" ] [ rdfs:label "pl" ; - rdf:value "pl_PL.utf8" ] [ rdfs:label "pt" ; - rdf:value "pt_PT.utf8" ] [ rdfs:label "pt_BR" ; - rdf:value "pt_BR.utf8" ] [ rdfs:label "ru" ; - rdf:value "ru_RU.utf8" ] [ rdfs:label "sv" ; - rdf:value "sv_SE.utf8" ] [ rdfs:label "zh" ; - rdf:value "zh_CN.utf8" ] ) ; - skosmos:logBrowserConsole true ; - skosmos:logCaughtExceptions true ; - skosmos:searchResultsSize 20 ; - skosmos:serviceName "Skosmos" ; - skosmos:sparqlCollationEnabled false ; - skosmos:sparqlDialect "JenaText" ; - skosmos:sparqlEndpoint ; - skosmos:sparqlTimeout 20 ; - skosmos:templateCache "/tmp/skosmos-template-cache" ; - skosmos:transitiveLimit 1000 ; - skosmos:uiHoneypotEnabled true ; - skosmos:uiHoneypotTime 5 ; - skosmos:uiLanguageDropdown true . - - diff --git a/skosmos-dev/config.ttl b/skosmos-dev/config.ttl index d5778cf..5599201 100644 --- a/skosmos-dev/config.ttl +++ b/skosmos-dev/config.ttl @@ -1,13 +1,11 @@ @prefix : . -@prefix dc: . @prefix rdf: . @prefix rdfs: . @prefix skosmos: . -@prefix void: . @prefix xsd: . :config a skosmos:Configuration ; - skosmos:baseHref ${BASEHREFDEV} ; + skosmos:baseHref "http://localhost:9000/skosmos-dev/" ; skosmos:customCss "resource/css/stylesheet.css" ; skosmos:feedbackAddress "" ; skosmos:feedbackEnvelopeSender "" ; @@ -48,4 +46,3 @@ skosmos:uiHoneypotTime 5 ; skosmos:uiLanguageDropdown true . - diff --git a/skosmos-live/config-template.ttl b/skosmos-live/config-template.ttl deleted file mode 100644 index dd9170c..0000000 --- a/skosmos-live/config-template.ttl +++ /dev/null @@ -1,50 +0,0 @@ -@prefix : . -@prefix dc: . -@prefix rdf: . -@prefix rdfs: . -@prefix skosmos: . -@prefix void: . -@prefix xsd: . - -:config a skosmos:Configuration ; - skosmos:baseHref ${BASEHREFLIVE} ; - skosmos:customCss "resource/css/stylesheet.css" ; - skosmos:feedbackAddress "" ; - skosmos:feedbackEnvelopeSender "" ; - skosmos:feedbackSender "" ; - skosmos:globalPlugins () ; - skosmos:httpTimeout 5 ; - skosmos:languages ( [ rdfs:label "en" ; - rdf:value "ar_AE.utf8" ] [ rdfs:label "da" ; - rdf:value "da_DK.utf8" ] [ rdfs:label "de" ; - rdf:value "de_DE.utf8" ] [ rdfs:label "en" ; - rdf:value "en_GB.utf8" ] [ rdfs:label "en_US" ; - rdf:value "en_US.utf8" ] [ rdfs:label "es" ; - rdf:value "es_ES.utf8" ] [ rdfs:label "fa" ; - rdf:value "fa_IR.utf8" ] [ rdfs:label "fi" ; - rdf:value "fi_FI.utf8" ] [ rdfs:label "fr" ; - rdf:value "fr_FR.utf8" ] [ rdfs:label "it" ; - rdf:value "it_IT.utf8" ] [ rdfs:label "nb" ; - rdf:value "nb_NO.utf8" ] [ rdfs:label "nl" ; - rdf:value "nl_NL.utf8" ] [ rdfs:label "nn" ; - rdf:value "nn_NO.utf8" ] [ rdfs:label "pl" ; - rdf:value "pl_PL.utf8" ] [ rdfs:label "pt" ; - rdf:value "pt_PT.utf8" ] [ rdfs:label "pt_BR" ; - rdf:value "pt_BR.utf8" ] [ rdfs:label "ru" ; - rdf:value "ru_RU.utf8" ] [ rdfs:label "sv" ; - rdf:value "sv_SE.utf8" ] [ rdfs:label "zh" ; - rdf:value "zh_CN.utf8" ] ) ; - skosmos:logBrowserConsole true ; - skosmos:logCaughtExceptions true ; - skosmos:searchResultsSize 20 ; - skosmos:serviceName "Skosmos" ; - skosmos:sparqlCollationEnabled false ; - skosmos:sparqlDialect "JenaText" ; - skosmos:sparqlEndpoint ; - skosmos:sparqlTimeout 20 ; - skosmos:templateCache "/tmp/skosmos-template-cache" ; - skosmos:transitiveLimit 1000 ; - skosmos:uiHoneypotEnabled true ; - skosmos:uiHoneypotTime 5 ; - skosmos:uiLanguageDropdown true . - diff --git a/skosmos-live/config.ttl b/skosmos-live/config.ttl index dd9170c..d382dca 100644 --- a/skosmos-live/config.ttl +++ b/skosmos-live/config.ttl @@ -1,14 +1,12 @@ @prefix : . -@prefix dc: . @prefix rdf: . @prefix rdfs: . @prefix skosmos: . -@prefix void: . @prefix xsd: . :config a skosmos:Configuration ; - skosmos:baseHref ${BASEHREFLIVE} ; - skosmos:customCss "resource/css/stylesheet.css" ; + skosmos:baseHref "http://localhost:9000/skosmos-live/" ; + skosmos:customCss "resource/css/stylesheet.css" ; skosmos:feedbackAddress "" ; skosmos:feedbackEnvelopeSender "" ; skosmos:feedbackSender "" ;