Skip to content

Commit

Permalink
Fix BASEHREF handling in Docker Compose
Browse files Browse the repository at this point in the history
- envsubst would overwrite existing skosmos config on every startup
- basehref now written to config.ttl with bash script
  • Loading branch information
FelixFrizzy committed Jan 10, 2025
1 parent 00c88f8 commit 3d175cd
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 113 deletions.
10 changes: 6 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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"]
Expand All @@ -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:
Expand All @@ -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"]
Expand Down
77 changes: 77 additions & 0 deletions helperscripts/replace-basehref.sh
Original file line number Diff line number Diff line change
@@ -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 <file_path> <basehref>"
exit 1
fi

file_path="$1"
basehref="$2"

replace "$file_path" "$basehref"
51 changes: 0 additions & 51 deletions skosmos-dev/config-template.ttl

This file was deleted.

5 changes: 1 addition & 4 deletions skosmos-dev/config.ttl
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
@prefix : <file:///code/evoks/tests/skosmos/config.ttl#> .
@prefix dc: <http://purl.org/dc/terms/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix skosmos: <http://purl.org/net/skosmos#> .
@prefix void: <http://rdfs.org/ns/void#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

:config a skosmos:Configuration ;
skosmos:baseHref ${BASEHREFDEV} ;
skosmos:baseHref "http://localhost:9000/skosmos-dev/" ;
skosmos:customCss "resource/css/stylesheet.css" ;
skosmos:feedbackAddress "" ;
skosmos:feedbackEnvelopeSender "" ;
Expand Down Expand Up @@ -48,4 +46,3 @@
skosmos:uiHoneypotTime 5 ;
skosmos:uiLanguageDropdown true .


50 changes: 0 additions & 50 deletions skosmos-live/config-template.ttl

This file was deleted.

6 changes: 2 additions & 4 deletions skosmos-live/config.ttl
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
@prefix : <file:///code/evoks/tests/skosmos/config.ttl#> .
@prefix dc: <http://purl.org/dc/terms/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix skosmos: <http://purl.org/net/skosmos#> .
@prefix void: <http://rdfs.org/ns/void#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

: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 "" ;
Expand Down

0 comments on commit 3d175cd

Please sign in to comment.