slfan1989 commented on code in PR #9014: URL: https://github.com/apache/hadoop/pull/9014#discussion_r4661000439
########## dev-support/bin/mvnw-bootstrap: ########## @@ -0,0 +1,183 @@ +#!/usr/bin/env bash +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Downloads what ./mvnw needs before its first real use, with retries. +# +# mvnw fetches maven-wrapper.jar once and without retry. When that download +# fails, it deletes the jar and then reports the misleading +# "Failed to validate Maven wrapper SHA-256" error. maven-wrapper.jar in turn +# downloads the Maven distribution, also without retry. This script does both +# downloads up front and retries each of them, so that a transient failure to +# reach the Maven repository does not fail the build. +# +# TODO: This is a temporary workaround. Remove this script, and the +# "Download Maven wrapper" steps that call it in .github/workflows/, once +# mvnw retries failed downloads itself and Hadoop has upgraded to that +# maven-wrapper release. See https://github.com/apache/maven-wrapper/pull/445 +# +# Environment variables: +# MVNW_BOOTSTRAP_ATTEMPTS number of attempts per download (default 5) +# MVNW_BOOTSTRAP_DELAY seconds to wait after the first failed attempt +# (default 10); the wait doubles after each attempt +# MVNW_BOOTSTRAP_TIMEOUT seconds after which an attempt is given up +# (default 300) +# MVNW_BOOTSTRAP_MIRROR mirror of Maven Central to download +# maven-wrapper.jar from when Maven Central fails; +# set to an empty value to disable + +attempts=${MVNW_BOOTSTRAP_ATTEMPTS:-5} +delay=${MVNW_BOOTSTRAP_DELAY:-10} +timeout=${MVNW_BOOTSTRAP_TIMEOUT:-300} +central="https://repo.maven.apache.org/maven2/" +mirror=${MVNW_BOOTSTRAP_MIRROR-https://maven-central.storage-download.googleapis.com/maven2} + +basedir=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd -P) || exit 1 +props="${basedir}/.mvn/wrapper/maven-wrapper.properties" +jar="${basedir}/.mvn/wrapper/maven-wrapper.jar" + +function property +{ + sed -n "s/^$1=//p" "${props}" | tr -d '[:space:]' +} + +function sha256 +{ + if command -v sha256sum >/dev/null; then + sha256sum "$1" | cut -d' ' -f1 + else + shasum -a 256 "$1" | cut -d' ' -f1 + fi +} + +## @description Run a command, and kill it if it runs for more than +## @description ${timeout} seconds. Without timeout(1), which macOS does not +## @description ship, the command runs unbounded. +## @param command and its arguments +function with_timeout +{ + if command -v timeout >/dev/null; then + timeout "${timeout}" "$@" + else + "$@" + fi +} + +function download +{ + if command -v curl >/dev/null; then + curl -f -L -sS --connect-timeout 30 --max-time "${timeout}" -o "$2" "$1" + else + # wget has no limit for the whole transfer, so bound it with timeout(1) + with_timeout wget --no-verbose --tries=1 --timeout=30 -O "$2" "$1" + fi +} + +## @description Run a command until it succeeds, up to ${attempts} times +## @param description of the command, for the log +## @param command and its arguments +function retry +{ + local what=$1 + local attempt + local wait=${delay} + shift + + for ((attempt = 1; attempt <= attempts; attempt++)); do + if "$@"; then + return 0 + fi + echo "Attempt ${attempt}/${attempts} to ${what} failed" >&2 + if [[ ${attempt} -lt ${attempts} ]]; then + echo "Retrying in ${wait} seconds" >&2 + sleep "${wait}" + wait=$((wait * 2)) + fi + done + echo "ERROR: Failed to ${what}" >&2 + return 1 +} + +## @description Download maven-wrapper.jar from a URL and verify it +## @param URL +function fetch_wrapper_jar_from +{ + rm -f "${jar}.tmp" + # compare the checksums ourselves: `sha256sum -c` skips a malformed line + # and still exits 0 + if download "$1" "${jar}.tmp"; then + if [[ "$(sha256 "${jar}.tmp")" == "${wrapper_sha}" ]]; then + mv "${jar}.tmp" "${jar}" + return 0 + fi + echo "SHA-256 of the file downloaded from $1 is not ${wrapper_sha}" >&2 + fi + rm -f "${jar}.tmp" + return 1 +} + +function fetch_wrapper_jar +{ + if fetch_wrapper_jar_from "${wrapper_url}"; then + return 0 + fi + # Maven Central sometimes answers GitHub Actions runners with HTTP 429 + # (Too Many Requests), and keeps doing so across retries. The mirror is + # safe to use, because the jar is verified against the checksum in + # maven-wrapper.properties. + if [[ -n "${mirror}" && "${wrapper_url}" == "${central}"* ]]; then + echo "Trying ${mirror}" >&2 + fetch_wrapper_jar_from "${mirror%/}/${wrapper_url#"${central}"}" + return $? + fi + return 1 +} + +## @description Have maven-wrapper.jar download, verify and unpack the Maven +## @description distribution +function fetch_distribution +{ + local name + + if with_timeout ./mvnw --version; then + return 0 + fi + # maven-wrapper.jar stores the archive in its cache before it verifies the + # checksum, and never downloads an archive that is already there. Unless + # the cache is cleared, every further attempt fails on the same archive. + name=$(property distributionUrl) + name=${name##*/} + name=${name%.*} + if [[ -n "${name}" ]]; then + rm -rf "${MAVEN_USER_HOME:-${HOME}/.m2}/wrapper/dists/${name}" Review Comment: Could we limit cleanup to the cache entry for this distribution URL? Maven Wrapper stores downloads from different URLs in separate subdirectories, so removing the parent directory can also delete valid cached downloads from other mirrors. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
