Skip to content

Commit

Permalink
Merge pull request #109 from scc-digitalhub/base64_context_source
Browse files Browse the repository at this point in the history
Change builder tool and update frameworks with context-sources-map
  • Loading branch information
matteo-s authored Jul 9, 2024
2 parents 960a0f5 + 18af076 commit 92f7e9a
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 6 deletions.
62 changes: 60 additions & 2 deletions core-builder-tool/builder-tool.sh
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,69 @@ handle_error() {
exit 1
}


# Function to synchronize files based on context-sources-map using rsync
sync_files_with_rsync() {
local map_file="$1"
local base64_dir="$2"
local destination_dir="$3"

# Function to decode base64 filename without padding
decode_base64_no_padding() {
local base64_filename="$1"
# Add padding if necessary for base64 decoding
while [ $((${#base64_filename} % 4)) -ne 0 ]; do
base64_filename="${base64_filename}="
done
echo "$base64_filename" | base64 --decode
}

# Check if MAP_FILE exists
if [ ! -f "$map_file" ]; then
log "Error: Context-sources-map file '$map_file' does not exist."
exit 1
fi

# Read the context-sources-map file line by line
while IFS=',' read -r base64_filename destination_path; do
# Trim whitespace
base64_filename=$(echo "$base64_filename" | xargs)
destination_path=$(echo "$destination_path" | xargs)

# Decode the base64 filename
decoded_filename=$(decode_base64_no_padding "$base64_filename")
source_file="$base64_dir/$base64_filename"

# Check if the source file exists
if [ ! -f "$source_file" ]; then
log "Error: Source file '$source_file' does not exist."
continue
fi

# Create the destination directory if it does not exist
mkdir -p "$(dirname "$destination_dir/$destination_path")"

# Use rsync to copy the source file to the destination path
cp "$source_file" "$destination_dir/$destination_path"

# Check rsync exit status
if [ $? -eq 0 ]; then
log "Successfully copied '$source_file' to '$destination_path'"
else
log "Error: Failed to rsync '$source_file' to '$destination_path'"
fi
done < "$map_file"
}


# Trap any error or signal and call the error handling function
trap 'handle_error $LINENO "$BASH_COMMAND"' ERR

# Copy everything from source directory to destination directory, excluding context-refs.txt
rsync -avL --exclude='context-refs.txt' --exclude='Dockerfile' --exclude='.*/' "$source_dir/" "$destination_dir/"
# Process context-sources.txt
if [ -f "$source_dir/context-sources-map.txt" ]; then
sync_files_with_rsync "$source_dir/context-sources-map.txt" "$source_dir/" "$destination_dir"
fi


# Process context-refs.txt
if [ -f "$source_dir/context-refs.txt" ]; then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ public K8sDeploymentRunnable run(K8sDeploymentRunnable runnable) throws K8sFrame
.stream()
.collect(
Collectors.toMap(
ContextSource::getName,
c ->
Base64
.getUrlEncoder()
.withoutPadding()
.encodeToString(c.getName().getBytes()),
c ->
new String(
Base64.getDecoder().decode(c.getBase64()),
Expand All @@ -119,6 +123,25 @@ public K8sDeploymentRunnable run(K8sDeploymentRunnable runnable) throws K8sFrame
)
)
)
.orElseGet(Map::of),
contextSourcesOpt
.map(contextSources ->
Map.of(
"context-sources-map.txt",
contextSources
.stream()
.map(c ->
Base64
.getUrlEncoder()
.withoutPadding()
.encodeToString(c.getName().getBytes()) +
"," +
c.getName() +
"\n"
)
.collect(Collectors.joining(""))
)
)
.orElseGet(Map::of)
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ public K8sJobRunnable run(K8sJobRunnable runnable) throws K8sFrameworkException
.stream()
.collect(
Collectors.toMap(
ContextSource::getName,
c ->
Base64
.getUrlEncoder()
.withoutPadding()
.encodeToString(c.getName().getBytes()),
c ->
new String(
Base64.getDecoder().decode(c.getBase64()),
Expand All @@ -132,6 +136,25 @@ public K8sJobRunnable run(K8sJobRunnable runnable) throws K8sFrameworkException
)
)
)
.orElseGet(Map::of),
contextSourcesOpt
.map(contextSources ->
Map.of(
"context-sources-map.txt",
contextSources
.stream()
.map(c ->
Base64
.getUrlEncoder()
.withoutPadding()
.encodeToString(c.getName().getBytes()) +
"," +
c.getName() +
"\n"
)
.collect(Collectors.joining(""))
)
)
.orElseGet(Map::of)
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ public K8sServeRunnable run(K8sServeRunnable runnable) throws K8sFrameworkExcept
.stream()
.collect(
Collectors.toMap(
ContextSource::getName,
c ->
Base64
.getUrlEncoder()
.withoutPadding()
.encodeToString(c.getName().getBytes()),
c ->
new String(
Base64.getDecoder().decode(c.getBase64()),
Expand All @@ -105,6 +109,25 @@ public K8sServeRunnable run(K8sServeRunnable runnable) throws K8sFrameworkExcept
)
)
)
.orElseGet(Map::of),
contextSourcesOpt
.map(contextSources ->
Map.of(
"context-sources-map.txt",
contextSources
.stream()
.map(c ->
Base64
.getUrlEncoder()
.withoutPadding()
.encodeToString(c.getName().getBytes()) +
"," +
c.getName() +
"\n"
)
.collect(Collectors.joining(""))
)
)
.orElseGet(Map::of)
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ public K8sKanikoRunnable run(K8sKanikoRunnable runnable) throws K8sFrameworkExce
.stream()
.collect(
Collectors.toMap(
ContextSource::getName,
c ->
Base64
.getUrlEncoder()
.withoutPadding()
.encodeToString(c.getName().getBytes()),
c ->
new String(
Base64.getDecoder().decode(c.getBase64()),
Expand All @@ -137,6 +141,25 @@ public K8sKanikoRunnable run(K8sKanikoRunnable runnable) throws K8sFrameworkExce
)
)
)
.orElseGet(Map::of),
contextSourcesOpt
.map(contextSources ->
Map.of(
"context-sources-map.txt",
contextSources
.stream()
.map(c ->
Base64
.getUrlEncoder()
.withoutPadding()
.encodeToString(c.getName().getBytes()) +
"," +
c.getName() +
"\n"
)
.collect(Collectors.joining(""))
)
)
.orElseGet(Map::of)
)
);
Expand Down

0 comments on commit 92f7e9a

Please sign in to comment.