From da4b38c469077b78b46d8b18cf5f97ed8910f520 Mon Sep 17 00:00:00 2001 From: MStokluska Date: Wed, 19 Aug 2020 15:25:04 +0100 Subject: [PATCH] refactor: extensions.sh script refactor --- keycloak-init-container/extensions.sh | 41 ++++++++++++++++++++------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/keycloak-init-container/extensions.sh b/keycloak-init-container/extensions.sh index 266fb49429..553ea48210 100755 --- a/keycloak-init-container/extensions.sh +++ b/keycloak-init-container/extensions.sh @@ -13,23 +13,42 @@ download_extension() { if [[ -z "$EXTENSION_URL" ]]; then return fi - + echo echo "Downloading extension from $EXTENSION_URL" - local FILENAME="$(curl --verbose --location --remote-name --remote-header-name --write-out '%{filename_effective}' --silent "$EXTENSION_URL" 2> /tmp/headers)" - - # 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 " \n").jar" - mv "$FILENAME" "$F" - FILENAME="$F" + 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 "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" + fi + echo " --> $FILENAME" + else + echo -e "Can not download the extension: $EXTENSION_URL\nError code: $STATUS_CODE" + ((STATUS+=1)) fi - echo " --> $FILENAME" } # Parse the environment variable and download the extensions from the list IFS=, -for EXT in $KEYCLOAK_EXTENSIONS ; do +STATUS=0 +for EXT in ${KEYCLOAK_EXTENSION[@]} ; do download_extension "$EXT" done +if [ "$STATUS" -ne 0 ]; then + echo + echo -e "Extensions.sh script failed at downloading all required extensions, number of failed downloads: $STATUS \n" + exit 1 +else + echo + echo -e "All extensions downloaded successfully \n" +fi