-
Notifications
You must be signed in to change notification settings - Fork 1.3k
extensions script cleanup #316
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,66 @@ | ||
#!/bin/bash -e | ||
#!/bin/bash | ||
|
||
EXTENSIONS_VOLUME="${EXTENSIONS_VOLUME:-/opt/extensions}" | ||
set -o pipefail -o errexit -o nounset | ||
|
||
mkdir -p "$EXTENSIONS_VOLUME" | ||
cd "$EXTENSIONS_VOLUME" | ||
echo "Target directory: $EXTENSIONS_VOLUME" | ||
## External variables used by this script | ||
declare -rx KEYCLOAK_EXTENSIONS | ||
declare -rx EXTENSIONS_VOLUME="${EXTENSIONS_VOLUME:-/opt/extensions}" | ||
|
||
# Download a single extension | ||
download_extension() { | ||
local EXTENSION_URL="$1" | ||
local extension_url="$1" | ||
|
||
if [[ -z "$EXTENSION_URL" ]]; then | ||
if [[ -z "$extension_url" ]]; then | ||
return | ||
fi | ||
echo | ||
echo "Downloading extension from $EXTENSION_URL" | ||
local CURL_COMMAND="$(curl --verbose --location --remote-name --remote-header-name --write-out "%{http_code} %{filename_effective}" --silent "$EXTENSION_URL" 2> /tmp/headers)" | ||
|
||
local STATUS_CODE=${CURL_COMMAND:0:3} | ||
|
||
if [ $STATUS_CODE -eq "200" ]; then | ||
local FILENAME=${CURL_COMMAND:4} | ||
echo | ||
echo "Downloading extension from $extension_url" | ||
local curl_command | ||
curl_command="$(curl --verbose --location --remote-name --remote-header-name --write-out "%{http_code} %{filename_effective}" --silent "$extension_url" 2> /tmp/headers)" | ||
|
||
local status_code=${curl_command:0:3} | ||
|
||
if [[ "$status_code" -eq "200" ]]; then | ||
local filename=${curl_command:4} | ||
echo "Extension downloaded successfully" | ||
|
||
# Try to get the filename from the response headers and return | ||
# a random name if that fails | ||
if ! grep -q -i '^< content-disposition:.*filename=' /tmp/headers ; then | ||
local F="$(od -N8 -tx1 -An -v /dev/urandom | tr -d "").jar" | ||
mv "$FILENAME" "$F" | ||
FILENAME="$F" | ||
if ! grep -q -i '^< content-disposition:.*filename=' /tmp/headers; then | ||
local F | ||
F="$(od -N8 -tx1 -An -v /dev/urandom | tr -d "").jar" | ||
mv "$filename" "$F" | ||
filename="$F" | ||
fi | ||
echo " --> $FILENAME" | ||
echo " --> $filename" | ||
else | ||
echo -e "Can not download the extension: $EXTENSION_URL\nError code: $STATUS_CODE" | ||
((STATUS+=1)) | ||
echo -e "Can not download the extension: ${extension_url}\nError code: ${status_code}" | ||
((FAILED_DOWNLOADS+=1)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since changing variable names to lowercase, should this one be lowercased too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's being used as a global variable, which I typically leave uppercase -- would you prefer it to be lowercase? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok to keep it uppercase then. |
||
return 1 | ||
fi | ||
|
||
} | ||
|
||
mkdir -p "$EXTENSIONS_VOLUME" | ||
if ! cd "$EXTENSIONS_VOLUME"; then | ||
echo "Failed to change directory to ${EXTENSIONS_VOLUME}" | ||
exit 1 | ||
fi | ||
echo "Target directory: ${EXTENSIONS_VOLUME}" | ||
|
||
# Parse the environment variable and download the extensions from the list | ||
IFS=, | ||
STATUS=0 | ||
for EXT in ${KEYCLOAK_EXTENSIONS[@]} ; do | ||
download_extension "$EXT" | ||
FAILED_DOWNLOADS=0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If going to lowercase this one, should be lowercased here too. |
||
|
||
for EXT in ${KEYCLOAK_EXTENSIONS[*]}; do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is where |
||
# The true guard allows us to continue attempting to download extensions if | ||
# one fails. This will help surface all failed extension downloads at once, | ||
# rather than needing to iterate through them one at a time. | ||
download_extension "$EXT" || true | ||
done | ||
if [ "$STATUS" -ne 0 ]; then | ||
|
||
if [[ "$FAILED_DOWNLOADS" -ne 0 ]]; then | ||
echo | ||
echo -e "Extensions.sh script failed at downloading all required extensions, number of failed downloads: $STATUS \n" | ||
echo -e "Extensions.sh script failed to download all required extensions, number of failed downloads: ${FAILED_DOWNLOADS} \n" | ||
exit 1 | ||
else | ||
echo | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This produces a filename with whitespaces included. See #390.