From 5e6dee3bd75c56d83ca8cadda5f507e9647f330e Mon Sep 17 00:00:00 2001 From: Bruno Verachten Date: Mon, 22 Apr 2024 05:58:40 +0200 Subject: [PATCH] chore(updatecli): Check the validity of releases thanks to the Adoptium API. (#1851) * chore(debian): Switch from Temurin base images to Temurin JDK binaries * chore(debian): Switch from Temurin base images to Temurin JDK binaries * chore(redhat): Switch from Temurin base images to Temurin JDK binaries * chore(redhat): Switch from Temurin base images to Temurin JDK binaries * chore(alma): Switch from Temurin base images to Temurin JDK binaries * chore(alpine): Switch from Temurin base images to Temurin JDK binaries * chore(debian): Switch from Temurin base images to Temurin JDK binaries * chore(ubi9): Switch from Temurin base images to Temurin JDK binaries * chore(alpine): Switch from Temurin base images to Temurin JDK binaries * chore(debian): Switch from Temurin base images to Temurin JDK binaries * chore(debian-slim): Switch from Temurin base images to Temurin JDK binaries * chore(ubi9): Switch from Temurin base images to Temurin JDK binaries * fix(docker): Not really needed, just for consistency. * fix(docker): Hadolint shellcheck. * fix(docker): Hadolint shellcheck. * fix(docker): Hadolint shellcheck. * fix(docker): Hadolint shellcheck. * fix(docker): Hadolint shellcheck. * fix(docker): Hadolint shellcheck. * fix(docker): Hadolint shellcheck. * fix(docker): Hadolint shellcheck. * fix(docker): Hadolint shellcheck. * fix(docker): Hadolint shellcheck. * fix(docker): Hadolint shellcheck. * fix(docker): Hadolint shellcheck. * fix(docker): Hadolint shellcheck. * fix(docker): Hadolint shellcheck. * fix(docker): Hadolint shellcheck. * fix(docker): Hadolint shellcheck. * fix(docker): Hadolint shellcheck. * fix(docker): Hadolint shellcheck. * fix(docker): Hadolint shellcheck. * fix(docker): Hadolint shellcheck. * fix(docker): Following Damien's recommendations. * Make shell script executable * fix(docker): Hadolint shellcheck. * fix(docker): Hadolint shellcheck. * fix(docker): Hadolint shellcheck. * fix(docker): Following Damien's recommendations. * feat(temurin): Computes the GitHub download URL thanks to the Adoptium API. * feat(temurin): Computes the GitHub download URL thanks to the Adoptium API. * fix(temurin): Uses two scripts to find the right JDK download URL. One for the URL gathering, and one for the JDK installation. * fix(temurin): Better error handling. * fix(temurin): ShellCheck suggestions. * fix(temurin): ShellCheck suggestions. * fix(temurin): ShellCheck suggestions. * fix(temurin): ShellCheck suggestions. * fix(temurin): ShellCheck suggestions. * fix(temurin): Try to add '--allowerasing' to command line to replace conflicting packages or '--skip-broken' to skip uninstallable packages or '--nobest' to use not only best candidate packages) 4.242 Error: 4.242 Problem: problem with installed package curl-minimal-7.76.1-26.el9_3.3.x86_64 4.242 - package curl-minimal-7.76.1-26.el9_3.3.x86_64 from @System conflicts with curl provided by curl-7.76.1-26.el9_3.3.x86_64 from ubi-9-baseos-rpms 4.242 - package curl-minimal-7.76.1-26.el9_3.3.x86_64 from ubi-9-baseos-rpms conflicts with curl provided by curl-7.76.1-26.el9_3.3.x86_64 from ubi-9-baseos-rpms 4.242 - conflicting requests * fix(temurin): Try to add '--allowerasing' to command line to replace conflicting packages or '--skip-broken' to skip uninstallable packages or '--nobest' to use not only best candidate packages) 4.242 Error: 4.242 Problem: problem with installed package curl-minimal-7.76.1-26.el9_3.3.x86_64 4.242 - package curl-minimal-7.76.1-26.el9_3.3.x86_64 from @System conflicts with curl provided by curl-7.76.1-26.el9_3.3.x86_64 from ubi-9-baseos-rpms 4.242 - package curl-minimal-7.76.1-26.el9_3.3.x86_64 from ubi-9-baseos-rpms conflicts with curl provided by curl-7.76.1-26.el9_3.3.x86_64 from ubi-9-baseos-rpms 4.242 - conflicting requests * Make shell script executable * fix(temurin): Better handling of 307 HTTP Code. * fix(docker): One less layer. Thanks Damien for the tip. * fix(docker): testing mv is not so clever/necessary. * fix(docker): Adoptium API bug workaround. * fix(temurin): ShellCheck suggestions. * fix(temurin): ShellCheck suggestions. * fix(temurin): Message. * fix(temurin): Message. * fix(temurin): JAVA_VERSION is only used in the first stage. * fix(temurin): Removed debug RUN instruction. * fix(temurin): Following Damien's remark. https://github.com/jenkinsci/docker/pull/1847#discussion_r1569091139 * fix(temurin): Factorize shell scripts. * fix(temurin): Factorize shell scripts. * fix(temurin): What was I thinking? * fix(temurin): URL compute simplification. * chore(updatecli): Checks the validity of release thanks to the Adoptium API. * chore(updatecli): Checks the validity of release thanks to the Adoptium API. * fix(shell): ShellCheck recommendations. * Use space separators when checking multiple ARCHS --------- Co-authored-by: Mark Waite --- jdk-download-url.sh | 82 +++++++++++++++++++++----------- updatecli/updatecli.d/jdk11.yaml | 25 ++++------ updatecli/updatecli.d/jdk17.yaml | 24 ++++------ updatecli/updatecli.d/jdk21.yaml | 24 ++++------ 4 files changed, 82 insertions(+), 73 deletions(-) diff --git a/jdk-download-url.sh b/jdk-download-url.sh index 34decb4467..bf7065be59 100755 --- a/jdk-download-url.sh +++ b/jdk-download-url.sh @@ -3,12 +3,17 @@ # Check if at least one argument was passed to the script # If one argument was passed and JAVA_VERSION is set, assign the argument to OS # If two arguments were passed, assign them to JAVA_VERSION and OS respectively +# If three arguments were passed, assign them to JAVA_VERSION, OS and ARCHS respectively # If not, check if JAVA_VERSION and OS are already set. If they're not set, exit the script with an error message if [ $# -eq 1 ] && [ -n "$JAVA_VERSION" ]; then OS=$1 elif [ $# -eq 2 ]; then JAVA_VERSION=$1 OS=$2 +elif [ $# -eq 3 ]; then + JAVA_VERSION=$1 + OS=$2 + ARCHS=$3 elif [ -z "$JAVA_VERSION" ] && [ -z "$OS" ]; then echo "Error: No Java version and OS specified. Please set the JAVA_VERSION and OS environment variables or pass them as arguments." >&2 exit 1 @@ -23,6 +28,21 @@ elif [ -z "$OS" ]; then fi fi +# Check if ARCHS is set. If it's not set, assign the current architecture to it +if [ -z "$ARCHS" ]; then + ARCHS=$(uname -m | sed -e 's/x86_64/x64/' -e 's/armv7l/arm/') +else + # Convert ARCHS to an array + OLD_IFS=$IFS + IFS=',' + set -- "$ARCHS" + ARCHS="" + for arch in "$@"; do + ARCHS="$ARCHS $arch" + done + IFS=$OLD_IFS +fi + # Check if jq and curl are installed # If they are not installed, exit the script with an error message if ! command -v jq >/dev/null 2>&1 || ! command -v curl >/dev/null 2>&1; then @@ -36,40 +56,48 @@ ARCHIVE_DIRECTORY=$(echo "$JAVA_VERSION" | tr '_' '+') # URL encode ARCHIVE_DIRECTORY ENCODED_ARCHIVE_DIRECTORY=$(echo "$ARCHIVE_DIRECTORY" | xargs -I {} printf %s {} | jq "@uri" -jRr) -# Convert the architecture name to the format used by the Adoptium API -CONVERTED_ARCH=$(uname -m | sed -e 's/x86_64/x64/' -e 's/armv7l/arm/') - # Determine the OS type for the URL OS_TYPE="linux" if [ "$OS" = "alpine" ]; then OS_TYPE="alpine-linux" fi -# Fetch the download URL from the Adoptium API -URL="https://api.adoptium.net/v3/binary/version/jdk-${ENCODED_ARCHIVE_DIRECTORY}/${OS_TYPE}/${CONVERTED_ARCH}/jdk/hotspot/normal/eclipse?project=jdk" +# Initialize a variable to store the URL for the first architecture +FIRST_ARCH_URL="" -if ! RESPONSE=$(curl -fsI "$URL"); then - echo "Error: Failed to fetch the URL. Exiting with status 1." >&2 - echo "Response: $RESPONSE" >&2 - exit 1 -fi +# Loop over the array of architectures +for ARCH in $ARCHS; do + # Fetch the download URL from the Adoptium API + URL="https://api.adoptium.net/v3/binary/version/jdk-${ENCODED_ARCHIVE_DIRECTORY}/${OS_TYPE}/${ARCH}/jdk/hotspot/normal/eclipse?project=jdk" -# Extract the redirect URL from the HTTP response -REDIRECTED_URL=$(echo "$RESPONSE" | grep Location | awk '{print $2}' | tr -d '\r') + if ! RESPONSE=$(curl -fsI "$URL"); then + echo "Error: Failed to fetch the URL for architecture ${ARCH}. Exiting with status 1." >&2 + echo "Response: $RESPONSE" >&2 + exit 1 + fi -# If no redirect URL was found, exit the script with an error message -if [ -z "$REDIRECTED_URL" ]; then - echo "Error: No redirect URL found. Exiting with status 1." >&2 - echo "Response: $RESPONSE" >&2 - exit 1 -fi + # Extract the redirect URL from the HTTP response + REDIRECTED_URL=$(echo "$RESPONSE" | grep Location | awk '{print $2}' | tr -d '\r') -# Use curl to check if the URL is reachable -# If the URL is reachable, print the URL -# If the URL is not reachable, print an error message and exit the script with status 1 -if ! curl -v -fs "$REDIRECTED_URL" >/dev/null 2>&1; then - echo "${REDIRECTED_URL}" is not reachable. >&2 - exit 1 -else - echo "$REDIRECTED_URL" -fi + # If no redirect URL was found, exit the script with an error message + if [ -z "$REDIRECTED_URL" ]; then + echo "Error: No redirect URL found for architecture ${ARCH}. Exiting with status 1." >&2 + echo "Response: $RESPONSE" >&2 + exit 1 + fi + + # Use curl to check if the URL is reachable + # If the URL is not reachable, print an error message and exit the script with status 1 + if ! curl -v -fs "$REDIRECTED_URL" >/dev/null 2>&1; then + echo "${REDIRECTED_URL}" is not reachable for architecture "${ARCH}". >&2 + exit 1 + fi + + # If FIRST_ARCH_URL is empty, store the current URL + if [ -z "$FIRST_ARCH_URL" ]; then + FIRST_ARCH_URL=$REDIRECTED_URL + fi +done + +# If all downloads are successful, print the URL for the first architecture +echo "$FIRST_ARCH_URL" diff --git a/updatecli/updatecli.d/jdk11.yaml b/updatecli/updatecli.d/jdk11.yaml index 443ac3063d..62194a7196 100644 --- a/updatecli/updatecli.d/jdk11.yaml +++ b/updatecli/updatecli.d/jdk11.yaml @@ -32,25 +32,18 @@ sources: to: _ conditions: - checkTemurinAlpineDockerImage: - kind: dockerimage - name: Check if the container image "eclipse-temurin:-jdk-alpine" is available - disablesourceinput: true + checkTemurinAlpineRelease: + name: Check if the "" is available for alpine + kind: shell spec: - architecture: amd64 - image: eclipse-temurin - tag: '{{source "lastVersion" }}-jdk-alpine' - checkTemurinDebianDockerImages: - kind: dockerimage - name: Check if the container image "eclipse-temurin:-jdk-jammy" is available + command: sh ./jdk-download-url.sh '{{source "lastVersion" }}' alpine "x64" disablesourceinput: true + checkTemurinLinuxRelease: + name: Check if the "" is available for Linux + kind: shell spec: - architectures: - - amd64 - - arm64 - - s390x - image: eclipse-temurin - tag: '{{source "lastVersion" }}-jdk-jammy' + command: sh ./jdk-download-url.sh '{{source "lastVersion" }}' standard "x64 aarch64 s390x" + disablesourceinput: true checkTemurinNanoserver2019DockerImage: kind: dockerimage name: Check if the container image "eclipse-temurin:-jdk-nanoserver-1809" is available diff --git a/updatecli/updatecli.d/jdk17.yaml b/updatecli/updatecli.d/jdk17.yaml index f20b25a155..ad91b7f94a 100644 --- a/updatecli/updatecli.d/jdk17.yaml +++ b/updatecli/updatecli.d/jdk17.yaml @@ -34,24 +34,18 @@ sources: to: _ conditions: - checkTemurinAlpineDockerImage: - kind: dockerimage - name: Check if the container image "eclipse-temurin:-jdk-alpine" is available - disablesourceinput: true + checkTemurinAlpineRelease: + name: Check if the "" is available for alpine + kind: shell spec: - image: eclipse-temurin - tag: '{{source "lastVersion" }}-jdk-alpine' - checkTemurinDebianDockerImages: - kind: dockerimage - name: Check if the container image "eclipse-temurin:-jdk-focal" is available + command: sh ./jdk-download-url.sh '{{source "lastVersion" }}' alpine "x64" disablesourceinput: true + checkTemurinLinuxRelease: + name: Check if the "" is available for Linux + kind: shell spec: - architectures: - - amd64 - - arm64 - - s390x - image: eclipse-temurin - tag: '{{source "lastVersion" }}-jdk-focal' + command: sh ./jdk-download-url.sh '{{source "lastVersion" }}' standard "x64 aarch64 s390x" + disablesourceinput: true checkTemurinNanoserver2019DockerImage: kind: dockerimage name: Check if the container image "eclipse-temurin:-jdk-nanoserver-1809" is available diff --git a/updatecli/updatecli.d/jdk21.yaml b/updatecli/updatecli.d/jdk21.yaml index 01f7e604a8..b4e9669e99 100644 --- a/updatecli/updatecli.d/jdk21.yaml +++ b/updatecli/updatecli.d/jdk21.yaml @@ -32,24 +32,18 @@ sources: to: _ conditions: - checkTemurinAlpineDockerImage: - kind: dockerimage - name: Check if the container image "eclipse-temurin:-jdk-alpine" is available - disablesourceinput: true + checkTemurinAlpineRelease: + name: Check if the "" is available for alpine + kind: shell spec: - image: eclipse-temurin - tag: '{{source "lastVersion" }}-jdk-alpine' - checkTemurinDebianDockerImages: - kind: dockerimage - name: Check if the container image "eclipse-temurin:-jdk-focal" is available + command: sh ./jdk-download-url.sh '{{source "lastVersion" }}' alpine "x64" disablesourceinput: true + checkTemurinLinuxRelease: + name: Check if the "" is available for Linux + kind: shell spec: - architectures: - - amd64 - - arm64 - - s390x - image: eclipse-temurin - tag: '{{source "lastVersion" }}-jdk-focal' + command: sh ./jdk-download-url.sh '{{source "lastVersion" }}' standard "x64 aarch64 s390x" + disablesourceinput: true checkTemurinNanoserver2019DockerImage: kind: dockerimage name: Check if the container image "eclipse-temurin:-jdk-nanoserver-1809" is available